|
| 1 | +import random |
| 2 | +from typing import Iterator |
| 3 | + |
| 4 | +from ollama import chat |
| 5 | +from ollama._types import ChatResponse |
| 6 | + |
| 7 | + |
| 8 | +def get_weather(city: str) -> str: |
| 9 | + """ |
| 10 | + Get the current temperature for a city |
| 11 | +
|
| 12 | + Args: |
| 13 | + city (str): The name of the city |
| 14 | +
|
| 15 | + Returns: |
| 16 | + str: The current temperature |
| 17 | + """ |
| 18 | + temperatures = list(range(-10, 35)) |
| 19 | + |
| 20 | + temp = random.choice(temperatures) |
| 21 | + |
| 22 | + return f'The temperature in {city} is {temp}°C' |
| 23 | + |
| 24 | + |
| 25 | +def get_weather_conditions(city: str) -> str: |
| 26 | + """ |
| 27 | + Get the weather conditions for a city |
| 28 | +
|
| 29 | + Args: |
| 30 | + city (str): The name of the city |
| 31 | +
|
| 32 | + Returns: |
| 33 | + str: The current weather conditions |
| 34 | + """ |
| 35 | + conditions = ['sunny', 'cloudy', 'rainy', 'snowy', 'foggy'] |
| 36 | + return random.choice(conditions) |
| 37 | + |
| 38 | + |
| 39 | +available_tools = {'get_weather': get_weather, 'get_weather_conditions': get_weather_conditions} |
| 40 | + |
| 41 | +messages = [{'role': 'user', 'content': 'What is the weather like in London? What are the conditions in Toronto?'}] |
| 42 | + |
| 43 | + |
| 44 | +model = 'gpt-oss:20b' |
| 45 | +# gpt-oss can call tools while "thinking" |
| 46 | +# a loop is needed to call the tools and get the results |
| 47 | +final = True |
| 48 | +while True: |
| 49 | + response_stream: Iterator[ChatResponse] = chat(model=model, messages=messages, tools=[get_weather, get_weather_conditions], stream=True) |
| 50 | + |
| 51 | + for chunk in response_stream: |
| 52 | + if chunk.message.content: |
| 53 | + if not (chunk.message.thinking or chunk.message.thinking == '') and final: |
| 54 | + print('\nFinal result: ') |
| 55 | + final = False |
| 56 | + print(chunk.message.content, end='', flush=True) |
| 57 | + if chunk.message.thinking: |
| 58 | + print(chunk.message.thinking, end='', flush=True) |
| 59 | + |
| 60 | + print() |
| 61 | + |
| 62 | + if chunk.message.tool_calls: |
| 63 | + for tool_call in chunk.message.tool_calls: |
| 64 | + function_to_call = available_tools.get(tool_call.function.name) |
| 65 | + if function_to_call: |
| 66 | + print('\nCalling tool: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments) |
| 67 | + result = function_to_call(**tool_call.function.arguments) |
| 68 | + print('Tool result: ', result + '\n') |
| 69 | + |
| 70 | + messages.append(chunk.message) |
| 71 | + messages.append({'role': 'tool', 'content': result, 'tool_name': tool_call.function.name}) |
| 72 | + else: |
| 73 | + print(f'Tool {tool_call.function.name} not found') |
| 74 | + |
| 75 | + else: |
| 76 | + # no more tool calls, we can stop the loop |
| 77 | + break |
0 commit comments