函数调用 Mistral Agent¶
本 notebook 展示了如何使用我们的 Mistral agent,它由函数调用功能提供支持。
初始设置¶
我们首先导入一些简单的构建块。
我们主要需要以下内容:
- OpenAI API (使用我们自己的
llama_index
LLM 类) - 一个保存对话历史的地方
- 定义我们的 agent 可以使用的工具。
如果您在 colab 上打开此 Notebook,您可能需要安装 LlamaIndex 🦙。
In [ ]
已复制!
%pip install llama-index
%pip install llama-index-llms-mistralai
%pip install llama-index-embeddings-mistralai
%pip install llama-index %pip install llama-index-llms-mistralai %pip install llama-index-embeddings-mistralai
让我们为我们的 agent 定义一些非常简单的计算器工具。
In [ ]
已复制!
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
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
参数。
In [ ]
已复制!
from llama_index.llms.mistralai import MistralAI
llm = MistralAI(model="mistral-large-latest", api_key="...")
from llama_index.llms.mistralai import MistralAI llm = MistralAI(model="mistral-large-latest", api_key="...")
初始化 Mistral Agent¶
这里我们使用计算器函数初始化一个简单的 Mistral agent。
In [ ]
已复制!
from llama_index.core.agent.workflow import FunctionAgent
agent = FunctionAgent(
tools=[multiply, add],
llm=llm,
)
from llama_index.core.agent.workflow import FunctionAgent agent = FunctionAgent( tools=[multiply, add], llm=llm, )
聊天¶
In [ ]
已复制!
response = await agent.run("What is (121 + 2) * 5?")
print(str(response))
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.
In [ ]
已复制!
# inspect sources
print(response.tool_calls)
# 检查来源 print(response.tool_calls)
管理上下文/内存¶
默认情况下,.run()
是无状态的。如果您想维护状态,可以传入一个 context
对象。
In [ ]
已复制!
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))
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 Agent¶
构建一个基于简单 10K 文档的 Mistral agent。我们使用 Mistral 嵌入和 mistral-medium 构建 RAG 管道,并将其作为工具传递给 Mistral agent。
In [ ]
已复制!
!mkdir -p 'data/10k/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/uber_2021.pdf' -O 'data/10k/uber_2021.pdf'
!mkdir -p 'data/10k/' !wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/10k/uber_2021.pdf' -O 'data/10k/uber_2021.pdf'
In [ ]
已复制!
from llama_index.core.tools import QueryEngineTool
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.embeddings.mistralai import MistralAIEmbedding
from llama_index.llms.mistralai import MistralAI
embed_model = MistralAIEmbedding(api_key="...")
query_llm = MistralAI(model="mistral-medium", api_key="...")
# load data
uber_docs = SimpleDirectoryReader(
input_files=["./data/10k/uber_2021.pdf"]
).load_data()
# build index
uber_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.tools import QueryEngineTool from llama_index.core import SimpleDirectoryReader, VectorStoreIndex from llama_index.embeddings.mistralai import MistralAIEmbedding from llama_index.llms.mistralai import MistralAI embed_model = MistralAIEmbedding(api_key="...") query_llm = MistralAI(model="mistral-medium", api_key="...") # load data uber_docs = SimpleDirectoryReader( input_files=["./data/10k/uber_2021.pdf"] ).load_data() # build index uber_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." ), )
In [ ]
已复制!
from llama_index.core.agent.workflow import FunctionAgent
agent = FunctionAgent(tools=[query_engine_tool], llm=llm)
from llama_index.core.agent.workflow import FunctionAgent agent = FunctionAgent(tools=[query_engine_tool], llm=llm)
In [ ]
已复制!
response = await agent.run(
"Tell me both the risk factors and tailwinds for Uber? Do two parallel tool calls."
)
print(str(response))
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