-
Notifications
You must be signed in to change notification settings - Fork 459
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structured outputs support with examples (#354)
- Loading branch information
1 parent
e956a33
commit 4b10dee
Showing
8 changed files
with
355 additions
and
18 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
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
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,32 @@ | ||
from pydantic import BaseModel | ||
from ollama import AsyncClient | ||
import asyncio | ||
|
||
|
||
# Define the schema for the response | ||
class FriendInfo(BaseModel): | ||
name: str | ||
age: int | ||
is_available: bool | ||
|
||
|
||
class FriendList(BaseModel): | ||
friends: list[FriendInfo] | ||
|
||
|
||
async def main(): | ||
client = AsyncClient() | ||
response = await client.chat( | ||
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'}], | ||
format=FriendList.model_json_schema(), # Use Pydantic to generate the schema | ||
options={'temperature': 0}, # Make responses more deterministic | ||
) | ||
|
||
# Use Pydantic to validate the response | ||
friends_response = FriendList.model_validate_json(response.message.content) | ||
print(friends_response) | ||
|
||
|
||
if __name__ == '__main__': | ||
asyncio.run(main()) |
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,50 @@ | ||
from pathlib import Path | ||
from pydantic import BaseModel | ||
from typing import List, Optional, Literal | ||
from ollama import chat | ||
from rich import print | ||
|
||
|
||
# Define the schema for image objects | ||
class Object(BaseModel): | ||
name: str | ||
confidence: float | ||
attributes: Optional[dict] = None | ||
|
||
|
||
class ImageDescription(BaseModel): | ||
summary: str | ||
objects: List[Object] | ||
scene: str | ||
colors: List[str] | ||
time_of_day: Literal['Morning', 'Afternoon', 'Evening', 'Night'] | ||
setting: Literal['Indoor', 'Outdoor', 'Unknown'] | ||
text_content: Optional[str] = None | ||
|
||
|
||
# Get path from user input | ||
path = input('Enter the path to your image: ') | ||
path = Path(path) | ||
|
||
# Verify the file exists | ||
if not path.exists(): | ||
raise FileNotFoundError(f'Image not found at: {path}') | ||
|
||
# Set up chat as usual | ||
response = chat( | ||
model='llama3.2-vision', | ||
format=ImageDescription.model_json_schema(), # Pass in the schema for the response | ||
messages=[ | ||
{ | ||
'role': 'user', | ||
'content': 'Analyze this image and return a detailed JSON description including objects, scene, colors and any text detected. If you cannot determine certain details, leave those fields empty.', | ||
'images': [path], | ||
}, | ||
], | ||
options={'temperature': 0}, # Set temperature to 0 for more deterministic output | ||
) | ||
|
||
|
||
# Convert received content to the schema | ||
image_analysis = ImageDescription.model_validate_json(response.message.content) | ||
print(image_analysis) |
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,26 @@ | ||
from ollama import chat | ||
from pydantic import BaseModel | ||
|
||
|
||
# Define the schema for the response | ||
class FriendInfo(BaseModel): | ||
name: str | ||
age: int | ||
is_available: bool | ||
|
||
|
||
class FriendList(BaseModel): | ||
friends: list[FriendInfo] | ||
|
||
|
||
# schema = {'type': 'object', 'properties': {'friends': {'type': 'array', 'items': {'type': 'object', 'properties': {'name': {'type': 'string'}, 'age': {'type': 'integer'}, 'is_available': {'type': 'boolean'}}, 'required': ['name', 'age', 'is_available']}}}, 'required': ['friends']} | ||
response = chat( | ||
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'}], | ||
format=FriendList.model_json_schema(), # Use Pydantic to generate the schema or format=schema | ||
options={'temperature': 0}, # Make responses more deterministic | ||
) | ||
|
||
# Use Pydantic to validate the response | ||
friends_response = FriendList.model_validate_json(response.message.content) | ||
print(friends_response) |
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
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
Oops, something went wrong.