The DashScope Python SDK provides a client library for interacting with Alibaba Cloud's DashScope AI services, offering access to various models including Large Language Models (LLMs), text-to-image, and embedding models. The library is actively developed, with its current version at 1.25.16, and new releases occurring frequently to incorporate updates and new features.
pip install dashscopeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use the `dashscope.Generation` API to interact with an LLM, such as `qwen-turbo`. It emphasizes setting the API key via an environment variable for security and includes basic error handling. It also highlights the importance of potentially setting the `base_http_api_url` for different regions.
Ensure `DASHSCOPE_API_KEY` environment variable is set or `dashscope.api_key = 'YOUR_API_KEY'` is used before making calls. For file-based keys, use `DASHSCOPE_API_KEY_FILE_PATH` or `dashscope.api_key_file_path`.
Explicitly set `dashscope.base_http_api_url` to the correct endpoint for your region. For international, often `https://dashscope-intl.aliyuncs.com/api/v1`. For Mainland China, `https://dashscope.aliyuncs.com/api/v1` or `https://dashscope.aliyuncs.com/compatible-mode/v1`.
Use `Generation.call()` for text-only models (like Qwen-Turbo, Qwen-Plus) and `MultiModalConversation.call()` for multimodal models (like Qwen-VL, Qwen-Omni) that accept image or video inputs.
If `stream=True`, iterate through the `responses` object (e.g., `for chunk in responses: print(chunk.output.choices[0].text)`).
Adhere to the specified file size limits and ensure image/video formats are supported. For local files, ensure they are correctly Base64 encoded if required by the API specifications for direct content inclusion.
Run `pip install dashscope` in your terminal to install the library.
Ensure your `DASHSCOPE_API_KEY` environment variable is set to a valid API key, or explicitly set `dashscope.api_key = 'YOUR_DASHSCOPE_API_KEY'` in your code. Verify the API base URL if you are in Mainland China or using an OpenAI-compatible client, as it might need to be set to `https://dashscope.aliyuncs.com/compatible-mode/v1` or another regional endpoint.
Always check the API response's status code or for the presence of `response.output` (e.g., `if response.output and response.output.choices:`) before attempting to access nested attributes. Implement error handling and retry mechanisms for transient failures like rate limits.
Review the documentation for the specific class or function you are using to ensure all required parameters are provided with correct types and values. For integration libraries, confirm how the API key and other configurations are expected to be passed to the DashScope components.