runbooks 0.7.5__py3-none-any.whl → 0.7.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.
- runbooks/__init__.py +1 -1
- runbooks/cfat/__init__.py +2 -2
- runbooks/finops/__init__.py +1 -1
- runbooks/finops/cli.py +1 -1
- runbooks/operate/__init__.py +2 -2
- runbooks/remediation/__init__.py +2 -2
- runbooks/remediation/acm_remediation.py +1 -1
- runbooks/remediation/base.py +1 -1
- runbooks/remediation/cloudtrail_remediation.py +1 -1
- runbooks/remediation/cognito_remediation.py +1 -1
- runbooks/remediation/dynamodb_remediation.py +1 -1
- runbooks/remediation/ec2_remediation.py +1 -1
- runbooks/remediation/ec2_unattached_ebs_volumes.py +1 -1
- runbooks/remediation/kms_enable_key_rotation.py +1 -1
- runbooks/remediation/kms_remediation.py +1 -1
- runbooks/remediation/lambda_remediation.py +1 -1
- runbooks/remediation/multi_account.py +1 -1
- runbooks/remediation/rds_remediation.py +1 -1
- runbooks/remediation/requirements.txt +2 -2
- runbooks/remediation/s3_block_public_access.py +1 -1
- runbooks/remediation/s3_enable_access_logging.py +1 -1
- runbooks/remediation/s3_encryption.py +1 -1
- runbooks/remediation/s3_remediation.py +1 -1
- runbooks/security/__init__.py +1 -1
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/METADATA +4 -2
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/RECORD +42 -62
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/top_level.txt +0 -1
- jupyter-agent/.env +0 -2
- jupyter-agent/.env.template +0 -2
- jupyter-agent/.gitattributes +0 -35
- jupyter-agent/.gradio/certificate.pem +0 -31
- jupyter-agent/README.md +0 -16
- jupyter-agent/__main__.log +0 -8
- jupyter-agent/app.py +0 -256
- jupyter-agent/cloudops-agent.png +0 -0
- jupyter-agent/ds-system-prompt.txt +0 -154
- jupyter-agent/jupyter-agent.png +0 -0
- jupyter-agent/llama3_template.jinja +0 -123
- jupyter-agent/requirements.txt +0 -9
- jupyter-agent/tmp/4ojbs8a02ir/jupyter-agent.ipynb +0 -68
- jupyter-agent/tmp/cm5iasgpm3p/jupyter-agent.ipynb +0 -91
- jupyter-agent/tmp/crqbsseag5/jupyter-agent.ipynb +0 -91
- jupyter-agent/tmp/hohanq1u097/jupyter-agent.ipynb +0 -57
- jupyter-agent/tmp/jns1sam29wm/jupyter-agent.ipynb +0 -53
- jupyter-agent/tmp/jupyter-agent.ipynb +0 -27
- jupyter-agent/utils.py +0 -409
- runbooks/inventory/aws_organization.png +0 -0
- /runbooks/inventory/{tests → Tests}/common_test_data.py +0 -0
- /runbooks/inventory/{tests → Tests}/common_test_functions.py +0 -0
- /runbooks/inventory/{tests → Tests}/script_test_data.py +0 -0
- /runbooks/inventory/{tests → Tests}/setup.py +0 -0
- /runbooks/inventory/{tests → Tests}/src.py +0 -0
- /runbooks/inventory/{tests/test_inventory_modules.py → Tests/test_Inventory_Modules.py} +0 -0
- /runbooks/inventory/{tests → Tests}/test_cfn_describe_stacks.py +0 -0
- /runbooks/inventory/{tests → Tests}/test_ec2_describe_instances.py +0 -0
- /runbooks/inventory/{tests → Tests}/test_lambda_list_functions.py +0 -0
- /runbooks/inventory/{tests → Tests}/test_moto_integration_example.py +0 -0
- /runbooks/inventory/{tests → Tests}/test_org_list_accounts.py +0 -0
- /runbooks/inventory/{Inventory_Modules.py → inventory_modules.py} +0 -0
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/WHEEL +0 -0
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/entry_points.txt +0 -0
- {runbooks-0.7.5.dist-info → runbooks-0.7.6.dist-info}/licenses/LICENSE +0 -0
jupyter-agent/utils.py
DELETED
@@ -1,409 +0,0 @@
|
|
1
|
-
"""
|
2
|
-
utils.py
|
3
|
-
|
4
|
-
This module provides helper functions to:
|
5
|
-
- Generate and update Jupyter Notebook structures.
|
6
|
-
- Execute code in a sandbox environment.
|
7
|
-
- Parse and convert execution results to notebook cell outputs.
|
8
|
-
- Export notebooks to HTML.
|
9
|
-
"""
|
10
|
-
|
11
|
-
import json
|
12
|
-
from pathlib import Path
|
13
|
-
from typing import Any, Dict, List, Tuple
|
14
|
-
|
15
|
-
import nbformat
|
16
|
-
from e2b_code_interpreter import Sandbox
|
17
|
-
from huggingface_hub import InferenceClient
|
18
|
-
from nbconvert import HTMLExporter
|
19
|
-
from nbformat.v4 import new_code_cell, new_markdown_cell, new_notebook
|
20
|
-
from traitlets.config import Config
|
21
|
-
from transformers import AutoTokenizer
|
22
|
-
|
23
|
-
## --- Global Configuration and Template Loading ---
|
24
|
-
|
25
|
-
## Create a configuration for nbconvert and set up the HTML exporter
|
26
|
-
config = Config()
|
27
|
-
html_exporter = HTMLExporter(config=config, template_name="classic")
|
28
|
-
|
29
|
-
## Load the Jinja template for the LLaMA model
|
30
|
-
TEMPLATE_PATH = Path("llama3_template.jinja")
|
31
|
-
try:
|
32
|
-
with TEMPLATE_PATH.open("r", encoding="utf-8") as f:
|
33
|
-
llama_template = f.read()
|
34
|
-
except FileNotFoundError:
|
35
|
-
raise FileNotFoundError(f"Template file {TEMPLATE_PATH} not found.")
|
36
|
-
|
37
|
-
MAX_TURNS = 4
|
38
|
-
|
39
|
-
## --- Code Execution Functions ---
|
40
|
-
|
41
|
-
|
42
|
-
def parse_exec_result_nb(execution: Any) -> List[Dict[str, Any]]:
|
43
|
-
"""
|
44
|
-
Convert an E2B execution object to a list of Jupyter notebook cell outputs format.
|
45
|
-
|
46
|
-
:param execution: Execution object from the sandbox.
|
47
|
-
:return: List of output dictionaries.
|
48
|
-
"""
|
49
|
-
outputs: List[Dict[str, Any]] = []
|
50
|
-
|
51
|
-
if execution.logs.stdout:
|
52
|
-
outputs.append(
|
53
|
-
{
|
54
|
-
"output_type": "stream",
|
55
|
-
"name": "stdout",
|
56
|
-
"text": "".join(execution.logs.stdout),
|
57
|
-
}
|
58
|
-
)
|
59
|
-
|
60
|
-
if execution.logs.stderr:
|
61
|
-
outputs.append(
|
62
|
-
{
|
63
|
-
"output_type": "stream",
|
64
|
-
"name": "stderr",
|
65
|
-
"text": "".join(execution.logs.stderr),
|
66
|
-
}
|
67
|
-
)
|
68
|
-
|
69
|
-
if execution.error:
|
70
|
-
outputs.append(
|
71
|
-
{
|
72
|
-
"output_type": "error",
|
73
|
-
"ename": execution.error.name,
|
74
|
-
"evalue": execution.error.value,
|
75
|
-
"traceback": [line for line in execution.error.traceback.split("\n")],
|
76
|
-
}
|
77
|
-
)
|
78
|
-
|
79
|
-
for result in execution.results:
|
80
|
-
output = {
|
81
|
-
"output_type": (
|
82
|
-
"execute_result" if result.is_main_result else "display_data"
|
83
|
-
),
|
84
|
-
"metadata": {},
|
85
|
-
"data": {},
|
86
|
-
}
|
87
|
-
|
88
|
-
if result.text:
|
89
|
-
output["data"]["text/plain"] = [result.text] # Array for text/plain
|
90
|
-
if result.html:
|
91
|
-
output["data"]["text/html"] = result.html
|
92
|
-
if result.png:
|
93
|
-
output["data"]["image/png"] = result.png
|
94
|
-
if result.svg:
|
95
|
-
output["data"]["image/svg+xml"] = result.svg
|
96
|
-
if result.jpeg:
|
97
|
-
output["data"]["image/jpeg"] = result.jpeg
|
98
|
-
if result.pdf:
|
99
|
-
output["data"]["application/pdf"] = result.pdf
|
100
|
-
if result.latex:
|
101
|
-
output["data"]["text/latex"] = result.latex
|
102
|
-
if result.json:
|
103
|
-
output["data"]["application/json"] = result.json
|
104
|
-
if result.javascript:
|
105
|
-
output["data"]["application/javascript"] = result.javascript
|
106
|
-
|
107
|
-
if result.is_main_result and execution.execution_count is not None:
|
108
|
-
output["execution_count"] = execution.execution_count
|
109
|
-
|
110
|
-
if output["data"]:
|
111
|
-
outputs.append(output)
|
112
|
-
|
113
|
-
return outputs
|
114
|
-
|
115
|
-
|
116
|
-
## HTML and CSS templates for notebook cells
|
117
|
-
system_template = """\
|
118
|
-
<details>
|
119
|
-
<summary style="display: flex; align-items: center;">
|
120
|
-
<div class="alert alert-block alert-info" style="margin: 0; width: 100%;">
|
121
|
-
<b>System: <span class="arrow">▶</span></b>
|
122
|
-
</div>
|
123
|
-
</summary>
|
124
|
-
<div class="alert alert-block alert-info">
|
125
|
-
{}
|
126
|
-
</div>
|
127
|
-
</details>
|
128
|
-
|
129
|
-
<style>
|
130
|
-
details > summary .arrow {{
|
131
|
-
display: inline-block;
|
132
|
-
transition: transform 0.2s;
|
133
|
-
}}
|
134
|
-
details[open] > summary .arrow {{
|
135
|
-
transform: rotate(90deg);
|
136
|
-
}}
|
137
|
-
</style>
|
138
|
-
"""
|
139
|
-
|
140
|
-
user_template = """<div class="alert alert-block alert-success">
|
141
|
-
<b>User:</b> {}
|
142
|
-
</div>
|
143
|
-
"""
|
144
|
-
|
145
|
-
header_message = """<p align="center">
|
146
|
-
<img src="cloudops-agent.png" alt="Jupyter Agent" />
|
147
|
-
</p>
|
148
|
-
|
149
|
-
|
150
|
-
<p style="text-align:center;">Let a LLM agent write and execute code inside a notebook!</p>"""
|
151
|
-
|
152
|
-
bad_html_bad = """input[type="file"] {
|
153
|
-
display: block;
|
154
|
-
}"""
|
155
|
-
|
156
|
-
|
157
|
-
## --- Notebook Creation and Update Functions ---
|
158
|
-
|
159
|
-
|
160
|
-
def create_base_notebook(messages: List[Dict[str, Any]]) -> Tuple[Dict[str, Any], int]:
|
161
|
-
"""
|
162
|
-
Create the base Jupyter Notebook structure with initial cells.
|
163
|
-
|
164
|
-
:param messages: List of conversation messages.
|
165
|
-
:return: A tuple of the notebook data dictionary and the current code cell counter.
|
166
|
-
"""
|
167
|
-
base_notebook = {
|
168
|
-
"metadata": {
|
169
|
-
"kernel_info": {"name": "python3"},
|
170
|
-
"language_info": {
|
171
|
-
"name": "python",
|
172
|
-
"version": "3.12",
|
173
|
-
},
|
174
|
-
},
|
175
|
-
"nbformat": 4,
|
176
|
-
"nbformat_minor": 0,
|
177
|
-
"cells": [],
|
178
|
-
}
|
179
|
-
base_notebook["cells"].append(
|
180
|
-
{"cell_type": "markdown", "metadata": {}, "source": header_message}
|
181
|
-
)
|
182
|
-
|
183
|
-
if len(messages) == 0:
|
184
|
-
base_notebook["cells"].append(
|
185
|
-
{
|
186
|
-
"cell_type": "code",
|
187
|
-
"execution_count": None,
|
188
|
-
"metadata": {},
|
189
|
-
"source": "",
|
190
|
-
"outputs": [],
|
191
|
-
}
|
192
|
-
)
|
193
|
-
|
194
|
-
code_cell_counter = 0
|
195
|
-
|
196
|
-
for message in messages:
|
197
|
-
if message["role"] == "system":
|
198
|
-
text = system_template.format(message["content"].replace("\n", "<br>"))
|
199
|
-
base_notebook["cells"].append(
|
200
|
-
{"cell_type": "markdown", "metadata": {}, "source": text}
|
201
|
-
)
|
202
|
-
elif message["role"] == "user":
|
203
|
-
text = user_template.format(message["content"].replace("\n", "<br>"))
|
204
|
-
base_notebook["cells"].append(
|
205
|
-
{"cell_type": "markdown", "metadata": {}, "source": text}
|
206
|
-
)
|
207
|
-
|
208
|
-
elif message["role"] == "assistant" and "tool_calls" in message:
|
209
|
-
base_notebook["cells"].append(
|
210
|
-
{
|
211
|
-
"cell_type": "code",
|
212
|
-
"execution_count": None,
|
213
|
-
"metadata": {},
|
214
|
-
"source": message["content"],
|
215
|
-
"outputs": [],
|
216
|
-
}
|
217
|
-
)
|
218
|
-
|
219
|
-
elif message["role"] == "ipython":
|
220
|
-
code_cell_counter += 1
|
221
|
-
base_notebook["cells"][-1]["outputs"] = message["nbformat"]
|
222
|
-
base_notebook["cells"][-1]["execution_count"] = code_cell_counter
|
223
|
-
|
224
|
-
elif message["role"] == "assistant" and "tool_calls" not in message:
|
225
|
-
base_notebook["cells"].append(
|
226
|
-
{"cell_type": "markdown", "metadata": {}, "source": message["content"]}
|
227
|
-
)
|
228
|
-
|
229
|
-
else:
|
230
|
-
raise ValueError(message)
|
231
|
-
|
232
|
-
return base_notebook, code_cell_counter
|
233
|
-
|
234
|
-
|
235
|
-
def execute_code(sbx: Sandbox, code: str) -> Tuple[str, Any]:
|
236
|
-
"""
|
237
|
-
Execute the given code in the provided sandbox.
|
238
|
-
|
239
|
-
:param sbx: Sandbox instance to run the code.
|
240
|
-
:param code: Code to execute.
|
241
|
-
:return: Tuple of aggregated output string and the raw execution object.
|
242
|
-
"""
|
243
|
-
execution = sbx.run_code(code, on_stdout=lambda data: print("stdout:", data))
|
244
|
-
output = ""
|
245
|
-
if len(execution.logs.stdout) > 0:
|
246
|
-
output += "\n".join(execution.logs.stdout)
|
247
|
-
if len(execution.logs.stderr) > 0:
|
248
|
-
output += "\n".join(execution.logs.stderr)
|
249
|
-
if execution.error is not None:
|
250
|
-
output += execution.error.traceback
|
251
|
-
return output, execution
|
252
|
-
|
253
|
-
|
254
|
-
def parse_exec_result_llm(execution: Any) -> str:
|
255
|
-
"""
|
256
|
-
Parse the execution results and return a single concatenated output string.
|
257
|
-
|
258
|
-
:param execution: Execution object from the sandbox.
|
259
|
-
:return: Concatenated string of output messages.
|
260
|
-
"""
|
261
|
-
output = ""
|
262
|
-
if len(execution.logs.stdout) > 0:
|
263
|
-
output += "\n".join(execution.logs.stdout)
|
264
|
-
if len(execution.logs.stderr) > 0:
|
265
|
-
output += "\n".join(execution.logs.stderr)
|
266
|
-
if execution.error is not None:
|
267
|
-
output += execution.error.traceback
|
268
|
-
return output
|
269
|
-
|
270
|
-
|
271
|
-
def update_notebook_display(notebook_data):
|
272
|
-
notebook = nbformat.from_dict(notebook_data)
|
273
|
-
notebook_body, _ = html_exporter.from_notebook_node(notebook)
|
274
|
-
notebook_body = notebook_body.replace(bad_html_bad, "")
|
275
|
-
return notebook_body
|
276
|
-
|
277
|
-
|
278
|
-
## --- Interactive Notebook Generation ---
|
279
|
-
|
280
|
-
|
281
|
-
def run_interactive_notebook(
|
282
|
-
client: InferenceClient,
|
283
|
-
model: str,
|
284
|
-
tokenizer: Any,
|
285
|
-
messages: List[Dict[str, Any]],
|
286
|
-
sbx: Sandbox,
|
287
|
-
max_new_tokens: int = 512,
|
288
|
-
) -> Any:
|
289
|
-
"""
|
290
|
-
Generator function that iteratively builds and updates the Jupyter Notebook.
|
291
|
-
|
292
|
-
:param client: Hugging Face InferenceClient for text generation.
|
293
|
-
:param model: Model identifier.
|
294
|
-
:param tokenizer: Tokenizer corresponding to the model.
|
295
|
-
:param messages: List of conversation messages.
|
296
|
-
:param sbx: Sandbox instance for executing code.
|
297
|
-
:param max_new_tokens: Maximum tokens to generate per turn.
|
298
|
-
:yield: Tuple containing updated notebook HTML, notebook data, and messages.
|
299
|
-
"""
|
300
|
-
notebook_data, code_cell_counter = create_base_notebook(messages)
|
301
|
-
turns = 0
|
302
|
-
|
303
|
-
# code_cell_counter = 0
|
304
|
-
while turns <= MAX_TURNS:
|
305
|
-
turns += 1
|
306
|
-
input_tokens = tokenizer.apply_chat_template(
|
307
|
-
messages,
|
308
|
-
chat_template=llama_template,
|
309
|
-
builtin_tools=["code_interpreter"],
|
310
|
-
add_generation_prompt=True,
|
311
|
-
)
|
312
|
-
model_input = tokenizer.decode(input_tokens)
|
313
|
-
|
314
|
-
print(f"Model input:\n{model_input}\n{'='*80}")
|
315
|
-
|
316
|
-
response_stream = client.text_generation(
|
317
|
-
model=model,
|
318
|
-
prompt=model_input,
|
319
|
-
details=True,
|
320
|
-
stream=True,
|
321
|
-
do_sample=True,
|
322
|
-
repetition_penalty=1.1,
|
323
|
-
temperature=0.8,
|
324
|
-
max_new_tokens=max_new_tokens,
|
325
|
-
)
|
326
|
-
|
327
|
-
assistant_response = ""
|
328
|
-
tokens = []
|
329
|
-
|
330
|
-
code_cell = False
|
331
|
-
for i, chunk in enumerate(response_stream):
|
332
|
-
if not chunk.token.special:
|
333
|
-
content = chunk.token.text
|
334
|
-
else:
|
335
|
-
content = ""
|
336
|
-
tokens.append(chunk.token.text)
|
337
|
-
assistant_response += content
|
338
|
-
|
339
|
-
if len(tokens) == 1:
|
340
|
-
create_cell = True
|
341
|
-
code_cell = "<|python_tag|>" in tokens[0]
|
342
|
-
if code_cell:
|
343
|
-
code_cell_counter += 1
|
344
|
-
else:
|
345
|
-
create_cell = False
|
346
|
-
|
347
|
-
## Update notebook cells in real-time
|
348
|
-
if create_cell:
|
349
|
-
if "<|python_tag|>" in tokens[0]:
|
350
|
-
notebook_data["cells"].append(
|
351
|
-
{
|
352
|
-
"cell_type": "code",
|
353
|
-
"execution_count": None,
|
354
|
-
"metadata": {},
|
355
|
-
"source": assistant_response,
|
356
|
-
"outputs": [],
|
357
|
-
}
|
358
|
-
)
|
359
|
-
else:
|
360
|
-
notebook_data["cells"].append(
|
361
|
-
{
|
362
|
-
"cell_type": "markdown",
|
363
|
-
"metadata": {},
|
364
|
-
"source": assistant_response,
|
365
|
-
}
|
366
|
-
)
|
367
|
-
else:
|
368
|
-
notebook_data["cells"][-1]["source"] = assistant_response
|
369
|
-
if i % 16 == 0:
|
370
|
-
yield update_notebook_display(notebook_data), notebook_data, messages
|
371
|
-
yield update_notebook_display(notebook_data), notebook_data, messages
|
372
|
-
|
373
|
-
## If a code cell was generated, execute the code
|
374
|
-
if code_cell:
|
375
|
-
notebook_data["cells"][-1]["execution_count"] = code_cell_counter
|
376
|
-
|
377
|
-
exec_result, execution = execute_code(sbx, assistant_response)
|
378
|
-
messages.append(
|
379
|
-
{
|
380
|
-
"role": "assistant",
|
381
|
-
"content": assistant_response,
|
382
|
-
"tool_calls": [
|
383
|
-
{
|
384
|
-
"type": "function",
|
385
|
-
"function": {
|
386
|
-
"name": "code_interpreter",
|
387
|
-
"arguments": {"code": assistant_response},
|
388
|
-
},
|
389
|
-
}
|
390
|
-
],
|
391
|
-
}
|
392
|
-
)
|
393
|
-
messages.append(
|
394
|
-
{
|
395
|
-
"role": "ipython",
|
396
|
-
"content": parse_exec_result_llm(execution),
|
397
|
-
"nbformat": parse_exec_result_nb(execution),
|
398
|
-
}
|
399
|
-
)
|
400
|
-
|
401
|
-
## Update the last code cell with execution results
|
402
|
-
notebook_data["cells"][-1]["outputs"] = parse_exec_result_nb(execution)
|
403
|
-
update_notebook_display(notebook_data)
|
404
|
-
else:
|
405
|
-
messages.append({"role": "assistant", "content": assistant_response})
|
406
|
-
if tokens[-1] == "<|eot_id|>":
|
407
|
-
break
|
408
|
-
|
409
|
-
yield update_notebook_display(notebook_data), notebook_data, messages
|
Binary file
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|