LlamaIndex Tool
LlamaIndexTool
Section titled “LlamaIndexTool”LlamaIndexTool 被设计为对 LlamaIndex tools 和 query engines 的通用封装,让你能够把 LlamaIndex 资源作为 RAG/agentic pipelines 中的工具接入 CrewAI agents。这个工具可以让你无缝地将 LlamaIndex 强大的数据处理和检索能力集成到 CrewAI 工作流中。
要使用这个工具,需要安装 LlamaIndex:
uv add llama-index要高效使用 LlamaIndexTool,请按以下步骤操作:
- 安装 LlamaIndex:使用上面的命令安装 LlamaIndex 包。
- 配置 LlamaIndex:按照 LlamaIndex 文档 设置 RAG/agent pipeline。
- 创建 Tool 或 Query Engine:创建你想与 CrewAI 一起使用的 LlamaIndex tool 或 query engine。
下面的示例展示了如何从不同的 LlamaIndex 组件初始化这个工具:
从 LlamaIndex Tool 初始化
Section titled “从 LlamaIndex Tool 初始化”from crewai_tools import LlamaIndexToolfrom crewai import Agentfrom llama_index.core.tools import FunctionTool
# Example 1: Initialize from FunctionTooldef search_data(query: str) -> str: """Search for information in the data.""" # Your implementation here return f"Results for: {query}"
# Create a LlamaIndex FunctionToolog_tool = FunctionTool.from_defaults( search_data, name="DataSearchTool", description="Search for information in the data")
# Wrap it with LlamaIndexTooltool = LlamaIndexTool.from_tool(og_tool)
# Define an agent that uses the tool@agentdef researcher(self) -> Agent: ''' This agent uses the LlamaIndexTool to search for information. ''' return Agent( config=self.agents_config["researcher"], tools=[tool] )从 LlamaHub Tools 初始化
Section titled “从 LlamaHub Tools 初始化”from crewai_tools import LlamaIndexToolfrom llama_index.tools.wolfram_alpha import WolframAlphaToolSpec
# Initialize from LlamaHub Toolswolfram_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 Query Engine 初始化
Section titled “从 LlamaIndex Query Engine 初始化”from crewai_tools import LlamaIndexToolfrom llama_index.core import VectorStoreIndexfrom llama_index.core.readers import SimpleDirectoryReader
# Load documentsdocuments = SimpleDirectoryReader("./data").load_data()
# Create an indexindex = VectorStoreIndex.from_documents(documents)
# Create a query enginequery_engine = index.as_query_engine()
# Create a LlamaIndexTool from the query enginequery_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
Section titled “from_tool”从一个 LlamaIndex tool 创建 LlamaIndexTool。
@classmethoddef from_tool(cls, tool: Any, **kwargs: Any) -> "LlamaIndexTool": # Implementation detailsfrom_query_engine
Section titled “from_query_engine”从一个 LlamaIndex query engine 创建 LlamaIndexTool。
@classmethoddef from_query_engine( cls, query_engine: Any, name: Optional[str] = None, description: Optional[str] = None, return_direct: bool = False, **kwargs: Any,) -> "LlamaIndexTool": # Implementation detailsfrom_query_engine 方法接受以下参数:
- query_engine:必填。要封装的 LlamaIndex query engine。
- name:可选。工具名称。
- description:可选。工具描述。
- return_direct:可选。是否直接返回响应。默认值为
False。
LlamaIndexTool 提供了一种强大的方式,可将 LlamaIndex 的能力集成到 CrewAI agents 中。通过封装 LlamaIndex tools 和 query engines,它让 agents 能够利用复杂的数据检索和处理功能,从而增强处理复杂信息源的能力。