跳到主要内容

检索增强

检索增强生成(RAG)是一种强大的技术,它结合了语言模型与外部知识检索,以提高生成响应的质量和相关性。

在AutoGen中实现RAG的一种方法是使用AssistantAgentRetrieveUserProxyAgent类构建代理聊天。

示例设置:使用检索增强代理的RAG

以下是一个示例设置,展示如何在AutoGen中创建检索增强代理:

第一步:创建 AssistantAgentRetrieveUserProxyAgent 的实例。

这里RetrieveUserProxyAgent实例充当一个代理代理,根据用户的输入检索相关信息。

请参考 doc 以获取关于详细配置的更多信息。

assistant = AssistantAgent(
name="assistant",
system_message="You are a helpful assistant.",
llm_config={
"timeout": 600,
"cache_seed": 42,
"config_list": config_list,
},
)
ragproxyagent = RetrieveUserProxyAgent(
name="ragproxyagent",
human_input_mode="NEVER",
max_consecutive_auto_reply=3,
retrieve_config={
"task": "code",
"docs_path": [
"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Examples/Integrate%20-%20Spark.md",
"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Research.md",
os.path.join(os.path.abspath(""), "..", "website", "docs"),
],
"custom_text_types": ["mdx"],
"chunk_token_size": 2000,
"model": config_list[0]["model"],
"client": chromadb.PersistentClient(path="/tmp/chromadb"),
"embedding_model": "all-mpnet-base-v2",
"get_or_create": True, # set to False if you don't want to reuse an existing collection, but you'll need to remove the collection manually
},
code_execution_config=False, # set to False if you don't want to execute the code
)

步骤2. 使用检索增强启动代理聊天

设置好检索增强代理后,您可以使用以下代码发起检索增强的聊天:

code_problem = "How can I use FLAML to perform a classification task and use spark to do parallel training. Train 30 seconds and force cancel jobs if time limit is reached."
ragproxyagent.initiate_chat(
assistant, message=ragproxyagent.message_generator, problem=code_problem, search_string="spark"
) # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain "spark".

如果您看到类似 #3551 的问题,您需要安装 chromadb<=0.5.0

示例设置:使用PGVector的检索增强型代理的RAG

以下是一个示例设置,展示如何在AutoGen中创建检索增强代理:

第一步:创建 AssistantAgentRetrieveUserProxyAgent 的实例。

这里RetrieveUserProxyAgent实例充当一个代理代理,根据用户的输入检索相关信息。

在db_config中指定connection_string,或主机、端口、数据库、用户名和密码。

assistant = AssistantAgent(
name="assistant",
system_message="You are a helpful assistant.",
llm_config={
"timeout": 600,
"cache_seed": 42,
"config_list": config_list,
},
)
ragproxyagent = RetrieveUserProxyAgent(
name="ragproxyagent",
human_input_mode="NEVER",
max_consecutive_auto_reply=3,
retrieve_config={
"task": "code",
"docs_path": [
"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Examples/Integrate%20-%20Spark.md",
"https://raw.githubusercontent.com/microsoft/FLAML/main/website/docs/Research.md",
os.path.join(os.path.abspath(""), "..", "website", "docs"),
],
"vector_db": "pgvector",
"collection_name": "autogen_docs",
"db_config": {
"connection_string": "postgresql://testuser:testpwd@localhost:5432/vectordb", # Optional - connect to an external vector database
# "host": None, # Optional vector database host
# "port": None, # Optional vector database port
# "database": None, # Optional vector database name
# "username": None, # Optional vector database username
# "password": None, # Optional vector database password
},
"custom_text_types": ["mdx"],
"chunk_token_size": 2000,
"model": config_list[0]["model"],
"get_or_create": True,
},
code_execution_config=False,
)

步骤2. 使用检索增强启动代理聊天

设置好检索增强代理后,您可以使用以下代码发起检索增强的聊天:

code_problem = "How can I use FLAML to perform a classification task and use spark to do parallel training. Train 30 seconds and force cancel jobs if time limit is reached."
ragproxyagent.initiate_chat(
assistant, message=ragproxyagent.message_generator, problem=code_problem, search_string="spark"
) # search_string is used as an extra filter for the embeddings search, in this case, we only want to search documents that contain "spark".

在线演示

Huggingface上的检索增强聊天演示

更多示例和笔记本

关于在AutoGen中使用检索增强代理的更多详细示例和笔记本,请参考以下内容:

路线图

探索我们详细的路线图 这里,了解围绕RAG的进一步发展规划。我们非常感谢您的贡献、反馈和使用案例!邀请您参与其中,在这一有影响力的功能开发中发挥关键作用。