Skip to content

Commit c9793cc

Browse files
authored
Merge pull request #223 from ollama/drifkin/thinking-support
add thinking support and examples
2 parents 83682a7 + f38831b commit c9793cc

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ollama.chat(request)
5959
- `images` `<Uint8Array[] | string[]>`: (Optional) Images to be included in the message, either as Uint8Array or base64 encoded strings.
6060
- `format` `<string>`: (Optional) Set the expected format of the response (`json`).
6161
- `stream` `<boolean>`: (Optional) When true an `AsyncGenerator` is returned.
62+
- `think` `<boolean>`: (Optional) When true, the model will think about the response before responding. Requires thinking support from the model.
6263
- `keep_alive` `<string | number>`: (Optional) How long to keep the model loaded. A number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc.)
6364
- `tools` `<Tool[]>`: (Optional) A list of tool calls the model may make.
6465
- `options` `<Options>`: (Optional) Options to configure the runtime.
@@ -81,6 +82,7 @@ ollama.generate(request)
8182
- `images` `<Uint8Array[] | string[]>`: (Optional) Images to be included, either as Uint8Array or base64 encoded strings.
8283
- `format` `<string>`: (Optional) Set the expected format of the response (`json`).
8384
- `stream` `<boolean>`: (Optional) When true an `AsyncGenerator` is returned.
85+
- `think` `<boolean>`: (Optional) When true, the model will think about the response before responding. Requires thinking support from the model.
8486
- `keep_alive` `<string | number>`: (Optional) How long to keep the model loaded. A number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc.)
8587
- `options` `<Options>`: (Optional) Options to configure the runtime.
8688
- Returns: `<GenerateResponse>`

examples/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## Examples
22

33
> [!IMPORTANT]
4-
> Note: Ensure that `npm build` has been run before running the examples.
4+
> Note: Ensure that `npm run build` has been run before running the examples.
55
66
To run the examples run:
77

examples/thinking/thinking-enabled.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import ollama from 'ollama'
2+
3+
async function main() {
4+
const response = await ollama.chat({
5+
model: 'deepseek-r1',
6+
messages: [
7+
{
8+
role: 'user',
9+
content: 'What is 10 + 23',
10+
},
11+
],
12+
stream: false,
13+
think: true,
14+
})
15+
16+
console.log('Thinking:\n========\n\n' + response.message.thinking)
17+
console.log('\nResponse:\n========\n\n' + response.message.content + '\n\n')
18+
}
19+
20+
main()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import ollama from 'ollama'
2+
3+
async function main() {
4+
const response = await ollama.chat({
5+
model: 'deepseek-r1',
6+
messages: [
7+
{
8+
role: 'user',
9+
content: 'What is 10 + 23',
10+
},
11+
],
12+
stream: true,
13+
think: true,
14+
})
15+
16+
let startedThinking = false
17+
let finishedThinking = false
18+
19+
for await (const chunk of response) {
20+
if (chunk.message.thinking && !startedThinking) {
21+
startedThinking = true
22+
process.stdout.write('Thinking:\n========\n\n')
23+
} else if (chunk.message.content && startedThinking && !finishedThinking) {
24+
finishedThinking = true
25+
process.stdout.write('\n\nResponse:\n========\n\n')
26+
}
27+
28+
if (chunk.message.thinking) {
29+
process.stdout.write(chunk.message.thinking)
30+
} else if (chunk.message.content) {
31+
process.stdout.write(chunk.message.content)
32+
}
33+
}
34+
}
35+
36+
main()

src/interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,15 @@ export interface GenerateRequest {
5656
format?: string | object
5757
images?: Uint8Array[] | string[]
5858
keep_alive?: string | number // a number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc)
59+
think?: boolean
5960

6061
options?: Partial<Options>
6162
}
6263

6364
export interface Message {
6465
role: string
6566
content: string
67+
thinking?: string
6668
images?: Uint8Array[] | string[]
6769
tool_calls?: ToolCall[]
6870
}
@@ -106,6 +108,7 @@ export interface ChatRequest {
106108
format?: string | object
107109
keep_alive?: string | number // a number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc)
108110
tools?: Tool[]
111+
think?: boolean
109112

110113
options?: Partial<Options>
111114
}
@@ -174,6 +177,7 @@ export interface GenerateResponse {
174177
model: string
175178
created_at: Date
176179
response: string
180+
thinking?: string
177181
done: boolean
178182
done_reason: string
179183
context: number[]

0 commit comments

Comments
 (0)