autogen_core.memory#
- class ListMemory(name: str | 无 = None, memory_contents: 列表[MemoryContent] | 无 = 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: # 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())
- Parameters:
name – 此内存实例的可选标识符
- classmethod _from_config(config: ListMemoryConfig) 自我 [源代码]#
从配置对象创建组件的新实例。
- Parameters:
config (T) – 配置对象。
- Returns:
Self – 组件的新实例。
- async add(content: MemoryContent, cancellation_token: CancellationToken | 无 = None) 无 [源代码]#
向内存添加新内容。
- Parameters:
内容 – 要存储的记忆内容
cancellation_token – 可选的取消操作令牌
- component_config_schema#
ListMemoryConfig
的别名
- component_provider_override: ClassVar[str | 无] = 'autogen_core.memory.ListMemory'#
覆盖组件的提供商字符串。这应用于防止内部模块名称成为模块名称的一部分。
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- property content: 列表[MemoryContent]#
获取当前的内存内容。
- Returns:
List[MemoryContent] – 存储内存内容列表
- async query(query: str | MemoryContent = '', cancellation_token: CancellationToken | 无 = None, **kwargs: 任何) MemoryQueryResult [源代码]#
返回所有记忆,不做任何过滤。
- Parameters:
query – 在此实现中忽略
cancellation_token – 可选令牌以取消操作
**kwargs – 附加参数(忽略)
- Returns:
MemoryQueryResult 包含所有存储的记忆
- async update_context(model_context: ChatCompletionContext) UpdateContextResult [源代码]#
通过附加内存内容来更新模型上下文。
该方法通过将所有记忆作为SystemMessage添加来改变提供的model_context。
- Parameters:
model_context – 要更新的上下文。如果存在记忆,将会被改变。
- Returns:
UpdateContextResult 包含已添加到上下文中的记忆
- class Memory[源代码]#
基础:
ABC
,ComponentBase
[BaseModel
]定义内存实现接口的协议。
内存是用于数据存储的空间,可以用来丰富或修改模型上下文。
内存实现可以使用任何存储机制,例如列表、数据库或文件系统。 它也可以使用任何检索机制,例如向量搜索或文本搜索。 由实现决定如何存储和检索数据。
更新模型上下文也是内存实现的责任,基于当前的模型上下文并查询内存存储,使用相关的内存内容。
请参见
ListMemory
以获得一个示例实现。- abstract async add(content: MemoryContent, cancellation_token: CancellationToken | 无 = None) 无 [源代码]#
向内存中添加新内容。
- Parameters:
content – 要添加的内存内容
cancellation_token – 可选的取消操作令牌
- component_type: ClassVar[ComponentType] = 'memory'#
组件的逻辑类型。
- abstract async query(query: str | MemoryContent, cancellation_token: CancellationToken | 无 = None, **kwargs: 任何) MemoryQueryResult [源代码]#
查询内存存储并返回相关条目。
- Parameters:
query – 查询内容项
cancellation_token – 可选令牌以取消操作
**kwargs – 额外的特定于实现的参数
- Returns:
包含具有相关性分数的内存条目的MemoryQueryResult
- abstract async update_context(model_context: ChatCompletionContext) UpdateContextResult [源代码]#
使用相关的内存内容更新提供的模型上下文。
- Parameters:
model_context – 需要更新的上下文。
- Returns:
包含相关记忆的UpdateContextResult
- pydantic model MemoryContent[源代码]#
基础:
BaseModel
一个内存内容项。
Show JSON schema
{ "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" ] }
- 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": "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" ] }
- Fields:
results (List[autogen_core.memory._base_memory.MemoryContent])
- field results: 列表[MemoryContent] [Required]#
- pydantic model UpdateContextResult[源代码]#
基础:
BaseModel
内存
update_context()
操作的结果。Show JSON schema
{ "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" ] }
- Fields:
memories (autogen_core.memory._base_memory.MemoryQueryResult)
- field memories: MemoryQueryResult [Required]#