ADK 智能体接入本地 Ollama 模型的配置要点

介绍 ADK 通过 LiteLLM 连接本地 Ollama 托管模型的方法,重点说明 ollama_chat 接口选择、OLLAMA_API_BASE 环境变量设置,以及工具调用模型模板调整和 OpenAI 兼容接口的替代配置。

## ADK 代理机的 Ollama 模型主机[¶](<https://adk.dev/agents/models/ollama/#ollama-model-host-for-adk-agents>) 支持于 ADKPython v0.1.0 [奥拉玛](<https://ollama.com/>) 是一个允许您在本地托管和运行开源模型的工具。 ADK 与 Ollama 托管的模型通过 [LiteLLM](<https://adk.dev/agents/models/litellm/>) 模型连接器库。 ## 开始吧[¶](<https://adk.dev/agents/models/ollama/#get-started>) 使用 LiteLLM 包装器创建 Ollama 托管模型的代理。 下面的代码示例显示了与您的代理使用 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`接口 确定您设置提供者 `ollama_chat` 改为 `ollama`。使用 `ollama` 可能导致出乎意料的行为,如无限工具调用循环,忽略之前的上下文. 使用( U) `OLLAMA_API_BASE` 环境变量 虽然您可以指定 `api_base` 用于生成的 LiteLLM 参数, 截至 v1.65.5 , 库在其它 API 调用时依赖于环境变量。 因此, 您应该设置 `OLLAMA_API_BASE` 用于您的 Ollama 服务器 URL 的环境变量,以确保所有请求的路由正确 。 ``` export OLLAMA_API_BASE="http://localhost:11434" adk web ``` ## 模式选择[¶](<https://adk.dev/agents/models/ollama/#model-choice>) 如果您的代理正在依赖工具, 请确保您从工具支持中选择一个模型 。 [奥拉马网站](<https://ollama.com/search?c=tools>)。对于可靠结果,请使用工具支持的模型。您可以使用以下命令检查模型的工具支持: ``` 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 ``` 你应该看看 **工具** 您也可以查看模型使用的模板,并根据您的需要对其进行调整。 ``` ollama show --modelfile llama3.2 > model_file_to_modify ``` 例如,上述模型的默认模板内在地表明该模型应一直调用一个函数。这可能导致函数调用无穷的循环。 ``` 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. ``` 然后可以创建新模型,其命令如下: ``` ollama create llama3.2-modified -f model_file_to_modify ``` ## 使用 OpenAI 提供者[¶](<https://adk.dev/agents/models/ollama/#use-openai-provider>) 或者,你可以使用 `openai` 作为提供者名称。此方法需要设置 `OPENAI_API_BASE=http://localhost:11434/v1` 财务报告和财务报告 `OPENAI_API_KEY=anything` env 变量代替 `OLLAMA_API_BASE`。注意: `API_BASE` 值为 *`/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, ], ) ``` ``` export OPENAI_API_BASE=http://localhost:11434/v1 export OPENAI_API_KEY=anything adk web ``` ### 调试[¶](<https://adk.dev/agents/models/ollama/#debugging>) 您可以在导入后在代理代码中添加以下内容, 从而看到发送到 Ollama 服务器的请求 。 ``` import litellm litellm._turn_on_debug() ``` 寻找一条如下的行: ``` Request Sent from LiteLLM: curl -X POST \ http://localhost:11434/api/chat \ -d '{'model': 'mistral-small3.1', 'messages': [{'role': 'system', 'content': ... ```

查看原始来源