autogen_core.memory#
- class Memory[source]#
基类:
ABC,ComponentBase[BaseModel]定义内存实现接口的协议。
内存是可用于丰富或修改模型上下文的数据存储。
内存实现可以使用任何存储机制,例如列表、数据库或文件系统。它还可以使用任何检索机制,例如向量搜索或文本搜索。如何存储和检索数据取决于具体实现。
内存实现还有责任根据当前模型上下文和查询内存存储,使用相关的内存内容更新模型上下文。
有关示例实现,请参见
ListMemory。- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- abstract async update_context(model_context: ChatCompletionContext) UpdateContextResult[source]#
使用相关的内存内容更新提供的模型上下文。
- 参数:
model_context – 要更新的上下文。
- 返回:
包含相关内存的 UpdateContextResult
- abstract async query(query: str | MemoryContent, cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult[source]#
查询内存存储并返回相关条目。
- 参数:
query – 查询内容项
cancellation_token – 用于取消操作的可选令牌
**kwargs – 额外的特定于实现的参数
- 返回:
包含具有相关性分数的内存条目的 MemoryQueryResult
- abstract async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None[source]#
向内存添加新内容。
- 参数:
content – 要添加的内存内容
cancellation_token – 用于取消操作的可选令牌
- 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 [必填]#
内存内容的 MIME 类型。
- serialize_mime_type(mime_type: MemoryMimeType | str) str[source]#
将 MIME 类型序列化为字符串。
- 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] [必填]#
- 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 [必填]#
- class MemoryMimeType(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#
基类:
Enum内存内容支持的 MIME 类型。
- TEXT = 'text/plain'#
- JSON = 'application/json'#
- MARKDOWN = 'text/markdown'#
- IMAGE = 'image/*'#
- BINARY = 'application/octet-stream'#
- 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 – 此内存实例的可选标识符
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- component_provider_override: ClassVar[str | None] = 'autogen_core.memory.ListMemory'#
覆盖组件的提供者字符串。这应该用于防止内部模块名称成为模块名称的一部分。
- component_config_schema#
ListMemoryConfig的别名
- property content: List[MemoryContent]#
获取当前内存内容。
- 返回:
List[MemoryContent] – 存储的内存内容列表
- async update_context(model_context: ChatCompletionContext) UpdateContextResult[source]#
通过附加内存内容来更新模型上下文。
此方法通过将所有内存作为 SystemMessage 添加来改变提供的 model_context。
- 参数:
model_context – 要更新的上下文。如果存在内存,将被改变。
- 返回:
包含已添加到上下文中的内存的 UpdateContextResult
- async query(query: str | MemoryContent = '', cancellation_token: CancellationToken | None = None, **kwargs: Any) MemoryQueryResult[source]#
返回所有内存,不进行任何过滤。
- 参数:
query – 在此实现中被忽略
cancellation_token – 用于取消操作的可选令牌
**kwargs – 附加参数(忽略)
- 返回:
包含所有存储内存的 MemoryQueryResult
- async add(content: MemoryContent, cancellation_token: CancellationToken | None = None) None[source]#
向内存添加新内容。
- 参数:
content – 要存储的内存内容
cancellation_token – 用于取消操作的可选令牌