Groq's compound systems excel at a wide range of use cases, particularly when real-time information is required.
Your application needs to answer questions or provide information that requires up-to-the-minute knowledge, such as:
Building and maintaining your own web scraping or search API integration is complex and time-consuming.
Simply send the user's query to compound-beta
. If the query requires current information beyond its training data, it will automatically trigger its built-in web search tool to fetch relevant, live data before formulating the answer.
import os
from groq import Groq
# Ensure your GROQ_API_KEY is set as an environment variable
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
user_query = "What were the main highlights from the latest Apple keynote event?"
# Or: "What's the current weather in San Francisco?"
# Or: "Summarize the latest developments in fusion energy research this week."
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": user_query,
}
],
# The *only* change needed: Specify the compound model!
model="compound-beta",
)
print(f"Query: {user_query}")
print(f"Compound Beta Response:\n{chat_completion.choices[0].message.content}")
# You might also inspect chat_completion.choices[0].message.executed_tools
# if you want to see if/which tool was used, though it's not necessary.
You want users to perform calculations, run simple data manipulations, or execute small code snippets using natural language commands within your application, without building a dedicated parser or execution environment.
Frame the user's request as a task involving computation or code. compound-beta-mini
can recognize these requests and use its secure code execution tool to compute the result.
import os
from groq import Groq
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Example 1: Calculation
computation_query = "Calculate the monthly payment for a $30,000 loan over 5 years at 6% annual interest."
# Example 2: Simple code execution
code_query = "What is the output of this Python code snippet: `data = {'a': 1, 'b': 2}; print(data.keys())`"
# Choose one query to run
selected_query = computation_query
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant capable of performing calculations and executing simple code when asked.",
},
{
"role": "user",
"content": selected_query,
}
],
# Use the compound model
model="compound-beta-mini",
)
print(f"Query: {selected_query}")
print(f"Compound Beta Response:\n{chat_completion.choices[0].message.content}")
Developers often need quick help understanding error messages or testing small code fixes. Searching documentation or running snippets requires switching contexts.
Users can paste an error message and ask for explanations or potential causes. Compound Beta Mini might use web search to find recent discussions or documentation about that specific error. Alternatively, users can provide a code snippet and ask "What's wrong with this code?" or "Will this Python code run: ...?". It can use code execution to test simple, self-contained snippets.
Note: compound-beta-mini
uses one tool per turn, so it might search OR execute, not both simultaneously in one response.
import os
from groq import Groq
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
# Example 1: Error Explanation (might trigger search)
debug_query_search = "I'm getting a 'Kubernetes CrashLoopBackOff' error on my pod. What are the common causes based on recent discussions?"
# Example 2: Code Check (might trigger code execution)
debug_query_exec = "Will this Python code raise an error? `import numpy as np; a = np.array([1,2]); b = np.array([3,4,5]); print(a+b)`"
# Choose one query to run
selected_query = debug_query_exec
chat_completion = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful coding assistant. You can explain errors, potentially searching for recent information, or check simple code snippets by executing them.",
},
{
"role": "user",
"content": selected_query,
}
],
# Use the compound model
model="compound-beta-mini",
)
print(f"Query: {selected_query}")
print(f"Compound Beta Response:\n{chat_completion.choices[0].message.content}")
Need to quickly create data visualizations from natural language descriptions? Compound Beta's code execution capabilities can help generate charts without writing visualization code directly.
Describe the chart you want in natural language, and Compound Beta will generate and execute the appropriate Python visualization code. The model automatically parses your request, generates the visualization code using libraries like matplotlib or seaborn, and returns the chart.
curl -X POST https://api.groq.com/openai/v1/chat/completions \
-H "Authorization: Bearer $GROQ_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "compound-beta",
"messages": [
{
"role": "user",
"content": "Create a scatter plot showing the relationship between market cap and daily trading volume for the top 5 tech companies (AAPL, MSFT, GOOGL, AMZN, META). Use current market data."
}
]
}'