Skip to content

feat(batch): support for mistral api#2168

Open
vcharvet wants to merge 3 commits into567-labs:mainfrom
vcharvet:feat/batch_mistral
Open

feat(batch): support for mistral api#2168
vcharvet wants to merge 3 commits into567-labs:mainfrom
vcharvet:feat/batch_mistral

Conversation

@vcharvet
Copy link
Copy Markdown

Supports for calling MistralAI batch API. The modification does not impact other aspects of the library.

Usage is similar as openai/anthropic

from pydantic import BaseModel

class User(BaseModel):
    name: str
    age: int

processor = BatchProcessor("mistral/mistral-small-latest", User)

Currently only support mistralai<2.0.0 as mentionned in #2137
Tested with instructor==1.14.5

@vcharvet
Copy link
Copy Markdown
Author

Mistral provider has been tested on the same snippet as https://python.useinstructor.com/concepts/batch/#in-memory-processing:

import time

from instructor.batch import BatchProcessor, extract_results
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int


processor = BatchProcessor("mistral/mistral-small-latest", User)


messages_list = [
    [
        {"role": "system", "content": "Extract user information from text."},
        {"role": "user", "content": "Hi, I'm Alice and I'm 28 years old."},
    ],
    [
        {"role": "system", "content": "Extract user information from text."},
        {"role": "user", "content": "Hello, I'm Bob, 35 years old."},
    ],
]

# Create batch file
processor.create_batch_from_messages(
    file_path="batch_requests.jsonl",
    messages_list=messages_list,
    max_tokens=100000,
    temperature=0.1,
)

# Submit batch job
batch_id = processor.submit_batch("batch_requests.jsonl")
print(f"Batch job submitted: {batch_id}")

# Check status and retrieve results
status = processor.get_batch_status(batch_id)
while status["status"] not in ["SUCCESS", "FAILED", "CANCELLED"]:
    status = processor.get_batch_status(batch_id)
    time.sleep(10)

print(f"Job status {status['status']}")

all_results = processor.retrieve_results(batch_id)
extracted_results = extract_results(all_results)
for user in extracted_results:
    print(f"Name: {user.name}, Age: {user.age}")
# Name: Alice, Age: 28
# Name: Bob,  Age: 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant