文档

子业务空间的模型调用

更新时间:
一键部署

本文主要介绍如何调用指定的业务空间的模型。

重要

通常情况下,通过主账号的API-KEY调用模型中心的模型时,不需要传递业务空间。只有当使用子账号的API-KEY调用模型时,需要通过某个业务空间进行模型的调用,才需要传递业务空间。

注:目前通过子账号的API-KEY无法访问默认业务空间,默认业务空间的模型和应用只能通过主账号的API-KEY进行调用。

前提条件

调用示例

SDK调用示例

以下示例展示了调用RAG检索增强应用进行企业知识库问答的代码。

说明

说明

需要使用您的API-KEY替换示例中的YOUR_API_KEY,并将WORKSPACE替换示例中的YOUR_WORKSPACE,代码才能正常运行。请参考Workspace ID获取WORKSPACE。

python sdk version: dashscope>=1.17.0

java sdk version: >=2.12.0

设置API-KEY

export DASHSCOPE_API_KEY=YOUR_API_KEY

代码示例

以下示例通过代码调用子空间的模型。

from http import HTTPStatus
import dashscope


def call_with_messages():
    messages = [{'role': 'system', 'content': 'You are a helpful assistant.'},
                {'role': 'user', 'content': '如何做炒西红柿鸡蛋?'}]

    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__':
    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();
        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("如何做西红柿鸡蛋?").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);
    }
}

API请求示例(SSE关闭)

以下示例展示通过CURL命令来调用子空间的模型的脚本。

说明

需要使用您的API-KEY替换示例中的YOUR_API_KEY,并将WORKSPACE替换示例中的YOUR_WORKSPACE,代码才能正常运行。请参考Workspace ID获取WORKSPACE。

curl --location 'https://dashscope.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": "如何做炒西红柿鸡蛋?"
            }
        ]
    },
    "parameters": {
    }
}'

API请求示例(SSE开启)

以下示例展示通过CURL命令来调用子空间的模型的脚本。

说明

需要使用您的API-KEY替换示例中的YOUR_API_KEY,并将WORKSPACE替换示例中的YOUR_WORKSPACE,代码才能正常运行。请参考Workspace ID获取WORKSPACE。

curl --location 'https://dashscope.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": "如何做炒西红柿鸡蛋?"
            }
        ]
    },
    "parameters": {
    }
}'

  • 本页导读 (1)