调用 Google Gemini 代理的函数¶
本笔记本将向您展示如何利用函数调用功能驱动的 Google Gemini Agent。
Google 的 Gemini 2.5 Pro、Gemini 2.5 Flash、Gemini 2.5 Flash-Lite 以及 Gemini 2.0 Flash 模型均支持函数调用功能。您可以在模型概览页面查看完整的功能说明。
初始设置¶
让我们从导入一些基础组件开始。
我们主要需要以下内容:
- Google Gemini API(使用我们自己的 llama_index LLM 类)
- 存储对话历史记录的容器
- 定义智能体可使用的工具集
如果你在 Colab 中打开这个 Notebook,可能需要先安装 LlamaIndex 🦙。
In [ ]:
Copied!
%pip install llama-index-llms-google-genai llama-index -q
%pip install llama-index-llms-google-genai llama-index -q
In [ ]:
Copied!
# import os
# os.environ["GOOGLE_API_KEY"] = "..."
# import os
# os.environ["GOOGLE_API_KEY"] = "..."
让我们为智能体定义一些非常简单的计算器工具。
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
确保已设置 GOOGLE_API_KEY
环境变量。若未设置,请显式指定 api_key
参数。
In [ ]:
Copied!
from llama_index.llms.google_genai import GoogleGenAI
from google.genai import types
llm = GoogleGenAI(
model="gemini-2.5-flash",
generation_config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_budget=0
) # Disables thinking
),
)
from llama_index.llms.google_genai import GoogleGenAI
from google.genai import types
llm = GoogleGenAI(
model="gemini-2.5-flash",
generation_config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(
thinking_budget=0
) # Disables thinking
),
)
初始化 Google Gemini 代理¶
此处我们初始化一个具备计算器功能的简易 Google Gemini 代理。
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!
from llama_index.core.agent.workflow import ToolCallResult
async def run_agent_verbose(query: str):
handler = agent.run(query)
async for event in handler.stream_events():
if isinstance(event, ToolCallResult):
print(
f"Called tool {event.tool_name} with args {event.tool_kwargs}\nGot result: {event.tool_output}"
)
return await handler
from llama_index.core.agent.workflow import ToolCallResult
async def run_agent_verbose(query: str):
handler = agent.run(query)
async for event in handler.stream_events():
if isinstance(event, ToolCallResult):
print(
f"Called tool {event.tool_name} with args {event.tool_kwargs}\nGot result: {event.tool_output}"
)
return await handler
聊天¶
In [ ]:
Copied!
response = await run_agent_verbose("What is (121 + 2) * 5?")
print(str(response))
response = await run_agent_verbose("What is (121 + 2) * 5?")
print(str(response))
Called tool add with args {'b': 2, 'a': 121} Got result: 123 Called tool multiply with args {'a': 123, 'b': 5} Got result: 615 The answer is 615.
In [ ]:
Copied!
# inspect sources
print(response.tool_calls)
# inspect sources
print(response.tool_calls)
[ToolCallResult(tool_name='add', tool_kwargs={'b': 2, 'a': 121}, tool_id='add', tool_output=ToolOutput(content='123', tool_name='add', raw_input={'args': (), 'kwargs': {'b': 2, 'a': 121}}, raw_output=123, is_error=False), return_direct=False), ToolCallResult(tool_name='multiply', tool_kwargs={'a': 123, 'b': 5}, tool_id='multiply', tool_output=ToolOutput(content='615', tool_name='multiply', raw_input={'args': (), 'kwargs': {'a': 123, 'b': 5}}, raw_output=615, is_error=False), return_direct=False)]
管理上下文/记忆¶
默认情况下,.run()
是无状态的。如需保持状态,可传入一个上下文对象。
In [ ]:
Copied!
from llama_index.core.workflow import Context
agent = FunctionAgent(llm=llm)
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
agent = FunctionAgent(llm=llm)
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))
Your name is John Doe.
基于 RAG 管道的 Google Gemini 智能体¶
基于一份简单的10K文档构建Anthropic智能体。我们使用OpenAI嵌入技术和Gemini 2.0 Flash构建RAG(检索增强生成)管道,并将其作为工具传递给Gemini 2.5 Flash智能体。
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.openai import OpenAIEmbedding
from llama_index.llms.google_genai import GoogleGenAI
embed_model = OpenAIEmbedding(model_name="text-embedding-3-large")
query_llm = GoogleGenAI(model="gemini-2.0-flash")
# 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.openai import OpenAIEmbedding
from llama_index.llms.google_genai import GoogleGenAI
embed_model = OpenAIEmbedding(model_name="text-embedding-3-large")
query_llm = GoogleGenAI(model="gemini-2.0-flash")
# 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, verbose=True)
from llama_index.core.agent.workflow import FunctionAgent
agent = FunctionAgent(tools=[query_engine_tool], llm=llm, verbose=True)
In [ ]:
Copied!
response = await agent.run(
"Tell me both the risk factors and tailwinds for Uber?"
)
print(str(response))
response = await agent.run(
"Tell me both the risk factors and tailwinds for Uber?"
)
print(str(response))