mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
34 lines
726 B
Python
34 lines
726 B
Python
import openai
|
|
import instructor
|
|
|
|
from typing import Iterable, Literal
|
|
from pydantic import BaseModel
|
|
|
|
|
|
class Weather(BaseModel):
|
|
location: str
|
|
units: Literal["imperial", "metric"]
|
|
|
|
|
|
class GoogleSearch(BaseModel):
|
|
query: str
|
|
|
|
|
|
client = openai.OpenAI()
|
|
|
|
client = instructor.patch(client, mode=instructor.Mode.PARALLEL_TOOLS)
|
|
resp = client.chat.completions.create(
|
|
model="gpt-4-turbo-preview",
|
|
messages=[
|
|
{"role": "system", "content": "You must always use tools"},
|
|
{
|
|
"role": "user",
|
|
"content": "What is the weather in toronto and dallas and who won the super bowl?",
|
|
},
|
|
],
|
|
response_model=Iterable[Weather | GoogleSearch],
|
|
)
|
|
|
|
for r in resp:
|
|
print(r)
|