-
Notifications
You must be signed in to change notification settings - Fork 319
/
main.py
106 lines (85 loc) · 3.16 KB
/
main.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
import os
import json
import argparse
import pathlib
from groq import Groq
from llama_index.core import SimpleDirectoryReader
import colorama
from termcolor import colored
from asciitree import LeftAligned
from asciitree.drawing import BoxStyle, BOX_LIGHT
from src.loader import get_doc_summaries
# import agentops
# import litellm
import click
colorama.init() # Initializes colorama to make it work on Windows as well
# agentops.init(api_key="483ce794-f363-4983-8c66-3f669e12884b")
@click.command()
@click.argument("src_path", type=click.Path(exists=True))
@click.argument("dst_path", type=click.Path())
@click.option("--auto-yes", is_flag=True, help="Automatically say yes to all prompts")
def main(src_path, dst_path, auto_yes=False):
os.environ["GROQ_API_KEY"] = (
"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"
)
summaries = get_doc_summaries(src_path)
for summary in summaries:
print(colored(summary["file_name"], "green")) # Print the filename in green
print(summary["summary"]) # Print the summary of the contents
print("-" * 80 + "\n") # Print a separator line with spacing for readability
# Create File Tree
PROMPT = """
You will be provided with list of files and a summary of their contents. Read them carefully, then propose a directory structure that optimally organizes the files using known conventions and best practices.
Your response must be a JSON object with the following schema:
```json
{
"files": [
{
"filename": "name of the file",
"path": "path under proposed directory structure"
}
]
}
```
""".strip()
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": PROMPT},
{"role": "user", "content": json.dumps(summaries)},
],
model="llama3-8b-8192",
response_format={"type": "json_object"}, # Uncomment if needed
temperature=0,
)
file_tree = json.loads(chat_completion.choices[0].message.content)["files"]
BASE_DIR = pathlib.Path(dst_path)
BASE_DIR.mkdir(exist_ok=True)
# Recursively create dictionary from file paths
tree = {}
for file in file_tree:
parts = pathlib.Path(file["path"]).parts
current = tree
for part in parts:
current = current.setdefault(part, {})
# for file in file_tree:
# parts = pathlib.Path(file["path"]).parts
# current = tree
# for part in parts:
# current = current.setdefault(part, {})
print(tree)
tr = LeftAligned(draw=BoxStyle(gfx=BOX_LIGHT, horiz_len=1))
click.echo(tr(tree))
if not auto_yes and not click.confirm(
"Proceed with directory structure?", default=True
):
click.echo("Operation cancelled.")
return
for file in file_tree:
file["path"] = pathlib.Path(file["path"])
# Create file in specified base directory
(BASE_DIR / file["path"]).parent.mkdir(parents=True, exist_ok=True)
with open(BASE_DIR / file["path"], "w") as f:
f.write("")
if __name__ == "__main__":
main()