class Memory[source]

基类: ABCComponentBase[BaseModel]

定义内存实现接口的协议。

内存是可用于丰富或修改模型上下文的数据存储。

内存实现可以使用任何存储机制,例如列表、数据库或文件系统。它还可以使用任何检索机制,例如向量搜索或文本搜索。如何存储和检索数据取决于具体实现。

内存实现还有责任根据当前模型上下文和查询内存存储,使用相关的内存内容更新模型上下文。

有关示例实现,请参见ListMemory

component_type: ClassVar[ComponentType] = 'memory'

组件的逻辑类型。

abstract async update_context(model_context: ChatCompletionContext) → UpdateContextResult[source]

使用相关的内存内容更新提供的模型上下文。

参数:

model_context – 要更新的上下文。

返回:

包含相关内存的 UpdateContextResult

abstract async query(query: str | MemoryContentcancellation_token: CancellationToken | None = None**kwargs: Any) → MemoryQueryResult[source]

查询内存存储并返回相关条目。

参数:

  • query – 查询内容项

  • cancellation_token – 用于取消操作的可选令牌

  • **kwargs – 额外的特定于实现的参数

返回:

包含具有相关性分数的内存条目的 MemoryQueryResult

abstract async add(content: MemoryContentcancellation_token: CancellationToken | None = None) → None[source]

向内存添加新内容。

参数:

  • content – 要添加的内存内容

  • cancellation_token – 用于取消操作的可选令牌

abstract async clear() → None[source]

清除内存中的所有条目。

abstract async close() → None[source]

清理内存实现使用的任何资源。

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 contentstr | bytes | Dict[strAny] | Image [必填]

内存项的内容。它可以是字符串、字节、字典或Image

field mime_typeMemoryMimeType | str [必填]

内存内容的 MIME 类型。

field metadataDict[strAny] | None = None

与内存项关联的元数据。

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 resultsList[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 memoriesMemoryQueryResult [必填]

class MemoryMimeType(valuenames=None*module=Nonequalname=Nonetype=Nonestart=1boundary=None)[source]

基类: Enum

内存内容支持的 MIME 类型。

TEXT = 'text/plain'

JSON = 'application/json'

MARKDOWN = 'text/markdown'

IMAGE = 'image/*'

BINARY = 'application/octet-stream'

class ListMemory(name: str | None = Nonememory_contents: List[MemoryContent] | None = None)[source]

基类: MemoryComponent[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 namestr

获取内存实例标识符。

返回:

str – 内存实例名称

property contentList[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: MemoryContentcancellation_token: CancellationToken | None = None) → None[source]

向内存添加新内容。

参数:

  • content – 要存储的内存内容

  • cancellation_token – 用于取消操作的可选令牌

async clear() → None[source]

清除所有内存内容。

async close() → None[source]

如果需要,清理资源。

classmethod _from_config(config: ListMemoryConfig) → Self[source]

从配置对象创建组件的新实例。

参数:

config (T) – 配置对象。

返回:

Self – 组件的新实例。

_to_config() → ListMemoryConfig[source]

转储创建与此实例配置匹配的组件新实例所需的配置。

返回:

T – 组件的配置。

《AI提示工程必知必会》为读者提供了丰富的AI提示工程知识与实战技能。《AI提示工程必知必会》主要内容包括各类提示词的应用,如问答式、指令式、状态类、建议式、安全类和感谢类提示词,以及如何通过实战演练掌握提示词的使用技巧;使用提示词进行文本摘要、改写重述、语法纠错、机器翻译等语言处理任务,以及在数据挖掘、程序开发等领域的应用;AI在绘画创作上的应用,百度文心一言和阿里通义大模型这两大智能平台的特性与功能,以及市场调研中提示词的实战应用。通过阅读《AI提示工程必知必会》,读者可掌握如何有效利用AI提示工程提升工作效率,创新工作流程,并在职场中脱颖而出。

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐