Qwen vision models on Alibaba Cloud Model Studio are compatible with the OpenAI interface specification. You only need to modify three parameters to migrate your existing OpenAI applications to Model Studio:
-
base_url:
- China (Beijing): https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1
- Singapore: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1. Replace {WorkspaceId} with your actual workspace ID.
- Japan (Tokyo): https://{WorkspaceId}.ap-northeast-1.maas.aliyuncs.com/compatible-mode/v1. Replace {WorkspaceId} with your actual workspace ID.
- US (Virginia): https://{WorkspaceId}.us-east-1.maas.aliyuncs.com/compatible-mode/v1
- China (Hong Kong): https://{WorkspaceId}.cn-hongkong.maas.aliyuncs.com/compatible-mode/v1. Replace
WorkspaceIdwith your actual workspace ID.
- api_key: Replace with your Model Studio API key
- model: Replace with a model name from the following list
Supported models
Supported models: Qwen-VL, QVQ, Qwen-OCR
Available models vary by region. For details, see the Model Studio console.
Model invocation
Examples
This section provides streaming call examples for Python (OpenAI SDK and LangChain_OpenAI SDK) and cURL (HTTP). For more programming languages or input method examples, see Vision request examples.
QVQ models only support streaming output. For details, see Visual reasoning.
- OpenAI SDK
LangChain OpenAI SDK
Prerequisites
- Make sure that Python is installed on your computer.
- Install the langchain_openai SDK by running the following command.
- You need to activate Model Studio and obtain an API key. For details, see Obtain an API key.
- We recommend that you configure the API key as an environment variable to reduce the risk of leakage. For details, see Configure API key as an environment variable. You can also configure the API key directly in the code, but this increases the risk of leakage.
Usage
You can use the following examples to call Qwen vision models through the langchain_openai SDK.
Non-streaming output
Non-streaming output uses the invoke method. See the following sample code:
Streaming output
The following examples do not apply to QVQ models. For QVQ usage, see Visual reasoning.
HTTP
You can call Qwen vision models through the HTTP interface and get responses in the same structure as calling OpenAI services through HTTP.
Prerequisites
- You need to activate Model Studio and obtain an API key. For details, see Obtain an API key.
- We recommend that you configure the API key as an environment variable to reduce the risk of leakage. For details, see Configure API key as an environment variable. You can also configure the API key directly in the code, but this increases the risk of leakage.
API endpoints
Request examples
The following examples show how to call the API using cURL commands.
If you have not configured the API key as an environment variable, replace $DASHSCOPE_API_KEY with your API key*.*