File size: 14,200 Bytes
d477d5c
 
 
 
 
 
 
 
 
 
 
 
c725745
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c725745
 
 
d477d5c
5d4cc46
d477d5c
 
 
 
 
 
 
 
5d4cc46
d477d5c
 
 
 
c56aab6
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d4cc46
f685ddc
 
5d4cc46
 
c725745
25d18e3
d477d5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d4cc46
 
 
 
 
 
 
 
 
ccb4963
 
 
 
 
 
 
 
 
 
 
 
d477d5c
5d4cc46
 
d477d5c
c725745
d477d5c
c725745
 
d477d5c
5d4cc46
d477d5c
 
 
 
 
 
 
5d4cc46
d477d5c
 
 
 
 
c725745
 
 
 
 
 
25d18e3
 
c725745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d4cc46
 
d477d5c
5d4cc46
 
 
25d18e3
 
 
 
5d4cc46
 
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from __future__ import annotations

import json
import logging
import os
import random
from abc import abstractmethod
from pathlib import Path
from typing import TYPE_CHECKING, cast

import requests
from dotenv import load_dotenv
from griptape.artifacts import ListArtifact, TextArtifact, InfoArtifact, BaseArtifact
from griptape.configs import Defaults
from griptape.configs.drivers import (
    OpenAiDriversConfig,
)
from griptape.drivers import (
    GriptapeCloudVectorStoreDriver,
    LocalStructureRunDriver,
    OpenAiChatPromptDriver,
)
from griptape.engines.rag import RagEngine
from griptape.engines.rag.modules import (
    TextChunksResponseRagModule,
    VectorStoreRetrievalRagModule,
)
from griptape.engines.rag.stages import ResponseRagStage, RetrievalRagStage
from griptape.events import (
    BaseEvent,
    EventBus,
    EventListener,
    FinishStructureRunEvent,
)
from griptape.memory.structure import ConversationMemory
from griptape.rules import Rule, Ruleset
from griptape.structures import Agent, Workflow
from griptape.tasks import CodeExecutionTask, StructureRunTask, ToolTask
from griptape.tools import RagTool
from statemachine import State, StateMachine
from statemachine.factory import StateMachineMetaclass

from parsers import UWConfigParser
from uw_programmatic.single_question_machine import SingleQuestion

logger = logging.getLogger(__name__)
logging.getLogger("griptape").setLevel(logging.ERROR)

if TYPE_CHECKING:
    from griptape.structures import Structure
    from griptape.tools import BaseTool
    from statemachine.event import Event

load_dotenv()

# Sets max tokens and OpenAI as the driver.
Defaults.drivers_config = OpenAiDriversConfig(
    prompt_driver=OpenAiChatPromptDriver(model="gpt-4o", max_tokens=4096)
)


def custom_dict_merge(dict1: dict, dict2: dict) -> dict:
    result = dict1.copy()
    for key, value in dict2.items():
        if key in result and isinstance(result[key], list) and isinstance(value, list):
            result[key] = result[key] + value
        else:
            result[key] = value
    return result


class UWBaseMachine(StateMachine):
    """Base class for a machine.


    Attributes:
        config_file (Path): The path to the configuration file.
        config (dict): The configuration data.
        outputs_to_user (list[str]): Outputs to return to the user.
    """

    def __init__(self, config_file: Path, **kwargs) -> None:
        self.config_parser = UWConfigParser(config_file)
        self.config = self.config_parser.parse()
        self._structures = {}
        self.vector_stores = {}  # Store here in case needs multiple uses
        self.question_list: list = []
        # For the parameters necessary from the user
        self.page_range: tuple = ()
        self.question_number: int = 0
        self.taxonomy: list = []
        # To track give up
        self.give_up_count = 0
        self.current_question_count = 0
        # To keep vector stores on track
        self.kb_ids = {}
        self.rejected_questions: list = []
        self.errored: bool = False

        self.state_status: dict[str, bool] = {}

        for key in self.state_transitions:
            self.state_status[key] = False

        def on_event(event: BaseEvent) -> None:
            """Takes in griptape events from eventbus and fixes them."""
            print(f"Received Griptape event: {json.dumps(event.to_dict(), indent=2)}")
            try:
                self.send(
                    "process_event",
                    event_={"type": "griptape_event", "value": event.to_dict()},
                )
            except Exception as e:
                errormsg = f"Would not allow process_event to be sent. Check to see if it is defined in the config.yaml. Error:{e}"
                raise ValueError(errormsg) from e

        EventBus.clear_event_listeners()
        EventBus.add_event_listener(
            EventListener(on_event, event_types=[FinishStructureRunEvent]),
        )
        super().__init__()

    @property
    def available_events(self) -> list[str]:
        return self.current_state.transitions.unique_events

    @property
    @abstractmethod
    def tools(self) -> dict[str, BaseTool]:
        """Returns the Tools for the machine."""
        ...

    @property
    def _current_state_config(self) -> dict:
        return self.config["states"][self.current_state_value]

    @classmethod
    def from_definition(  # noqa: C901, PLR0912
        cls, definition: dict, **extra_kwargs
    ) -> UWBaseMachine:
        try:
            states_instances = {}
            for state_id, state_kwargs in definition["states"].items():
                # These are the relevant states that need GOAP.
                states_instances[state_id] = State(**state_kwargs, value=state_id)
        except Exception as e:
            errormsg = f"""Error in state definition: {e}.
            """
            raise ValueError(errormsg) from e

        events = {}
        state_transitions = {}
        for event_name, transitions in definition["events"].items():
            for transition_data in transitions:
                try:
                    source_name = transition_data["from"]
                    source = states_instances[source_name]
                    target = states_instances[transition_data["to"]]
                    relevance = ""
                    if "relevance" in transition_data:
                        relevance = transition_data["relevance"]
                    if source_name not in state_transitions:
                        state_transitions[source_name] = {event_name: relevance}
                    else:
                        state_transitions[source_name][event_name] = relevance
                except Exception as e:
                    errormsg = f"Error:{e}. Please check your transitions to be sure each transition has a source and destination."
                    raise ValueError(errormsg) from e

                transition = source.to(
                    target,
                    event=event_name,
                    cond=transition_data.get("cond"),
                    unless=transition_data.get("unless"),
                    on=transition_data.get("on"),
                    internal=transition_data.get("internal"),
                )

                if event_name in events:
                    events[event_name] |= transition
                else:
                    events[event_name] = transition
        for state_id, state in states_instances.items():
            if state_id not in ("end", "start"):
                transition = state.to(
                    state,
                    event="process_event",
                    on=f"on_event_{state_id}",
                    internal=True,
                )
                if "process_event" in events:
                    events["process_event"] |= transition
                else:
                    events["process_event"] = transition

        attrs_mapper = {
            **extra_kwargs,
            **states_instances,
            **events,
            "state_transitions": state_transitions,
        }

        return cast(
            UWBaseMachine,
            StateMachineMetaclass(cls.__name__, (cls,), attrs_mapper)(**extra_kwargs),
        )

    @classmethod
    def from_config_file(
        cls,
        config_file: Path,
        **extra_kwargs,
    ) -> UWBaseMachine:
        """Creates a StateMachine class from a configuration file"""
        config_parser = UWConfigParser(config_file)
        config = config_parser.parse()
        extra_kwargs["config_file"] = config_file

        definition_states = {
            state_id: {
                "initial": state_value.get("initial", False),
                "final": state_value.get("final", False),
            }
            for state_id, state_value in config["states"].items()
        }
        definition_events = {
            event_name: list(event_value["transitions"])
            for event_name, event_value in config["events"].items()
        }
        definition = {"states": definition_states, "events": definition_events}

        return cls.from_definition(definition, **extra_kwargs)

    @abstractmethod
    def start_machine(self) -> None:
        """Starts the machine."""
        ...

    def reset_structures(self) -> None:
        """Resets the structures."""
        self._structures = {}

    def on_enter_state(self, source: State, state: State, event: Event) -> None:
        print(f"Transitioning from {source} to {state} with event {event}")

    def get_structure(self, structure_id: str) -> Structure:
        global_structure_config = self.config["structures"][structure_id]
        state_structure_config = self._current_state_config.get("structures", {}).get(
            structure_id, {}
        )
        structure_config = custom_dict_merge(
            global_structure_config, state_structure_config
        )
        if structure_id not in self._structures:
            # Initialize Structure with all the expensive setup
            structure = Agent(
                id=structure_id,
                conversation_memory=ConversationMemory(),
            )
            self._structures[structure_id] = structure

        # Create a new clone with state-specific stuff
        structure = self._structures[structure_id]
        structure = Agent(
            id=structure.id,
            prompt_driver=structure.prompt_driver,
            conversation_memory=structure.conversation_memory,
            rulesets=[
                *self._get_structure_rulesets(structure_config.get("ruleset_ids", [])),
            ],
        )
        print(f"Structure: {structure_id}")
        for ruleset in structure.rulesets:
            for rule in ruleset.rules:
                print(f"Rule: {rule.value}")
        return structure

    def _get_structure_rulesets(self, ruleset_ids: list[str]) -> list[Ruleset]:
        ruleset_configs = [
            self.config["rulesets"][ruleset_id] for ruleset_id in ruleset_ids
        ]

        # Convert ruleset configs to Rulesets
        return [
            Ruleset(
                name=ruleset_config["name"],
                rules=[Rule(rule) for rule in ruleset_config["rules"]],
            )
            for ruleset_config in ruleset_configs
        ]

    def retrieve_vector_stores(self) -> None:
        base_url = "https://cloud.griptape.ai/api/"
        kb_url = f"{base_url}/knowledge-bases"
        headers = {"Authorization": f"Bearer {os.getenv('GT_CLOUD_API_KEY')}"}
        response = requests.get(url=kb_url, headers=headers)
        response.raise_for_status()
        all_kbs = {}
        if response.status_code == 200:
            data = response.json()
            next_page = data["pagination"]["next_page"]
            while next_page is not None:
                for kb in data["knowledge_bases"]:
                    name = kb["name"]
                    kb_id = kb["knowledge_base_id"]
                    if "KB_section" in name:
                        all_kbs[name] = kb_id
                page_url = kb_url + f"?page={next_page}"
                response = requests.get(url=page_url, headers=headers)
                response.raise_for_status()
                data = response.json()
                next_page = data["pagination"]["next_page"]
        else:
            raise ValueError(response.status_code)
        self.kb_ids = all_kbs

        # ALL METHODS RELATING TO THE WORKFLOW AND PIPELINE ARE BELOW THIS LINE

        # This is the overarching workflow. Creates a workflow with get_single_question x amount of times.
        # def get_questions_workflow(self) -> Workflow:
        workflow = Workflow(id="create_question_workflow")
        # How many questions still need to be created?
        for _ in range(self.question_number - len(self.question_list)):
            task = StructureRunTask(
                structure_run_driver=LocalStructureRunDriver(
                    create_structure=self.get_single_question
                ),
                child_ids=["end_task"],
            )
            # Create X amount of workflows to run for X amount of questions needed.
            workflow.add_task(task)
        end_task = CodeExecutionTask(id="end_task", on_run=self.end_workflow)
        workflow.add_task(end_task)
        return workflow

    def workflow_cet(self, task: CodeExecutionTask) -> BaseArtifact:
        question_machine = SingleQuestion.create_statemachine(
            self.taxonomy, self.kb_ids, self.page_range
        )
        question_machine.send("start_up")
        if question_machine.rejected:
            if question_machine.reject_reason == "BAD KB PAGE RANGE":
                return InfoArtifact("Bad KB Range")
            self.rejected_questions.append(question_machine.generated_question)
            return InfoArtifact("Question is Rejected")
        return TextArtifact(question_machine.generated_question)

    def get_questions_workflow(self) -> Workflow:
        workflow = Workflow(id="create_question_workflow")
        # How many questions still need to be created?
        for _ in range(self.question_number - len(self.question_list)):
            task = CodeExecutionTask(
                on_run=self.workflow_cet,
                child_ids=["end_task"],
            )
            # Create X amount of workflows to run for X amount of questions needed.
            workflow.add_task(task)
        end_task = CodeExecutionTask(id="end_task", on_run=self.end_workflow)
        workflow.add_task(end_task)
        return workflow

    # Ends the get_questions_workflow. Compiles all workflow outputs into one output.
    def end_workflow(self, task: CodeExecutionTask) -> ListArtifact:
        parent_outputs = task.parent_outputs
        questions = []
        for outputs in parent_outputs.values():
            if outputs.type == "InfoArtifact":
                if outputs.value == "Bad KB Range":
                    self.errored = True
                    self.send("error_to_start")
                    return ListArtifact([])
                continue
            questions.append(outputs)
        return ListArtifact(questions)