Spaces:
Build error
Build error
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -498,11 +498,9 @@ def run_research_agent(
|
|
| 498 |
1) Tavily search (up to 20 URLs).
|
| 499 |
2) Firecrawl scrape => combined text
|
| 500 |
3) Truncate to 12k tokens total
|
| 501 |
-
4) Split
|
| 502 |
5) Single final merge => final PDF
|
| 503 |
-
=> 2 or
|
| 504 |
-
|
| 505 |
-
Logs at each step for clarity.
|
| 506 |
"""
|
| 507 |
print(f"[LOG] Starting LOW-CALL research agent for topic: {topic}")
|
| 508 |
|
|
@@ -547,17 +545,19 @@ def run_research_agent(
|
|
| 547 |
print("[LOG] Step 3: Truncating combined text to 12,000 tokens if needed.")
|
| 548 |
combined_content = truncate_text_tokens(combined_content, max_tokens=12000)
|
| 549 |
|
| 550 |
-
# Step 4:
|
| 551 |
-
print("[LOG] Step 4: Splitting text into
|
| 552 |
tokenizer = tiktoken.get_encoding("cl100k_base")
|
| 553 |
tokens = tokenizer.encode(combined_content)
|
| 554 |
-
chunk_size =
|
| 555 |
-
|
| 556 |
-
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 557 |
summaries = []
|
| 558 |
start = 0
|
| 559 |
chunk_index = 1
|
| 560 |
-
|
|
|
|
|
|
|
|
|
|
| 561 |
end = min(start + chunk_size, len(tokens))
|
| 562 |
chunk_text = tokenizer.decode(tokens[start:end])
|
| 563 |
print(f"[LOG] Summarizing chunk {chunk_index} with ~{len(tokens[start:end])} tokens.")
|
|
@@ -566,7 +566,6 @@ You are a specialized summarization engine. Summarize the following text
|
|
| 566 |
for a professional research report. Provide accurate details but do not
|
| 567 |
include chain-of-thought or internal reasoning. Keep it concise, but
|
| 568 |
include key data points and context:
|
| 569 |
-
|
| 570 |
{chunk_text}
|
| 571 |
"""
|
| 572 |
data = {
|
|
@@ -578,12 +577,8 @@ include key data points and context:
|
|
| 578 |
response = call_llm_with_retry(groq_client, **data)
|
| 579 |
summary_text = response.choices[0].message.content.strip()
|
| 580 |
summaries.append(summary_text)
|
| 581 |
-
|
| 582 |
start = end
|
| 583 |
chunk_index += 1
|
| 584 |
-
# Because chunk_size=6000, only 2 chunks max
|
| 585 |
-
if chunk_index > 2:
|
| 586 |
-
break
|
| 587 |
|
| 588 |
# Step 5: Single final merge call
|
| 589 |
print("[LOG] Step 5: Doing one final merge of chunk summaries.")
|
|
@@ -604,20 +599,16 @@ Produce a long, academic-style research paper with the following structure:
|
|
| 604 |
- Footnotes or inline citations referencing the URLs
|
| 605 |
- Conclusion
|
| 606 |
- References / Bibliography (list these URLs at the end)
|
| 607 |
-
|
| 608 |
Requirements:
|
| 609 |
- Minimal bullet points, prefer multi-paragraph
|
| 610 |
- Each section at least 2-3 paragraphs
|
| 611 |
- Aim for 1500+ words if possible
|
| 612 |
- Under 6000 tokens total
|
| 613 |
- Professional, academic tone
|
| 614 |
-
|
| 615 |
Partial Summaries:
|
| 616 |
{merged_input}
|
| 617 |
-
|
| 618 |
References (URLs):
|
| 619 |
{references_text}
|
| 620 |
-
|
| 621 |
Now, merge these partial summaries into one thoroughly expanded research paper:
|
| 622 |
"""
|
| 623 |
final_data = {
|
|
|
|
| 498 |
1) Tavily search (up to 20 URLs).
|
| 499 |
2) Firecrawl scrape => combined text
|
| 500 |
3) Truncate to 12k tokens total
|
| 501 |
+
4) Split into chunks (each 4500 tokens) => Summarize each chunk individually => summaries
|
| 502 |
5) Single final merge => final PDF
|
| 503 |
+
=> 2 or more total LLM calls (but no more than 10) to reduce the chance of rate limit errors.
|
|
|
|
|
|
|
| 504 |
"""
|
| 505 |
print(f"[LOG] Starting LOW-CALL research agent for topic: {topic}")
|
| 506 |
|
|
|
|
| 545 |
print("[LOG] Step 3: Truncating combined text to 12,000 tokens if needed.")
|
| 546 |
combined_content = truncate_text_tokens(combined_content, max_tokens=12000)
|
| 547 |
|
| 548 |
+
# Step 4: Splitting text into chunks (4500 tokens each) and summarizing each chunk.
|
| 549 |
+
print("[LOG] Step 4: Splitting text into chunks (4500 tokens each). Summarizing each chunk.")
|
| 550 |
tokenizer = tiktoken.get_encoding("cl100k_base")
|
| 551 |
tokens = tokenizer.encode(combined_content)
|
| 552 |
+
chunk_size = 4500 # Reduced chunk size to avoid exceeding the LLM's TPM limit.
|
| 553 |
+
max_chunks = 10 # Allow up to 10 chunks (and thus 10 LLM calls).
|
|
|
|
| 554 |
summaries = []
|
| 555 |
start = 0
|
| 556 |
chunk_index = 1
|
| 557 |
+
|
| 558 |
+
groq_client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 559 |
+
|
| 560 |
+
while start < len(tokens) and chunk_index <= max_chunks:
|
| 561 |
end = min(start + chunk_size, len(tokens))
|
| 562 |
chunk_text = tokenizer.decode(tokens[start:end])
|
| 563 |
print(f"[LOG] Summarizing chunk {chunk_index} with ~{len(tokens[start:end])} tokens.")
|
|
|
|
| 566 |
for a professional research report. Provide accurate details but do not
|
| 567 |
include chain-of-thought or internal reasoning. Keep it concise, but
|
| 568 |
include key data points and context:
|
|
|
|
| 569 |
{chunk_text}
|
| 570 |
"""
|
| 571 |
data = {
|
|
|
|
| 577 |
response = call_llm_with_retry(groq_client, **data)
|
| 578 |
summary_text = response.choices[0].message.content.strip()
|
| 579 |
summaries.append(summary_text)
|
|
|
|
| 580 |
start = end
|
| 581 |
chunk_index += 1
|
|
|
|
|
|
|
|
|
|
| 582 |
|
| 583 |
# Step 5: Single final merge call
|
| 584 |
print("[LOG] Step 5: Doing one final merge of chunk summaries.")
|
|
|
|
| 599 |
- Footnotes or inline citations referencing the URLs
|
| 600 |
- Conclusion
|
| 601 |
- References / Bibliography (list these URLs at the end)
|
|
|
|
| 602 |
Requirements:
|
| 603 |
- Minimal bullet points, prefer multi-paragraph
|
| 604 |
- Each section at least 2-3 paragraphs
|
| 605 |
- Aim for 1500+ words if possible
|
| 606 |
- Under 6000 tokens total
|
| 607 |
- Professional, academic tone
|
|
|
|
| 608 |
Partial Summaries:
|
| 609 |
{merged_input}
|
|
|
|
| 610 |
References (URLs):
|
| 611 |
{references_text}
|
|
|
|
| 612 |
Now, merge these partial summaries into one thoroughly expanded research paper:
|
| 613 |
"""
|
| 614 |
final_data = {
|