mirror of
https://github.com/ollama/ollama-python.git
synced 2026-09-04 18:49:54 +00:00
client/types: add logprobs support (#601)
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from typing import Iterable
|
||||
|
||||
import ollama
|
||||
|
||||
|
||||
def print_logprobs(logprobs: Iterable[dict], label: str) -> None:
|
||||
print(f'\n{label}:')
|
||||
for entry in logprobs:
|
||||
token = entry.get('token', '')
|
||||
logprob = entry.get('logprob')
|
||||
print(f' token={token!r:<12} logprob={logprob:.3f}')
|
||||
for alt in entry.get('top_logprobs', []):
|
||||
if alt['token'] != token:
|
||||
print(f' alt -> {alt["token"]!r:<12} ({alt["logprob"]:.3f})')
|
||||
|
||||
|
||||
messages = [
|
||||
{
|
||||
'role': 'user',
|
||||
'content': 'hi! be concise.',
|
||||
},
|
||||
]
|
||||
|
||||
response = ollama.chat(
|
||||
model='gemma3',
|
||||
messages=messages,
|
||||
logprobs=True,
|
||||
top_logprobs=3,
|
||||
)
|
||||
print('Chat response:', response['message']['content'])
|
||||
print_logprobs(response.get('logprobs', []), 'chat logprobs')
|
||||
@@ -15,7 +15,8 @@ messages = [
|
||||
},
|
||||
{
|
||||
'role': 'assistant',
|
||||
'content': 'The weather in Tokyo is typically warm and humid during the summer months, with temperatures often exceeding 30°C (86°F). The city experiences a rainy season from June to September, with heavy rainfall and occasional typhoons. Winter is mild, with temperatures rarely dropping below freezing. The city is known for its high-tech and vibrant culture, with many popular tourist attractions such as the Tokyo Tower, Senso-ji Temple, and the bustling Shibuya district.',
|
||||
'content': """The weather in Tokyo is typically warm and humid during the summer months, with temperatures often exceeding 30°C (86°F). The city experiences a rainy season from June to September, with heavy rainfall and occasional typhoons. Winter is mild, with temperatures
|
||||
rarely dropping below freezing. The city is known for its high-tech and vibrant culture, with many popular tourist attractions such as the Tokyo Tower, Senso-ji Temple, and the bustling Shibuya district.""",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
from typing import Iterable
|
||||
|
||||
import ollama
|
||||
|
||||
|
||||
def print_logprobs(logprobs: Iterable[dict], label: str) -> None:
|
||||
print(f'\n{label}:')
|
||||
for entry in logprobs:
|
||||
token = entry.get('token', '')
|
||||
logprob = entry.get('logprob')
|
||||
print(f' token={token!r:<12} logprob={logprob:.3f}')
|
||||
for alt in entry.get('top_logprobs', []):
|
||||
if alt['token'] != token:
|
||||
print(f' alt -> {alt["token"]!r:<12} ({alt["logprob"]:.3f})')
|
||||
|
||||
|
||||
response = ollama.generate(
|
||||
model='gemma3',
|
||||
prompt='hi! be concise.',
|
||||
logprobs=True,
|
||||
top_logprobs=3,
|
||||
)
|
||||
print('Generate response:', response['response'])
|
||||
print_logprobs(response.get('logprobs', []), 'generate logprobs')
|
||||
Reference in New Issue
Block a user