mirror of
https://github.com/kennethreitz/instructor.git
synced 2026-06-05 22:50:18 +00:00
Together ai support, blog post (#366)
Co-authored-by: Hassan El Mghari <hassan4709@gmail.com>
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.patch(
|
||||
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 OpenAISchema 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,
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import os
|
||||
import instructor
|
||||
|
||||
from openai import OpenAI
|
||||
@@ -5,7 +6,10 @@ 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.patch(OpenAI())
|
||||
client = instructor.patch(
|
||||
OpenAI(),
|
||||
mode=instructor.Mode.TOOLS,
|
||||
)
|
||||
|
||||
|
||||
# Now, we can use the response_model parameter using only a base model
|
||||
@@ -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.patch(client, mode=instructor.Mode.TOOLS)
|
||||
|
||||
|
||||
# Now, we can use the response_model parameter using only a base model
|
||||
# rather than having to use the OpenAISchema 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