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.
  3. Select a development language: Select a language and SDK that you are familiar with to call large model APIs.

Calling the model

OpenAI

  • Singapore
  • China (Beijing)
SDK base_url: https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1HTTP endpoint: POST https://{WorkspaceId}.ap-southeast-1.maas.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.
  • Python (SDK)
  • Java (SDK)
  • Node.js (SDK)
  • Go (SDK)
  • C# (HTTP)
  • PHP (HTTP)
  • curl (HTTP)
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"
    # The API keys for the Singapore and Beijing regions are different. Replace WorkspaceId with your actual workspace ID. To get an API key, see https://www.alibabacloud.com/help/model-studio/get-api-key
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    # This base_url is for the Singapore region. Replace WorkspaceId with your actual workspace ID. For models in the Beijing region, use: https://dashscope.aliyuncs.com/compatible-mode/v1
    base_url="https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/compatible-mode/v1",
)

completion = client.chat.completions.create(
    # This example uses qwen-plus. You can replace it with another model name as needed. For a list of models, see: https://www.alibabacloud.com/help/model-studio/getting-started/models
    model="qwen-plus",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who are you?"},
    ],
    # The enable_thinking parameter controls the thinking process for Qwen3 models (default is True for the open source version, False for the commercial version).
    # When using an open source Qwen3 model without enabling streaming output, uncomment the following line to avoid errors.
    # extra_body={"enable_thinking": False},
)
print(completion.model_dump_json())

DashScope

  • Singapore region
  • China (Beijing)
HTTP endpoint:
  • Qwen large language models: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/text-generation/generation
  • Qwen-VL/OCR models: POST https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1/services/aigc/multimodal-generation/generation
The base_url for SDK calls is:
  • Python
  • Java
dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
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

dashscope.base_http_api_url = 'https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1'
# The preceding line sets the base_url for the Singapore region. Replace WorkspaceId with your actual workspace ID. If you use a model in the China (Beijing) region, replace the base_url with: https://dashscope.aliyuncs.com/api/v1
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"
    # The API keys for the Singapore and China (Beijing) regions are different. To get an API key, see: https://www.alibabacloud.com/help/model-studio/get-api-key
    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://www.alibabacloud.com/help/model-studio/getting-started/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 models. 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.