|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.11" |
| 3 | +# dependencies = [ |
| 4 | +# "gpt-oss", |
| 5 | +# "ollama", |
| 6 | +# "rich", |
| 7 | +# ] |
| 8 | +# /// |
1 | 9 | import random
|
2 | 10 | from typing import Iterator
|
3 | 11 |
|
4 |
| -from ollama import chat |
| 12 | +from rich import print |
| 13 | + |
| 14 | +from ollama import Client |
5 | 15 | from ollama._types import ChatResponse
|
6 | 16 |
|
7 | 17 |
|
@@ -40,35 +50,53 @@ def get_weather_conditions(city: str) -> str:
|
40 | 50 |
|
41 | 51 | messages = [{'role': 'user', 'content': 'What is the weather like in London? What are the conditions in Toronto?'}]
|
42 | 52 |
|
| 53 | +client = Client( |
| 54 | + # Ollama Turbo |
| 55 | + # host="https://ollama.com", headers={'Authorization': (os.getenv('OLLAMA_API_KEY'))} |
| 56 | +) |
43 | 57 |
|
44 |
| -model = 'gpt-oss:20b' |
| 58 | +model = 'gpt-oss:120b' |
45 | 59 | # gpt-oss can call tools while "thinking"
|
46 | 60 | # a loop is needed to call the tools and get the results
|
47 | 61 | final = True
|
48 | 62 | 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 = '' |
50 | 67 |
|
51 | 68 | for chunk in response_stream:
|
| 69 | + if chunk.message.tool_calls: |
| 70 | + tool_calls.extend(chunk.message.tool_calls) |
| 71 | + |
52 | 72 | if chunk.message.content:
|
53 | 73 | if not (chunk.message.thinking or chunk.message.thinking == '') and final:
|
54 |
| - print('\nFinal result: ') |
| 74 | + print('\n\n' + '='*10) |
| 75 | + print('Final result: ') |
55 | 76 | final = False
|
56 | 77 | print(chunk.message.content, end='', flush=True)
|
| 78 | + |
57 | 79 | if chunk.message.thinking:
|
| 80 | + # accumulate thinking |
| 81 | + thinking += chunk.message.thinking |
58 | 82 | print(chunk.message.thinking, end='', flush=True)
|
59 | 83 |
|
| 84 | + |
| 85 | + if thinking != '' or content != '': |
| 86 | + messages.append({'role': 'assistant', 'thinking': thinking, 'content': content, 'tool_calls': tool_calls}) |
| 87 | + |
60 | 88 | print()
|
61 | 89 |
|
62 |
| - if chunk.message.tool_calls: |
63 |
| - for tool_call in chunk.message.tool_calls: |
| 90 | + if tool_calls: |
| 91 | + for tool_call in tool_calls: |
64 | 92 | function_to_call = available_tools.get(tool_call.function.name)
|
65 | 93 | if function_to_call:
|
66 |
| - print('\nCalling tool: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments) |
| 94 | + print('\nCalling tool:', tool_call.function.name, 'with arguments: ', tool_call.function.arguments) |
67 | 95 | result = function_to_call(**tool_call.function.arguments)
|
68 | 96 | print('Tool result: ', result + '\n')
|
69 | 97 |
|
70 |
| - messages.append(chunk.message) |
71 |
| - messages.append({'role': 'tool', 'content': result, 'tool_name': tool_call.function.name}) |
| 98 | + result_message = {'role': 'tool', 'content': result, 'tool_name': tool_call.function.name} |
| 99 | + messages.append(result_message) |
72 | 100 | else:
|
73 | 101 | print(f'Tool {tool_call.function.name} not found')
|
74 | 102 |
|
|
0 commit comments