|
1 | 1 | import asyncio |
2 | | -import requests |
| 2 | +import os |
| 3 | +from loguru import logger |
3 | 4 | import rigging as rg |
| 5 | +from rigging import logging |
4 | 6 | from rich import print |
5 | 7 |
|
| 8 | +os.environ["LOGFIRE_IGNORE_NO_CONFIG"] = "1" |
| 9 | +logging.configure_logging("DEBUG", None, "DEBUG") |
6 | 10 |
|
7 | | -# we need to wrap the tools in a class that Rigging can understand |
8 | | -class Wrapper(rg.Tool): |
9 | | - # we'll set these in the constructor |
10 | | - name = "_" |
11 | | - description = "_" |
12 | | - |
13 | | - def __init__(self, tool: dict): |
14 | | - self.tool = tool |
15 | | - self.name = tool["name"] |
16 | | - self.description = tool["description"] |
17 | | - |
18 | | - # declare dynamically the functions by their name |
19 | | - for function in tool["functions"]: |
20 | | - setattr( |
21 | | - Wrapper, |
22 | | - function["name"], |
23 | | - lambda self, *args, **kwargs: self._execute_function( |
24 | | - function["name"], *args, **kwargs |
25 | | - ), |
| 11 | + |
| 12 | +async def run(): |
| 13 | + """Main function that runs the chat with RoboPages tools""" |
| 14 | + |
| 15 | + try: |
| 16 | + logger.info("Fetching tools from RoboPages server") |
| 17 | + # Use the built-in robopages integration |
| 18 | + tools = rg.integrations.robopages("http://localhost:8000") |
| 19 | + |
| 20 | + logger.info(f"Fetched {len(tools)} tools from RoboPages server") |
| 21 | + |
| 22 | + prompt = """ |
| 23 | + I need you to find all open ports on the local machine (127.0.0.1). |
| 24 | + Please use the available tools to scan the ports and provide a summary of the results. |
| 25 | +
|
| 26 | + Be thorough but concise in your analysis. Present the information in a clear format. |
| 27 | +
|
| 28 | + After scanning, list all the open ports you found and what services might be running on them. |
| 29 | + """ |
| 30 | + |
| 31 | + logger.info("Starting chat with model") |
| 32 | + generator = rg.get_generator("gpt-4o") |
| 33 | + |
| 34 | + chat = await generator.chat(prompt).using(*tools, force=True).run() |
| 35 | + |
| 36 | + logger.info("Chat completed. Full conversation:") |
| 37 | + for i, message in enumerate(chat.messages): |
| 38 | + logger.info(f"Message {i + 1} ({message.role}):") |
| 39 | + logger.info( |
| 40 | + message.content[:200] + ("..." if len(message.content) > 200 else "") |
26 | 41 | ) |
27 | 42 |
|
28 | | - def _execute_function(self, func_name: str, *args, **kwargs): |
29 | | - print(f"executing {self.name}.{func_name}{kwargs} ...") |
30 | | - # execute the call via robopages and return the result to Rigging |
31 | | - return requests.post( |
32 | | - "http://localhost:8000/process", |
33 | | - json=[ |
34 | | - { |
35 | | - "type": "function", |
36 | | - "function": { |
37 | | - "name": func_name, |
38 | | - "arguments": kwargs, |
39 | | - }, |
40 | | - } |
41 | | - ], |
42 | | - ).json()[0]["content"] |
43 | | - |
44 | | - def get_description(self) -> rg.tool.ToolDescription: |
45 | | - """Creates a full description of the tool for use in prompting""" |
46 | | - |
47 | | - return rg.tool.ToolDescription( |
48 | | - name=self.name, |
49 | | - description=self.description, |
50 | | - functions=[ |
51 | | - rg.tool.ToolFunction( |
52 | | - name=function["name"], |
53 | | - description=function["description"], |
54 | | - parameters=[ |
55 | | - rg.tool.ToolParameter( |
56 | | - name=param["name"], |
57 | | - type=param["type"], |
58 | | - description=param["description"], |
59 | | - ) |
60 | | - for param in function["parameters"] |
61 | | - ], |
62 | | - ) |
63 | | - for function in self.tool["functions"] |
64 | | - ], |
65 | | - ) |
66 | | - |
67 | | - |
68 | | -async def run(model: str): |
69 | | - # get the tools from the Robopages server and wrap each function for Rigging |
70 | | - tools = [ |
71 | | - Wrapper(tool) |
72 | | - for tool in requests.get("http://localhost:8000/?flavor=rigging").json() |
73 | | - ] |
74 | | - |
75 | | - chat = ( |
76 | | - await rg.get_generator(model) |
77 | | - .chat("Find open ports on 127.0.0.1") |
78 | | - .using(*tools, force=True) |
79 | | - .run() |
80 | | - ) |
81 | | - |
82 | | - print(chat.last.content) |
83 | | - |
84 | | - |
85 | | -asyncio.run(run("gpt-4o")) |
| 43 | + print("\n--- RESULT ---\n") |
| 44 | + print(chat.last.content) |
| 45 | + |
| 46 | + return chat |
| 47 | + |
| 48 | + except Exception as e: |
| 49 | + logger.error(f"Error: {e}") |
| 50 | + import traceback |
| 51 | + |
| 52 | + traceback.print_exc() |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + chat = asyncio.run(run()) |
| 58 | + if chat: |
| 59 | + print(chat.conversation) |
0 commit comments