File size: 11,955 Bytes
563c5bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'''
Copyright 2024 Infosys Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies 
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
'''

import os
import json
# import logging
from typing import List
from ..language_models import AbstractLanguageModel
from ..operations import GraphOfOperations, Thought
from ..prompter import Prompter
from ..parser import Parser

from llm_explain.config.logger import CustomLogger

logging = CustomLogger()

class Controller:
    """
    Controller class to manage the execution flow of the Graph of Operations,
    generating the Graph Reasoning State.
    This involves language models, graph operations, prompting, and parsing.
    """

    def __init__(
        self,
        lm: AbstractLanguageModel,
        graph: GraphOfOperations,
        prompter: Prompter,
        parser: Parser,
        problem_parameters: dict,
    ) -> None:
        """
        Initialize the Controller instance with the language model,
        operations graph, prompter, parser, and problem parameters.

        :param lm: An instance of the AbstractLanguageModel.
        :type lm: AbstractLanguageModel
        :param graph: The Graph of Operations to be executed.
        :type graph: OperationsGraph
        :param prompter: An instance of the Prompter class, used to generate prompts.
        :type prompter: Prompter
        :param parser: An instance of the Parser class, used to parse responses.
        :type parser: Parser
        :param problem_parameters: Initial parameters/state of the problem.
        :type problem_parameters: dict
        """
        self.logger = CustomLogger()
        self.lm = lm
        self.graph = graph
        self.prompter = prompter
        self.parser = parser
        self.problem_parameters = problem_parameters
        self.run_executed = False

    def run(self) -> None:
        """
        Run the controller and execute the operations from the Graph of
        Operations based on their readiness.
        Ensures the program is in a valid state before execution.
        :raises AssertionError: If the Graph of Operation has no roots.
        :raises AssertionError: If the successor of an operation is not in the Graph of Operations.
        """
        # self.logger.debug("Checking that the program is in a valid state")
        assert self.graph.roots is not None, "The operations graph has no root"
        # self.logger.debug("The program is in a valid state")

        execution_queue = [
            operation
            for operation in self.graph.operations
            if operation.can_be_executed()
        ]
        # self.logger.info(execution_queue)
        while len(execution_queue) > 0:
            current_operation = execution_queue.pop(0)
            # self.logger.info("Executing operation %s", current_operation.operation_type)
            current_operation.execute(
                self.lm, self.prompter, self.parser, **self.problem_parameters
            )
            # self.logger.debug("Operation %s executed", current_operation.operation_type)
            for operation in current_operation.successors:
                assert (
                    operation in self.graph.operations
                ), "The successor of an operation is not in the operations graph"
                if operation.can_be_executed():
                    execution_queue.append(operation)
        # self.logger.info("All operations executed")
        self.run_executed = True

    def get_final_thoughts(self) -> List[List[Thought]]:
        """
        Retrieve the final thoughts after all operations have been executed.

        :return: List of thoughts for each operation in the graph's leaves.
        :rtype: List[List[Thought]]
        :raises AssertionError: If the `run` method hasn't been executed yet.
        """
        assert self.run_executed, "The run method has not been executed"
        return [operation.get_thoughts() for operation in self.graph.leaves]

    def output_graph(self, path: str) -> None:
        """
        Serialize the state and results of the operations graph to a JSON file.

        :param path: The path to the output file.
        :type path: str
        """
        output = []
        for operation in self.graph.operations:
            operation_serialized = {
                "operation": operation.operation_type.name,
                "thoughts": [thought.state for thought in operation.get_thoughts()],
            }
            if any([thought.scored for thought in operation.get_thoughts()]):
                operation_serialized["scored"] = [
                    thought.scored for thought in operation.get_thoughts()
                ]
                operation_serialized["scores"] = [
                    thought.score for thought in operation.get_thoughts()
                ]
            if any([thought.validated for thought in operation.get_thoughts()]):
                operation_serialized["validated"] = [
                    thought.validated for thought in operation.get_thoughts()
                ]
                operation_serialized["validity"] = [
                    thought.valid for thought in operation.get_thoughts()
                ]
            if any(
                [
                    thought.compared_to_ground_truth
                    for thought in operation.get_thoughts()
                ]
            ):
                operation_serialized["compared_to_ground_truth"] = [
                    thought.compared_to_ground_truth
                    for thought in operation.get_thoughts()
                ]
                operation_serialized["problem_solved"] = [
                    thought.solved for thought in operation.get_thoughts()
                ]
            output.append(operation_serialized)

        output.append(
            {
                "prompt_tokens": self.lm.prompt_tokens,
                "completion_tokens": self.lm.completion_tokens,
                "cost": self.lm.cost,
            }
        )

        with open(path, "w") as file:
            file.write(json.dumps(output, indent=2))
            
    def format_graph(self, source: str):
        
        def count_unique_matches(l1, l2):
            l1_set = set(l1)  # Convert l1 to a set for unique elements
            l2_set = set(l2)  # Convert l2 to a set for unique elements
            matches = l1_set & l2_set  # Find the intersection
            return len(matches)
        
        import copy
        
        with open(source, "r") as file:
            data = json.load(file)
        data_new = copy.deepcopy(data)
        
        global_thoughts = []
        global_thoughts_num = []
        data_thoughts = {}

        # generate
        l = []
        for i in range(len(data[0]['thoughts'])):
            l.append(data[0]['thoughts'][i]['current'])
            if data[0]['thoughts'][i]['current'] not in global_thoughts:
                global_thoughts.append(data[0]['thoughts'][i]['current'])
                global_thoughts_num.append(f"thought_{i+1}")
            data_new[0]['thoughts'][i]['current'] = f"thought_{i+1}"
            data_new[0]['thoughts'][i]['score'] = data_new[1]['scores'][i]
            data_thoughts[f"thought_{i+1}"] = data[0]['thoughts'][i]['current']

        # score
        for i in range(len(data[1]['thoughts'])):
            data_new[1]['thoughts'][i]['current'] = f"thought_{i+1}"

        # keep_best_n
        prev_thoughts = {}
        for i in range(len(data[2]['thoughts'])):
            if data[2]['thoughts'][i]['current'] in l:
                data_new[2]['thoughts'][i]['current'] = f"thought_{l.index(data[2]['thoughts'][i]['current'])+1}"
                data_new[2]['thoughts'][i]['score'] = data_new[2]['scores'][i]
                # data_thoughts[f"thought_{l.index(data[2]['thoughts'][i]['current'])+1}"] = data[2]['thoughts'][i]['current']
            elif data[2]['thoughts'][i]['current'] in global_thoughts:
                data_new[2]['thoughts'][i]['current'] = f"thought_{global_thoughts_num[global_thoughts.index(data[2]['thoughts'][i]['current'])]}"
                data_new[2]['thoughts'][i]['score'] = data_new[2]['scores'][i]
                # data_thoughts[f"thought_{global_thoughts_num[global_thoughts.index(data[2]['thoughts'][i]['current'])]}"] = data[2]['thoughts'][i]['current']
            prev_thoughts[str(i)] = data[2]['thoughts'][i]['current']

        # aggregate
        len1 = len(data[0]['thoughts'])
        l, l3 = [], []
        for i in range(len(data[3]['thoughts'])):
            l.append(data[3]['thoughts'][i]['current'])
            temp = []
            for j in range(len(data[2]['thoughts'])):
                temp.append(count_unique_matches(data[2]['thoughts'][j]['current'].split(), data[3]['thoughts'][i]['current'].split()))
            val = data_new[2]['thoughts'][temp.index(max(temp))]['current']
            if data[3]['thoughts'][i]['current'] not in global_thoughts:
                global_thoughts.append(data[3]['thoughts'][i]['current'])
                global_thoughts_num.append(f"aggregate_{val}")
            data_new[3]['thoughts'][i]['current'] = f"aggregate_{val}"
            data_new[3]['thoughts'][i]['score'] = data_new[4]['scores'][i]
            # data_thoughts[f"{val}_thought_{i+1+len1}"] = data[3]['thoughts'][i]['current']
            l3.append(f"aggregate_{val}")

        # score
        data_new[4]['thoughts'] = data_new[3]['thoughts']

        # keep_best_n
        for i in range(len(data[5]['thoughts'])):
            if data[5]['thoughts'][i]['current'] in l:
                data_new[5]['thoughts'][i]['current'] = l3[l.index(data[5]['thoughts'][0]['current'])]
                data_new[5]['thoughts'][i]['score'] = data_new[5]['scores'][i]
                data_thoughts[l3[l.index(data[5]['thoughts'][0]['current'])]] = data[5]['thoughts'][i]['current']
                # data_thoughts['final_thought'] = data[5]['thoughts'][i]['current']
            elif data[5]['thoughts'][i]['current'] in global_thoughts:
                data_new[5]['thoughts'][i]['current'] = global_thoughts_num[global_thoughts.index(data[5]['thoughts'][i]['current'])]
                data_new[5]['thoughts'][i]['score'] = data_new[5]['scores'][i]
                data_thoughts[global_thoughts_num[global_thoughts.index(data[5]['thoughts'][i]['current'])]] = data[5]['thoughts'][i]['current']
                # data_thoughts['final_thought'] = data[5]['thoughts'][i]['current']
        
        # data_new[5]['thoughts'][i]['current'] = 'final_thought'
        
        for i in range(len(data_new)):
            if i >= len(data_new):
                    break
            if 'operation' in data_new[i] and data_new[i]['operation'] == 'score':
                del data_new[i]
            if 'operation' in data_new[i] and data_new[i]['operation'] == 'keep_best_n':
                del data_new[i]['scored']
                del data_new[i]['scores']
                
        os.remove(source)
        
        return data_new, data_thoughts