Spaces:
Sleeping
Sleeping
Daniel Nichols
commited on
Commit
•
972361d
1
Parent(s):
63f2785
add max code len to control length
Browse files- src/rag.py +12 -0
src/rag.py
CHANGED
@@ -12,6 +12,11 @@ from function_grabber import get_function_at_line
|
|
12 |
|
13 |
from profiles import Profile
|
14 |
|
|
|
|
|
|
|
|
|
|
|
15 |
class PerfGuruPromptFormatter(ABC):
|
16 |
|
17 |
def __init__(self, name: str):
|
@@ -48,6 +53,8 @@ class BasicPromptFormatter(PerfGuruPromptFormatter):
|
|
48 |
for code_path, content in code_file_contents.items():
|
49 |
fname = basename(code_path)
|
50 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
|
|
|
|
51 |
|
52 |
if profile_path:
|
53 |
if not profile_type:
|
@@ -57,6 +64,7 @@ class BasicPromptFormatter(PerfGuruPromptFormatter):
|
|
57 |
|
58 |
profile = self._read_profile(profile_path, profile_type)
|
59 |
profile_content = profile.profile_to_tree_str()
|
|
|
60 |
else:
|
61 |
profile_content = ""
|
62 |
|
@@ -80,6 +88,8 @@ class SlowestFunctionPromptFormatter(PerfGuruPromptFormatter):
|
|
80 |
fname = basename(code_path)
|
81 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
82 |
|
|
|
|
|
83 |
if profile_path:
|
84 |
if not profile_type:
|
85 |
if error_fn:
|
@@ -159,6 +169,8 @@ class SlowestFunctionParsedPromptFormatter(PerfGuruPromptFormatter):
|
|
159 |
for code_path, content in code_file_contents.items():
|
160 |
fname = basename(code_path)
|
161 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
|
|
|
|
162 |
|
163 |
return f"Code:\n{concatenated_code}\n\n{profile_type} Profile:\n{profile_content}\n\n{prompt}"
|
164 |
|
|
|
12 |
|
13 |
from profiles import Profile
|
14 |
|
15 |
+
|
16 |
+
def truncate_string(s: str, max_len: int) -> str:
|
17 |
+
return s if len(s) <= max_len else s[:max_len] + "..."
|
18 |
+
|
19 |
+
|
20 |
class PerfGuruPromptFormatter(ABC):
|
21 |
|
22 |
def __init__(self, name: str):
|
|
|
53 |
for code_path, content in code_file_contents.items():
|
54 |
fname = basename(code_path)
|
55 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
56 |
+
|
57 |
+
concatenated_code = truncate_string(concatenated_code, 4000)
|
58 |
|
59 |
if profile_path:
|
60 |
if not profile_type:
|
|
|
64 |
|
65 |
profile = self._read_profile(profile_path, profile_type)
|
66 |
profile_content = profile.profile_to_tree_str()
|
67 |
+
profile_content = truncate_string(profile_content, 4000)
|
68 |
else:
|
69 |
profile_content = ""
|
70 |
|
|
|
88 |
fname = basename(code_path)
|
89 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
90 |
|
91 |
+
concatenated_code = truncate_string(concatenated_code, 4000)
|
92 |
+
|
93 |
if profile_path:
|
94 |
if not profile_type:
|
95 |
if error_fn:
|
|
|
169 |
for code_path, content in code_file_contents.items():
|
170 |
fname = basename(code_path)
|
171 |
concatenated_code += f"{fname}:\n{content}\n\n"
|
172 |
+
|
173 |
+
concatenated_code = truncate_string(concatenated_code, 4000)
|
174 |
|
175 |
return f"Code:\n{concatenated_code}\n\n{profile_type} Profile:\n{profile_content}\n\n{prompt}"
|
176 |
|