AutoGen-AgentChat-8-记忆和RAG

import os
from dotenv import load_dotenv
load_dotenv()
True

记忆和RAG

在一些用例中,维护一个有用的事实库非常有价值,这些事实库可以在特定步骤之前智能地添加到代理的上下文中。这里典型的用例是 RAG 模式,其中使用查询从数据库中检索相关信息,然后将其添加到代理的上下文中。

AgentChat 提供了一个Memory可扩展的协议来提供此功能。关键方法是query、update_context、 add、clear和close。

  • add:向内存存储添加新条目
  • query:从记忆库中检索相关信息
  • update_contextmodel_context:通过添加检索到的信息(在AssistantAgent类中使用)来改变代理的内部
  • clear:清除内存存储中的所有条目
  • close:清理内存存储使用的任何资源

ListMemory 示例

{py:class} ~autogen_core.memory.ListMemory 是 {py:class} ~autogen_core.memory.Memory 协议的一个示例实现。它是一个基于列表的简单记忆实现,按时间顺序维护记忆,并将最新的记忆附加到模型的上下文中。该实现设计简洁明了且可预测,易于理解和调试。在以下示例中,我们将使用 ListMemory 维护用户偏好记忆库,并演示如何使用它为代理响应提供随时间推移的一致上下文。

from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_core.memory import ListMemory, MemoryContent, MemoryMimeType
from autogen_ext.models.openai import OpenAIChatCompletionClient
# 初始化用户记忆
user_memory = ListMemory()

# 添加用户偏好至记忆中
# 偏好1:天气应使用公制单位
await user_memory.add(MemoryContent(content="天气应使用公制单位", mime_type=MemoryMimeType.TEXT))

# 偏好2:食谱必须是纯素(vegan)
await user_memory.add(MemoryContent(content="食谱必须为纯素", mime_type=MemoryMimeType.TEXT))


# 定义一个异步函数,用于获取天气信息
# 参数:
#   city:城市名称
#   units:单位,默认为 "imperial"(英制)
async def get_weather(city: str, units: str = "imperial") -> str:
    if units == "imperial":
        # 如果使用英制单位,返回华氏度的天气信息
        return f"{city} 的天气是 73 华氏度,晴朗。"
    elif units == "metric":
        # 如果使用公制单位,返回摄氏度的天气信息
        return f"{city} 的天气是 23 摄氏度,晴朗。"
    else:
        # 单位不识别时返回错误提示
        return f"对不起,我不知道 {city} 的天气。"


# 创建助手智能体(AssistantAgent)
assistant_agent = AssistantAgent(
    name="assistant_agent",  # 智能体名称
    model_client=OpenAIChatCompletionClient(
        model="gpt-4o-mini",  # 使用的 OpenAI 模型
    ),
    tools=[get_weather],  # 注册可用工具(此处为获取天气工具)
    memory=[user_memory],  # 加载用户记忆(包括天气单位与饮食偏好)
)
# 以一个任务运行助手智能体,并获取流式响应
stream = assistant_agent.run_stream(task="北京的天气如何?")

# 使用控制台输出流式响应内容(逐步显示)
await Console(stream)
---------- TextMessage (user) ----------
北京的天气如何?
---------- MemoryQueryEvent (assistant_agent) ----------
[MemoryContent(content='天气应使用公制单位', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None), MemoryContent(content='食谱必须为纯素', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None)]
---------- ToolCallRequestEvent (assistant_agent) ----------
[FunctionCall(id='call_lLwS19pBAmpIuysUb6sEJjo2', arguments='{"city":"北京","units":"metric"}', name='get_weather')]
---------- ToolCallExecutionEvent (assistant_agent) ----------
[FunctionExecutionResult(content='北京 的天气是 23 摄氏度,晴朗。', name='get_weather', call_id='call_lLwS19pBAmpIuysUb6sEJjo2', is_error=False)]
---------- ToolCallSummaryMessage (assistant_agent) ----------
北京 的天气是 23 摄氏度,晴朗。





TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 0, 52, 349096, tzinfo=datetime.timezone.utc), content='北京的天气如何?', type='TextMessage'), MemoryQueryEvent(source='assistant_agent', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 0, 52, 351646, tzinfo=datetime.timezone.utc), content=[MemoryContent(content='天气应使用公制单位', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None), MemoryContent(content='食谱必须为纯素', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None)], type='MemoryQueryEvent'), ToolCallRequestEvent(source='assistant_agent', models_usage=RequestUsage(prompt_tokens=121, completion_tokens=18), metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 0, 53, 688541, tzinfo=datetime.timezone.utc), content=[FunctionCall(id='call_lLwS19pBAmpIuysUb6sEJjo2', arguments='{"city":"北京","units":"metric"}', name='get_weather')], type='ToolCallRequestEvent'), ToolCallExecutionEvent(source='assistant_agent', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 0, 53, 691791, tzinfo=datetime.timezone.utc), content=[FunctionExecutionResult(content='北京 的天气是 23 摄氏度,晴朗。', name='get_weather', call_id='call_lLwS19pBAmpIuysUb6sEJjo2', is_error=False)], type='ToolCallExecutionEvent'), ToolCallSummaryMessage(source='assistant_agent', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 0, 53, 692804, tzinfo=datetime.timezone.utc), content='北京 的天气是 23 摄氏度,晴朗。', type='ToolCallSummaryMessage', tool_calls=[FunctionCall(id='call_lLwS19pBAmpIuysUb6sEJjo2', arguments='{"city":"北京","units":"metric"}', name='get_weather')], results=[FunctionExecutionResult(content='北京 的天气是 23 摄氏度,晴朗。', name='get_weather', call_id='call_lLwS19pBAmpIuysUb6sEJjo2', is_error=False)])], stop_reason=None)

我们可以检查assistant_agentmodel_context 是否确实已使用检索到的内存条目进行更新。该transform方法用于将检索到的内存条目格式化为代理可以使用的字符串。

在本例中,我们只需将每个内存条目的内容连接成一个字符串即可。

await assistant_agent._model_context.get_messages()
[UserMessage(content='北京的天气如何?', source='user', type='UserMessage'),
 SystemMessage(content='\nRelevant memory content (in chronological order):\n1. 天气应使用公制单位\n2. 食谱必须为纯素\n', type='SystemMessage'),
 AssistantMessage(content=[FunctionCall(id='call_lLwS19pBAmpIuysUb6sEJjo2', arguments='{"city":"北京","units":"metric"}', name='get_weather')], thought=None, source='assistant_agent', type='AssistantMessage'),
 FunctionExecutionResultMessage(content=[FunctionExecutionResult(content='北京 的天气是 23 摄氏度,晴朗。', name='get_weather', call_id='call_lLwS19pBAmpIuysUb6sEJjo2', is_error=False)], type='FunctionExecutionResultMessage')]

我们上面看到,天气以摄氏度为单位返回,如用户偏好中所述。

类似地,假设我们询问有关制定膳食计划的单独问题,代理能够从记忆存储中检索相关信息并提供个性化(素食)的回应。

# 运行助手智能体,处理一个任务:生成一个包含汤底的简短食谱
stream = assistant_agent.run_stream(task="写一个包含汤底的简短食谱")

# 使用控制台输出流式响应(逐步显示生成内容)
await Console(stream)
---------- TextMessage (user) ----------
写一个包含汤底的简短食谱
---------- MemoryQueryEvent (assistant_agent) ----------
[MemoryContent(content='天气应使用公制单位', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None), MemoryContent(content='食谱必须为纯素', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None)]
---------- TextMessage (assistant_agent) ----------
### 番茄香菜汤

#### 材料:
- 4 个成熟的番茄(切块)
- 1 个洋葱(切丁)
- 2 瓣大蒜(切片)
- 4 杯纯净水
- 1 茶匙橄榄油
- 1 茶匙盐
- 1/2 茶匙黑胡椒
- 新鲜香菜(任意量,用于装饰)

#### 做法:
1. 在锅中加热橄榄油,加入洋葱和大蒜,翻炒至洋葱变软。
2. 加入番茄块,继续翻炒约 5 分钟,直到番茄释放出汁液。
3. 倒入纯净水,加入盐和黑胡椒,搅拌均匀。
4. 煮沸后,转小火煮 15 分钟,让味道融合。
5. 用搅拌机将汤底搅拌至光滑(可根据喜好选择保留颗粒)。
6. 盛出汤,撒上新鲜香菜装饰,趁热享用。

这个汤是完全素食的,清新美味!





TaskResult(messages=[TextMessage(source='user', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 3, 41, 982053, tzinfo=datetime.timezone.utc), content='写一个包含汤底的简短食谱', type='TextMessage'), MemoryQueryEvent(source='assistant_agent', models_usage=None, metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 3, 41, 987254, tzinfo=datetime.timezone.utc), content=[MemoryContent(content='天气应使用公制单位', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None), MemoryContent(content='食谱必须为纯素', mime_type=<MemoryMimeType.TEXT: 'text/plain'>, metadata=None)], type='MemoryQueryEvent'), TextMessage(source='assistant_agent', models_usage=RequestUsage(prompt_tokens=207, completion_tokens=279), metadata={}, created_at=datetime.datetime(2025, 7, 2, 3, 3, 48, 984289, tzinfo=datetime.timezone.utc), content='### 番茄香菜汤\n\n#### 材料:\n- 4 个成熟的番茄(切块)\n- 1 个洋葱(切丁)\n- 2 瓣大蒜(切片)\n- 4 杯纯净水\n- 1 茶匙橄榄油\n- 1 茶匙盐\n- 1/2 茶匙黑胡椒\n- 新鲜香菜(任意量,用于装饰)\n\n#### 做法:\n1. 在锅中加热橄榄油,加入洋葱和大蒜,翻炒至洋葱变软。\n2. 加入番茄块,继续翻炒约 5 分钟,直到番茄释放出汁液。\n3. 倒入纯净水,加入盐和黑胡椒,搅拌均匀。\n4. 煮沸后,转小火煮 15 分钟,让味道融合。\n5. 用搅拌机将汤底搅拌至光滑(可根据喜好选择保留颗粒)。\n6. 盛出汤,撒上新鲜香菜装饰,趁热享用。\n\n这个汤是完全素食的,清新美味!', type='TextMessage')], stop_reason=None)

自定义内存存储(向量数据库等)

您可以基于该Memory协议构建更复杂的记忆存储。例如,您可以实现一个使用向量数据库存储和检索信息的自定义记忆存储,或者一个使用机器学习模型根据用户偏好生成个性化响应的记忆存储等等。

具体来说,您需要重载add、query和update_context 方法来实现所需的功能并将内存存储传递给您的代理。

目前,以下示例内存存储可作为autogen_ext扩展包的一部分使用。

  • autogen_ext.memory.chromadb.ChromaDBVectorMemory:使用矢量数据库来存储和检索信息的记忆存储。
import os
from pathlib import Path

# 引入 AutoGen 所需模块
from autogen_agentchat.agents import AssistantAgent  # 助手智能体
from autogen_agentchat.ui import Console  # 控制台输出模块
from autogen_core.memory import MemoryContent, MemoryMimeType  # 记忆内容与类型定义
from autogen_ext.memory.chromadb import ChromaDBVectorMemory, PersistentChromaDBVectorMemoryConfig  # ChromaDB 向量记忆
from autogen_ext.models.openai import OpenAIChatCompletionClient  # OpenAI 模型封装客户端

# 初始化 ChromaDB 向量记忆,使用本地持久化配置
chroma_user_memory = ChromaDBVectorMemory(
    config=PersistentChromaDBVectorMemoryConfig(
        collection_name="preferences",  # 存储集合名称为“preferences”
        persistence_path=os.path.join(str(Path.home()), ".chromadb_autogen"),  # 存储路径为用户主目录下 .chromadb_autogen 文件夹
        k=2,  # 检索相似记忆时返回前 2 个结果
        score_threshold=0.4,  # 相似度分数阈值为 0.4,低于此分数将被忽略
    )
)
# (说明:也可以使用 HttpChromaDBVectorMemoryConfig 连接远程 ChromaDB 服务器)

# 向记忆中添加用户偏好 —— 天气单位应为公制
await chroma_user_memory.add(
    MemoryContent(
        content="天气应使用公制单位",  # 偏好描述
        mime_type=MemoryMimeType.TEXT,  # 内容类型为文本
        metadata={"category": "preferences", "type": "units"},  # 元信息:类别为偏好,类型为单位
    )
)

# 向记忆中添加用户偏好 —— 食谱必须为纯素
await chroma_user_memory.add(
    MemoryContent(
        content="食谱必须为纯素",  # 偏好描述
        mime_type=MemoryMimeType.TEXT,  # 内容类型为文本
        metadata={"category": "preferences", "type": "dietary"},  # 元信息:类别为偏好,类型为饮食
    )
)

# 初始化 OpenAI 模型客户端(gpt-4o-mini)
model_client = OpenAIChatCompletionClient(
    model="gpt-4o-mini",
)

# 创建助手智能体,绑定模型客户端和 ChromaDB 向量记忆
assistant_agent = AssistantAgent(
    name="assistant_agent",  # 智能体名称
    model_client=model_client,  # 使用的模型
    tools=[get_weather],  # 注册工具(如天气查询函数)
    memory=[chroma_user_memory],  # 加载记忆模块(用于支持个性化偏好)
)

# 运行智能体任务:查询纽约的天气(已将英文任务翻译为中文)
stream = assistant_agent.run_stream(task="北京的天气如何?")

# 控制台实时输出智能体生成的回答
await Console(stream)

# 关闭模型客户端和 ChromaDB 记忆(清理资源)
await model_client.close()
await chroma_user_memory.close()
---------- TextMessage (user) ----------
北京的天气如何?
---------- MemoryQueryEvent (assistant_agent) ----------
[MemoryContent(content='天气应使用公制单位', mime_type='MemoryMimeType.TEXT', metadata={'category': 'preferences', 'mime_type': 'MemoryMimeType.TEXT', 'type': 'units', 'score': 0.7006773224795939, 'id': 'eb193c72-1433-4141-9256-266c98e949a2'}), MemoryContent(content='食谱必须为纯素', mime_type='MemoryMimeType.TEXT', metadata={'category': 'preferences', 'mime_type': 'MemoryMimeType.TEXT', 'type': 'dietary', 'score': 0.42277149136513226, 'id': 'bd36cf40-9018-4d29-b04b-90c04ab820f5'})]
---------- ToolCallRequestEvent (assistant_agent) ----------
[FunctionCall(id='call_ZfShdM12B07JT8jJmXxTF1f4', arguments='{"city":"北京","units":"metric"}', name='get_weather')]
---------- ToolCallExecutionEvent (assistant_agent) ----------
[FunctionExecutionResult(content='北京 的天气是 23 摄氏度,晴朗。', name='get_weather', call_id='call_ZfShdM12B07JT8jJmXxTF1f4', is_error=False)]
---------- ToolCallSummaryMessage (assistant_agent) ----------
北京 的天气是 23 摄氏度,晴朗。
注意,您还可以序列化 ChromaDBVectorMemory 并将其保存到磁盘。
chroma_user_memory.dump_component().model_dump_json()
'{"provider":"autogen_ext.memory.chromadb.ChromaDBVectorMemory","component_type":"memory","version":1,"component_version":1,"description":"Store and retrieve memory using vector similarity search powered by ChromaDB.","label":"ChromaDBVectorMemory","config":{"client_type":"persistent","collection_name":"preferences","distance_metric":"cosine","k":2,"score_threshold":0.4,"allow_reset":false,"tenant":"default_tenant","database":"default_database","persistence_path":"/Users/dcs/.chromadb_autogen"}}'

RAG 代理:整合所有内容

在构建 AI 系统中常见的 RAG(检索增强生成)模式包含两个不同的阶段:

  • 索引:加载文档、分块并将其存储在矢量数据库中
  • 检索:在对话运行时查找并使用相关块

在之前的示例中,我们手动将项目添加到内存并传递给代理。实际上,索引过程通常是自动化的,并且基于更大的文档源,例如产品文档、内部文件或知识库。

注意:RAG 系统的质量取决于分块和检索过程(模型、嵌入等)的质量。您可能需要尝试更高级的分块和检索模型才能获得最佳效果。

构建一个简单的 RAG

首先,让我们创建一个简单的文档索引器,我们将用它来加载文档、对文档进行分块,并将它们存储在ChromaDBVectorMemory内存存储中。

import re
from typing import List

import aiofiles
import aiohttp
from autogen_core.memory import Memory, MemoryContent, MemoryMimeType


class SimpleDocumentIndexer:
    """AutoGen 记忆系统的基础文档索引器。"""

    def __init__(self, memory: Memory, chunk_size: int = 1500) -> None:
        # 初始化索引器,指定记忆对象和文本分块大小(默认每块 1500 个字符)
        self.memory = memory
        self.chunk_size = chunk_size

    async def _fetch_content(self, source: str) -> str:
        """从 URL 或本地文件中获取文本内容。"""
        if source.startswith(("http://", "https://")):
            # 如果是 URL,则通过异步 HTTP 请求获取网页内容
            async with aiohttp.ClientSession() as session:
                async with session.get(source) as response:
                    return await response.text()
        else:
            # 否则读取本地文件内容
            async with aiofiles.open(source, "r", encoding="utf-8") as f:
                return await f.read()

    def _strip_html(self, text: str) -> str:
        """移除 HTML 标签并规范空白字符。"""
        text = re.sub(r"<[^>]*>", " ", text)  # 去除所有 HTML 标签
        text = re.sub(r"\s+", " ", text)      # 合并多个空格、换行等为一个空格
        return text.strip()

    def _split_text(self, text: str) -> List[str]:
        """将文本按固定长度切分为多个片段(chunk)。"""
        chunks: list[str] = []
        for i in range(0, len(text), self.chunk_size):
            chunk = text[i : i + self.chunk_size]  # 每次取 chunk_size 长度的文本
            chunks.append(chunk.strip())
        return chunks

    async def index_documents(self, sources: List[str]) -> int:
        """将多个文档索引并写入记忆系统,返回写入的 chunk 总数。"""
        total_chunks = 0  # 累计 chunk 数量

        for source in sources:
            try:
                # 获取原始内容(网页或文件)
                content = await self._fetch_content(source)

                # 如果检测到内容中有 HTML 标记,则进行清洗
                if "<" in content and ">" in content:
                    content = self._strip_html(content)

                # 将清洗后的文本切分成小段
                chunks = self._split_text(content)

                # 将每个 chunk 添加到记忆中
                for i, chunk in enumerate(chunks):
                    await self.memory.add(
                        MemoryContent(
                            content=chunk,  # 文本内容
                            mime_type=MemoryMimeType.TEXT,  # 文本类型为纯文本
                            metadata={"source": source, "chunk_index": i}  # 元信息包括来源和索引编号
                        )
                    )

                total_chunks += len(chunks)  # 累加 chunk 数量

            except Exception as e:
                # 出错时打印中文提示信息
                print(f"索引 {source} 时出错:{str(e)}")

        return total_chunks  # 返回总共写入的 chunk 数量

现在让我们使用索引器和 ChromaDBVectorMemory 来构建完整的 RAG 代理:

import os
from pathlib import Path

# 导入 AutoGen 模块所需组件
from autogen_agentchat.agents import AssistantAgent  # 智能体类
from autogen_agentchat.ui import Console  # 控制台输出组件
from autogen_ext.memory.chromadb import ChromaDBVectorMemory, PersistentChromaDBVectorMemoryConfig  # 向量记忆模块
from autogen_ext.models.openai import OpenAIChatCompletionClient  # OpenAI 模型客户端

# 初始化向量记忆模块(基于 ChromaDB,本地持久化存储)
rag_memory = ChromaDBVectorMemory(
    config=PersistentChromaDBVectorMemoryConfig(
        collection_name="autogen_docs",  # 设置集合名称为 autogen_docs
        persistence_path=os.path.join(str(Path.home()), ".chromadb_autogen"),  # 数据持久化路径为用户主目录下的 .chromadb_autogen 文件夹
        k=3,  # 每次相似度检索返回前 3 个最相关的结果
        score_threshold=0.4,  # 相似度最低分数阈值为 0.4
    )
)

# 清空现有的记忆内容(确保干净的环境重新索引)
await rag_memory.clear()


# 定义一个异步函数,用于索引 AutoGen 官方文档
async def index_autogen_docs() -> None:
    # 创建文档索引器,绑定向量记忆存储
    indexer = SimpleDocumentIndexer(memory=rag_memory)

    # 要索引的 AutoGen 文档源地址(包括 README 和用户指南)
    sources = [
        "https://raw.githubusercontent.com/microsoft/autogen/main/README.md",  # AutoGen 项目简介
        "https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/agents.html",  # 代理使用教程
        "https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/teams.html",  # 团队创建与使用教程
        "https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/termination.html",  # 会话终止教程
    ]

    # 开始异步索引文档内容,返回索引的文本块数量
    chunks: int = await indexer.index_documents(sources)

    # 打印索引结果,提示完成
    print(f"已从 {len(sources)} 个 AutoGen 文档中索引出 {chunks} 个文本块。")


# 执行索引操作
await index_autogen_docs()
已从 4 个 AutoGen 文档中索引出 70 个文本块。
# 创建 RAG 智能体(结合向量记忆的助手智能体)
rag_assistant = AssistantAgent(
    name="rag_assistant",  # 智能体名称
    model_client=OpenAIChatCompletionClient(model="gpt-4o-mini"),  # 使用 gpt-4o-mini 模型作为对话核心
    memory=[rag_memory],  # 加载用于检索增强生成(RAG)的向量记忆模块
)

# 提出与 AutoGen 相关的问题
stream = rag_assistant.run_stream(task="什么是 AgentChat?")

# 控制台输出智能体的回答(流式输出)
await Console(stream)

# 使用完毕后关闭记忆模块,释放资源
await rag_memory.close()
---------- TextMessage (user) ----------
什么是 AgentChat?
---------- MemoryQueryEvent (rag_assistant) ----------
[MemoryContent(content='e of the AssistantAgent , we can now proceed to the next section to learn about the teams feature in AgentChat. previous Messages next Teams On this page Assistant Agent Getting Result Multi-Modal Input Streaming Messages Using Tools and Workbench Built-in Tools and Workbench Function Tool Model Context Protocol (MCP) Workbench Agent as a Tool Parallel Tool Calls Tool Iterations Structured Output Streaming Tokens Using Model Context Other Preset Agents Next Step Edit on GitHub Show Source so the DOM is not blocked --> © Copyright 2024, Microsoft. Privacy Policy | Consumer Health Privacy Built with the PyData Sphinx Theme 0.16.0.', mime_type='MemoryMimeType.TEXT', metadata={'chunk_index': 16, 'mime_type': 'MemoryMimeType.TEXT', 'source': 'https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/agents.html', 'score': 0.5170652191106875, 'id': '3c588b05-dbc5-4f97-9f9f-21ca000669fb'}), MemoryContent(content='h Literature Review API Reference PyPi Source AgentChat Agents Agents # AutoGen AgentChat provides a set of preset Agents, each with variations in how an agent might respond to messages. All agents share the following attributes and methods: name : The unique name of the agent. description : The description of the agent in text. run : The method that runs the agent given a task as a string or a list of messages, and returns a TaskResult . Agents are expected to be stateful and this method is expected to be called with new messages, not complete history . run_stream : Same as run() but returns an iterator of messages that subclass BaseAgentEvent or BaseChatMessage followed by a TaskResult as the last item. See autogen_agentchat.messages for more information on AgentChat message types. Assistant Agent # AssistantAgent is a built-in agent that uses a language model and has the ability to use tools. Warning AssistantAgent is a “kitchen sink” agent for prototyping and educational purpose – it is very general. Make sure you read the documentation and implementation to understand the design choices. Once you fully understand the design, you may want to implement your own agent. See Custom Agent . from autogen_agentchat.agents import AssistantAgent from autogen_agentchat.messages import StructuredMessage from autogen_agentchat.ui import Console from autogen_ext.models.openai import OpenAIChatCompletionClient # Define a tool that searches the web for information. # For simplicity, we', mime_type='MemoryMimeType.TEXT', metadata={'chunk_index': 1, 'mime_type': 'MemoryMimeType.TEXT', 'source': 'https://microsoft.github.io/autogen/dev/user-guide/agentchat-user-guide/tutorial/agents.html', 'score': 0.4754601398129038, 'id': '0a2b5444-a3c3-41a8-ac6d-0d194846916d'})]
---------- TextMessage (rag_assistant) ----------
AgentChat是一个提供一组预设代理的系统,旨在处理消息和任务。每个代理都有独特的名称和描述,并通过执行特定方法来响应消息。这些代理是有状态的,能够根据接收到的新消息运行,而不是依赖于完整的历史记录。AgentChat的功能包涵了多种不同的代理,用于不同的应用场景,包括自动生成和教育目的。用户也可以根据需要实现自己的自定义代理。 

TERMINATE

此实现提供了一个 RAG 代理,可以根据 AutoGen 文档回答问题。当提出问题时,记忆系统会检索相关的词块并将其添加到上下文中,从而使助手能够生成明智的答案。

对于生产系统,您可能需要:

  • 实施更复杂的分块策略
  • 添加元数据过滤功能
  • 自定义检索评分
  • 针对您的特定领域优化嵌入模型
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值