功能介绍
本文主要介绍如何基于阿里云百炼的应用中心快速创建RAG应用,并通过API/SDK低代码方式调用RAG应用进行文档问答的最佳实践。
创建应用
Step 1:数据管理-导入数据
在左侧导航栏中,选择在非结构化数据页签,选择“默认类目”后,点击“导入数据”,然后通过“本地上传”的方式导入数据。导入数据需要花费一定的时间,需要耐心等待数据转为“导入完成”的状态(通过手动点击刷新按钮)。 。
Step 2:创建知识索引
在左侧导航栏中,选择点击创建知识库,输入知识库名称,保持默认配置,点击下一步。 。
选择相关文件,点击下一步。
保持默认配置,点击导入完成,系统自动进行文档解析。
文档解析需要一定时间,请您耐心等待,直至状态变更为“解析完成”状态,才能在后续的文档问答过程中被检索到。
返回知识库索引主页, 获取知识索引ID。知识索引支持与百炼Assistant API结合使用,支持RAG和插件的组合调用。
Step 3:创建应用
在左侧导航栏中,选择单击 。 ,进入创建应用页面,配置以下参数:
单击选择模型。同时,还支持配置与模型生成内容相关的参数,例如,温度系数等。
打开知识库检索增强开关,单击“配置知识库”。
选择知识库,即在Step2中创建的知识索引。
单击“发布”按钮。
应用发布后,即可在右侧的窗口进行效果测试。
在应用列表页面,获取应用ID。
调用应用
前提条件
您需要已获取API Key并配置API Key到环境变量。
如果通过API/SDK调用应用,需要获取到应用ID。
如果通过Assistant API调用,需要获取到知识索引ID。
已安装最新版SDK:安装SDK。
通过API/SDK调用应用
您需要将YOUR_APP_ID
替换为已获取的应用ID,代码才能正常运行。
Python
import os
from http import HTTPStatus
from dashscope import Application
def call_agent_app():
response = Application.call(app_id='YOUR_APP_ID',
prompt='百炼的业务空间是什么?如何使用业务空间?',
api_key=os.getenv("DASHSCOPE_API_KEY"), # 若没有配置环境变量,请用百炼API Key将本行替换为:api_key="sk-xxx",
)
if response.status_code != HTTPStatus.OK:
print('request_id=%s, code=%s, message=%s\n' % (response.request_id, response.status_code, response.message))
else:
print('request_id=%s\n output=%s\n usage=%s\n' % (response.request_id, response.output, response.usage))
if __name__ == '__main__':
call_agent_app()
Java
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
public class Main{
public static void callAgentApp()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
// 若没有配置环境变量,请用百炼API Key将下行替换为:.apiKey("sk-xxx")
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.appId("YOUR_APP_ID")
.prompt("百炼的业务空间是什么?如何使用业务空间?")
.build();
Application application = new Application();
ApplicationResult result = application.call(param);
System.out.printf("requestId: %s, text: %s, finishReason: %s\n",
result.getRequestId(), result.getOutput().getText(), result.getOutput().getFinishReason());
}
public static void main(String[] args) {
try {
callAgentApp();
} catch (ApiException | NoApiKeyException | InputRequiredException e) {
System.out.printf("Exception: %s", e.getMessage());
}
System.exit(0);
}
}
curl
curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
--header "Authorization: Bearer $DASHSCOPE_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "百炼的业务空间是什么?如何使用业务空间?"
},
"parameters": {},
"debug": {}
}' --verbose
通过Assistant API调用
您需要将YOUR_PIPELINE_ID
替换为已经获取的知识索引ID,代码才能正常运行。
其中${document1}
为占位符,知识库检索回来的内容会替换instructions
中的${document1}
,请确保RAG工具中定义query_word/value
的占位符与instructions
中占位符一致。
import os
from dashscope import Assistants, Messages, Runs, Threads
assistant = Assistants.create(
api_key=os.getenv("DASHSCOPE_API_KEY"),
model='qwen-max',
name='smart helper',
description='智能助手,支持知识库查询和插件调用。',
instructions='你是一个智能助手,请记住以下信息。${document1}',
tools=[
{
"type": "code_interpreter"
},
{
"type": "rag",
"prompt_ra": {
"pipeline_id": "YOUR_PIPELINE_ID",
"parameters": {
"type": "object",
"properties": {
"query_word": {
"type": "str",
"value": "${document1}"
}
}
}
}
}]
)
def send_message(assistant, message='百炼是什么?'):
print(f"Query: {message}")
# create thread.
# create a thread.
thread = Threads.create()
print(thread)
# create a message.
message = Messages.create(thread.id, content=message)
# create run
run = Runs.create(thread.id, assistant_id=assistant.id)
print(run)
# # get run statue
# run_status = Runs.get(run.id, thread_id=thread.id)
# print(run_status)
# wait for run completed or requires_action
run_status = Runs.wait(run.id, thread_id=thread.id)
# print(run_status)
# if prompt input tool result, submit tool result.
run_status = Runs.get(run.id, thread_id=thread.id)
print(run_status)
# verify_status_code(run_status)
# get the thread messages.
msgs = Messages.list(thread.id)
# print(msgs)
# print(json.dumps(msgs, default=lambda o: o.__dict__, sort_keys=True, indent=4))
print("运行结果:")
for message in msgs['data'][::-1]:
print("content: ", message['content'][0]['text']['value'])
print("\n")
if __name__ == "__main__":
send_message(assistant, message='百炼是什么?')
import com.alibaba.dashscope.assistants.Assistant;
import com.alibaba.dashscope.assistants.AssistantParam;
import com.alibaba.dashscope.assistants.Assistants;
import com.alibaba.dashscope.common.GeneralListParam;
import com.alibaba.dashscope.common.ListResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.InvalidateParameter;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.threads.AssistantThread;
import com.alibaba.dashscope.threads.ContentText;
import com.alibaba.dashscope.threads.ThreadParam;
import com.alibaba.dashscope.threads.Threads;
import com.alibaba.dashscope.threads.messages.Messages;
import com.alibaba.dashscope.threads.messages.TextMessageParam;
import com.alibaba.dashscope.threads.messages.ThreadMessage;
import com.alibaba.dashscope.threads.runs.Run;
import com.alibaba.dashscope.threads.runs.RunParam;
import com.alibaba.dashscope.threads.runs.Runs;
import com.alibaba.dashscope.tools.ToolBase;
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;
import java.lang.System;
public class AssistantApiRag {
public static Assistant createAssistant(String pipelineId) throws ApiException, NoApiKeyException {
//注意instructions的${document1}占位符和buildPromptRa的${document1}占位符必须保持一致
AssistantParam assistantParam = AssistantParam.builder()
.apiKey(System.getenv("DASHSCOPE_API_KEY"))
.model("qwen-max")
.name("smart helper")
.description("智能助手,支持知识库查询和插件调用。")
.instructions("你是一个智能助手,请记住以下信息。${document1}")
.tool(ToolRag.builder()
.promptRa(ToolRag.buildPromptRa("${document1}", pipelineId))
.build())
.build();
Assistants assistants = new Assistants();
return assistants.create(assistantParam);
}
public static void sendMessage(Assistant assistant, String message) throws NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
Threads threads = new Threads();
AssistantThread assistantThread = threads.create(ThreadParam.builder().build());
Runs runs = new Runs();
// create a new message
TextMessageParam textMessageParam = TextMessageParam.builder()
.role("user")
.content(message)
.build();
Messages messages = new Messages();
ThreadMessage threadMessage = messages.create(assistantThread.getId(), textMessageParam);
System.out.println(threadMessage);
RunParam runParam = RunParam.builder().assistantId(assistant.getId()).build();
Run run = runs.create(assistantThread.getId(), runParam);
while (true) {
if (run.getStatus().equals(Run.Status.CANCELLED) ||
run.getStatus().equals(Run.Status.COMPLETED) ||
run.getStatus().equals(Run.Status.FAILED) ||
run.getStatus().equals(Run.Status.REQUIRES_ACTION) ||
run.getStatus().equals(Run.Status.EXPIRED)) {
break;
} else {
Thread.sleep(1000);
}
run = runs.retrieve(assistantThread.getId(), run.getId());
}
System.out.println(run);
GeneralListParam listParam = GeneralListParam.builder().limit(100L).build();
ListResult<ThreadMessage> threadMessages = messages.list(assistantThread.getId(), listParam);
for (ThreadMessage threadMessage2 : threadMessages.getData()) {
System.out.printf("content: %s\n", ((ContentText) threadMessage2.getContent().get(0)).getText().getValue());
}
}
public static void main(String[] args) throws NoApiKeyException, InputRequiredException, InvalidateParameter, InterruptedException {
String pipelineId = "YOUR_PIPELINE_ID";
Assistant assistant = createAssistant(pipelineId);
sendMessage(assistant, "百炼是什么?");
}
}
@Data
@EqualsAndHashCode(callSuper = false)
@SuperBuilder
class ToolRag extends ToolBase {
static {
registerTool("rag", ToolRag.class);
}
@Builder.Default
private String type = "rag";
@SerializedName("prompt_ra")
private JsonObject promptRa;
@Override
public String getType() {
return type;
}
public static JsonObject buildPromptRa(String placeholder, String pipelineId) {
JsonObject queryWord = new JsonObject();
queryWord.addProperty("type", "str");
queryWord.addProperty("value", placeholder);
JsonObject properties = new JsonObject();
properties.add("query_word", queryWord);
JsonObject parameters = new JsonObject();
parameters.addProperty("type", "object");
parameters.add("properties", properties);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("pipeline_id", pipelineId);
jsonObject.add("parameters", parameters);
return jsonObject;
}
}