模型能力(二):流式 / 思考 / 判定 / Web 搜索
- 模型能力(二):流式 / 思考 / 判定 / Web 搜索
- 流式响应
- Key streaming concepts
- Handling streamed chunks
- 思考型模型(Thinking)
- Discover a model's thinking controls
- Enable thinking in API calls
- Stream the reasoning trace
- Decision 判定能力
- Setup
- Choose a label
- Judge an image
- Question types
- Ask multiple questions
- Web 搜索与网页抓取
- Authentication
- Web search API
- Web fetch API
- Building a search agent
- MCP Server
- 上下文长度
- Setting context length
本章覆盖流式输出、推理型模型的思考输出、轻量判定能力、云端 Web 搜索,以及上下文长度的设置方法。
流式响应
Streaming allows you to render text as it is produced by the model.
Streaming is enabled by default through the REST API, but disabled by default in the SDKs.
To enable streaming in the SDKs, set the stream parameter to True.
Key streaming concepts
- Chatting: Stream partial assistant messages. Each chunk includes the
contentso you can render messages as they arrive. - Thinking: Thinking-capable models emit a
thinkingfield alongside regular content in each chunk. Detect this field in streaming chunks to show or hide reasoning traces before the final answer arrives. - Tool calling: Watch for streamed
tool_callsin each chunk, execute the requested tool, and append tool outputs back into the conversation.
Handling streamed chunks
注意 It is necessary to accumulate the partial fields in order to maintain the history of the conversation. This is particularly important for tool calling where the thinking, tool call from the model, and the executed tool result must be passed back to the model in the next request.
from ollama import chat
stream = chat(
model='qwen3',
messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
stream=True,
)
in_thinking = False
content = ''
thinking = ''
for chunk in stream:
if chunk.message.thinking:
if not in_thinking:
in_thinking = True
print('Thinking:\n', end='', flush=True)
print(chunk.message.thinking, end='', flush=True)
# accumulate the partial thinking
thinking += chunk.message.thinking
elif chunk.message.content:
if in_thinking:
in_thinking = False
print('\n\nAnswer:\n', end='', flush=True)
print(chunk.message.content, end='', flush=True)
# accumulate the partial content
content += chunk.message.content
# append the accumulated fields to the messages for the next request
new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
import ollama from 'ollama'
async function main() {
const stream = await ollama.chat({
model: 'qwen3',
messages: [{ role: 'user', content: 'What is 17 × 23?' }],
stream: true,
})
let inThinking = false
let content = ''
let thinking = ''
for await (const chunk of stream) {
if (chunk.message.thinking) {
if (!inThinking) {
inThinking = true
process.stdout.write('Thinking:\n')
}
process.stdout.write(chunk.message.thinking)
// accumulate the partial thinking
thinking += chunk.message.thinking
} else if (chunk.message.content) {
if (inThinking) {
inThinking = false
process.stdout.write('\n\nAnswer:\n')
}
process.stdout.write(chunk.message.content)
// accumulate the partial content
content += chunk.message.content
}
}
// append the accumulated fields to the messages for the next request
new_messages = [{ role: 'assistant', thinking: thinking, content: content }]
}
main().catch(console.error)
思考型模型(Thinking)
Thinking-capable models emit a thinking field that separates their reasoning trace from the final answer.
Use this capability to audit model steps, animate the model thinking in a UI, or hide the trace entirely when you only need the final response.
See the full list of thinking models.
Discover a model's thinking controls
Thinking controls vary by model. Use /api/show to discover the values a model supports and the value Ollama uses by default:
curl http://localhost:11434/api/show -d '{
"model": "gpt-oss"
}'
The response includes a top-level thinking object:
{
"thinking": {
"values": ["low", "medium", "high"],
"default": "medium"
}
}
valuescan contain booleans (trueorfalse) for on/off controls. It can also contain model-defined strings for named levels.defaultis used whenthinkis not set.values: [false]means the model does not support thinking.- If
thinkingis omitted, the model has no thinking metadata. The model might conduct thinking based on its existing behavior.
Enable thinking in API calls
Set the think field on a chat or generate request:
true: request thinking output.false: request no thinking output, if the model permits it.null: use the model default.- A string: select a supported level from
thinking.values. Use the exact value from/api/show. Numbers are not supported.
If the model resolves named levels from /api/show metadata, Ollama applies supported names exactly. Unsupported names use the model default.
The reasoning output and answer use separate fields. Chat returns message.thinking and message.content. Generate returns thinking and response.
curl http://localhost:11434/api/chat -d '{
"model": "qwen3",
"messages": [{
"role": "user",
"content": "How many letter r are in strawberry?"
}],
"think": true,
"stream": false
}'
from ollama import chat
response = chat(
model='qwen3',
messages=[{'role': 'user', 'content': 'How many letter r are in strawberry?'}],
think=True,
stream=False,
)
print('Thinking:\n', response.message.thinking)
print('Answer:\n', response.message.content)
import ollama from 'ollama'
const response = await ollama.chat({
model: 'deepseek-r1',
messages: [{ role: 'user', content: 'How many letter r are in strawberry?' }],
think: true,
stream: false,
})
console.log('Thinking:\n', response.message.thinking)
console.log('Answer:\n', response.message.content)
Stream the reasoning trace
Thinking streams interleave reasoning tokens before answer tokens. Detect the first thinking chunk to render a "thinking" section, then switch to the final reply once message.content arrives.
from ollama import chat
stream = chat(
model='qwen3',
messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
think=True,
stream=True,
)
in_thinking = False
for chunk in stream:
if chunk.message.thinking and not in_thinking:
in_thinking = True
print('Thinking:\n', end='')
if chunk.message.thinking:
print(chunk.message.thinking, end='')
elif chunk.message.content:
if in_thinking:
print('\n\nAnswer:\n', end='')
in_thinking = False
print(chunk.message.content, end='')
import ollama from 'ollama'
async function main() {
const stream = await ollama.chat({
model: 'qwen3',
messages: [{ role: 'user', content: 'What is 17 × 23?' }],
think: true,
stream: true,
})
let inThinking = false
for await (const chunk of stream) {
if (chunk.message.thinking && !inThinking) {
inThinking = true
process.stdout.write('Thinking:\n')
}
if (chunk.message.thinking) {
process.stdout.write(chunk.message.thinking)
} else if (chunk.message.content) {
if (inThinking) {
process.stdout.write('\n\nAnswer:\n')
inThinking = false
}
process.stdout.write(chunk.message.content)
}
}
}
main()
Decision 判定能力
Classify text, estimate yes/no probabilities, and score ordered criteria.
注意 Currently available locally in Ollama.
Use System One to classify text, answer yes/no questions, or score text against a rubric.
See the full list of decision models.
Setup
Start Ollama v0.35.0 or later, then download Nimble:
<!-- setup.sh -->
ollama pull nimble
Nimble, Tev1, Clef, and Clef Flash are decision models. The Clef models can also judge images and require v0.35.1 or later.
Local requests do not need an API key.
Choose a label
Put your input in state and describe the possible answers in criteria:
<!-- choice.sh -->
curl http://localhost:11434/v1/systemone \
-H 'Content-Type: application/json' \
-d '{
"model": "nimble",
"state": "Our checkout has returned 500 errors since 9am.",
"questions": {
"label": {
"type": "choice",
"instructions": "Which label fits this ticket?",
"criteria": {
"billing": "Payments and refunds",
"bug": "Software errors",
"account": "Login and account access"
}
}
}
}'
Example response:
{
"model": "nimble",
"answers": {
"label": {
"type": "choice",
"choice": "bug",
"probabilities": {"billing": 0.0125, "bug": 0.9781, "account": 0.0093},
"confidence": 0.8906
}
},
"usage": {"input_tokens": 174, "output_tokens": 1}
}
Judge an image
Clef and Clef Flash read images alongside the state. Send base64-encoded PNG, JPEG, or WebP files in images; they are shared by all questions, in request order. URLs and data URLs are not accepted. state is still required — it holds the context for the decision.
<!-- image.sh -->
curl http://localhost:11434/v1/systemone \
-H 'Content-Type: application/json' \
-d '{
"model": "clef-flash",
"state": "A user took this screenshot and wants to know what it shows.",
"images": ["<base64-encoded image>"],
"questions": {
"has_ollama": {
"type": "noul",
"instructions": "Does this image contain Ollama?"
},
"app": {
"type": "choice",
"instructions": "Which application is shown in this screenshot?",
"criteria": {
"vscode": "Visual Studio Code",
"terminal": "A terminal emulator",
"browser": "A web browser",
"other": "A different application"
}
}
}
}'
Example response:
{
"model": "clef-flash",
"answers": {
"has_ollama": {"type": "noul", "noul": 0.959},
"app": {
"type": "choice",
"choice": "vscode",
"probabilities": {"vscode": 0.966, "other": 0.017, "browser": 0.010, "terminal": 0.007},
"confidence": 0.868
}
},
"usage": {"input_tokens": 677, "output_tokens": 0}
}
Question types
| Type | Criteria | Result |
|---|---|---|
choice |
2–26 named options with descriptions | The most likely option and each option's probability |
noul |
Descriptions for "false" and "true" |
The probability of true, from 0 to 1 |
score |
2–26 descriptions, ordered from lowest to highest | The probability-weighted average of the zero-based levels |
For the urgency criteria below:
score = 0 × P(Routine) + 1 × P(Soon) + 2 × P(Immediate)
For choice and score, confidence measures how strongly the model favors one answer over the others, from 0 to 1. A higher value does not guarantee the answer is correct.
Ask multiple questions
Combine question types in one request. Here, both questions use the same ticket:
<!-- multiple.sh -->
curl http://localhost:11434/v1/systemone \
-H 'Content-Type: application/json' \
-d '{
"model": "nimble",
"state": {"ticket": "I was charged twice. Please refund the extra payment."},
"questions": {
"refund": {
"type": "noul",
"instructions": "Is the customer requesting a refund?",
"criteria": {
"false": "No refund is requested",
"true": "The customer requests a refund"
}
},
"urgency": {
"type": "score",
"instructions": "How urgently does this ticket need a response?",
"criteria": [
"Routine: no time pressure",
"Soon: a customer is inconvenienced",
"Immediate: a critical service is unavailable"
]
}
}
}'
This returns a refund probability of 0.9989 and an urgency score of 0.8308 on the 0–2 scale (rounded). Results can vary with the model and configuration.
See the API reference for request fields, limits, errors, and token usage.
Web 搜索与网页抓取
Ollama's web search API can be used to augment models with the latest information to reduce hallucinations and improve accuracy.
Web search is provided as a REST API with deeper tool integrations in the Python and JavaScript libraries. This also enables models like OpenAI’s gpt-oss models to conduct long-running research tasks.
Authentication
For access to Ollama's web search API, create an API key. A free Ollama account is required.
Web search API
Performs a web search for a single query and returns relevant results.
Request
POST https://ollama.com/api/web_search
query(string, required): the search query stringmax_results(integer, optional): maximum results to return (default 5, max 10)
Response
Returns an object containing:
results(array): array of search result objects, each containing:title(string): the title of the web pageurl(string): the URL of the web pagecontent(string): relevant content snippet from the web page
Examples
注意 Ensure OLLAMA_API_KEY is set or it must be passed in the Authorization header.
cURL Request
curl https://ollama.com/api/web_search \
--header "Authorization: Bearer $OLLAMA_API_KEY" \
-d '{
"query":"what is ollama?"
}'
Response
{
"results": [
{
"title": "Ollama",
"url": "https://ollama.com/",
"content": "Cloud models are now available..."
},
{
"title": "What is Ollama? Introduction to the AI model management tool",
"url": "https://www.hostinger.com/tutorials/what-is-ollama",
"content": "Ariffud M. 6min Read..."
},
{
"title": "Ollama Explained: Transforming AI Accessibility and Language ...",
"url": "https://www.geeksforgeeks.org/artificial-intelligence/ollama-explained-transforming-ai-accessibility-and-language-processing/",
"content": "Data Science Data Science Projects Data Analysis..."
}
]
}
Python library
import ollama
response = ollama.web_search("What is Ollama?")
print(response)
Example output
results = [
{
"title": "Ollama",
"url": "https://ollama.com/",
"content": "Cloud models are now available in Ollama..."
},
{
"title": "What is Ollama? Features, Pricing, and Use Cases - Walturn",
"url": "https://www.walturn.com/insights/what-is-ollama-features-pricing-and-use-cases",
"content": "Our services..."
},
{
"title": "Complete Ollama Guide: Installation, Usage & Code Examples",
"url": "https://collabnix.com/complete-ollama-guide-installation-usage-code-examples",
"content": "Join our Discord Server..."
}
]
More Ollama Python example
JavaScript Library
import { Ollama } from "ollama";
const client = new Ollama();
const results = await client.webSearch("what is ollama?");
console.log(JSON.stringify(results, null, 2));
Example output
{
"results": [
{
"title": "Ollama",
"url": "https://ollama.com/",
"content": "Cloud models are now available..."
},
{
"title": "What is Ollama? Introduction to the AI model management tool",
"url": "https://www.hostinger.com/tutorials/what-is-ollama",
"content": "Ollama is an open-source tool..."
},
{
"title": "Ollama Explained: Transforming AI Accessibility and Language Processing",
"url": "https://www.geeksforgeeks.org/artificial-intelligence/ollama-explained-transforming-ai-accessibility-and-language-processing/",
"content": "Ollama is a groundbreaking..."
}
]
}
More Ollama JavaScript example
Web fetch API
Fetches a single web page by URL and returns its content.
Request
POST https://ollama.com/api/web_fetch
url(string, required): the URL to fetch
Response
Returns an object containing:
title(string): the title of the web pagecontent(string): the main content of the web pagelinks(array): array of links found on the page
Examples
cURL Request
curl --request POST \
--url https://ollama.com/api/web_fetch \
--header "Authorization: Bearer $OLLAMA_API_KEY" \
--header 'Content-Type: application/json' \
--data '{
"url": "ollama.com"
}'
Response
{
"title": "Ollama",
"content": "[Cloud models](https://ollama.com/blog/cloud-models) are now available in Ollama...",
"links": [
"http://ollama.com/",
"http://ollama.com/models",
"https://github.com/ollama/ollama"
]
Python SDK
from ollama import web_fetch
result = web_fetch('https://ollama.com')
print(result)
Result
WebFetchResponse(
title='Ollama',
content='[Cloud models](https://ollama.com/blog/cloud-models) are now available in Ollama\n\n**Chat & build
with open models**\n\n[Download](https://ollama.com/download) [Explore
models](https://ollama.com/models)\n\nAvailable for macOS, Windows, and Linux',
links=['https://ollama.com/', 'https://ollama.com/models', 'https://github.com/ollama/ollama']
)
JavaScript SDK
import { Ollama } from "ollama";
const client = new Ollama();
const fetchResult = await client.webFetch("https://ollama.com");
console.log(JSON.stringify(fetchResult, null, 2));
Result
{
"title": "Ollama",
"content": "[Cloud models](https://ollama.com/blog/cloud-models) are now available in Ollama...",
"links": [
"https://ollama.com/",
"https://ollama.com/models",
"https://github.com/ollama/ollama"
]
}
Building a search agent
Use Ollama’s web search API as a tool to build a mini search agent.
This example uses Alibaba’s Qwen 3 model with 4B parameters.
ollama pull qwen3:4b
from ollama import chat, web_fetch, web_search
available_tools = {'web_search': web_search, 'web_fetch': web_fetch}
messages = [{'role': 'user', 'content': "what is ollama's new engine"}]
while True:
response = chat(
model='qwen3:4b',
messages=messages,
tools=[web_search, web_fetch],
think=True
)
if response.message.thinking:
print('Thinking: ', response.message.thinking)
if response.message.content:
print('Content: ', response.message.content)
messages.append(response.message)
if response.message.tool_calls:
print('Tool calls: ', response.message.tool_calls)
for tool_call in response.message.tool_calls:
function_to_call = available_tools.get(tool_call.function.name)
if function_to_call:
args = tool_call.function.arguments
result = function_to_call(**args)
print('Result: ', str(result)[:200]+'...')
# Result is truncated for limited context lengths
messages.append({'role': 'tool', 'content': str(result)[:2000 * 4], 'tool_name': tool_call.function.name})
else:
messages.append({'role': 'tool', 'content': f'Tool {tool_call.function.name} not found', 'tool_name': tool_call.function.name})
else:
break
Result
Thinking: Okay, the user is asking about Ollama's new engine. I need to figure out what they're referring to. Ollama is a company that develops large language models, so maybe they've released a new model or an updated version of their existing engine....
Tool calls: [ToolCall(function=Function(name='web_search', arguments={'max_results': 3, 'query': 'Ollama new engine'}))]
Result: results=[WebSearchResult(content='# New model scheduling\n\n## September 23, 2025\n\nOllama now includes a significantly improved model scheduling system. Ahead of running a model, Ollama’s new engine
Thinking: Okay, the user asked about Ollama's new engine. Let me look at the search results.
First result is from September 23, 2025, talking about new model scheduling. It mentions improved memory management, reduced crashes, better GPU utilization, and multi-GPU performance. Examples show speed improvements and accurate memory reporting. Supported models include gemma3, llama4, qwen3, etc...
Content: Ollama has introduced two key updates to its engine, both released in 2025:
1. **Enhanced Model Scheduling (September 23, 2025)**
- **Precision Memory Management**: Exact memory allocation reduces out-of-memory crashes and optimizes GPU utilization.
- **Performance Gains**: Examples show significant speed improvements (e.g., 85.54 tokens/s vs 52.02 tokens/s) and full GPU layer utilization.
- **Multi-GPU Support**: Improved efficiency across multiple GPUs, with accurate memory reporting via tools like `nvidia-smi`.
- **Supported Models**: Includes `gemma3`, `llama4`, `qwen3`, `mistral-small3.2`, and more.
2. **Multimodal Engine (May 15, 2025)**
- **Vision Support**: First-class support for vision models, including `llama4:scout` (109B parameters), `gemma3`, `qwen2.5vl`, and `mistral-small3.1`.
- **Multimodal Tasks**: Examples include identifying animals in multiple images, answering location-based questions from videos, and document scanning.
These updates highlight Ollama's focus on efficiency, performance, and expanded capabilities for both text and vision tasks.
Context length and agents
Web search results can return thousands of tokens. It is recommended to increase the context length of the model to at least \~32000 tokens. Search agents work best with full context length. Ollama's cloud models run at the full context length.
MCP Server
You can enable web search in any MCP client through the Python MCP server.
Cline
Ollama's web search can be integrated with Cline easily using the MCP server configuration.
Manage MCP Servers > Configure MCP Servers > Add the following configuration:
{
"mcpServers": {
"web_search_and_fetch": {
"type": "stdio",
"command": "uv",
"args": ["run", "path/to/web-search-mcp.py"],
"env": { "OLLAMA_API_KEY": "your_api_key_here" }
}
}
}
<img src="https://mintcdn.com/ollama-9269c548/lS1IbrlCxMxm029K/images/cline-mcp.png?fit=max&auto=format&n=lS1IbrlCxMxm029K&q=85&s=046239fbe74a8e928752b97b1a8954fa" alt="Cline MCP Configuration" width="852" height="1078" data-path="images/cline-mcp.png" />
Codex
Ollama works well with OpenAI's Codex tool.
Add the following configuration to ~/.codex/config.toml
[mcp_servers.web_search]
command = "uv"
args = ["run", "path/to/web-search-mcp.py"]
env = { "OLLAMA_API_KEY" = "your_api_key_here" }
<img src="https://mintcdn.com/ollama-9269c548/lS1IbrlCxMxm029K/images/codex-mcp.png?fit=max&auto=format&n=lS1IbrlCxMxm029K&q=85&s=775b41bb85af7836b0a5a609de7d1f6f" alt="Codex MCP Configuration" width="1150" height="1014" data-path="images/codex-mcp.png" />
Goose
Ollama can integrate with Goose via its MCP feature.
<img src="https://mintcdn.com/ollama-9269c548/lS1IbrlCxMxm029K/images/goose-mcp-1.png?fit=max&auto=format&n=lS1IbrlCxMxm029K&q=85&s=5fea6e0aab7865dc950470f004c549e8" alt="Goose MCP Configuration 1" width="1152" height="1012" data-path="images/goose-mcp-1.png" />
<img src="https://mintcdn.com/ollama-9269c548/lS1IbrlCxMxm029K/images/goose-mcp-2.png?fit=max&auto=format&n=lS1IbrlCxMxm029K&q=85&s=c69c12389f7dd60ef1c53cd10af82a7d" alt="Goose MCP Configuration 2" width="1146" height="1006" data-path="images/goose-mcp-2.png" />
Other integrations
Ollama can be integrated into most of the tools available either through direct integration of Ollama's API, Python / JavaScript libraries, OpenAI compatible API, and MCP server integration.
上下文长度
Context length is the maximum number of tokens that the model has access to in memory.
注意 Ollama defaults to the following context lengths based on VRAM: * \< 24 GiB VRAM: 4k context * 24-48 GiB VRAM: 32k context * >= 48 GiB VRAM: 256k context
Tasks which require large context like web search, agents, and coding tools should be set to at least 64000 tokens.
Setting context length
Setting a larger context length will increase the amount of memory required to run a model. Ensure you have enough VRAM available to increase the context length.
Cloud models are set to their maximum context length by default.
App
Change the slider in the Ollama app under settings to your desired context length.
<img src="https://mintcdn.com/ollama-9269c548/SjntZZpXgbN5v4M5/images/ollama-settings.png?fit=max&auto=format&n=SjntZZpXgbN5v4M5&q=85&s=e8a7ccd30fd9cee5e93662db05b43dc7" alt="Context length in Ollama app" width="2724" height="2570" data-path="images/ollama-settings.png" />
CLI
If editing the context length for Ollama is not possible, the context length can also be updated when serving Ollama.
OLLAMA_CONTEXT_LENGTH=64000 ollama serve
Check allocated context length and model offloading
For best performance, use the maximum context length for a model, and avoid offloading the model to CPU. Verify the split under PROCESSOR using ollama ps.
ollama ps
NAME ID SIZE PROCESSOR CONTEXT UNTIL
gemma4:latest c6eb396dbd59 9.6 GB 100% GPU 131072 2 minutes from now
