97e91a83f3
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
64 lines
1.7 KiB
Markdown
64 lines
1.7 KiB
Markdown
---
|
|
title: Groq AI Integration - Fast Structured Outputs
|
|
description: Use Groq AI with Instructor for fast structured outputs. Leverage Groq's high-speed inference for real-time structured data extraction.
|
|
---
|
|
|
|
# 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
|
|
from pydantic import BaseModel, Field
|
|
from typing import List
|
|
import instructor
|
|
|
|
|
|
class Character(BaseModel):
|
|
name: str
|
|
fact: List[str] = Field(..., description="A list of facts about the subject")
|
|
|
|
|
|
# Use from_provider for simplified setup
|
|
client = instructor.from_provider("groq/mixtral-8x7b-32768", mode=instructor.Mode.TOOLS)
|
|
|
|
resp = client.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": [
|
|
"electric vehicle manufacturer",
|
|
"solar panel producer",
|
|
"based in Palo Alto, California",
|
|
"founded in 2003 by Elon Musk"
|
|
]
|
|
}
|
|
"""
|
|
```
|
|
You can find another example called groq_example2.py under examples/groq of this repository.
|