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()
        # uncomment the next line of code
        # and choose from the [supported embedders](https://qdrant.github.io/fastembed/examples/Supported_Models/)
        # if you don't want to use the default one
        # self.client._embedding_model_name = 'jinaai/jina-embeddings-v2-small-en'
        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 方法使用 FastEmbed 对数据进行向量化。但如果需要,您可以自定义它。

实例化您的 Crew

您可以在此处学习如何为您的 Crew 设置智能体和任务。我们可以更新 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 issue。