Skip to main content

检索增强

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

在 AutoGen 中实现 RAG 的一种方法是使用 RetrieveAssistantAgentRetrieveUserProxyAgent 类构建代理对话。

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

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

第一步:创建 RetrieveAssistantAgentRetrieveUserProxyAgent 的实例。

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

assistant = RetrieveAssistantAgent(
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, # 如果不想重用现有的集合,请将其设置为 False,但您需要手动删除集合
},
code_execution_config=False, # 如果不想执行代码,请将其设置为 False
)

第二步:使用检索增强启动代理对话

设置好检索增强代理后,您可以使用以下代码启动带有检索增强的对话:

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 用作嵌入搜索的额外过滤器,在这种情况下,我们只想搜索包含 "spark" 的文档。

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

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

第一步:创建 RetrieveAssistantAgentRetrieveUserProxyAgent 的实例。

这里,RetrieveUserProxyAgent 实例充当一个代理,根据用户的输入检索相关信息。 在 db_config 中指定 connection_string,或者指定 hostportdatabaseusernamepassword

assistant = RetrieveAssistantAgent(
name="assistant",
system_message="您是一个乐于助人的助手。",
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", # 可选 - 连接到外部向量数据库
# "host": None, # 可选 - 向量数据库主机
# "port": None, # 可选 - 向量数据库端口
# "database": None, # 可选 - 向量数据库名称
# "username": None, # 可选 - 向量数据库用户名
# "password": None, # 可选 - 向量数据库密码
},
"custom_text_types": ["mdx"],
"chunk_token_size": 2000,
"model": config_list[0]["model"],
"get_or_create": True,
},
code_execution_config=False,
)

第二步:使用检索增强代理进行对话

设置好检索增强代理后,您可以使用以下代码启动检索增强对话:

code_problem = "如何使用 FLAML 执行分类任务并使用 Spark 进行并行训练。训练 30 秒,如果达到时间限制则强制取消任务。"
ragproxyagent.initiate_chat(
assistant, message=ragproxyagent.message_generator, problem=code_problem, search_string="spark"
) # search_string 用作嵌入搜索的额外过滤器,在本例中,我们只想搜索包含 "spark" 的文档。

在线演示

Huggingface 上的检索增强对话演示

更多示例和笔记本

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

  • 使用检索增强代理进行自动代码生成和问答 - 查看笔记本
  • 使用基于 PGVector 的检索增强代理进行自动代码生成和问答 - 查看笔记本
  • 使用基于 Qdrant 的检索增强代理进行自动生成代码和问答 - 查看笔记本
  • 使用检索增强进行与 OpenAI 助手的聊天 - 查看笔记本
  • RAG: 使用检索增强生成的群组聊天(包括5个群组成员代理和1个管理代理) - 查看笔记本

路线图

请在此处查看我们详细的路线图,了解有关 RAG 的进一步发展计划。我们非常欢迎您的贡献、反馈和使用案例!我们邀请您与我们互动,并在这一重要功能的开发中发挥关键作用。