Using Qwen-Plus as an example, this topic explains how to invoke a model via an API in a sub-workspace (a non-default workspace).
Scenarios
- Control which models specific users can call: The API key for the default workspace can call all models, giving it excessive permissions. To control which models a RAM user can call, add the user to a sub-workspace, grant permissions for only the necessary models, and have them use that workspace's API key.
- Allocate model call costs: Using the default workspace for multiple services or scenarios makes it difficult to distinguish the model call costs for each. Create a separate sub-workspace for each service or scenario to generate separate bills and simplify cost allocation.
Prerequisites
-
Get the API key for the sub-workspace: To call large model APIs, create an API key in the sub-workspace and set it as an environment variable named
DASHSCOPE_API_KEY. -
Set model call permissions (required for standard models): To call a standard model, such as
qwen-plus, with the API key from a sub-workspace, you must first set model call permissions for that sub-workspace.Note: Models that you fine-tune and deploy in Alibaba Cloud Model Studio do not require model call permissions. However, you can only call these models with the API key from their workspace.
- Select a development language: Select a language and SDK that you are familiar with to call large model APIs.
Calling the model
OpenAI
- Beijing
- Singapore
SDK
base_url: https://dashscope.aliyuncs.com/compatible-mode/v1HTTP endpoint: POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completionsqwen-plus standard model in a sub-workspace using the OpenAI-compatible method. Unlike calls in the default workspace, you must use the API key from the sub-workspace.
Calling a model that has been fine-tuned on Alibaba Cloud Model Studio: You can call the model only by using DashScope. The OpenAI-compatible method is not supported.
Python (SDK)
DashScope
- China (Beijing)
- Singapore region
HTTP endpoint:
- Qwen large language models:
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation - Qwen-VL/Audio models:
POST https://dashscope.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
base_url for SDK calls.qwen-plus model in a sub-workspace using DashScope. The key difference from using the default workspace is that you must use the API key for the sub-workspace.
- Python (SDK)
- Java (SDK)
- PHP (HTTP)
- Node.js (HTTP)
- C# (HTTP)
- Go (HTTP)
- curl (HTTP)
Error codes
If a model call returns an error, see Error Messages.
Next steps
Explore more models | The sample code uses the |
Advanced usage | The sample code demonstrates a basic Q&A scenario. The Text generation overview covers advanced Qwen API features, including streaming output, structured output, and function calling. |