-
Notifications
You must be signed in to change notification settings - Fork 1k
client/types: update web search and fetch API #584
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
Changes from all commits
e69184d
d4e9af5
8039bfd
80ef0c1
b3a5767
844c5ba
3f2f4db
5a3b22a
81c71ba
d3ddff3
9d051dc
79eda0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,56 +9,47 @@ | |
|
|
||
| from rich import print | ||
|
|
||
| from ollama import WebCrawlResponse, WebSearchResponse, chat, web_crawl, web_search | ||
| from ollama import WebFetchResponse, WebSearchResponse, chat, web_fetch, web_search | ||
|
|
||
|
|
||
| def format_tool_results(results: Union[WebSearchResponse, WebCrawlResponse]): | ||
| def format_tool_results( | ||
| results: Union[WebSearchResponse, WebFetchResponse], | ||
| user_search: str, | ||
| ): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of two optionals, just have
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's actually just remove these - the model should have context from the attached message - we can just focus on the result |
||
| output = [] | ||
| if isinstance(results, WebSearchResponse): | ||
| if not results.success: | ||
| error_msg = ', '.join(results.errors) if results.errors else 'Unknown error' | ||
| return f'Web search failed: {error_msg}' | ||
|
|
||
| output = [] | ||
| for query, search_results in results.results.items(): | ||
| output.append(f'Search results for "{query}":') | ||
| for i, result in enumerate(search_results, 1): | ||
| output.append(f'{i}. {result.title}') | ||
| output.append(f' URL: {result.url}') | ||
| output.append(f' Content: {result.content}') | ||
| output.append('') | ||
|
|
||
| output.append(f'Search results for "{user_search}":') | ||
| for result in results.results: | ||
| output.append(f'{result.title}' if result.title else f'{result.content}') | ||
| output.append(f' URL: {result.url}') | ||
| output.append(f' Content: {result.content}') | ||
| output.append('') | ||
| return '\n'.join(output).rstrip() | ||
|
|
||
| elif isinstance(results, WebCrawlResponse): | ||
| if not results.success: | ||
| error_msg = ', '.join(results.errors) if results.errors else 'Unknown error' | ||
| return f'Web crawl failed: {error_msg}' | ||
|
|
||
| output = [] | ||
| for url, crawl_results in results.results.items(): | ||
| output.append(f'Crawl results for "{url}":') | ||
| for i, result in enumerate(crawl_results, 1): | ||
| output.append(f'{i}. {result.title}') | ||
| output.append(f' URL: {result.url}') | ||
| output.append(f' Content: {result.content}') | ||
| if result.links: | ||
| output.append(f' Links: {", ".join(result.links)}') | ||
| output.append('') | ||
|
|
||
| elif isinstance(results, WebFetchResponse): | ||
| output.append(f'Fetch results for "{user_search}":') | ||
| output.extend( | ||
| [ | ||
| f'Title: {results.title}', | ||
| f'URL: {user_search}' if user_search else '', | ||
| f'Content: {results.content}', | ||
| ] | ||
| ) | ||
| if results.links: | ||
| output.append(f'Links: {", ".join(results.links)}') | ||
| output.append('') | ||
| return '\n'.join(output).rstrip() | ||
|
|
||
|
|
||
| # Set OLLAMA_API_KEY in the environment variable or use the headers parameter to set the authorization header | ||
| # client = Client(headers={'Authorization': 'Bearer <OLLAMA_API_KEY>'}) | ||
| # client = Client(headers={'Authorization': f"Bearer {os.getenv('OLLAMA_API_KEY')}"} if api_key else None) | ||
| available_tools = {'web_search': web_search, 'web_fetch': web_fetch} | ||
|
|
||
| available_tools = {'web_search': web_search, 'web_crawl': web_crawl} | ||
|
|
||
| query = "ollama's new engine" | ||
| query = "what is ollama's new engine" | ||
| print('Query: ', query) | ||
|
|
||
| messages = [{'role': 'user', 'content': query}] | ||
| while True: | ||
| response = chat(model='qwen3', messages=messages, tools=[web_search, web_crawl], think=True) | ||
| response = chat(model='qwen3', messages=messages, tools=[web_search, web_fetch], think=True) | ||
| if response.message.thinking: | ||
| print('Thinking: ') | ||
| print(response.message.thinking + '\n\n') | ||
|
|
@@ -72,12 +63,20 @@ def format_tool_results(results: Union[WebSearchResponse, WebCrawlResponse]): | |
| for tool_call in response.message.tool_calls: | ||
| function_to_call = available_tools.get(tool_call.function.name) | ||
| if function_to_call: | ||
| result: WebSearchResponse | WebCrawlResponse = function_to_call(**tool_call.function.arguments) | ||
| print('Result from tool call name: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments) | ||
| print('Result: ', format_tool_results(result)[:200]) | ||
| args = tool_call.function.arguments | ||
| result: Union[WebSearchResponse, WebFetchResponse] = function_to_call(**args) | ||
| print('Result from tool call name:', tool_call.function.name, 'with arguments:') | ||
| print(args) | ||
| print() | ||
|
|
||
| user_search = args.get('query', '') or args.get('url', '') | ||
| formatted_tool_results = format_tool_results(result, user_search=user_search) | ||
|
|
||
| print(formatted_tool_results[:300]) | ||
| print() | ||
|
|
||
| # caps the result at ~2000 tokens | ||
| messages.append({'role': 'tool', 'content': format_tool_results(result)[: 2000 * 4], 'tool_name': tool_call.function.name}) | ||
| messages.append({'role': 'tool', 'content': formatted_tool_results[: 2000 * 4], 'tool_name': tool_call.function.name}) | ||
| else: | ||
| print(f'Tool {tool_call.function.name} not found') | ||
| messages.append({'role': 'tool', 'content': f'Tool {tool_call.function.name} not found', 'tool_name': tool_call.function.name}) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.