forked from spcl/graph-of-thoughts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thought.py
117 lines (97 loc) · 3.29 KB
/
thought.py
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
# Copyright (c) 2023 ETH Zurich.
# All rights reserved.
#
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# main author: Nils Blach
from __future__ import annotations
import logging
from typing import Iterator, Dict, Optional
import itertools
class Thought:
"""
Represents an LLM thought with its state, constructed by the parser, and various flags.
"""
_ids: Iterator[int] = itertools.count(0)
def __init__(self, state: Optional[Dict] = None) -> None:
"""
Initializes a new Thought instance with a state and various default flags.
:param state: The state of the thought. Defaults to None.
:type state: Optional[Dict]
"""
self.logger: logging.Logger = logging.getLogger(self.__class__.__name__)
self.id: int = next(Thought._ids)
self.state: Dict = state
self._score: float = 0.0
self._valid: bool = False
self._solved: bool = False
self.scored: bool = False
self.validated: bool = False
self.compared_to_ground_truth: bool = False
@staticmethod
def from_thought(thought: Thought) -> Thought:
"""
Creates a new thought from an existing one.
:param thought: An instance of a Thought to clone.
:return: A new Thought instance with properties copied from the input thought.
"""
new_thought = Thought(thought.state)
new_thought.score = thought.score
new_thought.valid = thought.valid
new_thought.solved = thought.solved
new_thought.scored = thought.scored
new_thought.validated = thought.validated
new_thought.compared_to_ground_truth = thought.compared_to_ground_truth
return new_thought
@property
def valid(self) -> bool:
"""
Returns the validity of the thought.
:return: The validity of the thought.
:rtype: bool
"""
return self._valid
@valid.setter
def valid(self, valid: bool) -> None:
"""
Sets the validity of the thought and the validated flag.
:param valid: The validity of the thought.
:type valid: bool
"""
self.validated = True
self._valid = valid
@property
def score(self) -> float:
"""
Returns the score of the thought.
:return: The score of the thought.
:rtype: float
"""
return self._score
@score.setter
def score(self, new_score: float) -> None:
"""
Sets the score of the thought and the scored flag.
:param new_score: The score of the thought.
:type new_score: float
"""
self.scored = True
self._score = new_score
@property
def solved(self) -> bool:
"""
Returns the solved flag of the thought.
:return: The solved flag of the thought.
:rtype: bool
"""
return self._solved
@solved.setter
def solved(self, solved: bool) -> None:
"""
Sets the solved flag of the thought and the compared_to_ground_truth flag.
:param solved: Whether the thought contains a solution to the problem.
:type solved: bool
"""
self.compared_to_ground_truth = True
self._solved = solved