autogen_core.memory#
- class ListMemory(name: str | None = None, memory_contents: List[MemoryContent] | None = None)[源代码]#
基类:
Memory
,Component
[ListMemoryConfig
]基于简单时间顺序列表的内存实现。
该内存实现将内容存储在列表中并按时间顺序检索。它有一个`update_context`方法, 通过追加所有存储的记忆来更新模型上下文。
内存内容可以通过content属性直接访问和修改, 允许外部应用程序直接管理内存内容。
示例:
import asyncio from autogen_core.memory import ListMemory, MemoryContent from autogen_core.model_context import BufferedChatCompletionContext async def main() -> None: # 初始化内存 memory = ListMemory(name="chat_history") # 添加内存内容 content = MemoryContent(content="User prefers formal language", mime_type="text/plain") await memory.add(content) # 直接修改内存内容 memory.content = [MemoryContent(content="New preference", mime_type="text/plain")] # 创建模型上下文 model_context = BufferedChatCompletionContext(buffer_size=10) # 用内存更新模型上下文 await memory.update_context(model_context) # 查看更新后的模型上下文 print(await model_context.get_messages()) asyncio.run(main())
- 参数:
name -- 该内存实例的可选标识符
- classmethod _from_config(config: ListMemoryConfig) Self [源代码]#
从配置对象创建组件的新实例。
- 参数:
config (T) -- 配置对象。
- Returns:
Self -- 组件的新实例。
- async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None [源代码]#
向内存中添加新内容。
- 参数:
content -- 要存储的内存内容
cancellation_token -- 用于取消操作的可选令牌
- component_config_schema#
ListMemoryConfig
的别名
- component_provider_override: ClassVar[str | None] = 'autogen_core.memory.ListMemory'#
覆盖组件的provider字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- property content: List[MemoryContent]#
获取当前内存内容。
- Returns:
List[MemoryContent] -- 存储的内存内容列表
- async query(query: str | MemoryContent = '', cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult [源代码]#
返回所有内存而不进行任何过滤。
- 参数:
query -- 在此实现中被忽略
cancellation_token -- 用于取消操作的可选令牌
**kwargs -- 附加参数(被忽略)
- Returns:
包含所有存储内存的MemoryQueryResult
- async update_context(model_context: ChatCompletionContext) UpdateContextResult [源代码]#
通过追加内存内容更新模型上下文。
此方法通过将所有内存作为SystemMessage添加到提供的model_context中来修改它。
- 参数:
model_context -- 要更新的上下文。如果存在内存将被修改。
- Returns:
包含已添加到上下文中的内存的UpdateContextResult
- class Memory[源代码]#
基类:
ABC
,ComponentBase
[BaseModel
]定义内存实现接口的协议。
内存是用于存储数据的容器,可用于丰富或修改模型上下文。
内存实现可以使用任何存储机制,例如列表、数据库或文件系统。 也可以使用任何检索机制,例如向量搜索或文本搜索。 具体如何存储和检索数据由实现决定。
内存实现还需负责根据当前模型上下文查询内存存储, 并用相关内存内容更新模型上下文。
示例实现可参考
ListMemory
。- abstract async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None [源代码]#
向记忆中添加新内容。
- 参数:
content -- 要添加的记忆内容
cancellation_token -- 可选的取消操作令牌
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- abstract async query(query: str | MemoryContent, cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult [源代码]#
查询记忆存储并返回相关条目。
- 参数:
query -- 查询内容项
cancellation_token -- 可选的取消操作令牌
**kwargs -- 特定实现的额外参数
- Returns:
包含带有相关性分数的记忆条目的MemoryQueryResult
- abstract async update_context(model_context: ChatCompletionContext) UpdateContextResult [源代码]#
使用相关记忆内容更新提供的模型上下文。
- 参数:
model_context -- 待更新的上下文
- Returns:
包含相关记忆的UpdateContextResult
- pydantic model MemoryContent[源代码]#
基类:
BaseModel
一个内存内容项。
Show JSON schema
{ "title": "MemoryContent", "description": "\u4e00\u4e2a\u5185\u5b58\u5185\u5bb9\u9879\u3002", "type": "object", "properties": { "content": { "anyOf": [ { "type": "string" }, { "format": "binary", "type": "string" }, { "type": "object" }, {} ], "title": "Content" }, "mime_type": { "anyOf": [ { "$ref": "#/$defs/MemoryMimeType" }, { "type": "string" } ], "title": "Mime Type" }, "metadata": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Metadata" } }, "$defs": { "MemoryMimeType": { "description": "\u652f\u6301\u7684\u5185\u5b58\u5185\u5bb9 MIME \u7c7b\u578b\u3002", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" } }, "required": [ "content", "mime_type" ] }
- Fields:
content (str | bytes | Dict[str, Any] | autogen_core._image.Image)
metadata (Dict[str, Any] | None)
mime_type (autogen_core.memory._base_memory.MemoryMimeType | str)
- field mime_type: MemoryMimeType | str [Required]#
内存内容的 MIME 类型。
- serialize_mime_type(mime_type: MemoryMimeType | str) str [源代码]#
将 MIME 类型序列化为字符串。
- class MemoryMimeType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[源代码]#
基类:
Enum
支持的内存内容 MIME 类型。
- BINARY = 'application/octet-stream'#
- IMAGE = 'image/*'#
- JSON = 'application/json'#
- MARKDOWN = 'text/markdown'#
- TEXT = 'text/plain'#
- pydantic model MemoryQueryResult[源代码]#
基类:
BaseModel
内存
query()
操作的返回结果。Show JSON schema
{ "title": "MemoryQueryResult", "description": "\u5185\u5b58 :meth:`~autogen_core.memory.Memory.query` \u64cd\u4f5c\u7684\u8fd4\u56de\u7ed3\u679c\u3002", "type": "object", "properties": { "results": { "items": { "$ref": "#/$defs/MemoryContent" }, "title": "Results", "type": "array" } }, "$defs": { "MemoryContent": { "description": "\u4e00\u4e2a\u5185\u5b58\u5185\u5bb9\u9879\u3002", "properties": { "content": { "anyOf": [ { "type": "string" }, { "format": "binary", "type": "string" }, { "type": "object" }, {} ], "title": "Content" }, "mime_type": { "anyOf": [ { "$ref": "#/$defs/MemoryMimeType" }, { "type": "string" } ], "title": "Mime Type" }, "metadata": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Metadata" } }, "required": [ "content", "mime_type" ], "title": "MemoryContent", "type": "object" }, "MemoryMimeType": { "description": "\u652f\u6301\u7684\u5185\u5b58\u5185\u5bb9 MIME \u7c7b\u578b\u3002", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" } }, "required": [ "results" ] }
- Fields:
results (List[autogen_core.memory._base_memory.MemoryContent])
- field results: List[MemoryContent] [Required]#
- pydantic model UpdateContextResult[源代码]#
基类:
BaseModel
内存
update_context()
操作的结果。Show JSON schema
{ "title": "UpdateContextResult", "description": "\u5185\u5b58 :meth:`~autogen_core.memory.Memory.update_context` \u64cd\u4f5c\u7684\u7ed3\u679c\u3002", "type": "object", "properties": { "memories": { "$ref": "#/$defs/MemoryQueryResult" } }, "$defs": { "MemoryContent": { "description": "\u4e00\u4e2a\u5185\u5b58\u5185\u5bb9\u9879\u3002", "properties": { "content": { "anyOf": [ { "type": "string" }, { "format": "binary", "type": "string" }, { "type": "object" }, {} ], "title": "Content" }, "mime_type": { "anyOf": [ { "$ref": "#/$defs/MemoryMimeType" }, { "type": "string" } ], "title": "Mime Type" }, "metadata": { "anyOf": [ { "type": "object" }, { "type": "null" } ], "default": null, "title": "Metadata" } }, "required": [ "content", "mime_type" ], "title": "MemoryContent", "type": "object" }, "MemoryMimeType": { "description": "\u652f\u6301\u7684\u5185\u5b58\u5185\u5bb9 MIME \u7c7b\u578b\u3002", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" }, "MemoryQueryResult": { "description": "\u5185\u5b58 :meth:`~autogen_core.memory.Memory.query` \u64cd\u4f5c\u7684\u8fd4\u56de\u7ed3\u679c\u3002", "properties": { "results": { "items": { "$ref": "#/$defs/MemoryContent" }, "title": "Results", "type": "array" } }, "required": [ "results" ], "title": "MemoryQueryResult", "type": "object" } }, "required": [ "memories" ] }
- Fields:
memories (autogen_core.memory._base_memory.MemoryQueryResult)
- field memories: MemoryQueryResult [Required]#