The Qianfan Python SDK provides a convenient way to interact with Baidu Wenxin Qianfan Large Model Platform. It supports various AI capabilities including chat completion, text completion, embeddings, text-to-image, and more. The library is actively maintained, with frequent releases across Python, Go, and JavaScript, ensuring up-to-date access to Qianfan services.
pip install qianfanVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to perform a simple chat completion using the `qianfan` SDK. It initializes a `ChatCompletion` client, constructs messages using `QfMessages`, and sends a request to the ERNIE-Bot-4 model. Ensure your `QIANFAN_AK` and `QIANFAN_SK` environment variables are set for authentication.
Set `QIANFAN_AK` and `QIANFAN_SK` in your environment, or initialize clients like `ChatCompletion(ak="YOUR_AK", sk="YOUR_SK")`.
Always use the official, case-sensitive model names provided by Qianfan, e.g., `model="ERNIE-Bot-4"`.
Process streaming responses with a loop: `for chunk in chat_comp.do(..., stream=True): print(chunk.result)`.
Translate error messages or consult the Baidu Qianfan API documentation for known error codes and their English descriptions.
Verify that `QIANFAN_AK` and `QIANFAN_SK` environment variables are correctly set and valid, or explicitly pass `ak` and `sk` to the client constructor, e.g., `ChatCompletion(ak='YOUR_AK', sk='YOUR_SK')`.
Ensure `qianfan` is installed (`pip install qianfan`) and up-to-date (`pip install --upgrade qianfan`). Verify the import statement for typos, it should be `from qianfan import ChatCompletion`.
Check the official Qianfan documentation for the exact, case-sensitive names of available models. Ensure the model is enabled for your account. For versions >=0.4.12.3, pay close attention to model name casing.
Remove the `stream=...` parameter if the API endpoint does not support streaming (e.g., embedding generation). For chat/completion APIs that do support streaming, ensure your `qianfan` SDK version is up-to-date and the `stream` parameter is used correctly for that specific model.
No dependency data recorded yet.