Repository navigation
types/examples: add tool_name to message and examples #537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0f2e783
types/examples: add tool_name to message and examples
ParthSareen 7a09310
add ignore to support current API: FBT001 Boolean-typed positional ar…
ParthSareen 0296031
update existing tool examples
ParthSareen ac9143d
add comment to ignore
ParthSareen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import random | ||
| from typing import Iterator | ||
|
|
||
| from ollama import ChatResponse, Client | ||
|
|
||
|
|
||
| def get_temperature(city: str) -> int: | ||
| """ | ||
| Get the temperature for a city in Celsius | ||
|
|
||
| Args: | ||
| city (str): The name of the city | ||
|
|
||
| Returns: | ||
| int: The current temperature in Celsius | ||
| """ | ||
| # This is a mock implementation - would need to use a real weather API | ||
| import random | ||
|
|
||
| if city not in ['London', 'Paris', 'New York', 'Tokyo', 'Sydney']: | ||
| return 'Unknown city' | ||
|
|
||
| return str(random.randint(0, 35)) + ' degrees Celsius' | ||
|
|
||
|
|
||
| def get_conditions(city: str) -> str: | ||
| """ | ||
| Get the weather conditions for a city | ||
| """ | ||
| if city not in ['London', 'Paris', 'New York', 'Tokyo', 'Sydney']: | ||
| return 'Unknown city' | ||
| # This is a mock implementation - would need to use a real weather API | ||
| conditions = ['sunny', 'cloudy', 'rainy', 'snowy'] | ||
| return random.choice(conditions) | ||
|
|
||
|
|
||
| available_functions = { | ||
| 'get_temperature': get_temperature, | ||
| 'get_conditions': get_conditions, | ||
| } | ||
|
|
||
|
|
||
| cities = ['London', 'Paris', 'New York', 'Tokyo', 'Sydney'] | ||
| city = random.choice(cities) | ||
| city2 = random.choice(cities) | ||
| messages = [{'role': 'user', 'content': f'What is the temperature in {city}? and what are the weather conditions in {city2}?'}] | ||
| print('----- Prompt:', messages[0]['content'], '\n') | ||
|
|
||
| model = 'qwen3' | ||
| client = Client() | ||
| response: Iterator[ChatResponse] = client.chat(model, stream=True, messages=messages, tools=[get_temperature, get_conditions], think=True) | ||
|
|
||
| for chunk in response: | ||
| if chunk.message.thinking: | ||
| print(chunk.message.thinking, end='', flush=True) | ||
| if chunk.message.content: | ||
| print(chunk.message.content, end='', flush=True) | ||
| if chunk.message.tool_calls: | ||
| for tool in chunk.message.tool_calls: | ||
| if function_to_call := available_functions.get(tool.function.name): | ||
| print('\nCalling function:', tool.function.name, 'with arguments:', tool.function.arguments) | ||
| output = function_to_call(**tool.function.arguments) | ||
| print('> Function output:', output, '\n') | ||
|
|
||
| # Add the assistant message and tool call result to the messages | ||
| messages.append(chunk.message) | ||
| messages.append({'role': 'tool', 'content': str(output), 'tool_name': tool.function.name}) | ||
| else: | ||
| print('Function', tool.function.name, 'not found') | ||
|
|
||
| print('----- Sending result back to model \n') | ||
| if any(msg.get('role') == 'tool' for msg in messages): | ||
| res = client.chat(model, stream=True, tools=[get_temperature, get_conditions], messages=messages, think=True) | ||
| done_thinking = False | ||
| for chunk in res: | ||
| if chunk.message.thinking: | ||
| print(chunk.message.thinking, end='', flush=True) | ||
| if chunk.message.content: | ||
| if not done_thinking: | ||
| print('\n----- Final result:') | ||
| done_thinking = True | ||
| print(chunk.message.content, end='', flush=True) | ||
| if chunk.message.tool_calls: | ||
| # Model should be explaining the tool calls and the results in this output | ||
| print('Model returned tool calls:') | ||
| print(chunk.message.tool_calls) | ||
| else: | ||
| print('No tool calls returned') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @mxyng