Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ ollama.chat(request)
- `format` `<string>`: (Optional) Set the expected format of the response (`json`).
- `stream` `<boolean>`: (Optional) When true an `AsyncGenerator` is returned.
- `think` `<boolean | "high" | "medium" | "low">`: (Optional) Enable model thinking. Use `true`/`false` or specify a level. Requires model support.
- `logprobs` `<boolean>`: (Optional) Return log probabilities for tokens. Requires model support.
- `top_logprobs` `<number>`: (Optional) Number of top log probabilities to return per token when `logprobs` is enabled.
- `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.)
- `tools` `<Tool[]>`: (Optional) A list of tool calls the model may make.
- `options` `<Options>`: (Optional) Options to configure the runtime.
Expand All @@ -157,6 +159,8 @@ ollama.generate(request)
- `format` `<string>`: (Optional) Set the expected format of the response (`json`).
- `stream` `<boolean>`: (Optional) When true an `AsyncGenerator` is returned.
- `think` `<boolean | "high" | "medium" | "low">`: (Optional) Enable model thinking. Use `true`/`false` or specify a level. Requires model support.
- `logprobs` `<boolean>`: (Optional) Return log probabilities for tokens. Requires model support.
- `top_logprobs` `<number>`: (Optional) Number of top log probabilities to return per token when `logprobs` is enabled.
- `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.)
- `options` `<Options>`: (Optional) Options to configure the runtime.
- Returns: `<GenerateResponse>`
Expand Down
31 changes: 31 additions & 0 deletions examples/logprobs/logprobs-chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Ollama, Logprob } from 'ollama';

function printLogprobs(entries: Logprob[], label: string) {
console.log(`\n${label}:`)
for (const entry of entries) {
console.log(` token=${entry.token.padEnd(12)} logprob=${entry.logprob.toFixed(3)}`)
for (const alt of entry.top_logprobs ?? []) {
console.log(` alt -> ${alt.token.padEnd(12)} (${alt.logprob.toFixed(3)})`)
}
}
}

async function main() {
const client = new Ollama()
console.log(`Using model: gemma3`)

const chatResponse = await client.chat({
model: 'gemma3',
messages: [{ role: 'user', content: 'Say hello in one word.' }],
logprobs: true,
top_logprobs: 3,
})
console.log('Chat response:', chatResponse.message.content)
printLogprobs(chatResponse.logprobs ?? [], 'chat logprobs')
}

main().catch((err) => {
console.error(err)
process.exitCode = 1
})

14 changes: 14 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
images?: Uint8Array[] | string[]
keep_alive?: string | number // a number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc)
think?: boolean | 'high' | 'medium' | 'low'
logprobs?: boolean
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add these fields in the README also

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good callout! will add

top_logprobs?: number

options?: Partial<Options>
}
Expand All @@ -74,7 +76,7 @@
function: {
name: string;
arguments: {
[key: string]: any;

Check warning on line 79 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
};
};
}
Expand All @@ -87,15 +89,15 @@
type?: string;
parameters?: {
type?: string;
$defs?: any;

Check warning on line 92 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
items?: any;

Check warning on line 93 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
required?: string[];
properties?: {
[key: string]: {
type?: string | string[];
items?: any;

Check warning on line 98 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
description?: string;
enum?: any[];

Check warning on line 100 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
};
};
};
Expand All @@ -110,6 +112,8 @@
keep_alive?: string | number // a number (seconds) or a string with a duration unit suffix ("300ms", "1.5h", "2h45m", etc)
tools?: Tool[]
think?: boolean | 'high' | 'medium' | 'low'
logprobs?: boolean
top_logprobs?: number

options?: Partial<Options>
}
Expand Down Expand Up @@ -174,6 +178,14 @@
}

// response types
export interface TokenLogprob {
token: string
logprob: number
}

export interface Logprob extends TokenLogprob {
top_logprobs?: TokenLogprob[]
}

export interface GenerateResponse {
model: string
Expand All @@ -189,6 +201,7 @@
prompt_eval_duration: number
eval_count: number
eval_duration: number
logprobs?: Logprob[]
}

export interface ChatResponse {
Expand All @@ -203,6 +216,7 @@
prompt_eval_duration: number
eval_count: number
eval_duration: number
logprobs?: Logprob[]
}

export interface EmbedResponse {
Expand Down Expand Up @@ -253,9 +267,9 @@
details: ModelDetails
messages: Message[]
modified_at: Date
model_info: Map<string, any>,

Check warning on line 270 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
capabilities: string[],
projector_info?: Map<string, any>

Check warning on line 272 in src/interfaces.ts

View workflow job for this annotation

GitHub Actions / test

Unexpected any. Specify a different type
}

export interface ListResponse {
Expand Down
49 changes: 49 additions & 0 deletions test/browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it, expect, vi } from 'vitest'
import { Ollama } from '../src/browser'
import type { ChatResponse, GenerateResponse } from '../src/interfaces'

describe('Ollama logprob request fields', () => {
it('forwards logprob settings in generate requests', async () => {
const client = new Ollama()
const spy = vi
.spyOn(client as any, 'processStreamableRequest')
.mockResolvedValue({} as GenerateResponse)

await client.generate({
model: 'dummy',
prompt: 'Hello',
logprobs: true,
top_logprobs: 5,
})

expect(spy).toHaveBeenCalledWith(
'generate',
expect.objectContaining({
logprobs: true,
top_logprobs: 5,
}),
)
})

it('forwards logprob settings in chat requests', async () => {
const client = new Ollama()
const spy = vi
.spyOn(client as any, 'processStreamableRequest')
.mockResolvedValue({} as ChatResponse)

await client.chat({
model: 'dummy',
messages: [{ role: 'user', content: 'hi' }],
logprobs: true,
top_logprobs: 3,
})

expect(spy).toHaveBeenCalledWith(
'chat',
expect.objectContaining({
logprobs: true,
top_logprobs: 3,
}),
)
})
})
Loading