-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodechat.py
More file actions
314 lines (260 loc) · 12.5 KB
/
codechat.py
File metadata and controls
314 lines (260 loc) · 12.5 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# codechat.py - Code rewriting and analysis tool
# python -B codechat.py --file writers_toolkit.py --task "stop using tools_config.json directly, and replace with TinyDB"
# pip install anthropic
# tested with: anthropic 0.49.0 circa March 2025
import anthropic
import os
import argparse
import re
import sys
import time
from datetime import datetime
parser = argparse.ArgumentParser(description='Analyze and rewrite code files.')
parser.add_argument('--file', type=str, required=True, help="Path to Python file to analyze or rewrite")
parser.add_argument('--task', type=str, required=True, help="Description of what to do with the code (optimize, explain, refactor, etc.)")
parser.add_argument('--output_file', type=str, help="File to write the modified code to (defaults to {original}_rewritten.py)")
parser.add_argument('--request_timeout', type=int, default=300, help='Maximum timeout for each *streamed chunk* of output (default: 300 seconds = 5 minutes)')
parser.add_argument('--max_retries', type=int, default=0, help='Maximum times to retry request, may get expensive if too many')
parser.add_argument('--context_window', type=int, default=200000, help='Context window for Claude 3.7 Sonnet (default: 200000)')
parser.add_argument('--betas_max_tokens', type=int, default=128000, help='Maximum tokens for AI output (default: 128000)')
parser.add_argument('--thinking_budget_tokens', type=int, default=32000, help='Maximum tokens for AI thinking (default: 32000)')
parser.add_argument('--desired_output_tokens', type=int, default=8000, help='User desired number of tokens to generate before stopping output')
parser.add_argument('--show_token_stats', action='store_true', help='Show tokens stats but do not call API (default: False)')
parser.add_argument('--save_dir', type=str, default=".")
args = parser.parse_args()
def count_words(text):
return len(re.sub(r'(\r\n|\r|\n)', ' ', text).split())
def read_file(filepath):
"""
Read the code file and return its content as a string.
Abort if the file doesn't exist.
"""
if not os.path.exists(filepath):
print(f"Error: File '{filepath}' not found.")
sys.exit(1)
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
return content.strip()
def save_output(filename, content):
"""
Save the generated content to a file
"""
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
print(f"Saved output to: {filename}")
def calculate_max_tokens(prompt):
try:
# Get accurate token count for prompt
response = client.beta.messages.count_tokens(
model="claude-3-7-sonnet-20250219",
messages=[{"role": "user", "content": prompt}],
thinking={
"type": "enabled",
"budget_tokens": args.thinking_budget_tokens
},
betas=["output-128k-2025-02-19"]
)
# calculate available tokens after prompt
prompt_tokens = response.input_tokens
available_tokens = args.context_window - prompt_tokens
# for API call, max_tokens must respect the API limit
max_tokens = min(available_tokens, args.betas_max_tokens)
# thinking budget must be LESS than max_tokens to leave room for visible output
thinking_budget = max_tokens - args.desired_output_tokens
if thinking_budget > args.thinking_budget_tokens:
thinking_budget = 32000
# ensure max_tokens is always greater than thinking budget
if max_tokens <= args.thinking_budget_tokens:
max_tokens = args.thinking_budget_tokens + args.desired_output_tokens
print(f"Adjusted max_tokens to {max_tokens} to exceed thinking budget of {args.thinking_budget_tokens}")
return max_tokens, prompt_tokens, available_tokens, thinking_budget
except Exception as e:
print(f"Error: client.beta.messages.count_tokens:\n{e}\n")
sys.exit(1)
def create_code_prompt(code_content, task):
"""
Create a prompt for code rewriting/analysis based on the file content and task
"""
prompt = f"""You are an expert software developer helping to analyze and rewrite Python code that uses NiceGUI and TinyDB.
=== ORIGINAL CODE ===
{code_content}
=== END ORIGINAL CODE ===
TASK: {task}
Provide the following in your response:
1. IMPROVED CODE
- Complete rewritten version of the code
- Include ALL necessary imports
- Maintain the same functionality while addressing the task
2. EXPLANATION
- Explain the key changes made
- Why these changes improve the code as per the task
IMPORTANT INSTRUCTIONS:
1. Keep the same overall functionality unless the task specifically requires changes
2. Maintain compatibility with existing interfaces
3. The rewritten code should be complete and ready to run
4. Format your code neatly with proper indentation and spacing
5. Include helpful comments where appropriate
Start with "1. IMPROVED CODE:" with a full rewrite of the code, and finally "2. EXPLANATION:" with your explanation.
"""
return prompt
# Print initial setup information
print(f"Starting code analysis/rewriting for file: '{args.file}'")
file_path = os.path.abspath(args.file)
print(f"Absolute file path: {file_path}")
print(f"Task: {args.task}")
print(f"Save directory: {os.path.abspath(args.save_dir)}")
print(f"Max request timeout: {args.request_timeout} seconds")
print(f"Max retries: {args.max_retries}")
print(f"Context window: {args.context_window} tokens")
print(f"Betas max tokens: {args.betas_max_tokens} tokens")
print(f"Thinking budget tokens: {args.thinking_budget_tokens} tokens")
print(f"Desired output tokens: {args.desired_output_tokens} tokens")
code_content = read_file(args.file)
if code_content:
code_lines = code_content.count('\n') + 1
print(f"Code file contains {code_lines} lines")
else:
print("Code file is empty")
sys.exit(1)
client = anthropic.Anthropic(
timeout=args.request_timeout,
max_retries=args.max_retries
)
def process_code():
prompt = create_code_prompt(code_content, args.task)
max_tokens, prompt_tokens, available_tokens, thinking_budget = calculate_max_tokens(prompt)
print(f"\nToken stats:")
print(f"Max retries: {args.max_retries}")
print(f"Max AI model context window: [{args.context_window}] tokens")
print(f"Input prompt tokens: [{prompt_tokens}] ...")
print(f"Available tokens: [{available_tokens}] = {args.context_window} - {prompt_tokens} = context_window - prompt")
print(f"Desired output tokens: [{args.desired_output_tokens}]")
print(f"\nMax output tokens (max_tokens): [{max_tokens}] tokens = min({available_tokens}, {args.betas_max_tokens})")
print(f" = can not exceed: 'betas=[\"output-128k-2025-02-19\"]'")
print(f"AI model thinking budget: [{thinking_budget}] tokens = {max_tokens} - {args.desired_output_tokens}")
print(f" = can not exceed: 32K")
if thinking_budget < args.thinking_budget_tokens:
print(f"Error: prompt is too large to have a {args.thinking_budget_tokens} thinking budget!")
sys.exit(1)
if args.show_token_stats:
print(f"FYI: token stats shown without sending to API, to aid in making adjustments.")
sys.exit(1)
print(f"\n--- Processing Code ---")
full_response = ""
thinking_content = ""
start_time = time.time()
dt = datetime.fromtimestamp(start_time)
formatted_time = dt.strftime("%A %B %d, %Y %I:%M:%S %p").replace(" 0", " ").lower()
print(f"****************************************************************************")
print(f"* sending to API at: {formatted_time}")
print(f"* ... standby, as this usually takes a few minutes")
print(f"****************************************************************************")
try:
with client.beta.messages.stream(
model="claude-3-7-sonnet-20250219",
max_tokens=max_tokens,
messages=[{"role": "user", "content": prompt}],
thinking={
"type": "enabled",
"budget_tokens": thinking_budget
},
betas=["output-128k-2025-02-19"]
) as stream:
# track both thinking and text output
for event in stream:
if event.type == "content_block_delta":
if event.delta.type == "thinking_delta":
thinking_content += event.delta.thinking
elif event.delta.type == "text_delta":
full_response += event.delta.text
except Exception as e:
print(f"\nError during API call:\n{e}\n")
elapsed = time.time() - start_time
minutes = int(elapsed // 60)
seconds = elapsed % 60
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
# Determine output filenames
if args.output_file:
output_filename = args.output_file
else:
base_name, ext = os.path.splitext(args.file)
output_filename = f"{base_name}_rewritten{ext}"
# Create analysis and results files
results_filename = f"{args.save_dir}/code_results_{timestamp}.txt"
# Extract the improved code section from the response
improved_code_match = re.search(r'IMPROVED CODE:(.*?)(?:EXPLANATION:|$)', full_response, re.DOTALL)
if improved_code_match:
improved_code = improved_code_match.group(1).strip()
# Remove any markdown code block formatting
improved_code = re.sub(r'^```python\s*', '', improved_code, flags=re.MULTILINE)
improved_code = re.sub(r'^```\s*$', '', improved_code, flags=re.MULTILINE)
save_output(output_filename, improved_code)
# Save the full analysis and results
save_output(results_filename, full_response)
output_word_count = count_words(full_response)
print(f"\nElapsed time: {minutes} minutes, {seconds:.2f} seconds.")
print(f"Generated response has {output_word_count} words.")
output_token_count = 0
try:
response = client.beta.messages.count_tokens(
model="claude-3-7-sonnet-20250219",
messages=[{"role": "user", "content": full_response}],
thinking={
"type": "enabled",
"budget_tokens": args.thinking_budget_tokens
},
betas=["output-128k-2025-02-19"]
)
output_token_count = response.input_tokens
print(f"Output is {output_token_count} tokens (via client.beta.messages.count_tokens)")
except Exception as e:
print(f"Error counting output tokens:\n{e}\n")
stats = f"""
Details:
Max request timeout: {args.request_timeout} seconds
Max retries: {args.max_retries}
Max AI model context window: {args.context_window} tokens
Betas max tokens: {args.betas_max_tokens} tokens
Thinking budget tokens: {args.thinking_budget_tokens} tokens
Desired output tokens: {args.desired_output_tokens} tokens
Prompt tokens: {prompt_tokens}
Available tokens after prompt: {available_tokens}
Dynamic thinking budget: {thinking_budget} tokens
Setting max_tokens to: {max_tokens} (requested: {args.betas_max_tokens})
Elapsed time: {minutes} minutes, {seconds:.2f} seconds
Output has {output_word_count} words
Output is {output_token_count} tokens (via client.beta.messages.count_tokens)
Full response saved to: {results_filename}
"""
if thinking_content:
thinking_filename = f"{args.save_dir}/code_thinking_{timestamp}.txt"
with open(thinking_filename, 'w', encoding='utf-8') as file:
file.write("=== PROMPT USED ===\n")
file.write(prompt)
file.write("\n\n=== AI'S THINKING PROCESS ===\n\n")
file.write(thinking_content)
file.write("\n=== END AI'S THINKING PROCESS ===\n")
file.write(stats)
print(f"AI thinking saved to: {thinking_filename}\n")
else:
print("No AI thinking content was captured.\n")
return results_filename
try:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
print(f"Created directory: {args.save_dir}")
results_file = process_code()
print("\nCode processing complete!")
print(f"Full analysis saved to: {results_file}")
if args.output_file:
print(f"Rewritten code saved to: {args.output_file}")
else:
base_name, ext = os.path.splitext(args.file)
output_filename = f"{base_name}_rewritten{ext}"
print(f"Rewritten code saved to: {output_filename}")
except Exception as e:
print(f"\nAn error occurred: {e}")
sys.exit(1)
finally:
# clean up resources
client = None