chore: import upstream snapshot with attribution
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import os
|
||||
import instructor
|
||||
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
# By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter
|
||||
client = instructor.from_openai(
|
||||
OpenAI(
|
||||
base_url="https://api.endpoints.anyscale.com/v1",
|
||||
api_key=os.environ["ANYSCALE_API_KEY"],
|
||||
),
|
||||
mode=instructor.Mode.JSON_SCHEMA,
|
||||
)
|
||||
|
||||
|
||||
# Now, we can use the response_model parameter using only a base model
|
||||
# rather than having to use the ResponseSchema class
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
|
||||
user: UserExtract = client.chat.completions.create(
|
||||
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
||||
response_model=UserExtract,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||
],
|
||||
) # type: ignore
|
||||
|
||||
print(user)
|
||||
{
|
||||
"name": "Jason",
|
||||
"age": 25,
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
import instructor
|
||||
|
||||
from openai import OpenAI
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
# By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter
|
||||
client = instructor.from_openai(
|
||||
OpenAI(),
|
||||
mode=instructor.Mode.TOOLS,
|
||||
)
|
||||
|
||||
|
||||
# Now, we can use the response_model parameter using only a base model
|
||||
# rather than having to use the ResponseSchema class
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
|
||||
user: UserExtract = client.chat.completions.create(
|
||||
model="gpt-3.5-turbo",
|
||||
response_model=UserExtract,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||
],
|
||||
) # type: ignore
|
||||
|
||||
print(user)
|
||||
{
|
||||
"name": "Jason",
|
||||
"age": 25,
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
from typing import Literal, Union
|
||||
from collections.abc import Iterable
|
||||
from pydantic import BaseModel
|
||||
from instructor import ResponseSchema
|
||||
|
||||
import time
|
||||
import openai
|
||||
import instructor
|
||||
|
||||
|
||||
client = openai.OpenAI()
|
||||
|
||||
|
||||
class Weather(ResponseSchema):
|
||||
location: str
|
||||
units: Literal["imperial", "metric"]
|
||||
|
||||
|
||||
class GoogleSearch(ResponseSchema):
|
||||
query: str
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
class Query(BaseModel):
|
||||
query: list[Union[Weather, GoogleSearch]]
|
||||
|
||||
client = instructor.from_openai(client, mode=instructor.Mode.PARALLEL_TOOLS)
|
||||
|
||||
start = time.perf_counter()
|
||||
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[Union[Weather, GoogleSearch]],
|
||||
)
|
||||
print(f"# Time: {time.perf_counter() - start:.2f}")
|
||||
|
||||
print("# Instructor: Question with Toronto and Super Bowl")
|
||||
print([model for model in resp])
|
||||
|
||||
start = time.perf_counter()
|
||||
resp = client.chat.completions.create(
|
||||
model="gpt-4-turbo-preview",
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is the weather in toronto and dallas?",
|
||||
},
|
||||
],
|
||||
tools=[
|
||||
{"type": "function", "function": Weather.openai_schema},
|
||||
{"type": "function", "function": GoogleSearch.openai_schema},
|
||||
],
|
||||
tool_choice="auto",
|
||||
)
|
||||
print(f"# Time: {time.perf_counter() - start:.2f}")
|
||||
|
||||
print("# Question with Toronto and Dallas")
|
||||
for tool_call in resp.choices[0].message.tool_calls:
|
||||
print(tool_call.model_dump_json(indent=2))
|
||||
|
||||
start = time.perf_counter()
|
||||
resp = client.chat.completions.create(
|
||||
model="gpt-4-turbo-preview",
|
||||
messages=[
|
||||
{
|
||||
"role": "user",
|
||||
"content": "What is the weather in toronto? and who won the super bowl?",
|
||||
},
|
||||
],
|
||||
tools=[
|
||||
{"type": "function", "function": Weather.openai_schema},
|
||||
{"type": "function", "function": GoogleSearch.openai_schema},
|
||||
],
|
||||
tool_choice="auto",
|
||||
)
|
||||
print(f"# Time: {time.perf_counter() - start:.2f}")
|
||||
|
||||
print("# Question with Toronto and Super Bowl")
|
||||
for tool_call in resp.choices[0].message.tool_calls:
|
||||
print(tool_call.model_dump_json(indent=2))
|
||||
@@ -0,0 +1,35 @@
|
||||
import os
|
||||
import openai
|
||||
from pydantic import BaseModel
|
||||
import instructor
|
||||
|
||||
client = openai.OpenAI(
|
||||
base_url="https://api.together.xyz/v1",
|
||||
api_key=os.environ["TOGETHER_API_KEY"],
|
||||
)
|
||||
|
||||
|
||||
# By default, the patch function will patch the ChatCompletion.create and ChatCompletion.acreate methods. to support response_model parameter
|
||||
client = instructor.from_openai(client, mode=instructor.Mode.TOOLS)
|
||||
|
||||
|
||||
# Now, we can use the response_model parameter using only a base model
|
||||
# rather than having to use the ResponseSchema class
|
||||
class UserExtract(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
|
||||
|
||||
user: UserExtract = client.chat.completions.create(
|
||||
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
||||
response_model=UserExtract,
|
||||
messages=[
|
||||
{"role": "user", "content": "Extract jason is 25 years old"},
|
||||
],
|
||||
) # type: ignore
|
||||
|
||||
print(user.model_dump_json(indent=2))
|
||||
{
|
||||
"name": "Jason",
|
||||
"age": 25,
|
||||
}
|
||||
Reference in New Issue
Block a user