autogen_ext.experimental.task_centric_memory#
- class MemoryController(reset: bool, client: ChatCompletionClient, task_assignment_callback: Callable[[str], Awaitable[元组[str, str]]] | 无 = None, config: MemoryControllerConfig | 无 = None, logger: PageLogger | 无 = None)[源代码]#
基础:
object
(实验性,研究进行中)
实现快速的基于内存的学习,并管理信息的流入和流出内存库。
- Parameters:
reset – 如果为True,在开始前清空内存库。
client – 内部使用的模型客户端。
task_assignment_callback – 一个可选的回调函数,用于将任务分配给由调用者管理的任何代理。
config –
可选的一个字典,可以用来覆盖以下值:
max_train_trials: 在任务训练时尝试的最大学习迭代次数。
max_test_trials: 在任务测试失败时的总尝试次数。
MemoryBank: 传递给 MemoryBank 的配置字典。
logger – 一个可选的日志记录器。如果为None,将创建一个默认的日志记录器。
示例
首先需要安装 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: 无 | str = None, index_on_both: bool = True) 无 [源代码]#
向记忆库中添加一个见解,使用任务(如果提供)作为上下文。
- async add_task_solution_pair_to_memory(task: str, solution: str) 无 [源代码]#
将任务-解决方案对添加到记忆库中,以便以后作为一个组合的见解一起检索。这在任务-解决方案对是解决与某些其他任务相关任务的范例时非常有用。
- async assign_task(task: str, use_memory: bool = True, should_await: bool = True) str [源代码]#
通过task_assignment_callback将任务分配给某个代理,并附上任何相关的记忆。
- async handle_user_message(text: str, should_await: bool = True) str [源代码]#
通过提取任何建议作为存储在内存中的洞察来处理用户消息,然后调用assign_task().