autogen_ext.experimental.task_centric_memory#

class MemoryBankConfig[source]#

基类:TypedDict

distance_threshold: int#
n_results: int#
path: str#
relevance_conversion_threshold: float#
class MemoryController(reset: bool, client: ChatCompletionClient, task_assignment_callback: Callable[[str], Awaitable[Tuple[str, str]]] | None = None, config: MemoryControllerConfig | None = None, logger: PageLogger | None = None)[source]#

基类:object

(实验性,研究进行中)

实现快速的、基于内存的学习,并管理进出记忆库的信息流。

参数:
  • reset – True 表示在开始之前清空记忆库。

  • client – 内部使用的模型客户端。

  • task_assignment_callback – 可选的回调函数,用于将任务分配给调用者管理的任何代理。

  • config

    一个可选的字典,可用于覆盖以下值

    • generalize_task:是否用更通用的术语重写任务。

    • revise_generalized_task:是否评论然后重写通用任务。

    • generate_topics:是否直接基于任务或从任务中提取的主题进行检索。

    • validate_memos:是否对检索到的备忘录应用最终验证阶段。

    • max_memos_to_retrieve:从 retrieve_relevant_memos() 返回的最大备忘录数量。

    • max_train_trials:尝试在任务上进行训练的最大学习迭代次数。

    • max_test_trials:在测试任务失败时尝试的总次数。

    • MemoryBank:传递给 MemoryBank 的配置字典。

  • logger – 可选的 logger。如果为 None,将创建一个默认的 logger。

示例

首先需要安装 task-centric-memory 额外功能

pip install "autogen-ext[task-centric-memory]"

以下代码段展示了如何使用此类进行最基本的记忆存储和检索。

import asyncio
from autogen_ext.models.openai import OpenAIChatCompletionClient
from autogen_ext.experimental.task_centric_memory import MemoryController
from autogen_ext.experimental.task_centric_memory.utils import PageLogger


async def main() -> None:
    client = OpenAIChatCompletionClient(model="gpt-4o")
    logger = PageLogger(config={"level": "DEBUG", "path": "./pagelogs/quickstart"})  # Optional, but very useful.
    memory_controller = MemoryController(reset=True, client=client, logger=logger)

    # Add a few task-insight pairs as memories, where an insight can be any string that may help solve the task.
    await memory_controller.add_memo(task="What color do I like?", insight="Deep blue is my favorite color")
    await memory_controller.add_memo(task="What's another color I like?", insight="I really like cyan")
    await memory_controller.add_memo(task="What's my favorite food?", insight="Halibut is my favorite")

    # Retrieve memories for a new task that's related to only two of the stored memories.
    memos = await memory_controller.retrieve_relevant_memos(task="What colors do I like most?")
    print("{} memories retrieved".format(len(memos)))
    for memo in memos:
        print("- " + memo.insight)


asyncio.run(main())
async add_memo(insight: str, task: None | str = None, index_on_both: bool = True) None[source]#

将一条见解添加到记忆库,使用任务(如果提供)作为上下文。

async add_task_solution_pair_to_memory(task: str, solution: str) None[source]#

将任务-解决方案对添加到记忆库中,以便稍后一起检索,作为组合的洞察。当任务-解决方案对是解决与某些其他任务相关的任务的范例时,这非常有用。

async assign_task(task: str, use_memory: bool = True, should_await: bool = True) str[source]#

通过 task_assignment_callback 将任务分配给某个代理,以及任何相关的记忆。

async consider_memo_storage(text: str) str | None[source]#

尝试从给定的文本中提取任何建议并将其添加到记忆中。

async handle_user_message(text: str, should_await: bool = True) str[source]#

通过提取任何建议作为要存储在记忆中的洞察力来处理用户消息,然后调用 assign_task()。

reset_memory() None[source]#

清空 RAM 和磁盘上的记忆库。

async retrieve_relevant_memos(task: str) List[Memo][source]#

从记忆中检索任何看起来与任务相关的备忘录。

async test_on_task(task: str, expected_answer: str, num_trials: int = 1) Tuple[str, int, int][source]#

将任务分配给代理,以及从记忆中检索到的任何相关备忘录。

async train_on_task(task: str, expected_answer: str) None[source]#

反复将任务分配给代理,并尝试通过创建有用的洞察力作为记忆来从失败中学习。

class MemoryControllerConfig[source]#

基类:TypedDict

MemoryBank: MemoryBankConfig#
generalize_task: bool#
generate_topics: bool#
max_memos_to_retrieve: int#
max_test_trials: int#
max_train_trials: int#
revise_generalized_task: bool#
validate_memos: bool#