Mistral 代理的函数调用功能¶
本笔记本将向您展示如何利用基于函数调用能力的 Mistral 智能代理。
初始设置¶
让我们从导入一些基础组件开始。
主要需要准备的是:
- OpenAI API(使用我们自己的
llama_index
LLM 类) - 存储对话历史的容器
- 为智能体定义可使用的工具集
如果您在 Colab 上打开此 Notebook,可能需要安装 LlamaIndex 🦙。
In [ ]:
Copied!
%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
让我们为智能体定义一些非常简单的计算器工具。
In [ ]:
Copied!
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 [ ]:
Copied!
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 智能体¶
这里我们初始化了一个带有计算器功能的简单 Mistral 智能体。
In [ ]:
Copied!
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 [ ]:
Copied!
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 [ ]:
Copied!
# inspect sources
print(response.tool_calls)
# inspect sources
print(response.tool_calls)
管理上下文/记忆¶
默认情况下,.run()
是无状态的。如需保持状态,可传入一个 context
对象。
In [ ]:
Copied!
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 智能体构建¶
在简单的 10K 文档上构建 Mistral 智能体。我们同时使用 Mistral 嵌入技术和 mistral-medium 模型来构建 RAG(检索增强生成)管道,并将其作为工具传递给 Mistral 智能体。
In [ ]:
Copied!
!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 [ ]:
Copied!
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 [ ]:
Copied!
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 [ ]:
Copied!
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