All Products
Search
Document Center

Alibaba Cloud Model Studio:Call a model in a sub-workspace

Last Updated:Jul 30, 2024

This topic describes how to call a model in the specified workspace.

Important

Typically, you do not need to specify the workspace ID when you call a model in the model center by using the API key of your Alibaba Cloud account. You must specify the workspace ID only when you call a model in a workspace by using the API key of a Resource Access Management (RAM) user.

Note: You cannot access the default workspace by using the API key of a RAM user. The models and applications in the default workspace can be called only by using the API key of the Alibaba Cloud account.

Prerequisites

  • An API key is created. For more information, see Obtain an API key.

  • A sub-workspace is created.

  • The RAM user is authorized to access the sub-workspace.

  • The sub-workspace is authorized to call models.

Call examples

SDK sample code

The following sample code shows how to call the Retrieval-Augmented Generation (RAG) application for answering questions by using an enterprise knowledge base.

Note

Note

To ensure that the code runs as expected, you must replace YOUR_API_KEY and YOUR_WORKSPACE in the code with your API key and workspace ID.

python sdk version: dashscope>=1.17.0

java sdk version: >=2.12.0

Specify the API key

export DASHSCOPE_API_KEY=YOUR_API_KEY

Sample code

The following sample code shows how to call a model in a sub-workspace.

from http import HTTPStatus
import dashscope


def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': 'Who are you'}]

    response = dashscope.Generation.call(
        dashscope.Generation.Models.qwen_turbo,
        messages=messages,
        result_format='message',  # set the result to be "message" format.
        workspace='YOUR_WORKSPACE'
    )
    if response.status_code == HTTPStatus.OK:
        print(response)
    else:
        print('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
            response.request_id, response.status_code,
            response.code, response.message
        ))


if __name__ == '__main__':
    dashscope.base_http_api_url = 'https://dashscope-intl.aliyuncs.com/api/v1'
    call_with_messages()
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;

public class Main {
    public static void callWithMessage()
            throws NoApiKeyException, ApiException, InputRequiredException {
        Generation gen = new Generation("http", "https://dashscope-intl.aliyuncs.com/api/v1");
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content("Who are you").build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        GenerationParam param =
                GenerationParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                        .workspace("YOUR_WORKSPACE")
                        .build();
        GenerationResult result = gen.call(param);
        System.out.println(result);
    }


    public static void main(String[] args){
        try {
            callWithMessage();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.println(e.getMessage());
        }
        System.exit(0);
    }
}

Sample request (SSE disabled)

The following sample code shows how to call a model in a sub-workspace by using a cURL command. In this example, Server-Sent Events (SSE) is disabled.

Note

To ensure that the code runs as expected, you must replace YOUR_API_KEY and YOUR_WORKSPACE in the code with your API key and workspace ID.

curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--header 'Content-Type: application/json' \
--header 'X-DashScope-WorkSpace: {YOUR_WORKSPACE}' \
--data '{
    "model": "qwen-turbo",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you"
            }
        ]
    },
    "parameters": {
    }
}'

Sample request (SSE enabled)

The following sample code shows how to call a model in a sub-workspace by using a cURL command. In this example, SSE is enabled.

Note

To ensure that the code runs as expected, you must replace YOUR_API_KEY and YOUR_WORKSPACE in the code with your API key and workspace ID.

curl --location 'https://dashscope-intl.aliyuncs.com/api/v1/services/aigc/text-generation/generation' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--header 'Content-Type: application/json' \
--header 'X-DashScope-WorkSpace: {YOUR_WORKSPACE}' \
--header 'X-DashScope-SSE: enable' \
--data '{
    "model": "qwen-turbo",
    "input":{
        "messages":[      
            {
                "role": "system",
                "content": "You are a helpful assistant."
            },
            {
                "role": "user",
                "content": "Who are you"
            }
        ]
    },
    "parameters": {
    }
}'