|
| 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