mirror of
https://github.com/ollama/ollama-python.git
synced 2026-05-01 19:58:17 +08:00
replied to comments
This commit is contained in:
parent
8039bfd8b9
commit
80ef0c15ae
@ -2,28 +2,59 @@
|
|||||||
# requires-python = ">=3.11"
|
# requires-python = ">=3.11"
|
||||||
# dependencies = [
|
# dependencies = [
|
||||||
# "rich",
|
# "rich",
|
||||||
# "ollama",
|
# "ollama @ file:///Users/nicole/Desktop/ollama-python",
|
||||||
# ]
|
# ]
|
||||||
# ///
|
# ///
|
||||||
from typing import Union
|
import os
|
||||||
|
from typing import Optional, Union
|
||||||
|
|
||||||
|
from rich import print
|
||||||
|
|
||||||
from ollama import Client, WebFetchResponse, WebSearchResponse
|
from ollama import Client, WebFetchResponse, WebSearchResponse
|
||||||
|
|
||||||
|
|
||||||
def format_tool_results(results: Union[WebSearchResponse, WebFetchResponse]):
|
def format_tool_results(
|
||||||
|
results: Union[WebSearchResponse, WebFetchResponse],
|
||||||
|
*,
|
||||||
|
query: Optional[str] = None,
|
||||||
|
url: Optional[str] = None,
|
||||||
|
):
|
||||||
if isinstance(results, WebSearchResponse):
|
if isinstance(results, WebSearchResponse):
|
||||||
output = []
|
output = []
|
||||||
for i, result in enumerate(results.results, 1):
|
if isinstance(results.results, dict):
|
||||||
output.append(f'{i}. {result.content}')
|
for q, search_results in results.results.items():
|
||||||
output.append('')
|
output.append(f'Search results for "{q}":')
|
||||||
|
for i, result in enumerate(search_results, 1):
|
||||||
|
title = getattr(result, 'title', None)
|
||||||
|
url_value = getattr(result, 'url', None)
|
||||||
|
output.append(f'{i}. {title}' if title else f'{i}. {getattr(result, "content", "")}')
|
||||||
|
if url_value:
|
||||||
|
output.append(f' URL: {url_value}')
|
||||||
|
output.append(f' Content: {getattr(result, "content", "")}')
|
||||||
|
output.append('')
|
||||||
|
else:
|
||||||
|
if query:
|
||||||
|
output.append(f'Search results for "{query}":')
|
||||||
|
for i, result in enumerate(results.results, 1):
|
||||||
|
title = getattr(result, 'title', None)
|
||||||
|
url_value = getattr(result, 'url', None)
|
||||||
|
output.append(f'{i}. {title}' if title else f'{i}. {getattr(result, "content", "")}')
|
||||||
|
if url_value:
|
||||||
|
output.append(f' URL: {url_value}')
|
||||||
|
output.append(f' Content: {getattr(result, "content", "")}')
|
||||||
|
output.append('')
|
||||||
|
|
||||||
return '\n'.join(output).rstrip()
|
return '\n'.join(output).rstrip()
|
||||||
|
|
||||||
elif isinstance(results, WebFetchResponse):
|
elif isinstance(results, WebFetchResponse):
|
||||||
output = [
|
output = []
|
||||||
|
if url:
|
||||||
|
output.append(f'Fetch results for "{url}":')
|
||||||
|
output.extend([
|
||||||
f'Title: {results.title}',
|
f'Title: {results.title}',
|
||||||
|
f'URL: {url}' if url else '',
|
||||||
f'Content: {results.content}',
|
f'Content: {results.content}',
|
||||||
]
|
])
|
||||||
if results.links:
|
if results.links:
|
||||||
output.append(f'Links: {", ".join(results.links)}')
|
output.append(f'Links: {", ".join(results.links)}')
|
||||||
output.append('')
|
output.append('')
|
||||||
@ -53,12 +84,21 @@ while True:
|
|||||||
for tool_call in response.message.tool_calls:
|
for tool_call in response.message.tool_calls:
|
||||||
function_to_call = available_tools.get(tool_call.function.name)
|
function_to_call = available_tools.get(tool_call.function.name)
|
||||||
if function_to_call:
|
if function_to_call:
|
||||||
result: Union[WebSearchResponse, WebFetchResponse] = function_to_call(**tool_call.function.arguments)
|
args = tool_call.function.arguments
|
||||||
print('Result from tool call name: ', tool_call.function.name, 'with arguments: ', tool_call.function.arguments)
|
result: Union[WebSearchResponse, WebFetchResponse] = function_to_call(**args)
|
||||||
print('Result: ', format_tool_results(result)[:200])
|
print('Result from tool call name: ', tool_call.function.name, 'with arguments: ', args)
|
||||||
|
|
||||||
|
if tool_call.function.name == 'web_search':
|
||||||
|
formatted = format_tool_results(result, query=args.get('query'))
|
||||||
|
elif tool_call.function.name == 'web_fetch':
|
||||||
|
formatted = format_tool_results(result, url=args.get('url'))
|
||||||
|
else:
|
||||||
|
formatted = format_tool_results(result)
|
||||||
|
|
||||||
|
print('Result: ', formatted[:200])
|
||||||
|
|
||||||
# caps the result at ~2000 tokens
|
# 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[: 2000 * 4], 'tool_name': tool_call.function.name})
|
||||||
else:
|
else:
|
||||||
print(f'Tool {tool_call.function.name} not found')
|
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})
|
messages.append({'role': 'tool', 'content': f'Tool {tool_call.function.name} not found', 'tool_name': tool_call.function.name})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user