Skip to main content
More

Model calls in a sub-workspace

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

  1. 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.
  2. 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.
  3. 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/completions
This example shows how to call the qwen-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)
import os
from openai import OpenAI

client = OpenAI(
    # If the environment variable is not set, replace the following line with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    # For a list of models, see https://help.aliyun.com/en/model-studio/models
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who are you?"},
    ]
)
print(completion.model_dump_json())

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
You do not need to configure base_url for SDK calls.
This example shows how to call the 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)
import os
import dashscope

messages = [
    {'role': 'system', 'content': 'You are a helpful assistant.'},
    {'role': 'user', 'content': 'Who are you?'}
]
response = dashscope.Generation.call(
    # If the environment variable is not set, replace the following line with your Model Studio API key: api_key="sk-xxx"
    api_key=os.getenv('DASHSCOPE_API_KEY'),
    model="qwen-plus", # This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see: https://help.aliyun.com/en/model-studio/models
    messages=messages,
    result_format='message'
    )
print(response)

Error codes

If a model call returns an error, see Error Messages.

Next steps

Explore more models

The sample code uses the qwen-plus model. Model Studio also supports other Qwen modelsand third-party models like DeepSeek. Refer to Models for a list of supported models and their API reference.

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.

Text Generation
Image Generation
Video Generation
World models
3D Model Generation
Audio
  • Audio generation
Realtime API
Text Embedding
Decision Model
TokenPlan
Model Production