跳到内容

如何在 CrewAI 中使用记忆工具

LangMem 的记忆工具让您的 CrewAI 代理能够存储和搜索记忆,从而实现跨对话的持久知识。

安装

安装 LangMem 和 CrewAI

pip install -U crewai langmem

基本用法

为您的 CrewAI 代理添加记忆工具

API: create_manage_memory_tool | create_search_memory_tool

from crewai import Agent
from langmem import create_manage_memory_tool, create_search_memory_tool
from langgraph.store.memory import InMemoryStore

# Set up memory store
store = InMemoryStore(
    index={
        "dims": 1536,
        "embed": "openai:text-embedding-3-small",
    }
)  # (1)!

# Create memory tools
memory_tools = [
    create_manage_memory_tool(namespace="memories", store=store),
    create_search_memory_tool(namespace="memories", store=store),
]

# Create an agent with memory tools
knowledge_agent = Agent(
    role='Knowledge Manager',
    goal='Build and maintain a knowledge base',
    backstory="""You are a knowledge management expert who excels at
    organizing and storing important information for future use.""",
    tools=memory_tools,
    verbose=True
)
  1. 对于生产用途,请将 InMemoryStore 替换为像 PostgresStore 这样的持久化存储。

完整示例

构建一个维护和使用共享知识库的 Crew

API: create_manage_memory_tool | create_search_memory_tool

from crewai import Agent, Crew, Task
from langgraph.store.memory import InMemoryStore

from langmem import create_manage_memory_tool, create_search_memory_tool

# Set up shared store
store = InMemoryStore(
            index={
                "dims": 1536,
                "embed": "openai:text-embedding-3-small",
            }
        )

# Create base tools
base_tools = [
    create_manage_memory_tool(namespace="memories", store=store),
    create_search_memory_tool(namespace="memories", store=store),
]

# Create agents
learner = Agent(
    role="Knowledge Learner",
    goal="Learn and store new information in the knowledge base",
    backstory="Your name is Hannah and you are a little too into Demon Slayers.",
    tools=base_tools,
    function_calling_llm="gpt-4o-mini",
)

teacher = Agent(
    role="Knowledge Teacher",
    goal="Use stored knowledge to answer questions",
    tools=base_tools,
    backstory="You were born on the Nile in the midst of the great pestilence..",
    function_calling_llm="gpt-4o-mini",
)

# Create tasks
learn_task = Task(
    description="Save some of your favorite Demon Slayer quotes in memory.",
    agent=learner,
    expected_output="Response:",
)


# Create and run crew
crew = Crew(agents=[learner, teacher], tasks=[learn_task])
result = crew.kickoff()

teach_task = Task(
    description="Search your memories for information about your teammates.",
    agent=teacher,
    expected_output="Response:",
)
crew = Crew(agents=[learner, teacher], tasks=[teach_task])
result = crew.kickoff()
print(store.search(("memories",)))
# Output:
# [
#     Item(
#         namespace=["memories"],
#         key="bd61f87e-d591-45b7-b950-3f9318604ea3",
#         value={
#             "content": '"The bond between Nezuko and I can never be severed. I will always protect her." - Tanjiro Kamado\n"You have to find your own path, you have to find your own way to live!" - Kanao Tsuyuri\n"It’s not the face that makes someone a monster; it’s the choices they make with their lives." - Giyu Tomioka\n"Give me strength! I want to be strong enough to face my own failures!" - Zenitsu Agatsuma\n"Never give up! Never stop fighting until your last breath!" - Giyu Tomioka'
#         },
#         created_at="2025-02-07T22:24:37.736962+00:00",
#         updated_at="2025-02-07T22:24:37.736969+00:00",
#         score=None,
#     )
# ]

评论