主题
LlamaIndex 工具
LlamaIndexTool是 LlamaIndex 工具和查询引擎的封装。
LlamaIndexTool
描述
LlamaIndexTool 旨在作为 LlamaIndex 工具和查询引擎的通用封装,使你能够将 RAG/智能体化流水线方面的 LlamaIndex 资源作为工具接入 CrewAI 智能体。该工具允许你将 LlamaIndex 强大的数据处理和检索能力无缝集成到你的 CrewAI 工作流中。
安装
要使用此工具,你需要安装 LlamaIndex:
shell
uv add llama-index入门步骤
要有效使用 LlamaIndexTool,请按以下步骤操作:
- 安装 LlamaIndex:使用上面的命令安装 LlamaIndex 包。
- 设置 LlamaIndex:按照 LlamaIndex 文档设置 RAG/智能体流水线。
- 创建工具或查询引擎:创建你想与 CrewAI 一起使用的 LlamaIndex 工具或查询引擎。
示例
以下示例演示如何从不同的 LlamaIndex 组件初始化该工具:
从 LlamaIndex 工具创建
python
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 工具创建
python
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 查询引擎创建
python
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。
python
@classmethod
def from_tool(cls, tool: Any, **kwargs: Any) -> "LlamaIndexTool":
# Implementation detailsfrom_query_engine
从 LlamaIndex 查询引擎创建 LlamaIndexTool。
python
@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 工具和查询引擎,它使智能体能够利用复杂的数据检索和处理功能,增强其处理复杂信息源的能力。