支持版本:Python v0.1.0。

Ollama 是一个允许你在本地托管和运行开源模型的工具。ADK 通过 LiteLLM 模型连接库与 Ollama 托管的模型集成。

入门

使用 LiteLLM 包装器来创建使用 Ollama 托管模型的 Agent。以下代码示例展示了使用 Gemma 开源模型与您的 Agent 的基本实现:

py 15 行 展开 收起
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,
    ],
)
bash 2 行 展开 收起
export OLLAMA_API_BASE="http://localhost:11434"
adk web

模型选择

如果您的 Agent 依赖于工具,请确保您选择了一个具有工具支持的模型,可从 Ollama 网站 获取。 为了获得可靠的结果,请使用具有工具支持的模型。 您可以使用以下命令检查模型的工具支持情况:

bash 12 行 展开 收起
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

您应该在功能中看到 工具 列出。您还可以查看模型正在使用的模板,并根据您的需要进行调整。

bash 1 行 展开 收起
ollama show --modelfile llama3.2 > model_file_to_modify

例如,上述模型的默认模板本身就表明模型应始终调用一个函数。这可能导致函数调用的无限循环。

text 5 行 展开 收起
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.

你可以将此类提示替换为更具描述性的提示,以防止无限工具调用循环,例如:

text 15 行 展开 收起
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.

然后您可以使用以下命令创建一个新模型:

bash 1 行 展开 收起
ollama create llama3.2-modified -f model_file_to_modify

使用 OpenAI 提供者

或者,你可以使用 openai 作为提供者名称。这种方法需要设置 OPENAI_API_BASE=http://localhost:11434/v1 和 OPENAI_API_KEY=anything 环境变量,而不是 OLLAMA_API_BASE。请注意,API_BASE 的值以 /v1 结尾。

py 15 行 展开 收起
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,
    ],
)
bash 3 行 展开 收起
export OPENAI_API_BASE=http://localhost:11434/v1
export OPENAI_API_KEY=anything
adk web

调试

你可以通过在 Agent 代码的导入语句之后添加以下内容来查看发送到 Ollama 服务器的请求。

py 2 行 展开 收起
import litellm
litellm._turn_on_debug()

寻找类似以下的行:

bash 4 行 展开 收起
Request Sent from LiteLLM:
curl -X POST \
http://localhost:11434/api/chat \
-d '{'model': 'mistral-small3.1', 'messages': [{'role': 'system', 'content': ...

原文:Ollama model host for ADK agents · 固定源版本

最后更新 2026-11-12 22:49 . history