跳转到主要内容

LlamaIndexTool

描述

`LlamaIndexTool` 被设计为 LlamaIndex 工具和查询引擎的通用包装器,使您能够将 LlamaIndex 的 RAG/代理管道资源作为工具插入到 CrewAI 代理中。此工具使您可以无缝地将 LlamaIndex 强大的数据处理和检索功能集成到您的 CrewAI 工作流中。

安装

要使用此工具,您需要安装 LlamaIndex
uv add llama-index

开始步骤

要有效使用 `LlamaIndexTool`,请遵循以下步骤
  1. 安装 LlamaIndex:使用上述命令安装 LlamaIndex 包。
  2. 设置 LlamaIndex:遵循 LlamaIndex 文档 设置一个 RAG/代理管道。
  3. 创建工具或查询引擎:创建一个您希望与 CrewAI 一起使用的 LlamaIndex 工具或查询引擎。

示例

以下示例演示了如何从不同的 LlamaIndex 组件初始化该工具

从 LlamaIndex 工具

代码
from crewai_tools import LlamaIndexTool
from crewai import Agent
from llama_index.core.tools import FunctionTool

# Example 1: Initialize from FunctionTool
def search_data(query: str) -> str:
    """Search for information in the data."""
    # Your implementation here
    return f"Results for: {query}"

# Create a LlamaIndex FunctionTool
og_tool = FunctionTool.from_defaults(
    search_data, 
    name="DataSearchTool",
    description="Search for information in the data"
)

# Wrap it with LlamaIndexTool
tool = LlamaIndexTool.from_tool(og_tool)

# Define an agent that uses the tool
@agent
def researcher(self) -> Agent:
    '''
    This agent uses the LlamaIndexTool to search for information.
    '''
    return Agent(
        config=self.agents_config["researcher"],
        tools=[tool]
    )

从 LlamaHub 工具

代码
from crewai_tools import LlamaIndexTool
from llama_index.tools.wolfram_alpha import WolframAlphaToolSpec

# Initialize from LlamaHub Tools
wolfram_spec = WolframAlphaToolSpec(app_id="your_app_id")
wolfram_tools = wolfram_spec.to_tool_list()
tools = [LlamaIndexTool.from_tool(t) for t in wolfram_tools]

从 LlamaIndex 查询引擎

代码
from crewai_tools import LlamaIndexTool
from llama_index.core import VectorStoreIndex
from llama_index.core.readers import SimpleDirectoryReader

# Load documents
documents = SimpleDirectoryReader("./data").load_data()

# Create an index
index = VectorStoreIndex.from_documents(documents)

# Create a query engine
query_engine = index.as_query_engine()

# Create a LlamaIndexTool from the query engine
query_tool = LlamaIndexTool.from_query_engine(
    query_engine,
    name="Company Data Query Tool",
    description="Use this tool to lookup information in company documents"
)

类方法

`LlamaIndexTool` 提供了两种主要的类方法用于创建实例

from_tool

从一个 LlamaIndex 工具创建一个 `LlamaIndexTool`。
代码
@classmethod
def from_tool(cls, tool: Any, **kwargs: Any) -> "LlamaIndexTool":
    # Implementation details

from_query_engine

从一个 LlamaIndex 查询引擎创建一个 `LlamaIndexTool`。
代码
@classmethod
def from_query_engine(
    cls,
    query_engine: Any,
    name: Optional[str] = None,
    description: Optional[str] = None,
    return_direct: bool = False,
    **kwargs: Any,
) -> "LlamaIndexTool":
    # Implementation details

参数

`from_query_engine` 方法接受以下参数
  • query_engine:必需。要包装的 LlamaIndex 查询引擎。
  • name:可选。工具的名称。
  • description:可选。工具的描述。
  • return_direct:可选。是否直接返回响应。默认为 `False`。

结论

`LlamaIndexTool` 提供了一种将 LlamaIndex 的功能集成到 CrewAI 代理中的强大方式。通过包装 LlamaIndex 工具和查询引擎,它使代理能够利用复杂的数据检索和处理功能,从而增强其处理复杂信息源的能力。