Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edits to batch.py to support Anthropic batch API #1193

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions instructor/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,25 +126,27 @@ def create_from_messages(
_, kwargs = handle_response_model(
response_model=response_model, mode=instructor.Mode.ANTHROPIC_JSON
)
with open(file_path, "w") as file:
for messages in messages_batch:
# Format specifically for Anthropic batch API
request = {
"custom_id": str(uuid.uuid4()),
"params": {
"model": model,
"max_tokens": max_tokens,
"temperature": temperature,
"messages": messages,
**kwargs,
}
}
file.write(json.dumps(request) + "\n")
else:
# Existing OpenAI format
_, kwargs = handle_response_model(
response_model=response_model, mode=instructor.Mode.TOOLS
)

with open(file_path, "w") as file:
for messages in messages_batch:
if use_anthropic:
batch_model = BatchModel(
custom_id=str(uuid.uuid4()),
params=RequestBody(
model=model,
messages=messages,
max_tokens=max_tokens,
temperature=temperature,
**kwargs,
),
)
else:
with open(file_path, "w") as file:
for messages in messages_batch:
batch_model = BatchModel(
custom_id=str(uuid.uuid4()),
params=RequestBody(
Expand All @@ -155,4 +157,4 @@ def create_from_messages(
**kwargs,
),
)
file.write(batch_model.model_dump_json() + "\n")
file.write(batch_model.model_dump_json() + "\n")
Loading