apps/opik-documentation/documentation/fern/docs/tracing/integrations/qianfan.mdx
Baidu Qianfan provides OpenAI-compatible API endpoints for hosted model access. This guide shows how to use the OpenAI SDK with Opik to trace and evaluate Qianfan calls.
First, ensure you have both opik and openai packages installed:
pip install opik openai
You will need a Qianfan API key and an OpenAI-compatible base URL. The Qianfan OpenAI-compatible base URL is:
https://api.baiduqianfan.ai/v1
Refer to the Qianfan documentation for the latest setup steps and model list.
from opik.integrations.openai import track_openai
from openai import OpenAI
# Initialize the OpenAI client with your Qianfan base URL
client = OpenAI(
base_url="https://api.baiduqianfan.ai/v1",
api_key="<QIANFAN_BEARER_TOKEN>", # Qianfan bearer token
default_headers={"appid": "<QIANFAN_APP_ID>"} # Optional Qianfan appid
)
client = track_openai(client)
response = client.chat.completions.create(
model="ernie-4.0-turbo-8k", # Use a model name from Qianfan
messages=[
{"role": "user", "content": "Hello, world!"}
],
temperature=0.7,
max_tokens=100
)
print(response.choices[0].message.content)
You can combine the tracked client with Opik's @track decorator for
end-to-end tracing:
from opik import track
from opik.integrations.openai import track_openai
from openai import OpenAI
client = OpenAI(
base_url="https://api.baiduqianfan.ai/v1",
api_key="<QIANFAN_BEARER_TOKEN>", # Qianfan bearer token
default_headers={"appid": "<QIANFAN_APP_ID>"} # Optional Qianfan appid
)
client = track_openai(client)
@track
def summarize_report(text: str) -> str:
response = client.chat.completions.create(
model="ernie-4.0-turbo-8k",
messages=[
{"role": "user", "content": text}
]
)
return response.choices[0].message.content
summary = summarize_report("Summarize this report in 3 bullets.")
print(summary)
Once you have Qianfan integrated with Opik, you can:
For more information about OpenAI-compatible APIs, see the OpenAI integration guide.