-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-anthropic-reask
- Loading branch information
Showing
14 changed files
with
436 additions
and
104 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,67 @@ | ||
# Structured Outputs using Groq | ||
Instead of using openai or antrophic you can now also use groq for inference by using from_groq. | ||
|
||
The examples are using mixtral-8x7b model. | ||
|
||
## GroqCloud API | ||
To use groq you need to obtain a groq API key. | ||
Goto [groqcloud](https://console.groq.com) and login. Select API Keys from the left menu and then select Create API key to create a new key. | ||
|
||
## Use example | ||
Some pip packages need to be installed to use the example: | ||
``` | ||
pip install instructor groq pydantic openai anthropic | ||
``` | ||
You need to export the groq API key: | ||
``` | ||
export GROQ_API_KEY=<your-api-key> | ||
``` | ||
|
||
An example: | ||
```python | ||
import os | ||
from pydantic import BaseModel, Field | ||
from typing import List | ||
from groq import Groq | ||
import instructor | ||
|
||
class Character(BaseModel): | ||
name: str | ||
fact: List[str] = Field(..., description="A list of facts about the subject") | ||
|
||
|
||
client = Groq( | ||
api_key=os.environ.get('GROQ_API_KEY'), | ||
) | ||
|
||
client = instructor.from_groq(client, mode=instructor.Mode.JSON) | ||
|
||
resp = client.chat.completions.create( | ||
model="mixtral-8x7b-32768", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Tell me about the company Tesla", | ||
} | ||
], | ||
response_model=Character, | ||
) | ||
print(resp.model_dump_json(indent=2)) | ||
""" | ||
{ | ||
"name": "Tesla", | ||
"fact": [ | ||
"An American electric vehicle and clean energy company.", | ||
"Co-founded by Elon Musk, JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003.", | ||
"Headquartered in Austin, Texas.", | ||
"Produces electric vehicles, energy storage solutions, and more recently, solar energy products.", | ||
"Known for its premium electric vehicles, such as the Model S, Model 3, Model X, and Model Y.", | ||
"One of the world's most valuable car manufacturers by market capitalization.", | ||
"Tesla's CEO, Elon Musk, is also the CEO of SpaceX, Neuralink, and The Boring Company.", | ||
"Tesla operates the world's largest global network of electric vehicle supercharging stations.", | ||
"The company aims to accelerate the world's transition to sustainable transport and energy through innovative technologies and products." | ||
] | ||
} | ||
""" | ||
``` | ||
You can find another example called groq_example2.py under examples/groq of this repository. |
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,45 @@ | ||
import os | ||
from pydantic import BaseModel, Field | ||
from typing import List | ||
from groq import Groq | ||
import instructor | ||
|
||
|
||
class Character(BaseModel): | ||
name: str | ||
fact: List[str] = Field(..., description="A list of facts about the subject") | ||
|
||
|
||
client = Groq( | ||
api_key=os.environ.get("GROQ_API_KEY"), | ||
) | ||
|
||
client = instructor.from_groq(client, mode=instructor.Mode.JSON) | ||
|
||
resp = client.chat.completions.create( | ||
model="mixtral-8x7b-32768", | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Tell me about the company Tesla", | ||
} | ||
], | ||
response_model=Character, | ||
) | ||
print(resp.model_dump_json(indent=2)) | ||
""" | ||
{ | ||
"name": "Tesla", | ||
"fact": [ | ||
"An American electric vehicle and clean energy company.", | ||
"Co-founded by Elon Musk, JB Straubel, Martin Eberhard, Marc Tarpenning, and Ian Wright in 2003.", | ||
"Headquartered in Austin, Texas.", | ||
"Produces electric vehicles, energy storage solutions, and more recently, solar energy products.", | ||
"Known for its premium electric vehicles, such as the Model S, Model 3, Model X, and Model Y.", | ||
"One of the world's most valuable car manufacturers by market capitalization.", | ||
"Tesla's CEO, Elon Musk, is also the CEO of SpaceX, Neuralink, and The Boring Company.", | ||
"Tesla operates the world's largest global network of electric vehicle supercharging stations.", | ||
"The company aims to accelerate the world's transition to sustainable transport and energy through innovative technologies and products." | ||
] | ||
} | ||
""" |
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,37 @@ | ||
import os | ||
from pydantic import BaseModel, Field | ||
from typing import List | ||
from groq import Groq | ||
import instructor | ||
|
||
client = Groq( | ||
api_key=os.environ.get("GROQ_API_KEY"), | ||
) | ||
|
||
client = instructor.from_groq(client, mode=instructor.Mode.JSON) | ||
|
||
|
||
class UserExtract(BaseModel): | ||
name: str | ||
age: int | ||
|
||
|
||
user: UserExtract = client.chat.completions.create( | ||
model="mixtral-8x7b-32768", | ||
response_model=UserExtract, | ||
messages=[ | ||
{"role": "user", "content": "Extract jason is 25 years old"}, | ||
], | ||
) | ||
|
||
assert isinstance(user, UserExtract), "Should be instance of UserExtract" | ||
assert user.name.lower() == "jason" | ||
assert user.age == 25 | ||
|
||
print(user.model_dump_json(indent=2)) | ||
""" | ||
{ | ||
"name": "jason", | ||
"age": 25 | ||
} | ||
""" |
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,51 @@ | ||
from pydantic import BaseModel, field_validator | ||
from openai import OpenAI | ||
import instructor | ||
import tenacity | ||
|
||
client = OpenAI() | ||
client = instructor.from_openai(client) | ||
|
||
|
||
class User(BaseModel): | ||
name: str | ||
age: int | ||
|
||
@field_validator("name") | ||
def name_is_uppercase(cls, v: str): | ||
assert v.isupper(), "Name must be uppercase" | ||
return v | ||
|
||
|
||
resp = client.messages.create( | ||
model="gpt-3.5-turbo", | ||
max_tokens=1024, | ||
max_retries=tenacity.Retrying( | ||
stop=tenacity.stop_after_attempt(3), | ||
before=lambda _: print("before:", _), | ||
after=lambda _: print("after:", _), | ||
), | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Extract John is 18 years old.", | ||
} | ||
], | ||
response_model=User, | ||
) # type: ignore | ||
|
||
assert isinstance(resp, User) | ||
assert resp.name == "JOHN" # due to validation | ||
assert resp.age == 18 | ||
print(resp) | ||
|
||
""" | ||
before: <RetryCallState 4421908816: attempt #1; slept for 0.0; last result: none yet> | ||
after: <RetryCallState 4421908816: attempt #1; slept for 0.0; last result: failed (ValidationError 1 validation error for User | ||
name | ||
Assertion failed, Name must be uppercase [type=assertion_error, input_value='John', input_type=str] | ||
For further information visit https://errors.pydantic.dev/2.6/v/assertion_error)> | ||
before: <RetryCallState 4421908816: attempt #2; slept for 0.0; last result: none yet> | ||
name='JOHN' age=18 | ||
""" |
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.