CrewAI

CrewAI 是一个用于编排角色扮演、自主AI代理的框架。通过利用协作智能,CrewAI 使代理能够无缝协作,处理复杂任务。

该框架拥有一个复杂的内存系统,旨在显著增强AI代理的能力。该系统帮助代理记住、推理并从过去的互动中学习。您可以使用Qdrant来存储CrewAI代理的短期记忆和实体记忆。

  • 短期记忆

使用RAG临时存储最近的交互和结果,使代理能够在当前执行期间回忆并利用与其当前上下文相关的信息。

  • 实体记忆

实体记忆捕获并组织在任务过程中遇到的实体(人、地点、概念)信息,促进更深入的理解和关系映射。使用RAG来存储实体信息。

与Qdrant的使用

我们将学习如何自定义CrewAI的默认内存存储以使用Qdrant。

安装

首先,安装CrewAI和Qdrant客户端包:

pip install 'crewai[tools]' 'qdrant-client[fastembed]'

设置一个CrewAI项目

你可以学习如何设置一个CrewAI项目这里。让我们假设项目名为mycrew

定义Qdrant存储

src/mycrew/storage.py

from typing import Any, Dict, List, Optional

from crewai.memory.storage.rag_storage import RAGStorage
from qdrant_client import QdrantClient


class QdrantStorage(RAGStorage):
    """
    Extends Storage to handle embeddings for memory entries using Qdrant.

    """

    def __init__(self, type, allow_reset=True, embedder_config=None, crew=None):
        super().__init__(type, allow_reset, embedder_config, crew)

    def search(
        self,
        query: str,
        limit: int = 3,
        filter: Optional[dict] = None,
        score_threshold: float = 0,
    ) -> List[Any]:
        points = self.client.query(
            self.type,
            query_text=query,
            query_filter=filter,
            limit=limit,
            score_threshold=score_threshold,
        )
        results = [
            {
                "id": point.id,
                "metadata": point.metadata,
                "context": point.document,
                "score": point.score,
            }
            for point in points
        ]

        return results

    def reset(self) -> None:
        self.client.delete_collection(self.type)

    def _initialize_app(self):
        self.client = QdrantClient()
        if not self.client.collection_exists(self.type):
            self.client.create_collection(
                collection_name=self.type,
                vectors_config=self.client.get_fastembed_vector_params(),
                sparse_vectors_config=self.client.get_fastembed_sparse_vector_params(),
            )

    def save(self, value: Any, metadata: Dict[str, Any]) -> None:
        self.client.add(self.type, documents=[value], metadata=[metadata or {}])

addquery 方法使用 快速嵌入 来向量化数据。如果需要,您可以自定义它。

实例化你的团队

您可以了解如何为您的团队设置代理和任务这里。我们可以更新Crew的实例化以使用我们的存储机制。

src/mycrew/crew.py

from crewai import Crew
from crewai.memory.entity.entity_memory import EntityMemory
from crewai.memory.short_term.short_term_memory import ShortTermMemory

from mycrew.storage import QdrantStorage

Crew(
    # Import the agents and tasks here.
    memory=True,
    entity_memory=EntityMemory(storage=QdrantStorage("entity")),
    short_term_memory=ShortTermMemory(storage=QdrantStorage("short-term")),
)

你现在可以使用crew run来运行你的Crew工作流。它将使用Qdrant进行内存的摄入和检索。

进一步阅读

这个页面有用吗?

感谢您的反馈!🙏

我们很抱歉听到这个消息。😔 你可以在GitHub上编辑这个页面,或者创建一个GitHub问题。