低代碼快速構(gòu)建RAG應(yīng)用
功能介紹
本文主要介紹如何基于阿里云百煉的應(yīng)用中心快速創(chuàng)建RAG應(yīng)用,并通過API/SDK低代碼方式調(diào)用RAG應(yīng)用進(jìn)行文檔問答的最佳實(shí)踐。
創(chuàng)建應(yīng)用
Step 1:數(shù)據(jù)管理-導(dǎo)入數(shù)據(jù)
在數(shù)據(jù)中心,選擇“默認(rèn)類目”后,點(diǎn)擊“導(dǎo)入數(shù)據(jù)”,然后通過“本地上傳”的方式導(dǎo)入數(shù)據(jù)。導(dǎo)入數(shù)據(jù)需要花費(fèi)一定的時(shí)間,需要耐心等待數(shù)據(jù)轉(zhuǎn)為“導(dǎo)入完成”的狀態(tài)(通過手動(dòng)點(diǎn)擊刷新按鈕)。
Step 2:創(chuàng)建知識(shí)索引
1)在數(shù)據(jù)應(yīng)用-知識(shí)索引頁(yè)面,創(chuàng)建知識(shí)庫(kù),輸入知識(shí)庫(kù)描述,選擇推薦配置即可。
2)點(diǎn)擊下一步,從數(shù)據(jù)中心選擇相關(guān)文件,選擇導(dǎo)入完成,進(jìn)行文檔切分、構(gòu)建索引等后續(xù)操作。
注:導(dǎo)入數(shù)據(jù)后,需要耐心等待知識(shí)庫(kù)中的數(shù)據(jù)轉(zhuǎn)為“解析完成”狀態(tài),才能在后續(xù)的文檔問答過程中被檢索到。
3) 獲取知識(shí)索引ID,支持與百煉Assistant API結(jié)合使用,支持RAG和插件的組合調(diào)用;
Step 3:創(chuàng)建應(yīng)用
進(jìn)入我的應(yīng)用后,單擊新增應(yīng)用,單擊智能體應(yīng)用,創(chuàng)建RAG應(yīng)用,然后在應(yīng)用配置中,進(jìn)行以下操作:
選擇模型。同時(shí),還支持配置與模型生成內(nèi)容相關(guān)的參數(shù),例如,溫度系數(shù)等。
單擊“配置知識(shí)庫(kù)”。
選擇知識(shí)庫(kù),即在Step2中創(chuàng)建的知識(shí)索引。
單擊“發(fā)布”按鈕。
應(yīng)用發(fā)布后,即可在右側(cè)的窗口進(jìn)行效果測(cè)試。
調(diào)用應(yīng)用
前提條件
已創(chuàng)建API-KEY:獲取API-KEY。
已獲取到APP_ID:獲取API-KEY、APP-ID和Workspace ID。
已安裝最新版SDK:安裝SDK。
通過API/SDK調(diào)用應(yīng)用
需要使用您的API-KEY替換示例中的YOUR_API_KEY,并將APP-ID替換示例中的YOUR_APP_ID,代碼才能正常運(yùn)行。
from http import HTTPStatus
from dashscope import Application
def call_agent_app():
response = Application.call(app_id='YOUR_APP_ID',
prompt='百煉的業(yè)務(wù)空間是什么?如何使用業(yè)務(wù)空間?',
api_key='YOUR_API_KEY',
)
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()
import com.alibaba.dashscope.app.*;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import java.util.List;
public class Main{
public static void callAgentApp()
throws ApiException, NoApiKeyException, InputRequiredException {
ApplicationParam param = ApplicationParam.builder()
.apiKey("YOUR_API_KEY")
.appId("YOUR_APP_ID")
.prompt("百煉的業(yè)務(wù)空間是什么?如何使用業(yè)務(wù)空間?")
.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 --location 'https://dashscope.aliyuncs.com/api/v1/apps/{YOUR_APP_ID}/completion' \
--header 'Authorization: Bearer {YOUR_API_KEY}' \
--header 'Content-Type: application/json' \
--data '{
"input": {
"prompt": "百煉的業(yè)務(wù)空間是什么?如何使用業(yè)務(wù)空間?"
},
"parameters": {},
"debug": {}
}' --verbose
通過Assistant API調(diào)用
設(shè)置您的API-KEY,替換YOUR_DASHCOPE_API_KEY為您自己的API key。
替換YOUR_PIPELINE_ID為百煉知識(shí)庫(kù)創(chuàng)建的知識(shí)索引ID,具體參考
Step 2:創(chuàng)建知識(shí)索引
第三小節(jié)內(nèi)容。其中${document1}為占位符,知識(shí)庫(kù)檢索回來(lái)的內(nèi)容會(huì)替換instructions中${document1},請(qǐng)確保RAG工具中定義
query_word/value
的占位符與instructions中占位符一致。
export DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY
from dashscope import Assistants, Messages, Runs, Threads
assistant = Assistants.create(
model='qwen-max',
name='smart helper',
description='智能助手,支持知識(shí)庫(kù)查詢和插件調(diào)用。',
instructions='你是一個(gè)智能助手,請(qǐng)記住以下信息。${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("運(yùn)行結(jié)果:")
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;
public class AssistantApiRag {
public static Assistant createAssistant(String pipelineId) throws ApiException, NoApiKeyException {
//注意instructions的${document1}占位符和buildPromptRa的${document1}占位符必須保持一致
AssistantParam assistantParam = AssistantParam.builder()
.model("qwen-max")
.name("smart helper")
.description("智能助手,支持知識(shí)庫(kù)查詢和插件調(diào)用。")
.instructions("你是一個(gè)智能助手,請(qǐng)記住以下信息。${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 = "wa9vj93fwv";
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;
}
}