Skip to content

Commit 07ab287

Browse files
authored
examples/gpt-oss: fix examples (#566)
1 parent b0f6b99 commit 07ab287

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

examples/gpt-oss-tools-stream.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "gpt-oss",
5+
# "ollama",
6+
# "rich",
7+
# ]
8+
# ///
19
import random
210
from typing import Iterator
311

4-
from ollama import chat
12+
from rich import print
13+
14+
from ollama import Client
515
from ollama._types import ChatResponse
616

717

@@ -40,35 +50,52 @@ def get_weather_conditions(city: str) -> str:
4050

4151
messages = [{'role': 'user', 'content': 'What is the weather like in London? What are the conditions in Toronto?'}]
4252

53+
client = Client(
54+
# Ollama Turbo
55+
# host="https://ollama.com", headers={'Authorization': (os.getenv('OLLAMA_API_KEY'))}
56+
)
4357

44-
model = 'gpt-oss:20b'
58+
model = 'gpt-oss:120b'
4559
# gpt-oss can call tools while "thinking"
4660
# a loop is needed to call the tools and get the results
4761
final = True
4862
while True:
49-
response_stream: Iterator[ChatResponse] = chat(model=model, messages=messages, tools=[get_weather, get_weather_conditions], stream=True)
63+
response_stream: Iterator[ChatResponse] = client.chat(model=model, messages=messages, tools=[get_weather, get_weather_conditions], stream=True)
64+
tool_calls = []
65+
thinking = ''
66+
content = ''
5067

5168
for chunk in response_stream:
69+
if chunk.message.tool_calls:
70+
tool_calls.extend(chunk.message.tool_calls)
71+
5272
if chunk.message.content:
5373
if not (chunk.message.thinking or chunk.message.thinking == '') and final:
54-
print('\nFinal result: ')
74+
print('\n\n' + '=' * 10)
75+
print('Final result: ')
5576
final = False
5677
print(chunk.message.content, end='', flush=True)
78+
5779
if chunk.message.thinking:
80+
# accumulate thinking
81+
thinking += chunk.message.thinking
5882
print(chunk.message.thinking, end='', flush=True)
5983

84+
if thinking != '' or content != '':
85+
messages.append({'role': 'assistant', 'thinking': thinking, 'content': content, 'tool_calls': tool_calls})
86+
6087
print()
6188

62-
if chunk.message.tool_calls:
63-
for tool_call in chunk.message.tool_calls:
89+
if tool_calls:
90+
for tool_call in tool_calls:
6491
function_to_call = available_tools.get(tool_call.function.name)
6592
if function_to_call:
66-
print('\nCalling tool: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments)
93+
print('\nCalling tool:', tool_call.function.name, 'with arguments: ', tool_call.function.arguments)
6794
result = function_to_call(**tool_call.function.arguments)
6895
print('Tool result: ', result + '\n')
6996

70-
messages.append(chunk.message)
71-
messages.append({'role': 'tool', 'content': result, 'tool_name': tool_call.function.name})
97+
result_message = {'role': 'tool', 'content': result, 'tool_name': tool_call.function.name}
98+
messages.append(result_message)
7299
else:
73100
print(f'Tool {tool_call.function.name} not found')
74101

examples/gpt-oss-tools.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "gpt-oss",
5+
# "ollama",
6+
# "rich",
7+
# ]
8+
# ///
19
import random
210

3-
from ollama import chat
11+
from rich import print
12+
13+
from ollama import Client
414
from ollama._types import ChatResponse
515

616

@@ -40,11 +50,15 @@ def get_weather_conditions(city: str) -> str:
4050
messages = [{'role': 'user', 'content': 'What is the weather like in London? What are the conditions in Toronto?'}]
4151

4252

53+
client = Client(
54+
# Ollama Turbo
55+
# host="https://ollama.com", headers={'Authorization': (os.getenv('OLLAMA_API_KEY'))}
56+
)
4357
model = 'gpt-oss:20b'
4458
# gpt-oss can call tools while "thinking"
4559
# a loop is needed to call the tools and get the results
4660
while True:
47-
response: ChatResponse = chat(model=model, messages=messages, tools=[get_weather, get_weather_conditions])
61+
response: ChatResponse = client.chat(model=model, messages=messages, tools=[get_weather, get_weather_conditions])
4862

4963
if response.message.content:
5064
print('Content: ')
@@ -53,14 +67,14 @@ def get_weather_conditions(city: str) -> str:
5367
print('Thinking: ')
5468
print(response.message.thinking + '\n')
5569

70+
messages.append(response.message)
71+
5672
if response.message.tool_calls:
5773
for tool_call in response.message.tool_calls:
5874
function_to_call = available_tools.get(tool_call.function.name)
5975
if function_to_call:
6076
result = function_to_call(**tool_call.function.arguments)
6177
print('Result from tool call name: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments, 'result: ', result + '\n')
62-
63-
messages.append(response.message)
6478
messages.append({'role': 'tool', 'content': result, 'tool_name': tool_call.function.name})
6579
else:
6680
print(f'Tool {tool_call.function.name} not found')

0 commit comments

Comments
 (0)