用于 ADK 代理的 Ollama 模型主机¶
Ollama 这是一个工具,允许你在本地托管和运行开源模型。ADK 通过特定的接口与 Ollama 托管的模型进行集成。 LiteLLM 模型连接器库(Model Connector Library)。
立即开始使用吧!¶
使用 LiteLLM 包装器来创建基于 Ollama 托管的模型的代理(agents)。以下代码示例展示了如何将 Gemma 开源模型与您的代理结合使用的基本实现方法:
root_agent = Agent(
model=LiteLlm(model="ollama_chat/gemma3:latest"),
name="dice_agent",
description=(
"hello world agent that can roll a dice of 8 sides and check prime"
" numbers."
),
instruction="""
You roll dice and answer questions about the outcome of the dice rolls.
""",
tools=[
roll_die,
check_prime,
],
)
警告:请谨慎使用。 ollama_chat接口
请确保您已设置了相应的提供者(provider)。 ollama_chat 而不是 ollama. 使用
ollama 这可能会导致一些意外的行为,例如工具调用陷入无限循环(即程序永远无法终止),或者程序忽略之前的执行上下文(即程序忽略之前已经处理过的数据或状态)。
使用 OLLAMA_API_BASE 环境变量
虽然你可以指定…… api_base 在 LiteLLM 中用于生成内容的参数:
从 v1.65.5 版本开始,该库在其他 API 调用时依赖于环境变量。因此,您需要设置相应的环境变量。 OLLAMA_API_BASE 这是一个环境变量,用于存储您的 Ollama 服务器的 URL,以确保所有请求都能被正确路由。
模型选择¶
如果您的代理依赖于某些工具,请确保选择了一个支持这些工具的模型。 Ollama 网站为了获得可靠的结果,请使用具有工具支持(即该模型提供了相应的工具或接口)的模型。 您可以使用以下命令来检查该模型是否支持相应的工具:
ollama show mistral-small3.1
Model
architecture mistral3
parameters 24.0B
context length 131072
embedding length 5120
quantization Q4_K_M
Capabilities
completion
vision
tools
你应该能看到…… tools 该模型被归类为“功能”(capabilities)类别之一。 您还可以查看该模型所使用的模板,并根据自己的需求对其进行修改或调整。
例如,上述模型的默认模板本质上意味着该模型必须一直调用某个函数;这可能会导致函数调用陷入无限循环(即程序永远无法终止)。
Given the following functions, please respond with a JSON for a function call
with its proper arguments that best answers the given prompt.
Respond in the format {"name": function name, "parameters": dictionary of
argument name and its value}. Do not use variables.
你可以将这些提示替换为描述性更强的提示,以防止工具调用陷入无限循环。例如:
Review the user's prompt and the available functions listed below.
First, determine if calling one of these functions is the most appropriate way
to respond. A function call is likely needed if the prompt asks for a specific
action, requires external data lookup, or involves calculations handled by the
functions. If the prompt is a general question or can be answered directly, a
function call is likely NOT needed.
If you determine a function call IS required: Respond ONLY with a JSON object in
the format {"name": "function_name", "parameters": {"argument_name": "value"}}.
Ensure parameter values are concrete, not variables.
If you determine a function call IS NOT required: Respond directly to the user's
prompt in plain text, providing the answer or information requested. Do not
output any JSON.
然后,你可以使用以下命令创建一个新的模型:
使用 OpenAI 提供商的服务。¶
或者,您也可以使用…… openai 作为提供商名称(provider name)。这种方法需要设置相关参数。 OPENAI_API_BASE=http://localhost:11434/v1 和
OPENAI_API_KEY=anything 使用环境变量(env variables)代替…… OLLAMA_API_BASE请注意: API_BASE “value has” 的意思是“某个值(或属性)已经被设置或赋值了”。 /v1 在最后。
root_agent = Agent(
model=LiteLlm(model="openai/mistral-small3.1"),
name="dice_agent",
description=(
"hello world agent that can roll a dice of 8 sides and check prime"
" numbers."
),
instruction="""
You roll dice and answer questions about the outcome of the dice rolls.
""",
tools=[
roll_die,
check_prime,
],
)
调试¶
你可以在代理代码中(在导入相关模块之后)添加以下内容,从而查看发送到 Ollama 服务器的请求信息。
请查找如下所示的这一行: