참고소스 수정본

This commit is contained in:
LASTA_DEV01\lasta
2026-05-12 19:40:31 +09:00
parent 0f34a451fc
commit 2e9204243d
8708 changed files with 3259488 additions and 869 deletions

View File

@@ -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,
}

View File

@@ -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,
}

View File

@@ -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))

View File

@@ -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,
}