flock-core 0.2.5__py3-none-any.whl → 0.2.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of flock-core might be problematic. Click here for more details.
- flock/config.py +3 -3
- flock/core/flock_agent.py +1 -1
- flock/core/logging/formatters/pprint_formatter.py +4 -3
- flock/core/mixin/dspy_integration.py +23 -10
- flock/core/tools/basic_tools.py +55 -154
- flock/core/tools/dev_tools/github.py +0 -33
- flock/core/util/input_resolver.py +19 -0
- flock/interpreter/python_interpreter.py +675 -0
- {flock_core-0.2.5.dist-info → flock_core-0.2.6.dist-info}/METADATA +4 -2
- {flock_core-0.2.5.dist-info → flock_core-0.2.6.dist-info}/RECORD +13 -12
- {flock_core-0.2.5.dist-info → flock_core-0.2.6.dist-info}/WHEEL +0 -0
- {flock_core-0.2.5.dist-info → flock_core-0.2.6.dist-info}/entry_points.txt +0 -0
- {flock_core-0.2.5.dist-info → flock_core-0.2.6.dist-info}/licenses/LICENSE +0 -0
flock/config.py
CHANGED
|
@@ -28,9 +28,9 @@ JAEGER_TRANSPORT = config(
|
|
|
28
28
|
).lower() # Options: "grpc" or "http"
|
|
29
29
|
OTEL_SQL_DATABASE_NAME = config("OTEL_SQL_DATABASE", "flock_events.db")
|
|
30
30
|
OTEL_FILE_NAME = config("OTEL_FILE_NAME", "flock_events.jsonl")
|
|
31
|
-
OTEL_ENABLE_SQL = config("OTEL_ENABLE_SQL", True)
|
|
32
|
-
OTEL_ENABLE_FILE = config("OTEL_ENABLE_FILE", True)
|
|
33
|
-
OTEL_ENABLE_JAEGER = config("OTEL_ENABLE_JAEGER", True)
|
|
31
|
+
OTEL_ENABLE_SQL: bool = config("OTEL_ENABLE_SQL", True) == "True"
|
|
32
|
+
OTEL_ENABLE_FILE: bool = config("OTEL_ENABLE_FILE", True) == "True"
|
|
33
|
+
OTEL_ENABLE_JAEGER: bool = config("OTEL_ENABLE_JAEGER", True) == "True"
|
|
34
34
|
|
|
35
35
|
|
|
36
36
|
TELEMETRY = TelemetryConfig(
|
flock/core/flock_agent.py
CHANGED
|
@@ -151,7 +151,7 @@ class FlockAgent(BaseModel, ABC, PromptParserMixin, DSPyIntegrationMixin):
|
|
|
151
151
|
),
|
|
152
152
|
)
|
|
153
153
|
|
|
154
|
-
tools: list[Callable[..., Any]] | None = Field(
|
|
154
|
+
tools: list[Callable[..., Any] | Any] | None = Field(
|
|
155
155
|
default=None,
|
|
156
156
|
description="An optional list of callable tools that the agent can leverage during execution.",
|
|
157
157
|
)
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import json
|
|
2
1
|
from typing import Any
|
|
3
2
|
|
|
4
3
|
from flock.core.logging.formatters.base_formatter import BaseFormatter
|
|
@@ -10,12 +9,14 @@ class PrettyPrintFormatter(BaseFormatter):
|
|
|
10
9
|
) -> None:
|
|
11
10
|
"""Print an agent's result using Rich formatting."""
|
|
12
11
|
from rich.console import Console
|
|
13
|
-
from rich.json import JSON
|
|
14
12
|
|
|
15
13
|
console = Console()
|
|
16
14
|
|
|
17
15
|
console.print(agent_name)
|
|
18
|
-
console.print(
|
|
16
|
+
console.print(str(result), markup=True)
|
|
17
|
+
# try:
|
|
18
|
+
# console.print(JSON(json.dumps(result)))
|
|
19
|
+
# except Exception:
|
|
19
20
|
|
|
20
21
|
def display_data(self, data: dict[str, Any], **kwargs) -> None:
|
|
21
22
|
"""Print an agent's result using Rich formatting."""
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"""Mixin class for integrating with the dspy library."""
|
|
2
2
|
|
|
3
|
+
import inspect
|
|
3
4
|
import sys
|
|
4
5
|
from typing import Any, Literal
|
|
5
6
|
|
|
6
7
|
from flock.core.logging.logging import get_logger
|
|
7
|
-
from flock.core.util.input_resolver import split_top_level
|
|
8
|
+
from flock.core.util.input_resolver import get_callable_members, split_top_level
|
|
8
9
|
|
|
9
10
|
logger = get_logger("flock")
|
|
10
11
|
|
|
@@ -68,15 +69,19 @@ class DSPyIntegrationMixin:
|
|
|
68
69
|
# TODO: We have to find a way to avoid using eval here.
|
|
69
70
|
# This is a security risk, as it allows arbitrary code execution.
|
|
70
71
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
72
|
+
field_type = dspy.PythonInterpreter(
|
|
73
|
+
sys.modules[__name__].__dict__
|
|
74
|
+
| sys.modules["__main__"].__dict__
|
|
75
|
+
).execute(type_str)
|
|
76
|
+
|
|
77
|
+
# try:
|
|
78
|
+
# field_type = eval(type_str, sys.modules[__name__].__dict__)
|
|
79
|
+
# except Exception:
|
|
80
|
+
# field_type = eval(
|
|
81
|
+
# type_str, sys.modules["__main__"].__dict__
|
|
82
|
+
# )
|
|
77
83
|
|
|
78
84
|
except Exception:
|
|
79
|
-
# If evaluation fails, default to str.
|
|
80
85
|
field_type = str
|
|
81
86
|
|
|
82
87
|
return name, field_type, desc
|
|
@@ -143,6 +148,14 @@ class DSPyIntegrationMixin:
|
|
|
143
148
|
"""
|
|
144
149
|
import dspy
|
|
145
150
|
|
|
151
|
+
processed_tools = []
|
|
152
|
+
if self.tools:
|
|
153
|
+
for tool in self.tools:
|
|
154
|
+
if inspect.ismodule(tool) or inspect.isclass(tool):
|
|
155
|
+
processed_tools.extend(get_callable_members(tool))
|
|
156
|
+
else:
|
|
157
|
+
processed_tools.append(tool)
|
|
158
|
+
|
|
146
159
|
dspy_solver = None
|
|
147
160
|
|
|
148
161
|
if agent_type_override:
|
|
@@ -153,7 +166,7 @@ class DSPyIntegrationMixin:
|
|
|
153
166
|
if agent_type_override == "ReAct":
|
|
154
167
|
dspy.ReAct(
|
|
155
168
|
signature,
|
|
156
|
-
tools=
|
|
169
|
+
tools=processed_tools,
|
|
157
170
|
max_iters=10,
|
|
158
171
|
)
|
|
159
172
|
if agent_type_override == "Completion":
|
|
@@ -164,7 +177,7 @@ class DSPyIntegrationMixin:
|
|
|
164
177
|
if self.tools:
|
|
165
178
|
dspy_solver = dspy.ReAct(
|
|
166
179
|
signature,
|
|
167
|
-
tools=
|
|
180
|
+
tools=processed_tools,
|
|
168
181
|
max_iters=10,
|
|
169
182
|
)
|
|
170
183
|
else:
|
flock/core/tools/basic_tools.py
CHANGED
|
@@ -2,29 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
import importlib
|
|
4
4
|
import os
|
|
5
|
+
from typing import Literal
|
|
5
6
|
|
|
6
7
|
from flock.core.logging.trace_and_logged import traced_and_logged
|
|
8
|
+
from flock.interpreter.python_interpreter import PythonInterpreter
|
|
7
9
|
|
|
8
10
|
|
|
9
11
|
@traced_and_logged
|
|
10
12
|
def web_search_tavily(query: str):
|
|
11
|
-
"""Performs a web search using the Tavily client.
|
|
12
|
-
|
|
13
|
-
This function checks if the optional 'tavily' dependency is installed. If so,
|
|
14
|
-
it creates a TavilyClient using the API key from the environment variable
|
|
15
|
-
'TAVILY_API_KEY' and performs a search with the provided query. The search response
|
|
16
|
-
is returned if successful.
|
|
17
|
-
|
|
18
|
-
Parameters:
|
|
19
|
-
query (str): The search query string.
|
|
20
|
-
|
|
21
|
-
Returns:
|
|
22
|
-
Any: The search response obtained from the Tavily client.
|
|
23
|
-
|
|
24
|
-
Raises:
|
|
25
|
-
ImportError: If the 'tavily' dependency is not installed.
|
|
26
|
-
Exception: Re-raises any exceptions encountered during the search.
|
|
27
|
-
"""
|
|
28
13
|
if importlib.util.find_spec("tavily") is not None:
|
|
29
14
|
from tavily import TavilyClient
|
|
30
15
|
|
|
@@ -41,22 +26,24 @@ def web_search_tavily(query: str):
|
|
|
41
26
|
|
|
42
27
|
|
|
43
28
|
@traced_and_logged
|
|
44
|
-
def
|
|
45
|
-
"""
|
|
29
|
+
def web_search_duckduckgo(
|
|
30
|
+
keywords: str, search_type: Literal["news", "web"] = "web"
|
|
31
|
+
):
|
|
32
|
+
try:
|
|
33
|
+
from duckduckgo_search import DDGS
|
|
46
34
|
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
if search_type == "news":
|
|
36
|
+
response = DDGS().news(keywords)
|
|
37
|
+
else:
|
|
38
|
+
response = DDGS().text(keywords)
|
|
49
39
|
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
return response
|
|
41
|
+
except Exception:
|
|
42
|
+
raise
|
|
52
43
|
|
|
53
|
-
Returns:
|
|
54
|
-
str: The web page content converted into Markdown format.
|
|
55
44
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
Exception: Re-raises any exceptions encountered during the HTTP request or conversion.
|
|
59
|
-
"""
|
|
45
|
+
@traced_and_logged
|
|
46
|
+
def get_web_content_as_markdown(url: str):
|
|
60
47
|
if (
|
|
61
48
|
importlib.util.find_spec("httpx") is not None
|
|
62
49
|
and importlib.util.find_spec("markdownify") is not None
|
|
@@ -79,22 +66,6 @@ def get_web_content_as_markdown(url: str):
|
|
|
79
66
|
|
|
80
67
|
@traced_and_logged
|
|
81
68
|
def get_anything_as_markdown(url_or_file_path: str):
|
|
82
|
-
"""Converts the content of a URL or file into Markdown format.
|
|
83
|
-
|
|
84
|
-
This function leverages the 'docling' library to convert various document types
|
|
85
|
-
(retrieved from a URL or a local file) into Markdown. It uses the DocumentConverter
|
|
86
|
-
from the 'docling.document_converter' module.
|
|
87
|
-
|
|
88
|
-
Parameters:
|
|
89
|
-
url_or_file_path (str): The URL or local file path of the document to convert.
|
|
90
|
-
|
|
91
|
-
Returns:
|
|
92
|
-
str: The converted document in Markdown format.
|
|
93
|
-
|
|
94
|
-
Raises:
|
|
95
|
-
ImportError: If the 'docling' dependency is not installed.
|
|
96
|
-
Exception: Re-raises any exceptions encountered during the document conversion.
|
|
97
|
-
"""
|
|
98
69
|
if importlib.util.find_spec("docling") is not None:
|
|
99
70
|
from docling.document_converter import DocumentConverter
|
|
100
71
|
|
|
@@ -113,49 +84,53 @@ def get_anything_as_markdown(url_or_file_path: str):
|
|
|
113
84
|
|
|
114
85
|
@traced_and_logged
|
|
115
86
|
def evaluate_math(expression: str) -> float:
|
|
116
|
-
"""Evaluates a mathematical expression using the dspy interpreter.
|
|
117
|
-
|
|
118
|
-
This function uses the 'dspy' library's PythonInterpreter to evaluate the provided
|
|
119
|
-
mathematical expression.
|
|
120
|
-
|
|
121
|
-
Parameters:
|
|
122
|
-
expression (str): A string containing the mathematical expression to evaluate.
|
|
123
|
-
|
|
124
|
-
Returns:
|
|
125
|
-
float: The result of the evaluated expression.
|
|
126
|
-
|
|
127
|
-
Raises:
|
|
128
|
-
Exception: Re-raises any exceptions encountered during the evaluation.
|
|
129
|
-
"""
|
|
130
|
-
import dspy
|
|
131
|
-
|
|
132
87
|
try:
|
|
133
|
-
result =
|
|
88
|
+
result = PythonInterpreter(
|
|
89
|
+
{},
|
|
90
|
+
[
|
|
91
|
+
"os",
|
|
92
|
+
"math",
|
|
93
|
+
"random",
|
|
94
|
+
"datetime",
|
|
95
|
+
"time",
|
|
96
|
+
"string",
|
|
97
|
+
"collections",
|
|
98
|
+
"itertools",
|
|
99
|
+
"functools",
|
|
100
|
+
"typing",
|
|
101
|
+
"enum",
|
|
102
|
+
"json",
|
|
103
|
+
"ast",
|
|
104
|
+
],
|
|
105
|
+
verbose=True,
|
|
106
|
+
).execute(expression)
|
|
134
107
|
return result
|
|
135
108
|
except Exception:
|
|
136
109
|
raise
|
|
137
110
|
|
|
138
111
|
|
|
139
112
|
@traced_and_logged
|
|
140
|
-
def code_eval(python_code: str) ->
|
|
141
|
-
"""Executes Python code using the dspy interpreter.
|
|
142
|
-
|
|
143
|
-
This function takes a string of Python code, executes it using the 'dspy' PythonInterpreter,
|
|
144
|
-
and returns the result.
|
|
145
|
-
|
|
146
|
-
Parameters:
|
|
147
|
-
python_code (str): A string containing Python code to execute.
|
|
148
|
-
|
|
149
|
-
Returns:
|
|
150
|
-
float: The result of the executed code.
|
|
151
|
-
|
|
152
|
-
Raises:
|
|
153
|
-
Exception: Re-raises any exceptions encountered during code execution.
|
|
154
|
-
"""
|
|
155
|
-
import dspy
|
|
156
|
-
|
|
113
|
+
def code_eval(python_code: str) -> str:
|
|
157
114
|
try:
|
|
158
|
-
result =
|
|
115
|
+
result = PythonInterpreter(
|
|
116
|
+
{},
|
|
117
|
+
[
|
|
118
|
+
"os",
|
|
119
|
+
"math",
|
|
120
|
+
"random",
|
|
121
|
+
"datetime",
|
|
122
|
+
"time",
|
|
123
|
+
"string",
|
|
124
|
+
"collections",
|
|
125
|
+
"itertools",
|
|
126
|
+
"functools",
|
|
127
|
+
"typing",
|
|
128
|
+
"enum",
|
|
129
|
+
"json",
|
|
130
|
+
"ast",
|
|
131
|
+
],
|
|
132
|
+
verbose=True,
|
|
133
|
+
).execute(python_code)
|
|
159
134
|
return result
|
|
160
135
|
except Exception:
|
|
161
136
|
raise
|
|
@@ -163,11 +138,6 @@ def code_eval(python_code: str) -> float:
|
|
|
163
138
|
|
|
164
139
|
@traced_and_logged
|
|
165
140
|
def get_current_time() -> str:
|
|
166
|
-
"""Retrieves the current time in ISO 8601 format.
|
|
167
|
-
|
|
168
|
-
Returns:
|
|
169
|
-
str: The current date and time as an ISO 8601 formatted string.
|
|
170
|
-
"""
|
|
171
141
|
import datetime
|
|
172
142
|
|
|
173
143
|
time = datetime.datetime.now().isoformat()
|
|
@@ -176,33 +146,12 @@ def get_current_time() -> str:
|
|
|
176
146
|
|
|
177
147
|
@traced_and_logged
|
|
178
148
|
def count_words(text: str) -> int:
|
|
179
|
-
"""Counts the number of words in the provided text.
|
|
180
|
-
|
|
181
|
-
This function splits the input text by whitespace and returns the count of resulting words.
|
|
182
|
-
|
|
183
|
-
Parameters:
|
|
184
|
-
text (str): The text in which to count words.
|
|
185
|
-
|
|
186
|
-
Returns:
|
|
187
|
-
int: The number of words in the text.
|
|
188
|
-
"""
|
|
189
149
|
count = len(text.split())
|
|
190
150
|
return count
|
|
191
151
|
|
|
192
152
|
|
|
193
153
|
@traced_and_logged
|
|
194
154
|
def extract_urls(text: str) -> list[str]:
|
|
195
|
-
"""Extracts all URLs from the given text.
|
|
196
|
-
|
|
197
|
-
This function uses a regular expression to find all substrings that match the typical
|
|
198
|
-
URL pattern (starting with http or https).
|
|
199
|
-
|
|
200
|
-
Parameters:
|
|
201
|
-
text (str): The text from which to extract URLs.
|
|
202
|
-
|
|
203
|
-
Returns:
|
|
204
|
-
list[str]: A list of URLs found in the text.
|
|
205
|
-
"""
|
|
206
155
|
import re
|
|
207
156
|
|
|
208
157
|
url_pattern = r"https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+"
|
|
@@ -212,17 +161,6 @@ def extract_urls(text: str) -> list[str]:
|
|
|
212
161
|
|
|
213
162
|
@traced_and_logged
|
|
214
163
|
def extract_numbers(text: str) -> list[float]:
|
|
215
|
-
"""Extracts all numerical values from the provided text.
|
|
216
|
-
|
|
217
|
-
This function uses a regular expression to find substrings that represent integers or decimals,
|
|
218
|
-
converts them to floats, and returns them as a list.
|
|
219
|
-
|
|
220
|
-
Parameters:
|
|
221
|
-
text (str): The text from which to extract numerical values.
|
|
222
|
-
|
|
223
|
-
Returns:
|
|
224
|
-
list[float]: A list of numbers (as floats) found in the text.
|
|
225
|
-
"""
|
|
226
164
|
import re
|
|
227
165
|
|
|
228
166
|
numbers = [float(x) for x in re.findall(r"-?\d*\.?\d+", text)]
|
|
@@ -231,17 +169,6 @@ def extract_numbers(text: str) -> list[float]:
|
|
|
231
169
|
|
|
232
170
|
@traced_and_logged
|
|
233
171
|
def json_parse_safe(text: str) -> dict:
|
|
234
|
-
"""Safely parses a JSON string into a dictionary.
|
|
235
|
-
|
|
236
|
-
This function attempts to load a JSON object from the given text. If parsing fails,
|
|
237
|
-
it returns an empty dictionary instead of raising an exception.
|
|
238
|
-
|
|
239
|
-
Parameters:
|
|
240
|
-
text (str): The JSON-formatted string to parse.
|
|
241
|
-
|
|
242
|
-
Returns:
|
|
243
|
-
dict: The parsed JSON object as a dictionary, or an empty dictionary if parsing fails.
|
|
244
|
-
"""
|
|
245
172
|
import json
|
|
246
173
|
|
|
247
174
|
try:
|
|
@@ -253,18 +180,6 @@ def json_parse_safe(text: str) -> dict:
|
|
|
253
180
|
|
|
254
181
|
@traced_and_logged
|
|
255
182
|
def save_to_file(content: str, filename: str):
|
|
256
|
-
"""Saves the given content to a file.
|
|
257
|
-
|
|
258
|
-
This function writes the provided content to a file specified by the filename.
|
|
259
|
-
If the file cannot be written, an exception is raised.
|
|
260
|
-
|
|
261
|
-
Parameters:
|
|
262
|
-
content (str): The content to be saved.
|
|
263
|
-
filename (str): The path to the file where the content will be saved.
|
|
264
|
-
|
|
265
|
-
Raises:
|
|
266
|
-
Exception: Re-raises any exceptions encountered during the file write operation.
|
|
267
|
-
"""
|
|
268
183
|
try:
|
|
269
184
|
with open(filename, "w") as f:
|
|
270
185
|
f.write(content)
|
|
@@ -274,20 +189,6 @@ def save_to_file(content: str, filename: str):
|
|
|
274
189
|
|
|
275
190
|
@traced_and_logged
|
|
276
191
|
def read_from_file(filename: str) -> str:
|
|
277
|
-
"""Reads and returns the content of a file.
|
|
278
|
-
|
|
279
|
-
This function opens the specified file, reads its content, and returns it as a string.
|
|
280
|
-
If the file cannot be read, an exception is raised.
|
|
281
|
-
|
|
282
|
-
Parameters:
|
|
283
|
-
filename (str): The path to the file to be read.
|
|
284
|
-
|
|
285
|
-
Returns:
|
|
286
|
-
str: The content of the file.
|
|
287
|
-
|
|
288
|
-
Raises:
|
|
289
|
-
Exception: Re-raises any exceptions encountered during the file read operation.
|
|
290
|
-
"""
|
|
291
192
|
try:
|
|
292
193
|
with open(filename) as f:
|
|
293
194
|
content = f.read()
|
|
@@ -10,23 +10,6 @@ from flock.core.logging.trace_and_logged import traced_and_logged
|
|
|
10
10
|
|
|
11
11
|
@traced_and_logged
|
|
12
12
|
def create_user_stories_as_github_issue(title: str, body: str) -> str:
|
|
13
|
-
"""Create a new GitHub issue representing a user story.
|
|
14
|
-
|
|
15
|
-
This function creates an issue in a GitHub repository using the specified title and body.
|
|
16
|
-
The title is used as the issue title, and the body should contain the full user story
|
|
17
|
-
description along with a formatted list of acceptance criteria. The following
|
|
18
|
-
environment variables must be set for this function to work correctly:
|
|
19
|
-
|
|
20
|
-
- GITHUB_PAT: Personal Access Token for GitHub API authentication.
|
|
21
|
-
- GITHUB_REPO: Repository identifier in the format "owner/repo".
|
|
22
|
-
|
|
23
|
-
Parameters:
|
|
24
|
-
title (str): The title for the GitHub issue (user story).
|
|
25
|
-
body (str): The detailed description including acceptance criteria.
|
|
26
|
-
|
|
27
|
-
Returns:
|
|
28
|
-
str: A message indicating whether the issue was created successfully or not.
|
|
29
|
-
"""
|
|
30
13
|
github_pat = os.getenv("GITHUB_PAT")
|
|
31
14
|
github_repo = os.getenv("GITHUB_REPO")
|
|
32
15
|
|
|
@@ -49,22 +32,6 @@ def create_user_stories_as_github_issue(title: str, body: str) -> str:
|
|
|
49
32
|
|
|
50
33
|
@traced_and_logged
|
|
51
34
|
def upload_readme(content: str):
|
|
52
|
-
"""Upload or update the README.md file in a GitHub repository.
|
|
53
|
-
|
|
54
|
-
This function uses the GitHub API to either create a new README.md file or update an
|
|
55
|
-
existing one in the specified repository. It encodes the provided content to base64 before
|
|
56
|
-
sending it via the API. The function requires the following environment variables to be set:
|
|
57
|
-
|
|
58
|
-
- GITHUB_USERNAME: Your GitHub username.
|
|
59
|
-
- GITHUB_REPO: The name of the repository.
|
|
60
|
-
- GITHUB_PAT: Your GitHub Personal Access Token for authentication.
|
|
61
|
-
|
|
62
|
-
Parameters:
|
|
63
|
-
content (str): The text content to be written into the README.md file.
|
|
64
|
-
|
|
65
|
-
Raises:
|
|
66
|
-
ValueError: If any of the required environment variables are missing.
|
|
67
|
-
"""
|
|
68
35
|
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
|
|
69
36
|
REPO_NAME = os.getenv("GITHUB_REPO")
|
|
70
37
|
GITHUB_TOKEN = os.getenv("GITHUB_PAT")
|
|
@@ -3,6 +3,25 @@
|
|
|
3
3
|
from flock.core.context.context import FlockContext
|
|
4
4
|
|
|
5
5
|
|
|
6
|
+
def get_callable_members(obj):
|
|
7
|
+
"""Extract all callable (methods/functions) members from a module or class.
|
|
8
|
+
Returns a list of callable objects.
|
|
9
|
+
"""
|
|
10
|
+
import inspect
|
|
11
|
+
|
|
12
|
+
# Get all members of the object
|
|
13
|
+
members = inspect.getmembers(obj)
|
|
14
|
+
|
|
15
|
+
# Filter for callable members that don't start with underscore (to exclude private/special methods)
|
|
16
|
+
callables = [
|
|
17
|
+
member[1]
|
|
18
|
+
for member in members
|
|
19
|
+
if inspect.isroutine(member[1]) and not member[0].startswith("_")
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
return callables
|
|
23
|
+
|
|
24
|
+
|
|
6
25
|
def split_top_level(s: str) -> list[str]:
|
|
7
26
|
"""Split a string on commas that are not enclosed within brackets, parentheses, or quotes.
|
|
8
27
|
|
|
@@ -0,0 +1,675 @@
|
|
|
1
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
2
|
+
# Licensed under the Apache License, Version 2.0 (the “License”);
|
|
3
|
+
# you may not use this file except in compliance with the License.
|
|
4
|
+
# You may obtain a copy of the License at
|
|
5
|
+
#
|
|
6
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
#
|
|
8
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
# distributed under the License is distributed on an “AS IS” BASIS,
|
|
10
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
# See the License for the specific language governing permissions and
|
|
12
|
+
# limitations under the License.
|
|
13
|
+
# =========== Copyright 2023 @ CAMEL-AI.org. All Rights Reserved. ===========
|
|
14
|
+
import ast
|
|
15
|
+
import builtins
|
|
16
|
+
import difflib
|
|
17
|
+
import importlib
|
|
18
|
+
import re
|
|
19
|
+
import typing
|
|
20
|
+
from collections.abc import Mapping
|
|
21
|
+
from typing import (
|
|
22
|
+
Any,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class InterpreterError(ValueError):
|
|
27
|
+
r"""An error raised when the interpreter cannot evaluate a Python
|
|
28
|
+
expression, due to syntax error or unsupported operations.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class PythonInterpreter:
|
|
35
|
+
r"""A customized python interpreter to control the execution of
|
|
36
|
+
LLM-generated codes. The interpreter makes sure the code can only execute
|
|
37
|
+
functions given in action space and import white list. It also supports
|
|
38
|
+
fuzzy variable matching to receive uncertain input variable name.
|
|
39
|
+
|
|
40
|
+
[Documentation omitted for brevity]
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
action_space (Dict[str, Any]): A dictionary mapping action names to
|
|
44
|
+
their corresponding functions or objects.
|
|
45
|
+
import_white_list (Optional[List[str]], optional): A list of allowed modules.
|
|
46
|
+
verbose (bool, optional): If True, the interpreter prints log messages
|
|
47
|
+
as it executes the code. (default: False)
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
action_space: dict[str, Any],
|
|
53
|
+
import_white_list: list[str] | None = None,
|
|
54
|
+
verbose: bool = False,
|
|
55
|
+
) -> None:
|
|
56
|
+
self.action_space = action_space
|
|
57
|
+
self.state = self.action_space.copy()
|
|
58
|
+
self.fuzz_state: dict[str, Any] = {}
|
|
59
|
+
self.import_white_list = import_white_list or [
|
|
60
|
+
"math",
|
|
61
|
+
"random",
|
|
62
|
+
"datetime",
|
|
63
|
+
"time",
|
|
64
|
+
"string",
|
|
65
|
+
"collections",
|
|
66
|
+
"itertools",
|
|
67
|
+
"functools",
|
|
68
|
+
"typing",
|
|
69
|
+
"enum",
|
|
70
|
+
"json",
|
|
71
|
+
"ast",
|
|
72
|
+
] # default imports
|
|
73
|
+
self.verbose = verbose
|
|
74
|
+
|
|
75
|
+
def log(self, message: str) -> None:
|
|
76
|
+
"""Print a log message immediately."""
|
|
77
|
+
print(message, flush=True)
|
|
78
|
+
|
|
79
|
+
def execute(
|
|
80
|
+
self,
|
|
81
|
+
code: str,
|
|
82
|
+
state: dict[str, Any] | None = None,
|
|
83
|
+
fuzz_state: dict[str, Any] | None = None,
|
|
84
|
+
keep_state: bool = True,
|
|
85
|
+
) -> Any:
|
|
86
|
+
r"""Execute the input python codes in a secure environment.
|
|
87
|
+
|
|
88
|
+
[Documentation omitted for brevity]
|
|
89
|
+
"""
|
|
90
|
+
if state is not None:
|
|
91
|
+
self.state.update(state)
|
|
92
|
+
if fuzz_state is not None:
|
|
93
|
+
self.fuzz_state.update(fuzz_state)
|
|
94
|
+
|
|
95
|
+
try:
|
|
96
|
+
expression = ast.parse(code)
|
|
97
|
+
except SyntaxError as e:
|
|
98
|
+
error_line = code.splitlines()[e.lineno - 1]
|
|
99
|
+
raise InterpreterError(
|
|
100
|
+
f"Syntax error in code at line {e.lineno}: {error_line}\nError: {e}"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
result = None
|
|
104
|
+
if self.verbose:
|
|
105
|
+
self.log("[Interpreter] Starting code execution...")
|
|
106
|
+
|
|
107
|
+
for idx, node in enumerate(expression.body):
|
|
108
|
+
# Log the AST node being executed (using unparse if available)
|
|
109
|
+
if self.verbose:
|
|
110
|
+
try:
|
|
111
|
+
node_repr = ast.unparse(node)
|
|
112
|
+
except Exception:
|
|
113
|
+
node_repr = ast.dump(node)
|
|
114
|
+
self.log(f"[Interpreter] Executing node {idx}: {node_repr}")
|
|
115
|
+
|
|
116
|
+
try:
|
|
117
|
+
line_result = self._execute_ast(node)
|
|
118
|
+
except InterpreterError as e:
|
|
119
|
+
if not keep_state:
|
|
120
|
+
self.clear_state()
|
|
121
|
+
msg = f"Evaluation of the code stopped at node {idx}. See:\n{e}"
|
|
122
|
+
raise InterpreterError(msg)
|
|
123
|
+
if line_result is not None:
|
|
124
|
+
result = line_result
|
|
125
|
+
if self.verbose:
|
|
126
|
+
self.log(f"[Interpreter] Node {idx} result: {result}")
|
|
127
|
+
|
|
128
|
+
if self.verbose:
|
|
129
|
+
self.log("[Interpreter] Finished code execution.")
|
|
130
|
+
if not keep_state:
|
|
131
|
+
self.clear_state()
|
|
132
|
+
|
|
133
|
+
return result
|
|
134
|
+
|
|
135
|
+
def clear_state(self) -> None:
|
|
136
|
+
r"""Initialize :obj:`state` and :obj:`fuzz_state`"""
|
|
137
|
+
self.state = self.action_space.copy()
|
|
138
|
+
self.fuzz_state = {}
|
|
139
|
+
|
|
140
|
+
# ast.Index is deprecated after python 3.9, which cannot pass type check,
|
|
141
|
+
# but is still necessary for older versions.
|
|
142
|
+
@typing.no_type_check
|
|
143
|
+
def _execute_ast(self, expression: ast.AST) -> Any:
|
|
144
|
+
if isinstance(expression, ast.Assign):
|
|
145
|
+
return self._execute_assign(expression)
|
|
146
|
+
elif isinstance(expression, ast.Attribute):
|
|
147
|
+
value = self._execute_ast(expression.value)
|
|
148
|
+
return getattr(value, expression.attr)
|
|
149
|
+
elif isinstance(expression, ast.AugAssign):
|
|
150
|
+
return self._execute_augassign(expression)
|
|
151
|
+
elif isinstance(expression, ast.BinOp):
|
|
152
|
+
return self._execute_binop(expression)
|
|
153
|
+
elif isinstance(expression, ast.BoolOp):
|
|
154
|
+
return self._execute_condition(expression)
|
|
155
|
+
elif isinstance(expression, ast.Call):
|
|
156
|
+
return self._execute_call(expression)
|
|
157
|
+
elif isinstance(expression, ast.Compare):
|
|
158
|
+
return self._execute_condition(expression)
|
|
159
|
+
elif isinstance(expression, ast.Constant):
|
|
160
|
+
return expression.value
|
|
161
|
+
elif isinstance(expression, ast.Dict):
|
|
162
|
+
result: dict = {}
|
|
163
|
+
for k, v in zip(expression.keys, expression.values):
|
|
164
|
+
if k is not None:
|
|
165
|
+
result[self._execute_ast(k)] = self._execute_ast(v)
|
|
166
|
+
else:
|
|
167
|
+
result.update(self._execute_ast(v))
|
|
168
|
+
return result
|
|
169
|
+
elif isinstance(expression, ast.Expr):
|
|
170
|
+
return self._execute_ast(expression.value)
|
|
171
|
+
elif isinstance(expression, ast.For):
|
|
172
|
+
return self._execute_for(expression)
|
|
173
|
+
elif isinstance(expression, ast.FormattedValue):
|
|
174
|
+
return self._execute_ast(expression.value)
|
|
175
|
+
elif isinstance(expression, ast.FunctionDef):
|
|
176
|
+
self.state[expression.name] = expression
|
|
177
|
+
return None
|
|
178
|
+
elif isinstance(expression, ast.GeneratorExp):
|
|
179
|
+
return self._execute_generatorexp(expression)
|
|
180
|
+
elif isinstance(expression, ast.If):
|
|
181
|
+
return self._execute_if(expression)
|
|
182
|
+
elif isinstance(expression, ast.IfExp):
|
|
183
|
+
return self._execute_ifexp(expression)
|
|
184
|
+
elif isinstance(expression, ast.Import):
|
|
185
|
+
self._execute_import(expression)
|
|
186
|
+
return None
|
|
187
|
+
elif isinstance(expression, ast.ImportFrom):
|
|
188
|
+
self._execute_import_from(expression)
|
|
189
|
+
return None
|
|
190
|
+
elif hasattr(ast, "Index") and isinstance(expression, ast.Index):
|
|
191
|
+
return self._execute_ast(expression.value)
|
|
192
|
+
elif isinstance(expression, ast.JoinedStr):
|
|
193
|
+
return "".join(
|
|
194
|
+
[str(self._execute_ast(v)) for v in expression.values]
|
|
195
|
+
)
|
|
196
|
+
elif isinstance(expression, ast.Lambda):
|
|
197
|
+
return self._execute_lambda(expression)
|
|
198
|
+
elif isinstance(expression, ast.List):
|
|
199
|
+
return [self._execute_ast(elt) for elt in expression.elts]
|
|
200
|
+
elif isinstance(expression, ast.Name):
|
|
201
|
+
return self._execute_name(expression)
|
|
202
|
+
elif isinstance(expression, ast.Return):
|
|
203
|
+
return self._execute_ast(expression.value)
|
|
204
|
+
elif isinstance(expression, ast.Subscript):
|
|
205
|
+
return self._execute_subscript(expression)
|
|
206
|
+
elif isinstance(expression, ast.Tuple):
|
|
207
|
+
return tuple([self._execute_ast(elt) for elt in expression.elts])
|
|
208
|
+
elif isinstance(expression, ast.UnaryOp):
|
|
209
|
+
return self._execute_unaryop(expression)
|
|
210
|
+
elif isinstance(expression, ast.While):
|
|
211
|
+
return self._execute_while(expression)
|
|
212
|
+
elif isinstance(expression, ast.ListComp):
|
|
213
|
+
return self._execute_listcomp(expression)
|
|
214
|
+
elif isinstance(expression, ast.DictComp):
|
|
215
|
+
return self._execute_dictcomp(expression)
|
|
216
|
+
elif isinstance(expression, ast.SetComp):
|
|
217
|
+
return self._execute_setcomp(expression)
|
|
218
|
+
elif isinstance(expression, ast.Break):
|
|
219
|
+
raise BreakException()
|
|
220
|
+
elif isinstance(expression, ast.Continue):
|
|
221
|
+
raise ContinueException()
|
|
222
|
+
elif isinstance(expression, ast.Try):
|
|
223
|
+
return self._execute_try(expression)
|
|
224
|
+
elif isinstance(expression, ast.Raise):
|
|
225
|
+
return self._execute_raise(expression)
|
|
226
|
+
elif isinstance(expression, ast.Pass):
|
|
227
|
+
return None
|
|
228
|
+
elif isinstance(expression, ast.Assert):
|
|
229
|
+
return self._execute_assert(expression)
|
|
230
|
+
else:
|
|
231
|
+
raise InterpreterError(
|
|
232
|
+
f"{expression.__class__.__name__} is not supported."
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
def _execute_assign(self, assign: ast.Assign) -> Any:
|
|
236
|
+
targets = assign.targets
|
|
237
|
+
result = self._execute_ast(assign.value)
|
|
238
|
+
|
|
239
|
+
for target in targets:
|
|
240
|
+
self._assign(target, result)
|
|
241
|
+
return result
|
|
242
|
+
|
|
243
|
+
def _assign(self, target: ast.expr, value: Any):
|
|
244
|
+
if isinstance(target, ast.Name):
|
|
245
|
+
self.state[target.id] = value
|
|
246
|
+
elif isinstance(target, ast.Tuple):
|
|
247
|
+
if not isinstance(value, tuple):
|
|
248
|
+
raise InterpreterError(
|
|
249
|
+
f"Expected type tuple, but got {value.__class__.__name__} instead."
|
|
250
|
+
)
|
|
251
|
+
if len(target.elts) != len(value):
|
|
252
|
+
raise InterpreterError(
|
|
253
|
+
f"Expected {len(target.elts)} values but got {len(value)}."
|
|
254
|
+
)
|
|
255
|
+
for t, v in zip(target.elts, value):
|
|
256
|
+
self.state[self._execute_ast(t)] = v
|
|
257
|
+
else:
|
|
258
|
+
raise InterpreterError(
|
|
259
|
+
f"Unsupported variable type. Expected ast.Name or ast.Tuple, got {target.__class__.__name__} instead."
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
def _execute_call(self, call: ast.Call) -> Any:
|
|
263
|
+
callable_func = self._execute_ast(call.func)
|
|
264
|
+
|
|
265
|
+
args = [self._execute_ast(arg) for arg in call.args]
|
|
266
|
+
kwargs = {
|
|
267
|
+
keyword.arg: self._execute_ast(keyword.value)
|
|
268
|
+
for keyword in call.keywords
|
|
269
|
+
}
|
|
270
|
+
if isinstance(callable_func, ast.FunctionDef):
|
|
271
|
+
old_state = self.state.copy()
|
|
272
|
+
for param_name, arg_value in zip(
|
|
273
|
+
[param.arg for param in callable_func.args.args], args
|
|
274
|
+
):
|
|
275
|
+
self.state[param_name] = arg_value
|
|
276
|
+
result = None
|
|
277
|
+
for stmt in callable_func.body:
|
|
278
|
+
result = self._execute_ast(stmt)
|
|
279
|
+
if isinstance(stmt, ast.Return):
|
|
280
|
+
break
|
|
281
|
+
self.state = old_state
|
|
282
|
+
return result
|
|
283
|
+
return callable_func(*args, **kwargs)
|
|
284
|
+
|
|
285
|
+
def _execute_augassign(self, augassign: ast.AugAssign):
|
|
286
|
+
current_value = self.state[augassign.target.id]
|
|
287
|
+
increment_value = self._execute_ast(augassign.value)
|
|
288
|
+
if not (
|
|
289
|
+
isinstance(current_value, (int, float))
|
|
290
|
+
and isinstance(increment_value, (int, float))
|
|
291
|
+
):
|
|
292
|
+
raise InterpreterError(
|
|
293
|
+
f"Invalid types for augmented assignment: {type(current_value)}, {type(increment_value)}"
|
|
294
|
+
)
|
|
295
|
+
if isinstance(augassign.op, ast.Add):
|
|
296
|
+
new_value = current_value + increment_value
|
|
297
|
+
elif isinstance(augassign.op, ast.Sub):
|
|
298
|
+
new_value = current_value - increment_value
|
|
299
|
+
elif isinstance(augassign.op, ast.Mult):
|
|
300
|
+
new_value = current_value * increment_value
|
|
301
|
+
elif isinstance(augassign.op, ast.Div):
|
|
302
|
+
new_value = current_value / increment_value
|
|
303
|
+
else:
|
|
304
|
+
raise InterpreterError(
|
|
305
|
+
f"Augmented assignment operator {augassign.op} is not supported"
|
|
306
|
+
)
|
|
307
|
+
self._assign(augassign.target, new_value)
|
|
308
|
+
return new_value
|
|
309
|
+
|
|
310
|
+
def _execute_subscript(self, subscript: ast.Subscript):
|
|
311
|
+
index = self._execute_ast(subscript.slice)
|
|
312
|
+
value = self._execute_ast(subscript.value)
|
|
313
|
+
if not isinstance(subscript.ctx, ast.Load):
|
|
314
|
+
raise InterpreterError(
|
|
315
|
+
f"{subscript.ctx.__class__.__name__} is not supported for subscript."
|
|
316
|
+
)
|
|
317
|
+
if isinstance(value, (list, tuple)):
|
|
318
|
+
return value[int(index)]
|
|
319
|
+
if index in value:
|
|
320
|
+
return value[index]
|
|
321
|
+
if isinstance(index, str) and isinstance(value, Mapping):
|
|
322
|
+
close_matches = difflib.get_close_matches(index, list(value.keys()))
|
|
323
|
+
if len(close_matches) > 0:
|
|
324
|
+
return value[close_matches[0]]
|
|
325
|
+
raise InterpreterError(f"Could not index {value} with '{index}'.")
|
|
326
|
+
|
|
327
|
+
def _execute_name(self, name: ast.Name):
|
|
328
|
+
if name.id in dir(builtins):
|
|
329
|
+
return getattr(builtins, name.id)
|
|
330
|
+
if isinstance(name.ctx, ast.Store):
|
|
331
|
+
return name.id
|
|
332
|
+
elif isinstance(name.ctx, ast.Load):
|
|
333
|
+
return self._get_value_from_state(name.id)
|
|
334
|
+
else:
|
|
335
|
+
raise InterpreterError(f"{name.ctx} is not supported.")
|
|
336
|
+
|
|
337
|
+
def _execute_condition(self, condition):
|
|
338
|
+
if isinstance(condition, ast.BoolOp):
|
|
339
|
+
if isinstance(condition.op, ast.And):
|
|
340
|
+
results = [
|
|
341
|
+
self._execute_ast(value) for value in condition.values
|
|
342
|
+
]
|
|
343
|
+
return all(results)
|
|
344
|
+
elif isinstance(condition.op, ast.Or):
|
|
345
|
+
results = [
|
|
346
|
+
self._execute_ast(value) for value in condition.values
|
|
347
|
+
]
|
|
348
|
+
return any(results)
|
|
349
|
+
else:
|
|
350
|
+
raise InterpreterError(
|
|
351
|
+
f"Boolean operator {condition.op} is not supported"
|
|
352
|
+
)
|
|
353
|
+
elif isinstance(condition, ast.Compare):
|
|
354
|
+
if len(condition.ops) > 1:
|
|
355
|
+
raise InterpreterError(
|
|
356
|
+
"Cannot evaluate conditions with multiple operators"
|
|
357
|
+
)
|
|
358
|
+
left = self._execute_ast(condition.left)
|
|
359
|
+
comparator = condition.ops[0]
|
|
360
|
+
right = self._execute_ast(condition.comparators[0])
|
|
361
|
+
if isinstance(comparator, ast.Eq):
|
|
362
|
+
return left == right
|
|
363
|
+
elif isinstance(comparator, ast.NotEq):
|
|
364
|
+
return left != right
|
|
365
|
+
elif isinstance(comparator, ast.Lt):
|
|
366
|
+
return left < right
|
|
367
|
+
elif isinstance(comparator, ast.LtE):
|
|
368
|
+
return left <= right
|
|
369
|
+
elif isinstance(comparator, ast.Gt):
|
|
370
|
+
return left > right
|
|
371
|
+
elif isinstance(comparator, ast.GtE):
|
|
372
|
+
return left >= right
|
|
373
|
+
elif isinstance(comparator, ast.Is):
|
|
374
|
+
return left is right
|
|
375
|
+
elif isinstance(comparator, ast.IsNot):
|
|
376
|
+
return left is not right
|
|
377
|
+
elif isinstance(comparator, ast.In):
|
|
378
|
+
return left in right
|
|
379
|
+
elif isinstance(comparator, ast.NotIn):
|
|
380
|
+
return left not in right
|
|
381
|
+
else:
|
|
382
|
+
raise InterpreterError("Unsupported comparison operator")
|
|
383
|
+
elif isinstance(condition, ast.UnaryOp):
|
|
384
|
+
return self._execute_unaryop(condition)
|
|
385
|
+
elif isinstance(condition, ast.Name) or isinstance(condition, ast.Call):
|
|
386
|
+
return bool(self._execute_ast(condition))
|
|
387
|
+
elif isinstance(condition, ast.Constant):
|
|
388
|
+
return bool(condition.value)
|
|
389
|
+
else:
|
|
390
|
+
raise InterpreterError(
|
|
391
|
+
f"Unsupported condition type: {type(condition).__name__}"
|
|
392
|
+
)
|
|
393
|
+
|
|
394
|
+
def _execute_if(self, if_statement: ast.If):
|
|
395
|
+
result = None
|
|
396
|
+
if self._execute_condition(if_statement.test):
|
|
397
|
+
for line in if_statement.body:
|
|
398
|
+
line_result = self._execute_ast(line)
|
|
399
|
+
if line_result is not None:
|
|
400
|
+
result = line_result
|
|
401
|
+
else:
|
|
402
|
+
for line in if_statement.orelse:
|
|
403
|
+
line_result = self._execute_ast(line)
|
|
404
|
+
if line_result is not None:
|
|
405
|
+
result = line_result
|
|
406
|
+
return result
|
|
407
|
+
|
|
408
|
+
def _execute_ifexp(self, ifexp: ast.IfExp) -> Any:
|
|
409
|
+
test_result = self._execute_condition(ifexp.test)
|
|
410
|
+
if test_result:
|
|
411
|
+
return self._execute_ast(ifexp.body)
|
|
412
|
+
else:
|
|
413
|
+
return self._execute_ast(ifexp.orelse)
|
|
414
|
+
|
|
415
|
+
def _execute_import(self, import_module: ast.Import) -> None:
|
|
416
|
+
for module in import_module.names:
|
|
417
|
+
self._validate_import(module.name)
|
|
418
|
+
alias = module.asname or module.name
|
|
419
|
+
self.state[alias] = importlib.import_module(module.name)
|
|
420
|
+
|
|
421
|
+
def _execute_import_from(self, import_from: ast.ImportFrom):
|
|
422
|
+
if import_from.module is None:
|
|
423
|
+
raise InterpreterError('"from . import" is not supported.')
|
|
424
|
+
for import_name in import_from.names:
|
|
425
|
+
full_name = import_from.module + f".{import_name.name}"
|
|
426
|
+
self._validate_import(full_name)
|
|
427
|
+
imported_module = importlib.import_module(import_from.module)
|
|
428
|
+
alias = import_name.asname or import_name.name
|
|
429
|
+
self.state[alias] = getattr(imported_module, import_name.name)
|
|
430
|
+
|
|
431
|
+
# Note: Two versions of _execute_for and _execute_while appear in this file.
|
|
432
|
+
# We keep both as provided, but you may wish to consolidate these in your code.
|
|
433
|
+
|
|
434
|
+
def _execute_for(self, for_statement: ast.For):
|
|
435
|
+
class BreakException(Exception):
|
|
436
|
+
pass
|
|
437
|
+
|
|
438
|
+
class ContinueException(Exception):
|
|
439
|
+
pass
|
|
440
|
+
|
|
441
|
+
result = None
|
|
442
|
+
try:
|
|
443
|
+
for value in self._execute_ast(for_statement.iter):
|
|
444
|
+
self._assign(for_statement.target, value)
|
|
445
|
+
try:
|
|
446
|
+
for line in for_statement.body:
|
|
447
|
+
line_result = self._execute_ast(line)
|
|
448
|
+
if line_result is not None:
|
|
449
|
+
result = line_result
|
|
450
|
+
except ContinueException:
|
|
451
|
+
continue
|
|
452
|
+
except BreakException:
|
|
453
|
+
pass
|
|
454
|
+
return result
|
|
455
|
+
|
|
456
|
+
def _execute_while(self, while_statement: ast.While):
|
|
457
|
+
class BreakException(Exception):
|
|
458
|
+
pass
|
|
459
|
+
|
|
460
|
+
class ContinueException(Exception):
|
|
461
|
+
pass
|
|
462
|
+
|
|
463
|
+
result = None
|
|
464
|
+
try:
|
|
465
|
+
while self._execute_condition(while_statement.test):
|
|
466
|
+
try:
|
|
467
|
+
for line in while_statement.body:
|
|
468
|
+
line_result = self._execute_ast(line)
|
|
469
|
+
if line_result is not None:
|
|
470
|
+
result = line_result
|
|
471
|
+
except ContinueException:
|
|
472
|
+
continue
|
|
473
|
+
except BreakException:
|
|
474
|
+
pass
|
|
475
|
+
return result
|
|
476
|
+
|
|
477
|
+
def _execute_try(self, try_statement: ast.Try):
|
|
478
|
+
try:
|
|
479
|
+
for line in try_statement.body:
|
|
480
|
+
self._execute_ast(line)
|
|
481
|
+
except Exception as e:
|
|
482
|
+
handled = False
|
|
483
|
+
for handler in try_statement.handlers:
|
|
484
|
+
if handler.type is None or isinstance(
|
|
485
|
+
e, self._execute_ast(handler.type)
|
|
486
|
+
):
|
|
487
|
+
if handler.name:
|
|
488
|
+
self.state[handler.name.id] = e
|
|
489
|
+
for line in handler.body:
|
|
490
|
+
self._execute_ast(line)
|
|
491
|
+
handled = True
|
|
492
|
+
break
|
|
493
|
+
if not handled:
|
|
494
|
+
raise
|
|
495
|
+
finally:
|
|
496
|
+
for line in try_statement.finalbody:
|
|
497
|
+
self._execute_ast(line)
|
|
498
|
+
|
|
499
|
+
def _execute_raise(self, raise_statement: ast.Raise):
|
|
500
|
+
if raise_statement.exc:
|
|
501
|
+
exception = self._execute_ast(raise_statement.exc)
|
|
502
|
+
raise exception
|
|
503
|
+
else:
|
|
504
|
+
raise
|
|
505
|
+
|
|
506
|
+
def _execute_assert(self, assert_statement: ast.Assert):
|
|
507
|
+
test_result = self._execute_condition(assert_statement.test)
|
|
508
|
+
if not test_result:
|
|
509
|
+
if assert_statement.msg:
|
|
510
|
+
msg = self._execute_ast(assert_statement.msg)
|
|
511
|
+
raise AssertionError(msg)
|
|
512
|
+
else:
|
|
513
|
+
raise AssertionError
|
|
514
|
+
|
|
515
|
+
def _execute_lambda(self, lambda_node: ast.Lambda) -> Any:
|
|
516
|
+
def lambda_function(*args):
|
|
517
|
+
old_state = self.state.copy()
|
|
518
|
+
for param, arg in zip(lambda_node.args.args, args):
|
|
519
|
+
self.state[param.arg] = arg
|
|
520
|
+
result = self._execute_ast(lambda_node.body)
|
|
521
|
+
self.state = old_state # Restore the state
|
|
522
|
+
return result
|
|
523
|
+
|
|
524
|
+
return lambda_function
|
|
525
|
+
|
|
526
|
+
def _validate_import(self, full_name: str):
|
|
527
|
+
tmp_name = ""
|
|
528
|
+
found_name = False
|
|
529
|
+
for name in full_name.split("."):
|
|
530
|
+
tmp_name += name if tmp_name == "" else f".{name}"
|
|
531
|
+
if tmp_name in self.import_white_list:
|
|
532
|
+
found_name = True
|
|
533
|
+
return
|
|
534
|
+
if not found_name:
|
|
535
|
+
raise InterpreterError(
|
|
536
|
+
f"It is not permitted to import modules "
|
|
537
|
+
f"than module white list (try to import {full_name})."
|
|
538
|
+
)
|
|
539
|
+
|
|
540
|
+
def _execute_binop(self, binop: ast.BinOp):
|
|
541
|
+
left = self._execute_ast(binop.left)
|
|
542
|
+
operator = binop.op
|
|
543
|
+
right = self._execute_ast(binop.right)
|
|
544
|
+
|
|
545
|
+
if isinstance(operator, ast.Add):
|
|
546
|
+
return left + right
|
|
547
|
+
elif isinstance(operator, ast.Sub):
|
|
548
|
+
return left - right
|
|
549
|
+
elif isinstance(operator, ast.Mult):
|
|
550
|
+
return left * right
|
|
551
|
+
elif isinstance(operator, ast.Div):
|
|
552
|
+
return left / right
|
|
553
|
+
elif isinstance(operator, ast.FloorDiv):
|
|
554
|
+
return left // right
|
|
555
|
+
elif isinstance(operator, ast.Mod):
|
|
556
|
+
return left % right
|
|
557
|
+
elif isinstance(operator, ast.Pow):
|
|
558
|
+
return left**right
|
|
559
|
+
elif isinstance(operator, ast.LShift):
|
|
560
|
+
return left << right
|
|
561
|
+
elif isinstance(operator, ast.RShift):
|
|
562
|
+
return left >> right
|
|
563
|
+
elif isinstance(operator, ast.BitAnd):
|
|
564
|
+
return left & right
|
|
565
|
+
elif isinstance(operator, ast.BitOr):
|
|
566
|
+
return left | right
|
|
567
|
+
elif isinstance(operator, ast.BitXor):
|
|
568
|
+
return left ^ right
|
|
569
|
+
elif isinstance(operator, ast.MatMult):
|
|
570
|
+
return left @ right
|
|
571
|
+
else:
|
|
572
|
+
raise InterpreterError(f"Operator not supported: {operator}")
|
|
573
|
+
|
|
574
|
+
def _execute_unaryop(self, unaryop: ast.UnaryOp):
|
|
575
|
+
operand = self._execute_ast(unaryop.operand)
|
|
576
|
+
operator = unaryop.op
|
|
577
|
+
|
|
578
|
+
if isinstance(operator, ast.UAdd):
|
|
579
|
+
return +operand
|
|
580
|
+
elif isinstance(operator, ast.USub):
|
|
581
|
+
return -operand
|
|
582
|
+
elif isinstance(operator, ast.Not):
|
|
583
|
+
return not operand
|
|
584
|
+
elif isinstance(operator, ast.Invert):
|
|
585
|
+
return ~operand
|
|
586
|
+
else:
|
|
587
|
+
raise InterpreterError(f"Operator not supported: {operator}")
|
|
588
|
+
|
|
589
|
+
def _execute_listcomp(self, comp: ast.ListComp):
|
|
590
|
+
return [self._execute_comp(comp.elt, comp.generators)]
|
|
591
|
+
|
|
592
|
+
def _execute_dictcomp(self, comp: ast.DictComp):
|
|
593
|
+
return {
|
|
594
|
+
self._execute_comp(comp.key, comp.generators): self._execute_comp(
|
|
595
|
+
comp.value, comp.generators
|
|
596
|
+
)
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
def _execute_setcomp(self, comp: ast.SetComp):
|
|
600
|
+
return {self._execute_comp(comp.elt, comp.generators)}
|
|
601
|
+
|
|
602
|
+
def _execute_comp(self, elt, generators):
|
|
603
|
+
if not generators:
|
|
604
|
+
return self._execute_ast(elt)
|
|
605
|
+
gen = generators[0]
|
|
606
|
+
result = []
|
|
607
|
+
for value in self._execute_ast(gen.iter):
|
|
608
|
+
self._assign(gen.target, value)
|
|
609
|
+
if all(self._execute_condition(if_cond) for if_cond in gen.ifs):
|
|
610
|
+
result.extend(self._execute_comp(elt, generators[1:]))
|
|
611
|
+
return result
|
|
612
|
+
|
|
613
|
+
def _execute_generatorexp(self, genexp: ast.GeneratorExp):
|
|
614
|
+
def generator():
|
|
615
|
+
for value in self._execute_comp(genexp.elt, genexp.generators):
|
|
616
|
+
yield value
|
|
617
|
+
|
|
618
|
+
return generator()
|
|
619
|
+
|
|
620
|
+
def _get_value_from_state(self, key: str) -> Any:
|
|
621
|
+
if key in self.state:
|
|
622
|
+
return self.state[key]
|
|
623
|
+
elif key in self.fuzz_state:
|
|
624
|
+
return self.fuzz_state[key]
|
|
625
|
+
else:
|
|
626
|
+
raise InterpreterError(f"The variable `{key}` is not defined.")
|
|
627
|
+
|
|
628
|
+
|
|
629
|
+
class TextPrompt(str):
|
|
630
|
+
r"""A class that represents a text prompt. The :obj:`TextPrompt` class
|
|
631
|
+
extends the built-in :obj:`str` class to provide a property for retrieving
|
|
632
|
+
the set of keywords in the prompt.
|
|
633
|
+
"""
|
|
634
|
+
|
|
635
|
+
@property
|
|
636
|
+
def key_words(self) -> set[str]:
|
|
637
|
+
pattern = re.compile(r"\{([^{}]+)\}")
|
|
638
|
+
found = pattern.findall(self)
|
|
639
|
+
return set(found)
|
|
640
|
+
|
|
641
|
+
def format(self, *args: Any, **kwargs: Any) -> "TextPrompt":
|
|
642
|
+
default_kwargs = {key: "{" + f"{key}" + "}" for key in self.key_words}
|
|
643
|
+
default_kwargs.update(kwargs)
|
|
644
|
+
return TextPrompt(super().format(*args, **default_kwargs))
|
|
645
|
+
|
|
646
|
+
|
|
647
|
+
class CodePrompt(TextPrompt):
|
|
648
|
+
r"""A class that represents a code prompt. It extends the :obj:`TextPrompt`
|
|
649
|
+
class with a :obj:`code_type` property.
|
|
650
|
+
"""
|
|
651
|
+
|
|
652
|
+
def __new__(cls, *args: Any, **kwargs: Any) -> "CodePrompt":
|
|
653
|
+
code_type = kwargs.pop("code_type", None)
|
|
654
|
+
instance = super().__new__(cls, *args, **kwargs)
|
|
655
|
+
instance._code_type = code_type
|
|
656
|
+
return instance
|
|
657
|
+
|
|
658
|
+
@property
|
|
659
|
+
def code_type(self) -> str | None:
|
|
660
|
+
return self._code_type
|
|
661
|
+
|
|
662
|
+
def set_code_type(self, code_type: str) -> None:
|
|
663
|
+
self._code_type = code_type
|
|
664
|
+
|
|
665
|
+
def execute(
|
|
666
|
+
self,
|
|
667
|
+
interpreter: PythonInterpreter | None = None,
|
|
668
|
+
user_variable: dict[str, Any] | None = None,
|
|
669
|
+
) -> tuple[Any, PythonInterpreter]:
|
|
670
|
+
if not interpreter:
|
|
671
|
+
interpreter = PythonInterpreter(action_space=globals())
|
|
672
|
+
execution_res = interpreter.execute(
|
|
673
|
+
self, fuzz_state=user_variable, keep_state=True
|
|
674
|
+
)
|
|
675
|
+
return execution_res, interpreter
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: flock-core
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.6
|
|
4
4
|
Summary: Declarative LLM Orchestration at Scale
|
|
5
5
|
Author-email: Andre Ratzenberger <andre.ratzenberger@whiteduck.de>
|
|
6
6
|
License-File: LICENSE
|
|
@@ -38,7 +38,9 @@ Description-Content-Type: text/markdown
|
|
|
38
38
|
|
|
39
39
|
<p align="center">
|
|
40
40
|
<img src="docs/img/flock.png" width="600"><br>
|
|
41
|
-
<img alt="Dynamic TOML Badge" src="https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fwhiteducksoftware%2Fflock%2Frefs%2Fheads%
|
|
41
|
+
<img alt="Dynamic TOML Badge" src="https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fwhiteducksoftware%2Fflock%2Frefs%2Fheads%2Fmaster%2Fpyproject.toml&query=%24.project.version&style=for-the-badge&logo=pypi&label=pip%20version">
|
|
42
|
+
<img alt="X (formerly Twitter) Follow" src="https://img.shields.io/twitter/follow/whiteduck_gmbh?style=for-the-badge&logo=X">
|
|
43
|
+
|
|
42
44
|
|
|
43
45
|
|
|
44
46
|
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
flock/__init__.py,sha256=P175tsTOByIhw_CIeMAybUEggKhtoSrc8a0gKotBJfo,177
|
|
2
|
-
flock/config.py,sha256=
|
|
2
|
+
flock/config.py,sha256=jmW1PQ2oiCUpERLhNFzvrcHlYS3KM_jJyMXrnoeA0gs,1547
|
|
3
3
|
flock/core/__init__.py,sha256=0Xq_txurlxxjKGXjRn6GNJusGTiBcd7zw2eF0L7JyuU,183
|
|
4
4
|
flock/core/flock.py,sha256=0NC-J_ZCojwWDepI6rbX-9jG_Hr2AKgY43ieijhD8DU,9820
|
|
5
|
-
flock/core/flock_agent.py,sha256=
|
|
5
|
+
flock/core/flock_agent.py,sha256=prA2jslYuAABzIG6uw55f0XCNR-V4Ptaaju93ZuCc6E,27767
|
|
6
6
|
flock/core/context/context.py,sha256=jH06w4C_O5CEL-YxjX_x_dmgLe9Rcllnn1Ebs0dvwaE,6171
|
|
7
7
|
flock/core/context/context_manager.py,sha256=qMySVny_dbTNLh21RHK_YT0mNKIOrqJDZpi9ZVdBsxU,1103
|
|
8
8
|
flock/core/context/context_vars.py,sha256=0Hn6fM2iNc0_jIIU0B7KX-K2o8qXqtZ5EYtwujETQ7U,272
|
|
@@ -14,7 +14,7 @@ flock/core/logging/telemetry.py,sha256=yEOfEZ3HBFeLCaHZA6QmsRdwZKtmUC6bQtEOTVeRR
|
|
|
14
14
|
flock/core/logging/trace_and_logged.py,sha256=5vNrK1kxuPMoPJ0-QjQg-EDJL1oiEzvU6UNi6X8FiMs,2117
|
|
15
15
|
flock/core/logging/formatters/base_formatter.py,sha256=CyG-X2NWq8sqEhFEO2aG7Mey5tVkIzoWiihW301_VIo,1023
|
|
16
16
|
flock/core/logging/formatters/formatter_factory.py,sha256=hmH-NpCESHkioX0GBQ5CuQR4axyIXnSRWwAZCHylx6Q,1283
|
|
17
|
-
flock/core/logging/formatters/pprint_formatter.py,sha256=
|
|
17
|
+
flock/core/logging/formatters/pprint_formatter.py,sha256=cONC9hS-QFXqj9iStVpLwsoNG8asVcc8tduTRRhGQ5o,742
|
|
18
18
|
flock/core/logging/formatters/rich_formatters.py,sha256=h1FD0_cIdQBQ8P2x05XhgD1cmmP80IBNVT5jb3cAV9M,4776
|
|
19
19
|
flock/core/logging/formatters/theme_builder.py,sha256=1RUEwPIDfCjwTapbK1liasA5SdukOn7YwbZ4H4j1WkI,17364
|
|
20
20
|
flock/core/logging/formatters/themed_formatter.py,sha256=CbxmqUC7zkLzyIxngk-3dcpQ6vxPR6zaDNA2TAMitCI,16714
|
|
@@ -22,14 +22,15 @@ flock/core/logging/span_middleware/baggage_span_processor.py,sha256=gJfRl8FeB6jd
|
|
|
22
22
|
flock/core/logging/telemetry_exporter/base_exporter.py,sha256=rQJJzS6q9n2aojoSqwCnl7ZtHrh5LZZ-gkxUuI5WfrQ,1124
|
|
23
23
|
flock/core/logging/telemetry_exporter/file_exporter.py,sha256=nKAjJSZtA7FqHSTuTiFtYYepaxOq7l1rDvs8U8rSBlA,3023
|
|
24
24
|
flock/core/logging/telemetry_exporter/sqlite_exporter.py,sha256=CDsiMb9QcqeXelZ6ZqPSS56ovMPGqOu6whzBZRK__Vg,3498
|
|
25
|
-
flock/core/mixin/dspy_integration.py,sha256=
|
|
25
|
+
flock/core/mixin/dspy_integration.py,sha256=eFCe6B8vMmgKXY0eFIN_x_5DYyt0AZsffYXPSHTsO0U,7379
|
|
26
26
|
flock/core/mixin/prompt_parser.py,sha256=eOqI-FK3y17gVqpc_y5GF-WmK1Jv8mFlkZxTcgweoxI,5121
|
|
27
27
|
flock/core/registry/agent_registry.py,sha256=QHdr3Cb-32PEdz8jFCIZSH9OlfpRwAJMtSRpHCWJDq4,4889
|
|
28
|
-
flock/core/tools/basic_tools.py,sha256=
|
|
29
|
-
flock/core/tools/dev_tools/github.py,sha256=
|
|
28
|
+
flock/core/tools/basic_tools.py,sha256=OwWaFu4NoVrc3Uijj56RY9XDDaP_mOnEa5B3wSWwwLE,4756
|
|
29
|
+
flock/core/tools/dev_tools/github.py,sha256=a2OTPXS7kWOVA4zrZHynQDcsmEi4Pac5MfSjQOLePzA,5308
|
|
30
30
|
flock/core/util/cli_helper.py,sha256=aHLKjl5JBLIczLzjYeUcGQlVQRlypunxV2TYeAFX0KE,1030
|
|
31
|
-
flock/core/util/input_resolver.py,sha256=
|
|
31
|
+
flock/core/util/input_resolver.py,sha256=g9vDPdY4OH-G7qjas5ksGEHueokHGFPMoLOvC-ngeLo,5984
|
|
32
32
|
flock/core/util/serializable.py,sha256=SymJ0YrjBx48mOBItYSqoRpKuzIc4vKWRS6ScTzre7s,2573
|
|
33
|
+
flock/interpreter/python_interpreter.py,sha256=pq2e7KJfAYtBCP2hhbtFNeg18QdMFF66esoYn3MHfA4,26177
|
|
33
34
|
flock/platform/docker_tools.py,sha256=fpA7-6rJBjPOUBLdQP4ny2QPgJ_042nmqRn5GtKnoYw,1445
|
|
34
35
|
flock/platform/jaeger_install.py,sha256=MyOMJQx4TQSMYvdUJxfiGSo3YCtsfkbNXcAcQ9bjETA,2898
|
|
35
36
|
flock/themes/3024-day.toml,sha256=uOVHqEzSyHx0WlUk3D0lne4RBsNBAPCTy3C58yU7kEY,667
|
|
@@ -373,8 +374,8 @@ flock/workflow/activities.py,sha256=YEg-Gr8kzVsxWsmsZguIVhX2XwMRvhZ2OlnsJoG5g_A,
|
|
|
373
374
|
flock/workflow/agent_activities.py,sha256=NhBZscflEf2IMfSRa_pBM_TRP7uVEF_O0ROvWZ33eDc,963
|
|
374
375
|
flock/workflow/temporal_setup.py,sha256=VWBgmBgfTBjwM5ruS_dVpA5AVxx6EZ7oFPGw4j3m0l0,1091
|
|
375
376
|
flock/workflow/workflow.py,sha256=I9MryXW_bqYVTHx-nl2epbTqeRy27CAWHHA7ZZA0nAk,1696
|
|
376
|
-
flock_core-0.2.
|
|
377
|
-
flock_core-0.2.
|
|
378
|
-
flock_core-0.2.
|
|
379
|
-
flock_core-0.2.
|
|
380
|
-
flock_core-0.2.
|
|
377
|
+
flock_core-0.2.6.dist-info/METADATA,sha256=5N_MIPv2rCJtiGwp3jYf-Bz8EWfj8h-B_mK-jPsCsT0,11569
|
|
378
|
+
flock_core-0.2.6.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
379
|
+
flock_core-0.2.6.dist-info/entry_points.txt,sha256=rWaS5KSpkTmWySURGFZk6PhbJ87TmvcFQDi2uzjlagQ,37
|
|
380
|
+
flock_core-0.2.6.dist-info/licenses/LICENSE,sha256=iYEqWy0wjULzM9GAERaybP4LBiPeu7Z1NEliLUdJKSc,1072
|
|
381
|
+
flock_core-0.2.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|