File size: 1,647 Bytes
9f79cce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# rentbot/tool_handler.py
import json

def execute_tool_call(tool_call):
    """
    Executes a tool call and returns a result message for the LLM.
    """
    function_name = tool_call.function.name
    try:
        arguments = json.loads(tool_call.function.arguments)
        
        if function_name == "create_event":
            # In a real app, you'd hit Google Calendar's API here.
            # For this example, we'll just simulate success.
            print(f"--- TOOL CALL: create_event ---")
            print(f"  Summary: {arguments.get('summary')}")
            print(f"  Start: {arguments.get('start_time')}")
            print(f"  Duration: {arguments.get('duration_minutes', 30)} minutes")
            print(f"-----------------------------")
            
            result_message = f"Successfully booked the viewing for '{arguments.get('summary')}'."
            return {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": json.dumps({"status": "success", "message": result_message}),
            }
        else:
            return {
                "tool_call_id": tool_call.id,
                "role": "tool",
                "name": function_name,
                "content": json.dumps({"status": "error", "message": f"Unknown function: {function_name}"}),
            }
    except json.JSONDecodeError:
        return {
            "tool_call_id": tool_call.id,
            "role": "tool",
            "name": function_name,
            "content": json.dumps({"status": "error", "message": "Invalid arguments format."}),
        }