Skip to content

Commit 3960197

Browse files
authored
Merge pull request #55 from harlansgithub/docs
create RagAgent.md
2 parents 78cd308 + a6a0583 commit 3960197

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
检索增强生成,是一种结合信息检索(Retrieval)和文本生成(Generation)的技术。
2+
RAG技术通过实时检索相关文档或信息,并将其作为上下文输入到生成模型中,从而提高生成结果的时效性和准确性。
3+
## Overview
4+
本模块提供了 ChatAgent 类,该类通过管理对话记忆、处理用户查询,并协调语言模型生成回复,从而实现对话式人工智能的交互。
5+
## Quick Start
6+
### Basic Usage
7+
OxyGent支持通过knowledge参数向prompts注入知识。以下将展示一个最简单的RAG示例:
8+
您需要先创建一个retrieval方法:
9+
```python
10+
def retrieval(query):
11+
# 替换为实际的数据库
12+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
13+
```
14+
然后,需要在update_query中将检索的知识进行注入:
15+
```python
16+
def update_query(oxy_request: OxyRequest):
17+
current_query = oxy_request.get_query()
18+
19+
def retrieval(query):
20+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
21+
22+
oxy_request.arguments["knowledge"] = retrieval(current_query) # Key Method
23+
return oxy_request
24+
```
25+
## Complete runnable example
26+
以下是可运行的完整代码示例:
27+
```python
28+
import asyncio
29+
30+
from oxygent import MAS, OxyRequest, oxy
31+
from oxygent.utils.env_utils import get_env_var
32+
33+
INSTRUCTION = """
34+
You are a helpful assistant and can use these tools:
35+
${tools_description}
36+
37+
Experience in choosing tools:
38+
${knowledge}
39+
40+
Select the appropriate tool based on the user's question.
41+
If no tool is needed, reply directly.
42+
If answering the user's question requires calling multiple tools, call only one tool at a time. After the user receives the tool result, they will give you feedback on the tool call result.
43+
44+
Important notes:
45+
1. When you have collected enough information to answer the user's question, please respond in the following format:
46+
<think>Your reasoning (if analysis is needed)</think>
47+
Your response content
48+
2. When you find that the user's question lacks certain conditions, you can ask them back. Please respond in the following format:
49+
<think>Your reasoning (if analysis is needed)</think>
50+
Your follow-up question to the user
51+
3. When you need to use a tool, you must respond **only** with the following exact JSON object format, and nothing else:
52+
53+
{
54+
"think": "Your reasoning (if analysis is needed)",
55+
"tool_name": "Tool name",
56+
"arguments": {
57+
"Parameter name": "Parameter value"
58+
}
59+
}
60+
"""
61+
62+
63+
def update_query(oxy_request: OxyRequest):
64+
current_query = oxy_request.get_query()
65+
66+
def retrieval(query):
67+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
68+
69+
oxy_request.arguments["knowledge"] = retrieval(current_query)
70+
return oxy_request
71+
72+
73+
oxy_space = [
74+
oxy.HttpLLM(
75+
name="default_llm",
76+
api_key=get_env_var("DEFAULT_LLM_API_KEY"),
77+
base_url=get_env_var("DEFAULT_LLM_BASE_URL"),
78+
model_name=get_env_var("DEFAULT_LLM_MODEL_NAME"),
79+
llm_params={"temperature": 0.01},
80+
semaphore=4,
81+
),
82+
oxy.ReActAgent(
83+
name="master_agent",
84+
is_master=True,
85+
llm_model="default_llm",
86+
timeout=100,
87+
prompt=INSTRUCTION,
88+
func_process_input=update_query,
89+
),
90+
]
91+
92+
93+
async def main():
94+
async with MAS(oxy_space=oxy_space) as mas:
95+
await mas.start_web_service(
96+
first_query="This is an example for rag. Please modify it according to the specific needs",
97+
)
98+
99+
if __name__ == "__main__":
100+
asyncio.run(main())
101+
```
102+
## Configuration Options
103+
### Core Parameters
104+
#### knowledge
105+
1.调用外部注入的检索函数,根据请求内容获取相关知识。
106+
2.将检索到的知识写入 oxy_request.arguments 中的占位符键(knowledge_placeholder),以便后续进行模板渲染或提示词组合。
107+
#### retrieval(str)
108+
根据当前请求执行知识检索(例如,从向量数据库、搜索服务或自托管知识库中检索)。
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
Rag_agent (Retrieval-Augmented Generation) conversational agent.
2+
Before generating a response with a large language model, relevant external knowledge is retrieved through a search function and injected into the prompt to improve the accuracy and controllability of the answers.
3+
## Overview
4+
This module provides the ChatAgent class, which handles conversational AI interactions by managing conversation memory, processing user queries, and coordinating with language models to generate responses.
5+
## Quick Start
6+
### Basic Usage
7+
OxyGent supports injecting knowledge into prompts via the knowledge parameter. Below is a simple example of RAG:
8+
You need to first create a retrieval method:
9+
```python
10+
def retrieval(query):
11+
# Replace with the actual database
12+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
13+
```
14+
Next, you need to inject the retrieved knowledge in update_query:
15+
```python
16+
def update_query(oxy_request: OxyRequest):
17+
current_query = oxy_request.get_query()
18+
19+
def retrieval(query):
20+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
21+
22+
oxy_request.arguments["knowledge"] = retrieval(current_query) # Key Method
23+
return oxy_request
24+
```
25+
## Complete runnable example
26+
Below is a complete runnable code example:
27+
```python
28+
import asyncio
29+
30+
from oxygent import MAS, OxyRequest, oxy
31+
from oxygent.utils.env_utils import get_env_var
32+
33+
INSTRUCTION = """
34+
You are a helpful assistant and can use these tools:
35+
${tools_description}
36+
37+
Experience in choosing tools:
38+
${knowledge}
39+
40+
Select the appropriate tool based on the user's question.
41+
If no tool is needed, reply directly.
42+
If answering the user's question requires calling multiple tools, call only one tool at a time. After the user receives the tool result, they will give you feedback on the tool call result.
43+
44+
Important notes:
45+
1. When you have collected enough information to answer the user's question, please respond in the following format:
46+
<think>Your reasoning (if analysis is needed)</think>
47+
Your response content
48+
2. When you find that the user's question lacks certain conditions, you can ask them back. Please respond in the following format:
49+
<think>Your reasoning (if analysis is needed)</think>
50+
Your follow-up question to the user
51+
3. When you need to use a tool, you must respond **only** with the following exact JSON object format, and nothing else:
52+
53+
{
54+
"think": "Your reasoning (if analysis is needed)",
55+
"tool_name": "Tool name",
56+
"arguments": {
57+
"Parameter name": "Parameter value"
58+
}
59+
}
60+
"""
61+
62+
63+
def update_query(oxy_request: OxyRequest):
64+
current_query = oxy_request.get_query()
65+
66+
def retrieval(query):
67+
return "\n".join(["knowledge1", "knowledge2", "knowledge3"])
68+
69+
oxy_request.arguments["knowledge"] = retrieval(current_query)
70+
return oxy_request
71+
72+
73+
oxy_space = [
74+
oxy.HttpLLM(
75+
name="default_llm",
76+
api_key=get_env_var("DEFAULT_LLM_API_KEY"),
77+
base_url=get_env_var("DEFAULT_LLM_BASE_URL"),
78+
model_name=get_env_var("DEFAULT_LLM_MODEL_NAME"),
79+
llm_params={"temperature": 0.01},
80+
semaphore=4,
81+
),
82+
oxy.ReActAgent(
83+
name="master_agent",
84+
is_master=True,
85+
llm_model="default_llm",
86+
timeout=100,
87+
prompt=INSTRUCTION,
88+
func_process_input=update_query,
89+
),
90+
]
91+
92+
93+
async def main():
94+
async with MAS(oxy_space=oxy_space) as mas:
95+
await mas.start_web_service(
96+
first_query="This is an example for rag. Please modify it according to the specific needs",
97+
)
98+
99+
if __name__ == "__main__":
100+
asyncio.run(main())
101+
```
102+
## Configuration Options
103+
### Core Parameters
104+
#### knowledge
105+
1.Call the externally injected retrieval function to obtain relevant knowledge based on the request content.
106+
2.Write theretrieved knowledge into oxy_request.arguments under the placeholder key(knowledge_placeholder) for subsequent template rendering/prompt composition.
107+
#### retrieval(str)
108+
Perform knowledge retrieval based on the current request(e.g., from a vector database, searchservice, or self-hosted knowledgebase)

0 commit comments

Comments
 (0)