File size: 5,779 Bytes
f20b100
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import autogen
from autogen.agentchat.groupchat import GroupChat
from autogen.agentchat.agent import Agent
from autogen.agentchat.user_proxy_agent import UserProxyAgent
from autogen.agentchat.assistant_agent import AssistantAgent
from copy import deepcopy
from jsonschema import validate
import json

from schemas import layout_refiner_schema
from agents import is_termination_msg

class JSONSchemaAgent(UserProxyAgent):
    def __init__(self, name : str, is_termination_msg):
        super().__init__(name, is_termination_msg=is_termination_msg)

    def get_human_input(self, prompt: str) -> str:
        message = self.last_message()
        preps_layout = ["left-side", "right-side", "in the middle"]
        preps_objs = ['on', 'left of', 'right of', 'in front', 'behind', 'under', 'above']

        json_obj_new = json.loads(message["content"])
        if "items" in json_obj_new["children_objects"]:
            json_obj_new = {"children_objects" : json_obj_new["children_objects"]["items"]}
        is_success  = False
        try:
            validate(instance=json_obj_new, schema=layout_refiner_schema)
            is_success = True
        except Exception as e:
            feedback = str(e.message)
            if e.validator == "enum":
                if str(preps_objs) in e.message:
                    feedback += f"Change the preposition {e.instance} to something suitable with the intended positioning from the list {preps_objs}"
                elif str(preps_layout) in e.message:
                    feedback += f"Change the preposition {e.instance} to something suitable with the intended positioning from the list {preps_layout}"
        if is_success:
            return "SUCCESS"
        return feedback

config_list_gpt4 = autogen.config_list_from_json(
    "OAI_CONFIG_LIST.json",
)

gpt4_config = {
    "cache_seed": 42,
    "temperature": 0.0,
    "config_list": config_list_gpt4,
    "timeout": 500,
}
gpt4_json_config = deepcopy(gpt4_config)
gpt4_json_config["config_list"][0]["response_format"] = { "type": "json_object" }

def get_refiner_agents():
    user_proxy = autogen.UserProxyAgent(
        name="Admin",
        system_message = "A human admin.",
        is_termination_msg = is_termination_msg,
        human_input_mode = "NEVER",
        code_execution_config=False
    )

    json_schema_debugger = JSONSchemaAgent(
        name = "Json_schema_debugger",
        is_termination_msg = is_termination_msg,
    )

    layout_refiner = autogen.AssistantAgent(
        name = "Layout_refiner",
        llm_config = gpt4_json_config,
        is_termination_msg = is_termination_msg,
        human_input_mode = "NEVER",
        system_message = """ Layout Refiner. Every time when the Admin speaks; you will look at the parent object and children objects, the first  
        preposition that connects these objects and find a second suitable relative placement for the children objects whilst considering the initial positioning of the object. 
        Give the relative placement of the children objects with each other and with the parent object! For example, if there are five children objects that are 'on' the parent
        object, give the relative positions of the children objects to one another and the second preposition to the the parent object ('on' is the first preposition).

        Use only the following JSON Schema to save the JSON object:
        {
            "children_objects" : {
                "type" : "array",
                "items" : {
                    "type" : "object",
                    "properties" : {
                        "name_id" : {
                            "type" : "string"
                        },
                        "placement" : {
                            "type" : "object",
                            "properties" : {
                                "children_objects" : {
                                    "type" : "array",
                                    "items" : {
                                        "type" : "object",
                                        "properties" : {
                                            "name_id" : {
                                                "type" : "string",
                                                "description" : "The name_id of the other child object"
                                            },
                                            "preposition" : {
                                                "type" : "string",
                                                "description" : "The preposition that connects this object and the connected object, ex. left of the desk, behind the plant, the rug is under the desk...",
                                                "enum" : ["on", "left of", "right of", "in front", "behind", "under", "above"]
                                            },
                                            "is_adjacent" : {
                                                "type" : "boolean",
                                                "description" : "Whether this object and the connected object are adjacent to each other, ex. an object on the desk is adjacent to the desk."
                                            }
                                        },
                                        "required" : ["name_id", "preposition", "is_adjacent"]
                                    }
                                }
                            },
                            "required" : ["children_objects"]
                        }
                    },
                    "required" : ["name_id", "placement"]
                }
            },
        }
        """
    )

    return user_proxy, json_schema_debugger, layout_refiner