autogen_core.memory#
- class ListMemory(name: str | None = None, memory_contents: List[MemoryContent] | None = None)[source]#
基类:
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: # Initialize memory memory = ListMemory(name="chat_history") # Add memory content content = MemoryContent(content="User prefers formal language", mime_type="text/plain") await memory.add(content) # Directly modify memory contents memory.content = [MemoryContent(content="New preference", mime_type="text/plain")] # Create a model context model_context = BufferedChatCompletionContext(buffer_size=10) # Update a model context with memory await memory.update_context(model_context) # See the updated model context print(await model_context.get_messages()) asyncio.run(main())
- 参数:
name – 此内存实例的可选标识符
- classmethod _from_config(config: ListMemoryConfig) Self [source]#
从配置对象创建一个组件的新实例。
- 参数:
config (T) – 配置对象。
- 返回:
Self – 组件的新实例。
- async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None [source]#
将新内容添加到内存中。
- 参数:
content – 要存储的内存内容
cancellation_token – 用于取消操作的可选令牌
- component_config_schema#
别名为
ListMemoryConfig
- component_provider_override: ClassVar[str | None] = 'autogen_core.memory.ListMemory'#
覆盖组件的提供程序字符串。 这应该用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- property content: List[MemoryContent]#
获取当前的内存内容。
- 返回:
List[MemoryContent] – 存储的内存内容列表
- async query(query: str | MemoryContent = '', cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult [source]#
返回所有记忆,不进行任何过滤。
- 参数:
query – 在此实现中被忽略
cancellation_token – 用于取消操作的可选令牌
**kwargs – 附加参数(已忽略)
- 返回:
包含所有存储记忆的 MemoryQueryResult
- async update_context(model_context: ChatCompletionContext) UpdateContextResult [source]#
通过附加记忆内容来更新模型上下文。
此方法通过将所有记忆添加为 SystemMessage 来改变提供的 model_context。
- 参数:
model_context – 要更新的上下文。如果存在记忆,将会被改变。
- 返回:
包含添加到上下文的记忆的 UpdateContextResult
- class Memory[source]#
基类:
ABC
,ComponentBase
[BaseModel
]定义记忆实现接口的协议。
记忆是用于丰富或修改模型上下文的数据存储。
记忆实现可以使用任何存储机制,例如列表、数据库或文件系统。它也可以使用任何检索机制,例如向量搜索或文本搜索。由实现决定如何存储和检索数据。
记忆实现还负责根据当前模型上下文和查询记忆存储,使用相关的记忆内容更新模型上下文。
有关示例实现,请参见
ListMemory
。- abstract async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None [source]#
向记忆中添加新内容。
- 参数:
content – 要添加的记忆内容
cancellation_token – 用于取消操作的可选令牌
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- abstract async query(query: str | MemoryContent, cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult [source]#
查询记忆存储并返回相关的条目。
- 参数:
query – 查询内容项
cancellation_token – 用于取消操作的可选令牌
**kwargs – 附加的特定于实现的参数
- 返回:
MemoryQueryResult 包含带有相关性分数的记忆条目
- abstract async update_context(model_context: ChatCompletionContext) UpdateContextResult [source]#
使用相关的记忆内容更新提供的模型上下文。
- 参数:
model_context – 要更新的上下文。
- 返回:
UpdateContextResult 包含相关的记忆
- pydantic model MemoryContent[source]#
基类:
BaseModel
一个记忆内容项。
显示 JSON 模式
{ "title": "MemoryContent", "description": "A memory content item.", "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": "Supported MIME types for memory content.", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" } }, "required": [ "content", "mime_type" ] }
- 字段:
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 [source]#
将 MIME 类型序列化为字符串。
- class MemoryMimeType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
基类:
Enum
记忆内容支持的 MIME 类型。
- BINARY = 'application/octet-stream'#
- IMAGE = 'image/*'#
- JSON = 'application/json'#
- MARKDOWN = 'text/markdown'#
- TEXT = 'text/plain'#
- pydantic model MemoryQueryResult[source]#
基类:
BaseModel
记忆
query()
操作的结果。显示 JSON 模式
{ "title": "MemoryQueryResult", "description": "Result of a memory :meth:`~autogen_core.memory.Memory.query` operation.", "type": "object", "properties": { "results": { "items": { "$ref": "#/$defs/MemoryContent" }, "title": "Results", "type": "array" } }, "$defs": { "MemoryContent": { "description": "A memory content item.", "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": "Supported MIME types for memory content.", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" } }, "required": [ "results" ] }
- 字段:
results (List[autogen_core.memory._base_memory.MemoryContent])
- field results: List[MemoryContent] [Required]#
- pydantic model UpdateContextResult[source]#
基类:
BaseModel
记忆
update_context()
操作的结果。显示 JSON 模式
{ "title": "UpdateContextResult", "description": "Result of a memory :meth:`~autogen_core.memory.Memory.update_context` operation.", "type": "object", "properties": { "memories": { "$ref": "#/$defs/MemoryQueryResult" } }, "$defs": { "MemoryContent": { "description": "A memory content item.", "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": "Supported MIME types for memory content.", "enum": [ "text/plain", "application/json", "text/markdown", "image/*", "application/octet-stream" ], "title": "MemoryMimeType", "type": "string" }, "MemoryQueryResult": { "description": "Result of a memory :meth:`~autogen_core.memory.Memory.query` operation.", "properties": { "results": { "items": { "$ref": "#/$defs/MemoryContent" }, "title": "Results", "type": "array" } }, "required": [ "results" ], "title": "MemoryQueryResult", "type": "object" } }, "required": [ "memories" ] }
- 字段:
memories (autogen_core.memory._base_memory.MemoryQueryResult)
- field memories: MemoryQueryResult [Required]#