11import asyncio
22import os
33from loguru import logger
4- import requests
54import rigging as rg
65from rigging import logging
76from rich import print
8- from typing import Annotated
97
108os .environ ["LOGFIRE_IGNORE_NO_CONFIG" ] = "1"
119logging .configure_logging ("DEBUG" , None , "DEBUG" )
1210
1311
14- class RoboPagesTool (rg .Tool ):
15- """Base class for RoboPages tools that are dynamically wrapped from the server"""
16-
17- name = "robopages_tool"
18- description = "A tool from the RoboPages server"
19-
20- def __init__ (self , tool_data ):
21- self .data = tool_data
22- self .name = tool_data ["name" ]
23- self .description = tool_data ["description" ]
24-
25- for function in tool_data ["functions" ]:
26- func_name = function ["name" ]
27- params = function .get ("parameters" , [])
28-
29- def make_func (f_name , f_params ):
30- param_list = []
31- annotations = {"return" : str }
32-
33- for p in f_params :
34- param_name = p ["name" ]
35- param_desc = p .get ("description" , "" )
36- annotations [param_name ] = Annotated [str , param_desc ]
37- param_list .append (param_name )
38-
39- def dynamic_func (self , ** kwargs ):
40- """Dynamically created function"""
41- filtered_kwargs = {
42- k : v for k , v in kwargs .items () if k in param_list
43- }
44- return self ._call_function (f_name , filtered_kwargs )
45-
46- dynamic_func .__name__ = f_name
47- dynamic_func .__annotations__ = annotations
48- dynamic_func .__doc__ = function .get ("description" , "" )
49-
50- return dynamic_func
51-
52- setattr (
53- self ,
54- func_name ,
55- make_func (func_name , params ).__get__ (self , self .__class__ ),
56- )
57-
58- def _call_function (self , func_name : str , args : dict ) -> str :
59- """Call the function on the RoboPages server"""
60- print (f"Calling { self .name } .{ func_name } ({ args } )" )
61-
62- try :
63- response = requests .post (
64- "http://localhost:8000/process" ,
65- json = [
66- {
67- "type" : "function" ,
68- "function" : {
69- "name" : func_name ,
70- "arguments" : args ,
71- },
72- }
73- ],
74- )
75-
76- return response .json ()[0 ]["content" ]
77- except Exception as e :
78- print (f"Error calling function: { e } " )
79- return f"Error: { str (e )} "
80-
81-
82- def create_tool_class (tool_data ):
83- """Create a new Tool class dynamically for a specific tool from RoboPages"""
84- tool_name = tool_data ["name" ]
85- tool_desc = tool_data ["description" ]
86-
87- class_name = f"{ tool_name .replace (' ' , '' )} Tool"
88-
89- new_class = type (
90- class_name ,
91- (rg .Tool ,),
92- {
93- "name" : tool_name ,
94- "description" : tool_desc ,
95- },
96- )
97-
98- for function in tool_data ["functions" ]:
99- func_name = function ["name" ]
100- params = function .get ("parameters" , [])
101-
102- param_list = []
103- annotations = {"return" : str }
104-
105- for p in params :
106- param_name = p ["name" ]
107- param_desc = p .get ("description" , "" )
108- annotations [param_name ] = Annotated [str , param_desc ]
109- param_list .append (param_name )
110-
111- def make_function (fn_name , fn_params ):
112- def dynamic_func (self , ** kwargs ):
113- """Dynamically created function"""
114- filtered_kwargs = {k : v for k , v in kwargs .items () if k in fn_params }
115-
116- # Call the RoboPages server
117- try :
118- response = requests .post (
119- "http://localhost:8000/process" ,
120- json = [
121- {
122- "type" : "function" ,
123- "function" : {
124- "name" : fn_name ,
125- "arguments" : filtered_kwargs ,
126- },
127- }
128- ],
129- )
130- return response .json ()[0 ]["content" ]
131- except Exception as e :
132- print (f"Error calling function: { e } " )
133- return f"Error: { str (e )} "
134-
135- dynamic_func .__name__ = fn_name
136- dynamic_func .__annotations__ = annotations
137- dynamic_func .__doc__ = function .get ("description" , "" )
138-
139- return dynamic_func
140-
141- setattr (new_class , func_name , make_function (func_name , param_list ))
142-
143- return new_class
144-
145-
14612async def run ():
14713 """Main function that runs the chat with RoboPages tools"""
14814
14915 try :
150- # Fetch tools from the RoboPages server
151- response = requests .get ("http://localhost:8000/?flavor=rigging" )
152- tools_data = response .json ()
153-
154- logger .info (f"Fetched { len (tools_data )} tools from RoboPages server" )
155- for tool in tools_data :
156- logger .info (f"Tool: { tool ['name' ]} - { tool ['description' ]} " )
157- for func in tool .get ("functions" , []):
158- logger .info (f" Function: { func ['name' ]} " )
16+ logger .info ("Fetching tools from RoboPages server" )
17+ # Use the built-in robopages integration
18+ tools = rg .integrations .robopages ("http://localhost:8000" )
15919
160- tools = []
161- for tool_data in tools_data :
162- tool_class = create_tool_class (tool_data )
163- tools .append (tool_class ())
164-
165- logger .info (f"Created { len (tools )} tool instances" )
20+ logger .info (f"Fetched { len (tools )} tools from RoboPages server" )
16621
16722 prompt = """
16823 I need you to find all open ports on the local machine (127.0.0.1).
@@ -188,12 +43,17 @@ async def run():
18843 print ("\n --- RESULT ---\n " )
18944 print (chat .last .content )
19045
46+ return chat
47+
19148 except Exception as e :
19249 logger .error (f"Error: { e } " )
19350 import traceback
19451
19552 traceback .print_exc ()
53+ return None
19654
19755
19856if __name__ == "__main__" :
199- asyncio .run (run ())
57+ chat = asyncio .run (run ())
58+ if chat :
59+ print (chat .conversation )
0 commit comments