mirror of
https://github.com/dogkeeper886/ollama37.git
synced 2025-12-10 15:57:04 +00:00
docs: update readmes for structured outputs (#7962)
This commit is contained in:
@@ -59,6 +59,40 @@ embeddings = client.embeddings.create(
|
||||
input=["why is the sky blue?", "why is the grass green?"],
|
||||
)
|
||||
```
|
||||
#### Structured outputs
|
||||
```py
|
||||
rom pydantic import BaseModel
|
||||
from openai import OpenAI
|
||||
|
||||
client = OpenAI(base_url="http://localhost:11434/v1", api_key="ollama")
|
||||
|
||||
# Define the schema for the response
|
||||
class FriendInfo(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
is_available: bool
|
||||
|
||||
class FriendList(BaseModel):
|
||||
friends: list[FriendInfo]
|
||||
|
||||
try:
|
||||
completion = client.beta.chat.completions.parse(
|
||||
temperature=0,
|
||||
model="llama3.1:8b",
|
||||
messages=[
|
||||
{"role": "user", "content": "I have two friends. The first is Ollama 22 years old busy saving the world, and the second is Alonso 23 years old and wants to hang out. Return a list of friends in JSON format"}
|
||||
],
|
||||
response_format=FriendList,
|
||||
)
|
||||
|
||||
friends_response = completion.choices[0].message
|
||||
if friends_response.parsed:
|
||||
print(friends_response.parsed)
|
||||
elif friends_response.refusal:
|
||||
print(friends_response.refusal)
|
||||
except Exception as e:
|
||||
print(f"Error: {e}")
|
||||
```
|
||||
|
||||
### OpenAI JavaScript library
|
||||
|
||||
|
||||
Reference in New Issue
Block a user