Skip to content

Commit

Permalink
Support Agentic RAG with intent and functioncalling (#154)
Browse files Browse the repository at this point in the history
* Add intent detection module

* Remove LlmQuery class

* Support API

* Refactor agent module and format toml

* Refactor module tool

* Refactor query api

* Add demo and UI

* remove

* Fix reviews

* Add test for intent and api
  • Loading branch information
wwxxzz authored Aug 16, 2024
1 parent 24fff62 commit 95e4bf3
Show file tree
Hide file tree
Showing 40 changed files with 1,355 additions and 367 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ curl -X 'POST' http://127.0.0.1:8000/service/evaluate/retrieval
curl -X 'POST' http://127.0.0.1:8000/service/evaluate/response
```

# Function Calling
# Agentic RAG

You can use function calling tools in PAI-RAG, please refer to the documentation:
[Function Calling Instruction](./docs/function_calling/readme.md)
You can use agent with function calling api-tools in PAI-RAG, please refer to the documentation:
[Agentic RAG](./example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/README.md)

# Parameter Configuration

Expand Down
7 changes: 3 additions & 4 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,10 @@ curl -X 'POST' http://127.0.0.1:8000/service/evaluate/retrieval
curl -X 'POST' http://127.0.0.1:8000/service/evaluate/response
```

# Function Calling
# Agentic RAG

您也可以在PAI-RAG中使用function calling功能,请参考文档:

[Function Calling 使用说明](./docs/function_calling/readme.md)
您也可以在PAI-RAG中使用支持API function calling功能的Agent,请参考文档:
[Agentic RAG](./example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/README.md)

# 参数配置

Expand Down
179 changes: 0 additions & 179 deletions docs/function_calling/readme.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Agentic RAG Demo Guide

此示例提供了一个旅游小助手的功能,仅用于演示Agentic RAG中如何结合意图识别和FunctionCalling达到智能旅游攻略搜索和调用外部系统API的目的。实际使用视业务情况而有所不同。

## Mock API Service

我们提供了一个使用 FastAPI 实现的简单 API 系统,包含从北京到上海的机票查询、高铁查询和上海酒店查询。这些 API 将返回我们提前设定的 mock 数据。

使用以下命令启动 FastAPI 应用:

```bash
cd example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/mock_api
python main.py
```

## PAI-RAG Service

使用以下命令启动PAI-RAG服务端:

```bash
cd PAI-RAG/
pai_rag serve -p 8071
```

## (Option-1) PAI-RAG UI

使用以下命令启动PAI-RAG WebUI:

```bash
pai_rag ui -p 8072 -c http://127.0.0.1:8071
```

在WebUI页面,在`Settings Tab`先配置好所需的模型,并点击connect进行连接。
![settings](figures/settings.jpg)

`Upload Tab`上传所需要的知识库内容,此例子中可以使用 **example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/dataset** 准备好的北京和上海的旅游攻略数据。
![upload](figures/upload.jpg)

`Agent Tab`上传Agent配置文件,此例子中可以使用**example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/config.json**,上传后点击Submit进行agent模块注册,成功后即可在右侧Chatbot中进行agent对话测试。
![agent_config](figures/agent_config.jpg)
![agent_chat](figures/agent_chat.jpg)

## (Option-2) API

若不需要使用webui,也可以直接使用API进行知识库上传和agent调用。

1. 上传知识库文件:此例子中使用 **example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/dataset** 准备好的北京和上海的旅游攻略数据。

```bash
load_data -d example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/dataset
```

2. 注册Agent相关模块:此例子中使用**example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/config.json**

```bash
curl -X 'POST' http://127.0.0.1:8071/service/config/agent -H 'Content-Type: multipart/form-data' -F 'file=@example_data/function_tools/api-tool-with-intent-detection-for-travel-assistant/config.json'
```

3. AgenticRAG对话测试

注意: API中需要显式指定 `"with_intent":"True"` 才可以调用到意图识别模块进行意图的分发。

(1) 知识库检索

```bash
curl -X 'POST' http://127.0.0.1:8071/service/query -H "Content-Type: application/json" -d '{"question":"北京旅游攻略", "with_intent":true}'

# "answer"
# 北京旅游攻略包括以下景点推荐、开放时间、特色美食、旅行建议等信息:
# 景点推荐与开放时间
# - **故宫博物院**
# - 地址:北京市东城区景⼭前街 4 号
# - 亮点:世界上规模最⼤、保存最完整的木质结构古建筑之⼀,珍藏大量文化遗产和珍宝。
# - 开放时间: 旺季(4 ⽉ 1 ⽇-10 ⽉ 31 ⽇):8:30-16:30(15:40 停⽌⼊馆) - 淡季(11 ⽉ 1 ⽇-3 ⽉ 31 ⽇):8:30-16:00(15:10 停⽌⼊馆)
# - **天安⻔⼴场**
# - 地址:北京市东城区东⻓安街
# - 亮点:世界最⼤的城市⼴场,中华⼈⺠共和国的⼼脏,可观看升国旗仪式、参观⽑主席纪念堂和⼈⺠英雄纪念碑。
# - 开放时间:全天开放
# - **天坛公园**
# - 地址:北京市东城区天坛东⾥甲 1 号
# - 亮点:中国古代皇帝祭天和祈年之所。
# 特色美食
# - **京酱⾁丝** - 推荐餐厅:东来顺饭庄,位于北京市东城区东直⻔内⼤街,传统⽼字号餐馆。 - 特⾊:京酱(甜酱)炒制的猪⾁丝,颜⾊红亮,味道浓郁,常⽤⾖⽪或葱白包着吃,⼝感极佳。
# 旅行建议
# - **最佳旅游时间**:北京春秋季节⽓候宜人,适合旅游。春季(3 ⽉⾄ 5 ⽉),秋季(9 ⽉⾄ 11 ⽉)- **交通⽅式**:北京公共交通⾮常⽅便,地铁覆盖主要景点,推荐使⽤北京交通卡(Yikatong)方便出⾏。- **注意事项**:北京夏季天⽓炎热,⾼温多⾬;冬季寒冷⼲燥。建议根据季节和天⽓预报准备适合的⾐物。
# 希望以上攻略对您能有所帮助,祝您在北京玩得愉快!"
```

(2) API调用

```bash
curl -X 'POST' http://127.0.0.1:8071/service/query -H "Content-Type: application/json" -d '{"question":"2024年8月6号从北京到上海的机票信息", "with_intent":true}'

# "answer"
# assistant: 2024年8月6日从北京到上海的航班信息如下:
# 1. 航班号:MU456,出发时间:14:00,到达时间:16:00,价格:1300元
# 2. 航班号:HU789,出发时间:18:00,到达时间:20:00,价格:1100元
# 请注意选择合适的航班和时间。"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"intent": {
"retrieval": "关于一些通用的信息检索,比如搜索旅游攻略、搜索美食攻略、搜索注意事项等信息。",
"agent": "实时性的信息查询,比如查询航班信息、查询高铁信息、查询天气等时效性很强的信息。"
},
"agent": {
"system_prompt": "你是一个旅游小助手,可以帮助用户查询指定时间从A地区到B地区的机票信息,以及火车票信息等。请严格使用输入的工具,不要虚构任何细节。",
"functions": [
{
"name": "search_flight_ticket_api",
"api": "http://127.0.0.1:8070/demo/api/flights",
"headers": {
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
},
"method": "GET",
"request_body_type": "params",
"description": "帮助用户获取机票信息,用户需要输入出发地、目的地",
"parameters": {
"from_city": {
"type": "str",
"description": "出发城市,如'北京'、'上海'、'南京''"
},
"to_city": {
"type": "str",
"description": "目的地城市,如'北京'、'上海'、'南京'"
},
"date": {
"type": "str",
"description": "出发时间,如'2024-03-29'"
}
},
"required": ["from_city", "to_city", "date"]
},
{
"name": "search_train_ticket_api",
"api": "http://127.0.0.1:8070/demo/api/trains",
"headers": {
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
},
"method": "GET",
"request_body_type": "params",
"description": "帮助用户查询火车票或者高铁票信息",
"parameters": {
"from_city": {
"type": "str",
"description": "出发城市,如'北京'、'上海'、'南京''"
},
"to_city": {
"type": "str",
"description": "目的地城市,如'北京'、'上海'、'南京'"
},
"date": {
"type": "str",
"description": "出发时间,如'2024-03-29'"
}
},
"required": ["from_city", "to_city", "date"]
},
{
"name": "search_hotels_api",
"api": "http://127.0.0.1:8070/demo/api/hotels",
"headers": {
"Authorization": "Bearer YOUR_ACCESS_TOKEN"
},
"method": "GET",
"request_body_type": "params",
"description": "帮助用户查询酒店信息",
"parameters": {
"city": {
"type": "str",
"description": "查询的城市,如'北京'、'上海'、'南京''"
},
"date": {
"type": "str",
"description": "出发时间,如'2024-03-29'"
}
},
"required": ["city", "date"]
}
]
}
}
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 95e4bf3

Please sign in to comment.