指南:Groq 模型的可观测性(Python)
本指南展示了两种与 Groq 模型交互并通过 Litefuse 追踪它们的方式:
- 使用 OpenAI SDK 与 Groq 模型交互
- 使用 OpenInference 插桩库与 Groq 模型交互
通过这些示例,你将学会如何记录和追踪与 Groq 语言模型的交互,从而调试和评估你 AI 驱动应用的性能。
注意: Litefuse 还原生集成了 LangChain、LlamaIndex、LiteLLM 以及其他框架。如果你使用其中任何一个,对 Groq 模型的所有调用都会被立即插桩。
要开始使用,请为 Litefuse 和 Groq 设置环境变量:
import os
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your Groq API key
os.environ["GROQ_API_KEY"] = "gsk_..."方式一:使用 OpenAI SDK 与 Groq 模型交互
注意: 本示例展示如何使用 OpenAI Python SDK。如果你使用 JS/TS,请查看我们的 OpenAI JS/TS SDK。
安装所需依赖
%pip install langfuse openai --upgrade导入所需模块
不要直接导入 openai,而是从 langfuse.openai 导入。同时导入其他需要的模块。
# Instead of: import openai
from langfuse.openai import OpenAI为 Groq 模型初始化 OpenAI 客户端
初始化 OpenAI 客户端,但将其指向 Groq 模型端点。请用你自己的 access token 替换。
client = OpenAI(
base_url="https://api.groq.com/openai/v1",
api_key=os.environ.get("GROQ_API_KEY")
)Chat completion 请求
使用 client 向 Groq 模型发起一次 chat completion 请求。
completion = client.chat.completions.create(
model="llama3-8b-8192",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "Write a poem about language models"
}
]
)
print(completion.choices[0].message.content)方式二:使用 OpenInference 插桩
这种方式会使用 OpenInference 插桩库 将 trace 发送到 Litefuse。
关于 Groq SDK 更详细的指引,请参阅 Groq 文档 和 Litefuse 文档。
安装所需依赖
%pip install groq langfuse openinference-instrumentation-groqimport os
# Get keys for your project from the project settings page: https://litefuse.cloud
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_BASE_URL"] = "https://litefuse.cloud"
# Your Groq API key
os.environ["GROQ_API_KEY"] = "gsk_..."设置好环境变量后,我们就可以初始化 Langfuse 客户端了。get_client() 会使用环境变量中提供的凭据初始化 Langfuse 客户端。
from langfuse import get_client
# Initialise Langfuse client and verify connectivity
langfuse = get_client()
assert langfuse.auth_check(), "Langfuse auth failed - check your keys ✋"OpenTelemetry 插桩
使用 OpenInference 插桩库 包装 Groq SDK 调用,并将 OpenTelemetry span 发送到 Litefuse。
from openinference.instrumentation.groq import GroqInstrumentor
GroqInstrumentor().instrument()LLM 调用示例
from groq import Groq
# Initialize Groq client
groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])chat_completion = groq_client.chat.completions.create(
messages=[
{
"role": "user",
"content": "Explain the importance of fast language models",
}
],
model="llama-3.3-70b-versatile",
)
print(chat_completion.choices[0].message.content)在 Litefuse 中查看 trace
运行示例模型调用后,你可以在 Litefuse 中查看 trace。你将看到关于 Groq API 调用的详细信息,包括:
- 请求参数(模型、messages、temperature 等)
- 响应内容
- token 使用统计
- 延迟指标

Interoperability with the Python SDK
You can use this integration together with the Litefuse SDKs to add additional attributes to the observation.
The @observe() decorator provides a convenient way to automatically wrap your instrumented code and add additional attributes to the observation.
from langfuse import observe, propagate_attributes, get_client
langfuse = get_client()
@observe()
def my_llm_pipeline(input):
# Add additional attributes (user_id, session_id, metadata, version, tags) to all spans created within this execution scope
with propagate_attributes(
user_id="user_123",
session_id="session_abc",
tags=["agent", "my-observation"],
metadata={"email": "user@litefuse.ai"},
version="1.0.0"
):
# YOUR APPLICATION CODE HERE
result = call_llm(input)
return result
# Run the function
my_llm_pipeline("Hi")Learn more about using the Decorator in the Langfuse SDK instrumentation docs.
Troubleshooting
No observations appearing
First, enable debug mode in the Python SDK:
export LANGFUSE_DEBUG="True"Then run your application and check the debug logs:
- OTel observations appear in the logs: Your application is instrumented correctly but observations are not reaching Litefuse. To resolve this:
- Call
langfuse.flush()at the end of your application to ensure all observations are exported. - Verify that you are using the correct API keys and base URL.
- Call
- No OTel spans in the logs: Your application is not instrumented correctly. Make sure the instrumentation runs before your application code.
Unwanted observations in Litefuse
The Langfuse SDK is based on OpenTelemetry. Other libraries in your application may emit OTel spans that are not relevant to you. These still count toward your billable units, so you should filter them out. See Unwanted spans in Litefuse for details.
Missing attributes
Some attributes may be stored in the metadata object of the observation rather than being mapped to the Litefuse data model. If a mapping or integration does not work as expected, please raise an issue on GitHub.
Next Steps
Once you have instrumented your code, you can manage, evaluate and debug your application: