函数调用Mistral智能体
本笔记本向您展示如何使用我们的Mistral智能体,该智能体由函数调用功能驱动。
让我们从导入一些简单的构建模块开始。
我们主要需要的是:
- OpenAI API(使用我们自己的
llama_indexLLM 类) - 一个用于保存对话历史记录的地方
- 一个定义我们智能体可以使用的工具。
如果您在 Colab 上打开这个笔记本,您可能需要安装 LlamaIndex 🦙。
%pip install llama-index%pip install llama-index-llms-mistralai%pip install llama-index-embeddings-mistralai让我们为我们的智能体定义一些非常简单的计算器工具。
def multiply(a: int, b: int) -> int: """Multiple two integers and returns the result integer""" return a * b
def add(a: int, b: int) -> int: """Add two integers and returns the result integer""" return a + b确保您的 MISTRAL_API_KEY 已设置。否则请明确指定 api_key 参数。
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(model="mistral-large-latest", api_key="...")初始化 Mistral 智能体
Section titled “Initialize Mistral Agent”这里我们初始化一个带有计算器功能的简单Mistral智能体。
from llama_index.core.agent.workflow import FunctionAgent
agent = FunctionAgent( tools=[multiply, add], llm=llm,)response = await agent.run("What is (121 + 2) * 5?")print(str(response))Added user message to memory: What is (121 + 2) * 5?=== Calling Function ===Calling function: add with args: {"a": 121, "b": 2}=== Calling Function ===Calling function: multiply with args: {"a": 123, "b": 5}assistant: The result of (121 + 2) * 5 is 615.# inspect sourcesprint(response.tool_calls)Managing Context/Memory
Section titled “Managing Context/Memory”默认情况下,.run() 是无状态的。如果您想要维持状态,可以传入一个 context 对象。
from llama_index.core.workflow import Context
ctx = Context(agent)
response = await agent.run("My name is John Doe", ctx=ctx)response = await agent.run("What is my name?", ctx=ctx)
print(str(response))基于RAG管道的Mistral智能体
Section titled “Mistral Agent over RAG Pipeline”基于一个简单的10K文档构建一个Mistral智能体。我们同时使用Mistral嵌入和mistral-medium来构建RAG流程,并将其作为工具传递给Mistral智能体。
!mkdir -p 'data/10k/'!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/10k/uber_2021.pdf' -O 'data/10k/uber_2021.pdf'from llama_index.core.tools import QueryEngineToolfrom llama_index.core import SimpleDirectoryReader, VectorStoreIndexfrom llama_index.embeddings.mistralai import MistralAIEmbeddingfrom llama_index.llms.mistralai import MistralAI
embed_model = MistralAIEmbedding(api_key="...")query_llm = MistralAI(model="mistral-medium", api_key="...")
# load datauber_docs = SimpleDirectoryReader( input_files=["./data/10k/uber_2021.pdf"]).load_data()# build indexuber_index = VectorStoreIndex.from_documents( uber_docs, embed_model=embed_model)uber_engine = uber_index.as_query_engine(similarity_top_k=3, llm=query_llm)query_engine_tool = QueryEngineTool.from_defaults( query_engine=uber_engine, name="uber_10k", description=( "Provides information about Uber financials for year 2021. " "Use a detailed plain text question as input to the tool." ),)from llama_index.core.agent.workflow import FunctionAgent
agent = FunctionAgent(tools=[query_engine_tool], llm=llm)response = await agent.run( "Tell me both the risk factors and tailwinds for Uber? Do two parallel tool calls.")print(str(response))Added user message to memory: Tell me both the risk factors and tailwinds for Uber? Do two parallel tool calls.=== Calling Function ===Calling function: uber_10k with args: {"input": "What are the risk factors for Uber in 2021?"}=== Calling Function ===Calling function: uber_10k with args: {"input": "What are the tailwinds for Uber in 2021?"}assistant: Based on the information provided, here are the risk factors for Uber in 2021:
1. Failure to offer or develop autonomous vehicle technologies, which could result in inferior performance or safety concerns compared to competitors.2. Dependence on high-quality personnel and the potential impact of attrition or unsuccessful succession planning on the business.3. Security or data privacy breaches, unauthorized access, or destruction of proprietary, employee, or user data.4. Cyberattacks, such as malware, ransomware, viruses, spamming, and phishing attacks, which could harm the company's reputation and operations.5. Climate change risks, including physical and transitional risks, that may adversely impact the business if not managed effectively.6. Reliance on third parties to maintain open marketplaces for distributing products and providing software, which could negatively affect the business if interfered with.7. The need for additional capital to support business growth, which may not be available on reasonable terms or at all.8. Difficulties in identifying, acquiring, and integrating suitable businesses, which could harm operating results and prospects.9. Legal and regulatory risks, including extensive government regulation and oversight related to payment and financial services.10. Intellectual property risks, such as the inability to protect intellectual property or claims of misappropriation by third parties.11. Volatility in the market price of common stock, which could result in steep declines and loss of investment for shareholders.12. Economic risks related to the COVID-19 pandemic, which has adversely impacted and could continue to adversely impact the business, financial condition, and results of operations.13. The potential reclassification of Drivers as employees, workers, or quasi-employees, which could result in material costs associated with defending, settling, or resolving lawsuits and demands for arbitration.
On the other hand, here are some tailwinds for Uber in 2021:
1. Launch of Uber One, a single cross-platform membership program in the United States, which offers discounts, special pricing, priority service, and exclusive perks across rides, delivery, and grocery offerings.2. Introduction of a "Super App" view on iOS