You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromopenaiimportOpenAIimportopenaifrompydanticimportBaseModelclient=OpenAI(base_url="http://192.168.202.137:11434/v1", api_key="ollama")
classPet(BaseModel):
name: stranimal: strage: intcolor: str|Nonefavorite_toy: str|NoneclassPetList(BaseModel):
pets: list[Pet]
try:
completion=client.beta.chat.completions.parse(
temperature=0,
model='llama3.2:3b',
messages=[
{"role": "user", "content": ''' I have two pets. A cat named Luna who is 5 years old and loves playing with yarn. She has grey fur. I also have a 2 year old black cat named Loki who loves tennis balls. '''}
],
response_format=PetList,
)
pet_response=completion.choices[0].messageifpet_response.parsed:
print(pet_response.parsed)
elifpet_response.refusal:
print(pet_response.refusal)
exceptExceptionase:
iftype(e) ==openai.LengthFinishReasonError:
print("Too many tokens: ", e)
passelse:
print(e)
pass
In pydantic-al I try
frompydanticimportBaseModelfrompydantic_aiimportAgentfrompydantic_ai.models.ollamaimportOllamaModelollama_model=OllamaModel(
model_name='llama3.2:3b',
base_url='http://192.168.202.137:11434/v1'
)
classPet(BaseModel):
name: stranimal: strage: intcolor: str|Nonefavorite_toy: str|NoneclassPetList(BaseModel):
pets: list[Pet]
# # Create a system prompt to guide the modelSYSTEM_PROMPT="""You are a helper that extracts pet information from text and formats it as a list.For each pet mentioned, extract:- name- animal type- age- color (if mentioned)- favorite toy (if mentioned)Format as a JSON object with a 'pets' array containing each pet's details."""agent3=Agent(model=ollama_model, result_type=PetList, retries=3)
result3=agent3.run_sync(('I have two pets. A cat named Luna who is 5 years old'' and loves playing with yarn. She has grey fur. I also ''have a 2 year old black cat named Loki who loves tennis balls.'))
pet_data=result3.dataprint(pet_data)
passing PetList to the result_type. This will fail.
If I just pass Pet, ie, result_type=Pet it will sometimes work, getting only 1 cat of course, but also fail sometimes.
Any guidance on how to address this would be appreciated.
The text was updated successfully, but these errors were encountered:
I don't know if this is related to #242 or not.
I am trying to replicate this from the Ollama examples (ref: https://ollama.com/blog/structured-outputs)
In pydantic-al I try
passing PetList to the result_type. This will fail.
If I just pass Pet, ie,
result_type=Pet
it will sometimes work, getting only 1 cat of course, but also fail sometimes.Any guidance on how to address this would be appreciated.
The text was updated successfully, but these errors were encountered: