模型应用

重要

历史文档仅适用于百炼1.0版本的APISDK调用,鉴于旧版功能已停止迭代更新,强烈建议您升级至最新版APISDK,具体详情请参考新版本升级说明

功能描述

本文主要介绍如何使用SDK调用阿里云百炼的模型应用,包括从模型广场中创建的官方大模型应用(如通义千问-Plus、通义千问等)、自训练模型应用和官方预置应用(如营销标题生成、营销文案生成、摘要抽取、文案续写和商品评论等)。

说明

首先,请参考文档模型应用接口说明,查看请求参数和响应参数说明。

其次,请参考文档安装SDK, 安装对应编程语言的SDK。

调用说明

调用示例中的ACCESS_KEY_ID、ACCESS_KEY_SECRET、AGENT_KEYAPP_ID从系统环境变量中获取。执行示例前,请确保已正确设置相关的变量,或者在代码中直接修改变量。比如:

String accessKeyId = "abc******123";
String accessKeySecret = "abd******124";
String agentKey = "abe******125";
String appId = "abf******126";
access_key_id = "abc******123"
access_key_secret = "abd******124"
agent_key = "abe******125"
app_id = "abf******126"
accessKeyId = "abc******123"
accessKeySecret = "abd******124"
agentKey = "abe******125"
appId = "abf******126"

示例

应用调用示例

import com.aliyun.broadscope.bailian.sdk.models.*;
import com.aliyun.broadscope.bailian.sdk.*;
import com.aliyun.broadscope.bailian.sdk.utils.UUIDGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ApplicationClientTest {
    /**
     * 官方模型调用应用、自训练模型应用示例
     */
    public void testModelCompletions() {
        String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");

        String agentKey = System.getenv("AGENT_KEY");
        String appId = System.getenv("APP_ID");

        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();
        ApplicationClient client = ApplicationClient.builder()
                .token(token)
                .build();

        List<ChatRequestMessage> messages = new ArrayList<>();
        messages.add(new ChatSystemMessage("你是一名历史学家, 帮助回答各种历史问题和历史知识"));
        messages.add(new ChatUserMessage("帮我生成一篇200字的文章,描述一下春秋战国的经济和文化"));

        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setMessages(messages)
                .setParameters(new CompletionsRequest.Parameter().setResultFormat("message"));

        CompletionsResponse response = client.completions(request);

        if (!response.isSuccess()) {
            System.out.printf("failed to create completion, requestId: %s, code: %s, message: %s\n",
                    response.getRequestId(), response.getCode(), response.getMessage());
            return;
        }

        System.out.printf("requestId: %s, content: %s, ", response.getRequestId(), response.getData().getChoices().get(0).getMessage().getContent());
    }
}
import os
import broadscope_bailian


class CompletionTest():
    def test_model_completions(self):
        """ 官方大模型调用应用、自训练模型应用示例 """

        access_key_id = os.environ.get("ACCESS_KEY_ID")
        access_key_secret = os.environ.get("ACCESS_KEY_SECRET")
        agent_key = os.environ.get("AGENT_KEY")
        app_id = os.environ.get("APP_ID")

        client = broadscope_bailian.AccessTokenClient(access_key_id=access_key_id, access_key_secret=access_key_secret,
                                                      agent_key=agent_key)
        token = client.get_token()

        resp = broadscope_bailian.Completions(token=token).create(
            app_id=app_id,
            messages=[
                {"role": "system", "content": "你是一名历史学家"},
                {"role": "user", "content": "帮我生成一篇200字的文章,描述一下春秋战国的经济和文化"}
            ],
            result_format="message"
        )

        if not resp.get("Success"):
            print('failed to create completion, request_id: %s, code: %s, message: %s' % (
                resp.get("RequestId"), resp.get("Code"), resp.get("Message")))
            return

        content = resp.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content")
        print("request_id: %s, content: %s\n" % (resp.get("RequestId"), content))
import (
	"encoding/json"
	"fmt"
	client "github.com/aliyun/alibabacloud-bailian-go-sdk/client"
	"github.com/google/uuid"
	"log"
	"os"
	"strings"
	"time"
)

/**
官方大模型调用应用、自训练模型应用示例
*/
func TestCreateModelCompletion() {
	accessKeyId := os.Getenv("ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")

	agentKey := os.Getenv("AGENT_KEY")
	appId := os.Getenv("APP_ID")

	//尽量避免多次初始化
	tokenClient := client.AccessTokenClient{AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, AgentKey: agentKey}
	token, err := tokenClient.GetToken()
	if err != nil {
		log.Printf("%v\n", err)
		return
	}

	cc := client.CompletionClient{Token: token}

	request := &client.CompletionRequest{
		AppId: appId,
		Messages: []client.ChatCompletionMessage{
			{Role: "system", Content: "你是一名历史学家, 帮助回答各种历史问题和历史知识"},
			{Role: "user", Content: "帮我生成一篇200字的文章,描述一下春秋战国的经济和文化"},
		},
		Parameters: &client.CompletionRequestModelParameter{ResultFormat: "message"},
	}

	response, err := cc.CreateCompletion(request)
	if err != nil {
		log.Printf("%v\n", err)
		return
	}

	if !response.Success {
		log.Printf("failed to create completion, requestId: %s, code: %s, message: %s\n",
			response.RequestId, response.Code, response.Message)
		return
	}

	log.Printf("requestId: %s, content : %s\n", response.RequestId, response.Data.Choices[0].Message.Content)

	usages := response.Data.Usage
	if usages != nil && len(usages) > 0 {
		usage := usages[0]
		log.Printf("modelId: %s, inputTokens: %d, outputTokens: %d\n", usage.ModelId, usage.InputTokens, usage.OutputTokens)
	}
}

应用调用示例(其他参数说明)

import com.aliyun.broadscope.bailian.sdk.models.*;
import com.aliyun.broadscope.bailian.sdk.*;
import com.aliyun.broadscope.bailian.sdk.utils.UUIDGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ApplicationClientTest {
    /**
     * 官方大模型调用应用、自训练模型应用-其他参数使用示例
     */
    public void testCompletionsWithParams() {
        String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");

        String agentKey = System.getenv("AGENT_KEY");
        String appId = System.getenv("APP_ID");

        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();

        ApplicationClient client = ApplicationClient.builder()
                .token(token)
                .connectOptions(new ConnectOptions(30000, 60000, 60000))
                .build();

        List<ChatRequestMessage> messages = new ArrayList<>();
        messages.add(new ChatSystemMessage("你是一个旅行专家, 能够帮我们制定旅行计划"));
        messages.add(new ChatUserMessage("我想去北京"));
        messages.add(new ChatAssistantMessage("北京是一个非常值得去的地方"));
        messages.add(new ChatUserMessage("那边有什么推荐的旅游景点"));

        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                //设置模型参数topP的值
                .setTopP(0.3)
                //设置prompt和历史上下文
                .setMessages(messages)
                .setParameters(new CompletionsRequest.Parameter()
                        //设置模型参数topK
                        .setTopK(50)
                        //设置模型参数seed
                        .setSeed(2222)
                        //设置模型参数temperature
                        .setTemperature(0.7)
                        //设置最大内容token数
                        .setMaxTokens(50)
                        //设置停止词
                        .setStop(Collections.singletonList("景点"))
                        //设置内容返回结构为message
                        .setResultFormat("message"));

        CompletionsResponse response = client.completions(request);
        if (!response.isSuccess()) {
            System.out.printf("failed to create completion, requestId: %s, code: %s, message: %s",
                    response.getRequestId(), response.getCode(), response.getMessage());
            return;
        }

        System.out.printf("requestId: %s, content: %s。", response.getRequestId(), response.getData().getChoices().get(0).getMessage().getContent());
        if (response.getData().getUsage() != null && response.getData().getUsage().size() > 0) {
            CompletionsResponse.Usage usage = response.getData().getUsage().get(0);
            System.out.printf("model: %s, input tokens: %d, output tokens: %d\n", usage.getModelId(), usage.getInputTokens(), usage.getOutputTokens());
        }
    }
}
import os
import broadscope_bailian


class CompletionTest():
    def test_completions_with_params(self):
        """ 官方大模型调用应用、自训练模型应用-其他参数使用示例 """

        access_key_id = os.environ.get("ACCESS_KEY_ID")
        access_key_secret = os.environ.get("ACCESS_KEY_SECRET")
        agent_key = os.environ.get("AGENT_KEY")
        app_id = os.environ.get("APP_ID")

        client = broadscope_bailian.AccessTokenClient(access_key_id=access_key_id, access_key_secret=access_key_secret,
                                                      agent_key=agent_key)
        token = client.get_token()

        # 设置prompt和历史上下文
        messages = [
            {"role": "system", "content": "你是一个旅行专家, 能够帮我们制定旅行计划"},
            {"role": "user", "content": "我想去北京"},
            {"role": "assistant", "content": "北京是一个非常值得去的地方"},
            {"role": "user", "content": "那边有什么推荐的旅游景点"}
        ]

        resp = broadscope_bailian.Completions(token=token).create(
            app_id=app_id,
            messages=messages,
            # 设置模型参数topP的值
            top_p=0.2,
            # 设置模型参数topK
            top_k=50,
            # 设置模型参数seed
            seed=2222,
            # 设置模型参数temperature
            temperature=0.3,
            # 设置模型参数max tokens
            max_tokens=50,
            # 按message方式返回结果
            result_format="message",
            # 设置停止词
            stop=["景点"],
            # 超时设置, 单位秒
            timeout=30,
        )

        if not resp.get("Success"):
            print('failed to create completion, request_id: %s, code: %s, message: %s' % (
                resp.get("RequestId"), resp.get("Code"), resp.get("Message")))
            return

        content = resp.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content")
        print("request_id: %s, content: %s\n" % (resp.get("RequestId"), content))
        if resp.get("Data", "").get("Usage") is not None and len(resp.get("Data", "").get("Usage")) > 0:
            usage = resp.get("Data", "").get("Usage")[0]
            print("input tokens: %d, output tokens: %d" % (usage.get("InputTokens"), usage.get("OutputTokens")))
import (
	"encoding/json"
	"fmt"
	client "github.com/aliyun/alibabacloud-bailian-go-sdk/client"
	"github.com/google/uuid"
	"log"
	"os"
	"strings"
	"time"
)

/**
官方大模型调用应用、自训练模型应用-其他参数使用示例
*/
func TestCreateCompletionWithParams() {
	accessKeyId := os.Getenv("ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")

	agentKey := os.Getenv("AGENT_KEY")
	appId := os.Getenv("APP_ID")

	//尽量避免多次初始化
	tokenClient := client.AccessTokenClient{AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, AgentKey: agentKey}
	token, err := tokenClient.GetToken()
	if err != nil {
		log.Printf("%v\n", err)
		return
	}

	cc := client.CompletionClient{Token: token}

	//设置超时时间
	cc.Timeout = 30 * time.Second

	request := &client.CompletionRequest{
		AppId: appId,
		//设置模型参数topP的值
		TopP: 0.2,
		//设置prompt和历史上下文
		Messages: []client.ChatCompletionMessage{
			{Role: "system", Content: "你是一个旅行专家, 能够帮我们制定旅行计划"},
			{Role: "user", Content: "我想去北京"},
			{Role: "assistant", Content: "北京是一个非常值得去的地方"},
			{Role: "user", Content: "那边有什么推荐的旅游景点"},
		},
		Parameters: &client.CompletionRequestModelParameter{
			//设置模型参数topK
			TopK: 50,
			//设置模型参数seed
			Seed: 2222,
			//设置模型参数temperature
			Temperature: 0.7,
			//设置模型参数max tokens
			MaxTokens: 20,
			//设置停止词
			Stop: []string{"景点"},
			//设置内容返回结构为message
			ResultFormat: "message",
		},
	}

	//调用文本生成接口
	response, err := cc.CreateCompletion(request)
	if err != nil {
		log.Printf("%v\n", err)
		return
	}

	if !response.Success {
		log.Printf("failed to create completion, requestId: %s, code: %s, message: %s\n",
			response.RequestId, response.Code, response.Message)
		return
	}

	log.Printf("requestId: %s, text: %s\n", response.RequestId, response.Data.Choices[0].Message.Content)
}

应用调用示例(最大连接池设置)

import com.aliyun.broadscope.bailian.sdk.models.*;
import com.aliyun.broadscope.bailian.sdk.*;
import com.aliyun.broadscope.bailian.sdk.utils.UUIDGenerator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class ApplicationClientTest {
    /**
     * 官方大模型调用应用、自训练模型应用-其他参数使用示例
     */
    public void testModelCompletions() {
        String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");

        String agentKey = System.getenv("AGENT_KEY");
        String appId = System.getenv("APP_ID");

        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();

        ConnectOptions connectOptions = new ConnectOptions();
        // 设置连接池最大数据量为20, 默认为5
        connectOptions.setConnectPoolSize(20);

        ApplicationClient client = ApplicationClient.builder()
                .token(token)
                .connectOptions(connectOptions)
                .build();

        List<ChatRequestMessage> messages = new ArrayList<>();
        messages.add(new ChatSystemMessage("你是一名历史学家, 帮助回答各种历史问题和历史知识"));
        messages.add(new ChatUserMessage("帮我生成一篇200字的文章,描述一下春秋战国的文化和经济"));

        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setMessages(messages)
                .setParameters(new CompletionsRequest.Parameter().setResultFormat("message"));

        CompletionsResponse response = client.completions(request);

        if (!response.isSuccess()) {
            System.out.printf("failed to create completion, requestId: %s, code: %s, message: %s\n",
                    response.getRequestId(), response.getCode(), response.getMessage());
            return;
        }

        System.out.printf("requestId: %s, content: %s, ", response.getRequestId(), response.getData().getChoices().get(0).getMessage().getContent());
    }
}

应用调用示例(流式SSE)

import com.aliyun.broadscope.bailian.sdk.models.*;
import com.aliyun.broadscope.bailian.sdk.*;
import com.aliyun.broadscope.bailian.sdk.utils.UUIDGenerator;
import org.apache.commons.lang3.exception.ExceptionUtils;
import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;


public class ApplicationClientTest {
    /**
     * 流式响应使用示例
     */
    public void testCompletionsWithStream() {
        String accessKeyId = System.getenv("ACCESS_KEY_ID");
        String accessKeySecret = System.getenv("ACCESS_KEY_SECRET");

        String agentKey = System.getenv("AGENT_KEY");
        String appId = System.getenv("APP_ID");

        AccessTokenClient accessTokenClient = new AccessTokenClient(accessKeyId, accessKeySecret, agentKey);
        String token = accessTokenClient.getToken();
        ApplicationClient client = ApplicationClient.builder()
                .token(token)
                .build();

        List<ChatRequestMessage> messages = new ArrayList<>();
        messages.add(new ChatSystemMessage("你是一名天文学家, 能够帮助小学生回答宇宙与天文方面的问题"));
        messages.add(new ChatUserMessage("宇宙中为什么会存在黑洞"));

        CompletionsRequest request = new CompletionsRequest()
                .setAppId(appId)
                .setMessages(messages)
                .setParameters(new CompletionsRequest.Parameter()
                        //开启增量输出模式,后面输出不会包含已经输出的内容
                        .setIncrementalOutput(true)
                        //返回choice message结果
                        .setResultFormat("message")
                );

        CountDownLatch latch = new CountDownLatch(1);
        Flux<CompletionsResponse> response = client.streamCompletions(request);

        response.subscribe(
                data -> {
                    if (data.isSuccess()) {
                        System.out.printf("%s", data.getData().getChoices().get(0).getMessage().getContent());
                    } else {
                        System.out.printf("failed to create completion, requestId: %s, code: %s, message: %s\n",
                                data.getRequestId(), data.getCode(), data.getMessage());
                    }
                },
                err -> {
                    System.out.printf("failed to create completion, err: %s\n", ExceptionUtils.getStackTrace(err));
                    latch.countDown();
                },
                () -> {
                    System.out.println("\ncreate completion completely");
                    latch.countDown();
                }
        );

        try {
            latch.await(30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new BaiLianSdkException(e);
        }
    }
}
import os
import broadscope_bailian


class CompletionTest():
    def test_stream_completions(self):
        """ 流式响应使用示例 """

        access_key_id = os.environ.get("ACCESS_KEY_ID")
        access_key_secret = os.environ.get("ACCESS_KEY_SECRET")
        agent_key = os.environ.get("AGENT_KEY")
        app_id = os.environ.get("APP_ID")

        client = broadscope_bailian.AccessTokenClient(access_key_id=access_key_id, access_key_secret=access_key_secret,
                                                      agent_key=agent_key)
        token = client.get_token()

        resp = broadscope_bailian.Completions(token=token).create(
            app_id=app_id,
            messages=[
                {"role": "system", "content": "你是一名天文学家, 能够帮助小学生回答宇宙与天文方面的问题"},
                {"role": "user", "content": "宇宙中为什么会存在黑洞"}
            ],
            stream=True,
            # 返回choice message结果
            result_format="message",
            # 开启增量输出模式,后面输出不会包含已经输出的内容
            incremental_output=True
        )

        for result in resp:
            if not result.get("Success"):
                print("failed to create completion, request_id: %s, code: %s, message: %s" %
                      (result.get("RequestId"), result.get("Code"), result.get("Message")))
            else:
                print("%s" % result.get("Data", {}).get("Choices", [])[0].get("Message", {}).get("Content"),
                      end="", flush=True)
import (
	"encoding/json"
	"fmt"
	client "github.com/aliyun/alibabacloud-bailian-go-sdk/client"
	"github.com/google/uuid"
	"log"
	"os"
	"strings"
	"time"
)

/**
流式响应使用示例
*/
func TestCreateStreamCompletion() {
	accessKeyId := os.Getenv("ACCESS_KEY_ID")
	accessKeySecret := os.Getenv("ACCESS_KEY_SECRET")

	agentKey := os.Getenv("AGENT_KEY")
	appId := os.Getenv("APP_ID")

	//尽量避免多次初始化
	tokenClient := client.AccessTokenClient{AccessKeyId: accessKeyId, AccessKeySecret: accessKeySecret, AgentKey: agentKey}
	token, err := tokenClient.GetToken()
	if err != nil {
		log.Printf("%v\n", err)
		return
	}

	cc := client.CompletionClient{Token: token}

	request := &client.CompletionRequest{
		AppId: appId,
		Messages: []client.ChatCompletionMessage{
			{Role: "system", Content: "你是一名天文学家, 能够帮助小学生回答宇宙与天文方面的问题"},
			{Role: "user", Content: "宇宙中为什么会存在黑洞"},
		},
		Parameters: &client.CompletionRequestModelParameter{
			//返回choice message结果
			ResultFormat: "message",
			//开启增量输出模式,后面输出不会包含已经输出的内容
			IncrementalOutput: true,
		},
	}

	response, err := cc.CreateStreamCompletion(request)
	if err != nil {
		log.Printf("failed to create completion, err: %v\n", err)
		return
	}

	for result := range response {
		if !result.Success {
			log.Printf("get result with error, requestId: %s, code: %s, message: %s\n",
				result.RequestId, result.Code, result.Message)
		} else {
			fmt.Printf("%s", result.Data.Choices[0].Message.Content)
		}
	}
	fmt.Printf("\n")
}