Skip to content

Commit 80d9d7b

Browse files
committed
fix: script updates for LLM tokens optimization
1 parent aa2ce17 commit 80d9d7b

2 files changed

Lines changed: 333 additions & 106 deletions

File tree

.github/scripts/generate_intros_and_keywords.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def create_smart_batches(files_to_process):
188188
batches = []
189189
current_batch = {}
190190
current_tokens = 0
191-
PROMPT_OVERHEAD = 2000 # Overhead for prompt template
191+
PROMPT_OVERHEAD = 5000 # Increased overhead for prompt template + formatting
192192

193193
for filepath, contents, tokens in file_tokens:
194194
batch_is_full = len(current_batch) >= MAX_FILES_PER_BATCH
@@ -256,26 +256,27 @@ def process_batch_with_llm(batch_files):
256256
# Build the prompt
257257
prompt = get_prompt_for_files(file_list)
258258

259-
# Build the content parts: prompt + all file contents
260-
parts = [{"text": prompt}]
259+
# Build the content as a single text string (more efficient than inline_data)
260+
batch_input = prompt + "\n\n"
261261
for filepath, contents in batch_files.items():
262-
parts.append({"text": f"\n\n### File: {filepath.name}\n\n"})
263-
parts.append(
264-
{
265-
"inline_data": {
266-
"mime_type": "text/plain",
267-
"data": contents.encode("utf-8"),
268-
}
269-
}
270-
)
262+
batch_input += f"\n\n### File: {filepath.name}\n\n{contents}\n\n"
263+
264+
# Log API call
265+
print(f" Making API request #{rate_limit_state['requests_today'] + 1}")
266+
print(f" Input: {total_input_tokens:,} tokens")
271267

272-
# Send to Gemini
268+
# Send to Gemini as single text part (more efficient)
273269
try:
274-
response = model.generate_content(contents=[{"role": "user", "parts": parts}])
270+
response = model.generate_content(
271+
contents=[{"role": "user", "parts": [{"text": batch_input}]}]
272+
)
275273

276274
# Record successful API usage
277275
record_api_request(total_input_tokens)
278276
total_tokens_sent += total_input_tokens
277+
print(
278+
f" API request successful (total today: {rate_limit_state['requests_today']})"
279+
)
279280

280281
raw_text = response.text.strip()
281282

@@ -496,14 +497,26 @@ def save_cached_result(cache_path, data):
496497
if files_to_process:
497498
batches = create_smart_batches(files_to_process)
498499
print(f"Created {len(batches)} batch(es)")
500+
print(f"Expected API calls: {len(batches)} (1 per batch)")
501+
502+
total_api_calls = 0 # Track total API requests including retries
499503

500504
# Process each batch with retry logic
501505
for batch_idx, batch_files in enumerate(batches, 1):
502506
print(f"\n=== Processing batch {batch_idx}/{len(batches)} ===")
507+
initial_request_count = rate_limit_state["requests_today"]
503508

504509
# Process the batch with automatic retry/split on failure
505510
batch_results = process_batch_with_retry(batch_files)
506511

512+
# Count how many API calls this batch actually made (including retries)
513+
batch_api_calls = rate_limit_state["requests_today"] - initial_request_count
514+
total_api_calls += batch_api_calls
515+
516+
print(
517+
f"📊 Batch {batch_idx}/{len(batches)} complete ({batch_api_calls} API call(s), total so far: {total_api_calls})"
518+
)
519+
507520
if not batch_results:
508521
print(
509522
f"[ERROR] Batch {batch_idx} failed completely after retries, skipping..."
@@ -522,6 +535,13 @@ def save_cached_result(cache_path, data):
522535
save_cached_result(cache_path, cache)
523536
print(f" ✓ Saved result for {filepath.name}")
524537

538+
# Summary of API usage
539+
print(f"\n Total API calls made: {total_api_calls} (expected: {len(batches)})")
540+
if total_api_calls > len(batches):
541+
print(
542+
f" Made {total_api_calls - len(batches)} extra calls due to retries/splits"
543+
)
544+
525545
# Apply all results (both newly processed and cached) to .qmd files
526546
print("\n=== Updating .qmd files ===")
527547
all_files = set(files_to_process.keys()) | set(files_cached.keys())

0 commit comments

Comments
 (0)