重要
历史文档仅适用于百炼1.0版本的API和SDK调用,鉴于旧版功能已停止迭代更新,强烈建议您升级至最新版API和SDK,具体详情请参考新版本升级说明。
功能描述
本文主要介绍如何使用SDK调用阿里云百炼的检索增强模型应用,包括从应用广场中创建的检索增强生成应用(如RAG检索增强应用模板、企业知识检索增强、企业知识检索增强-Max)。
调用说明
调用示例中的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.consts.DocReferenceTypeEnum;
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 testRagAppCompletions() {
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("API接口如何使用", "API接口需要传入prompt、app id并通过post方法调用"));
String prompt = "API接口说明中, TopP参数改如何传递?";
CompletionsRequest request = new CompletionsRequest()
.setAppId(appId)
.setPrompt(prompt)
.setHistory(history)
// 返回文档检索的文档引用数据, 传入为simple或indexed
.setDocReferenceType(DocReferenceTypeEnum.SIMPLE.getType())
// 文档标签code列表
.setDocTagCodes(Arrays.asList("471d*******3427", "881f*****0c232"));
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\n", response.getRequestId(), response.getData().getText());
List<CompletionsResponse.DocReference> docReferences = response.getData().getDocReferences();
if (docReferences != null && docReferences.size() > 0) {
System.out.printf("Doc ref: %s", docReferences.get(0).getDocName());
}
}
}
import os
import broadscope_bailian
class CompletionTest():
def test_rag_app_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": "API接口如何使用", "bot": "API接口需要传入prompt、app id并通过post方法调用"}
]
prompt = "API接口说明中, TopP参数改如何传递?"
resp = broadscope_bailian.Completions(token=token).create(
app_id=app_id,
prompt=prompt,
history=chat_history,
# 返回文档检索的文档引用数据, 传入为simple或indexed
doc_reference_type="simple",
# 文档标签code列表
doc_tag_codes=["471d*******3427", "881f*****0c232"]
)
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\n" % (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"
"testing"
"time"
)
/**
检索增强应用示例
*/
func TestCreateRagAppCompletion() {
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 := "API接口说明中, TopP参数改如何传递?"
request := &client.CompletionRequest{
AppId: appId,
Prompt: prompt,
History: []client.ChatQaMessage{
{User: "API接口如何使用", Bot: "API接口需要传入prompt、app id并通过post方法调用"},
},
// 返回文档检索的文档引用数据, 传入为simple或indexed
DocReferenceType: client.DocReferenceTypeSimple,
// 文档标签code列表
DocTagCodes: []string{"471d*******3427", "881f*****0c232"},
}
//调用文本生成接口
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)
if response.Data.DocReferences != nil && len(response.Data.DocReferences) > 0 {
log.Printf("Doc ref: %s\n", response.Data.DocReferences[0].DocName)
}
}
文档内容是否对您有帮助?