-
-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented a new api for creating batch jobs
- Loading branch information
Showing
3 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
from typing import Literal, Any, Union | ||
from pydantic import BaseModel, Field | ||
from instructor.process_response import handle_response_model | ||
import uuid | ||
|
||
openai_models = Literal[ | ||
"gpt-4o", | ||
"gpt-4-turbo", | ||
"gpt-4", | ||
"gpt-4-32k", | ||
"gpt-3.5-turbo", | ||
"gpt-3.5-turbo-16k", | ||
"gpt-4-turbo-preview", | ||
"gpt-4-vision-preview", | ||
"gpt-4-turbo-2024-04-09", | ||
"gpt-4-0314", | ||
"gpt-4-32k-0314", | ||
"gpt-4-32k-0613", | ||
"gpt-3.5-turbo-0301", | ||
"gpt-3.5-turbo-16k-0613", | ||
"gpt-3.5-turbo-1106", | ||
"gpt-3.5-turbo-0613", | ||
] | ||
|
||
|
||
class Function(BaseModel): | ||
name: str | ||
description: str | ||
parameters: Any | ||
|
||
|
||
class Tool(BaseModel): | ||
type: str | ||
function: Function | ||
|
||
|
||
class RequestBody(BaseModel): | ||
model: openai_models | str | ||
Check failure on line 38 in instructor/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
messages: list[dict[str, Any]] | ||
max_tokens: int = Field(default=1000) | ||
tools: list[Tool] | ||
|
||
|
||
class BatchModel(BaseModel): | ||
custom_id: str | ||
method: Literal["POST"] | ||
url: Literal["/v1/chat/completions"] | ||
body: RequestBody | ||
|
||
|
||
class BatchJob: | ||
@classmethod | ||
def create_from_messages( | ||
cls, | ||
messages_batch: list[list[dict[str, Any]]], | ||
model: Union[openai_models, str], | ||
response_model: type[BaseModel], | ||
max_tokens: int = 1000, | ||
): | ||
_, tools = handle_response_model(response_model=response_model) | ||
return [ | ||
BatchModel( | ||
custom_id=str(uuid.uuid4()), | ||
method="POST", | ||
url="/v1/chat/completions", | ||
body=RequestBody( | ||
model=model, | ||
max_tokens=max_tokens, | ||
messages=messages, | ||
**tools, | ||
), | ||
).model_dump_json() | ||
for messages in messages_batch | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
from rich.console import Console | ||
from rich.table import Table | ||
from rich.live import Live | ||
from openai import OpenAI | ||
from openai.types.batch import Batch | ||
import typer | ||
import datetime | ||
import time | ||
|
||
client = OpenAI() | ||
app = typer.Typer() | ||
|
||
console = Console() | ||
|
||
|
||
def generate_table(batch_jobs: list[Batch]): | ||
table = Table( | ||
title="OpenAI Batch Jobs", | ||
) | ||
table = Table( | ||
title="OpenAI Batch Jobs", | ||
) | ||
|
||
table.add_column("Batch ID", style="dim") | ||
table.add_column("Created At") | ||
table.add_column("Status") | ||
table.add_column("Failed") | ||
table.add_column("Completed") | ||
table.add_column("Total") | ||
|
||
for batch_job in batch_jobs: | ||
table.add_row( | ||
batch_job.id, | ||
str(datetime.datetime.fromtimestamp(batch_job.created_at)), | ||
batch_job.status, | ||
str(batch_job.request_counts.failed), | ||
Check failure on line 36 in instructor/cli/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
str(batch_job.request_counts.completed), | ||
Check failure on line 37 in instructor/cli/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
str(batch_job.request_counts.total), | ||
Check failure on line 38 in instructor/cli/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
) | ||
|
||
return table | ||
|
||
|
||
def get_jobs(limit: int = 10): | ||
return client.batches.list(limit=limit).data | ||
|
||
|
||
@app.command(name="list", help="See all existing batch jobs") | ||
def watch( | ||
limit: int = typer.Option(10, help="Total number of batch jobs to show"), | ||
poll: int = typer.Option( | ||
10, help="Time in seconds to wait for the batch job to complete" | ||
), | ||
screen: bool = typer.Option(False, help="Enable or disable screen output"), | ||
): | ||
""" | ||
Monitor the status of the most recent batch jobs | ||
""" | ||
batch_jobs = get_jobs(limit) | ||
table = generate_table(batch_jobs) | ||
with Live( | ||
generate_table(batch_jobs), refresh_per_second=2, screen=screen | ||
) as live_table: | ||
while True: | ||
batch_jobs = get_jobs(limit) | ||
table = generate_table(batch_jobs) | ||
live_table.update(table) | ||
time.sleep(poll) | ||
|
||
|
||
@app.command( | ||
help="Create a batch job from a file", | ||
) | ||
def create_from_file( | ||
file_path: str = typer.Option(..., help="File containing the batch job requests"), | ||
Check failure on line 75 in instructor/cli/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
): | ||
with console.status(f"[bold green] Uploading batch job file...", spinner="dots"): | ||
batch_input_file = client.files.create( | ||
file=open(file_path, "rb"), purpose="batch" | ||
) | ||
|
||
batch_input_file_id = batch_input_file.id | ||
|
||
with console.status( | ||
f"[bold green] Creating batch job from ID {batch_input_file_id}", spinner="dots" | ||
): | ||
client.batches.create( | ||
input_file_id=batch_input_file_id, | ||
endpoint="/v1/chat/completions", | ||
completion_window="24h", | ||
metadata={"description": "testing job"}, | ||
) | ||
watch(limit=5, poll=2, screen=False) | ||
|
||
|
||
@app.command(help="Cancel a batch job") | ||
def cancel(batch_id: str = typer.Option(..., help="Batch job ID to cancel")): | ||
Check failure on line 97 in instructor/cli/batch.py GitHub Actions / Pyright (ubuntu-latest, 3.11)
|
||
try: | ||
client.batches.cancel(batch_id) | ||
console.log(f"[bold red]Job {id} cancelled successfully!") | ||
except Exception as e: | ||
console.log(f"[bold red]Error cancelling job {id}: {e}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters