9
9
10
10
from rich import print
11
11
12
- from ollama import WebCrawlResponse , WebSearchResponse , chat , web_crawl , web_search
12
+ from ollama import WebFetchResponse , WebSearchResponse , chat , web_fetch , web_search
13
13
14
14
15
- def format_tool_results (results : Union [WebSearchResponse , WebCrawlResponse ]):
15
+ def format_tool_results (
16
+ results : Union [WebSearchResponse , WebFetchResponse ],
17
+ user_search : str ,
18
+ ):
19
+ output = []
16
20
if isinstance (results , WebSearchResponse ):
17
- if not results .success :
18
- error_msg = ', ' .join (results .errors ) if results .errors else 'Unknown error'
19
- return f'Web search failed: { error_msg } '
20
-
21
- output = []
22
- for query , search_results in results .results .items ():
23
- output .append (f'Search results for "{ query } ":' )
24
- for i , result in enumerate (search_results , 1 ):
25
- output .append (f'{ i } . { result .title } ' )
26
- output .append (f' URL: { result .url } ' )
27
- output .append (f' Content: { result .content } ' )
28
- output .append ('' )
29
-
21
+ output .append (f'Search results for "{ user_search } ":' )
22
+ for result in results .results :
23
+ output .append (f'{ result .title } ' if result .title else f'{ result .content } ' )
24
+ output .append (f' URL: { result .url } ' )
25
+ output .append (f' Content: { result .content } ' )
26
+ output .append ('' )
30
27
return '\n ' .join (output ).rstrip ()
31
28
32
- elif isinstance (results , WebCrawlResponse ):
33
- if not results .success :
34
- error_msg = ', ' .join (results .errors ) if results .errors else 'Unknown error'
35
- return f'Web crawl failed: { error_msg } '
36
-
37
- output = []
38
- for url , crawl_results in results .results .items ():
39
- output .append (f'Crawl results for "{ url } ":' )
40
- for i , result in enumerate (crawl_results , 1 ):
41
- output .append (f'{ i } . { result .title } ' )
42
- output .append (f' URL: { result .url } ' )
43
- output .append (f' Content: { result .content } ' )
44
- if result .links :
45
- output .append (f' Links: { ", " .join (result .links )} ' )
46
- output .append ('' )
47
-
29
+ elif isinstance (results , WebFetchResponse ):
30
+ output .append (f'Fetch results for "{ user_search } ":' )
31
+ output .extend (
32
+ [
33
+ f'Title: { results .title } ' ,
34
+ f'URL: { user_search } ' if user_search else '' ,
35
+ f'Content: { results .content } ' ,
36
+ ]
37
+ )
38
+ if results .links :
39
+ output .append (f'Links: { ", " .join (results .links )} ' )
40
+ output .append ('' )
48
41
return '\n ' .join (output ).rstrip ()
49
42
50
43
51
- # Set OLLAMA_API_KEY in the environment variable or use the headers parameter to set the authorization header
52
- # client = Client(headers={'Authorization ': 'Bearer <OLLAMA_API_KEY>'})
44
+ # client = Client( headers={'Authorization': f"Bearer {os.getenv('OLLAMA_API_KEY')}"} if api_key else None)
45
+ available_tools = { 'web_search ' : web_search , 'web_fetch' : web_fetch }
53
46
54
- available_tools = {'web_search' : web_search , 'web_crawl' : web_crawl }
55
-
56
- query = "ollama's new engine"
47
+ query = "what is ollama's new engine"
57
48
print ('Query: ' , query )
58
49
59
50
messages = [{'role' : 'user' , 'content' : query }]
60
51
while True :
61
- response = chat (model = 'qwen3' , messages = messages , tools = [web_search , web_crawl ], think = True )
52
+ response = chat (model = 'qwen3' , messages = messages , tools = [web_search , web_fetch ], think = True )
62
53
if response .message .thinking :
63
54
print ('Thinking: ' )
64
55
print (response .message .thinking + '\n \n ' )
@@ -72,12 +63,20 @@ def format_tool_results(results: Union[WebSearchResponse, WebCrawlResponse]):
72
63
for tool_call in response .message .tool_calls :
73
64
function_to_call = available_tools .get (tool_call .function .name )
74
65
if function_to_call :
75
- result : WebSearchResponse | WebCrawlResponse = function_to_call (** tool_call .function .arguments )
76
- print ('Result from tool call name: ' , tool_call .function .name , 'with arguments: ' , tool_call .function .arguments )
77
- print ('Result: ' , format_tool_results (result )[:200 ])
66
+ args = tool_call .function .arguments
67
+ result : Union [WebSearchResponse , WebFetchResponse ] = function_to_call (** args )
68
+ print ('Result from tool call name:' , tool_call .function .name , 'with arguments:' )
69
+ print (args )
70
+ print ()
71
+
72
+ user_search = args .get ('query' , '' ) or args .get ('url' , '' )
73
+ formatted_tool_results = format_tool_results (result , user_search = user_search )
74
+
75
+ print (formatted_tool_results [:300 ])
76
+ print ()
78
77
79
78
# caps the result at ~2000 tokens
80
- messages .append ({'role' : 'tool' , 'content' : format_tool_results ( result ) [: 2000 * 4 ], 'tool_name' : tool_call .function .name })
79
+ messages .append ({'role' : 'tool' , 'content' : formatted_tool_results [: 2000 * 4 ], 'tool_name' : tool_call .function .name })
81
80
else :
82
81
print (f'Tool { tool_call .function .name } not found' )
83
82
messages .append ({'role' : 'tool' , 'content' : f'Tool { tool_call .function .name } not found' , 'tool_name' : tool_call .function .name })
0 commit comments