重要
历史文档仅适用于百炼1.0版本的API和SDK调用,鉴于旧版功能已停止迭代更新,强烈建议您升级至最新版API和SDK,具体详情请参考新版本升级说明。
功能描述
本文主要介绍如何使用SDK调用阿里云百炼的第三方模型应用,包括从模型广场中创建的第三方大模型应用(如Llama2-7B、Llama2-13B、ChatGLM2-6B、百川2-7B和姜子牙-13B)。
调用说明
调用示例中的ACCESS_KEY_ID、ACCESS_KEY_SECRET、AGENT_KEY和APP_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 java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class ApplicationClientTest {
/**
* 三方模型应用示例
*/
public void testThirdModelCompletions() {
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<CompletionsRequest.ChatQaPair> history = new ArrayList<>();
history.add(new CompletionsRequest.ChatQaPair("我想去北京", "北京是一个非常值得去的地方"));
String prompt = "那边有什么推荐的旅游景点";
CompletionsRequest request = new CompletionsRequest()
.setAppId(appId)
.setPrompt(prompt)
.setHistory(history);
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, text: %s", response.getRequestId(), response.getData().getText());
}
}
import os
import broadscope_bailian
class CompletionTest():
def test_third_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()
chat_history = [
{"user": "我想去北京", "bot": "北京是一个非常值得去的地方"}
]
prompt = "那边有什么推荐的旅游景点"
resp = broadscope_bailian.Completions(token=token).create(
app_id=app_id,
prompt=prompt,
history=chat_history,
)
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")))
else:
print("request_id: %s, text: %s" % (resp.get("RequestId"), resp.get("Data", {}).get("Text")))
doc_references = resp.get("Data", {}).get("DocReferences")
if doc_references is not None and len(doc_references) > 0:
print("doc ref: %s" % doc_references[0].get("DocName"))
import (
"encoding/json"
"fmt"
client "github.com/aliyun/alibabacloud-bailian-go-sdk/client"
"github.com/google/uuid"
"log"
"os"
"strings"
"time"
)
/**
三方模型应用示例
*/
func TestCreateThirdModelCompletion() {
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}
prompt := "那边有什么推荐的旅游景点"
request := &client.CompletionRequest{
AppId: appId,
Prompt: prompt,
History: []client.ChatQaMessage{
{User: "我想去北京", Bot: "北京是一个非常值得去的地方"},
},
}
//调用文本生成接口
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.Text)
}
文档内容是否对您有帮助?