-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsummarization.py
More file actions
46 lines (39 loc) · 1.76 KB
/
summarization.py
File metadata and controls
46 lines (39 loc) · 1.76 KB
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
import os
import sys
import subprocess
from openai import OpenAI
from file_utils import get_file_contents, update_file
openai_api_key = os.getenv("OPENAI_API_KEY")
if not openai_api_key:
print("Set OPENAI_API_KEY in your environment.")
sys.exit(1)
client = OpenAI(api_key=openai_api_key)
def generate_summary(file_path):
return
print("Generating summary for:", file_path)
code = get_file_contents(file_path)
messages = [
{"role": "system", "content": "You are a helpful assistant that concisely summarizes code."},
{"role": "user", "content": (
f"Summarize the following code file by providing one sentence summary per function and an overall summary of the file:\n\n```{code}```"
)}
]
response = client.chat.completions.create(model="o3-mini", messages=messages, reasoning_effort='low')
summary = response.choices[0].message.content.strip()
print("Generated")
return summary
def get_project_summaries(project_dir, config):
summaries = {}
for root, dirs, files in os.walk(project_dir):
dirs[:] = [d for d in dirs if not os.path.exists(os.path.join(root, d, 'pyvenv.cfg'))]
for file in files:
if any(file.endswith(ext) for ext in config.get('file_types', [])) and not file.endswith('.summary'):
rel_path = os.path.relpath(os.path.join(root, file), project_dir)
summary_path = os.path.join(root, file + ".summary")
if not os.path.exists(summary_path):
summary = generate_summary(os.path.join(root, file))
update_file(summary_path, summary)
else:
summary = get_file_contents(summary_path)
summaries[rel_path] = summary
return summaries