Spaces:
Running
Running
refactor: update content limits for quiz and summary generation modules
Browse files- quiz_generator/constants.py +7 -0
- quiz_generator/quiz.py +10 -1
- rag/constants.py +7 -0
- rag/rag.py +13 -6
- summary_generator/constants.py +8 -1
- summary_generator/routes.py +2 -1
- summary_generator/summary.py +16 -3
quiz_generator/constants.py
CHANGED
|
@@ -8,6 +8,13 @@ MAX_TF_COUNT = 20
|
|
| 8 |
MAX_SAMPLE_CHUNKS = 10
|
| 9 |
RETRIEVER_K = 5
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
QUIZ_PROMPT_TEMPLATE = PromptTemplate(
|
| 12 |
input_variables=[
|
| 13 |
"difficulty", "mcq_count", "tf_count",
|
|
|
|
| 8 |
MAX_SAMPLE_CHUNKS = 10
|
| 9 |
RETRIEVER_K = 5
|
| 10 |
|
| 11 |
+
# Web search configuration
|
| 12 |
+
WIKI_TOP_K_RESULTS = 2
|
| 13 |
+
WIKI_DOC_CONTENT_CHARS_MAX = 15000
|
| 14 |
+
|
| 15 |
+
ARXIV_TOP_K_RESULTS = 4
|
| 16 |
+
ARXIV_DOC_CONTENT_CHARS_MAX = 10000
|
| 17 |
+
|
| 18 |
QUIZ_PROMPT_TEMPLATE = PromptTemplate(
|
| 19 |
input_variables=[
|
| 20 |
"difficulty", "mcq_count", "tf_count",
|
quiz_generator/quiz.py
CHANGED
|
@@ -11,6 +11,10 @@ from .constants import (
|
|
| 11 |
QUIZ_PROMPT_TEMPLATE,
|
| 12 |
MAX_SAMPLE_CHUNKS,
|
| 13 |
RETRIEVER_K,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
logger = logging.getLogger(__name__)
|
|
@@ -121,7 +125,12 @@ def _web_quiz(difficulty, mcq_count, tf_count, topic_title):
|
|
| 121 |
try:
|
| 122 |
prompt = _quiz_prompt()
|
| 123 |
llm = get_llm()
|
| 124 |
-
tools = web_search_tools(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
agent = create_tool_calling_agent(llm, tools, prompt)
|
| 126 |
|
| 127 |
executor = AgentExecutor(
|
|
|
|
| 11 |
QUIZ_PROMPT_TEMPLATE,
|
| 12 |
MAX_SAMPLE_CHUNKS,
|
| 13 |
RETRIEVER_K,
|
| 14 |
+
WIKI_TOP_K_RESULTS,
|
| 15 |
+
WIKI_DOC_CONTENT_CHARS_MAX,
|
| 16 |
+
ARXIV_TOP_K_RESULTS,
|
| 17 |
+
ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 18 |
)
|
| 19 |
|
| 20 |
logger = logging.getLogger(__name__)
|
|
|
|
| 125 |
try:
|
| 126 |
prompt = _quiz_prompt()
|
| 127 |
llm = get_llm()
|
| 128 |
+
tools = web_search_tools(
|
| 129 |
+
wiki_k=WIKI_TOP_K_RESULTS,
|
| 130 |
+
wiki_chars=WIKI_DOC_CONTENT_CHARS_MAX,
|
| 131 |
+
arxiv_k=ARXIV_TOP_K_RESULTS,
|
| 132 |
+
arxiv_chars=ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 133 |
+
)
|
| 134 |
agent = create_tool_calling_agent(llm, tools, prompt)
|
| 135 |
|
| 136 |
executor = AgentExecutor(
|
rag/constants.py
CHANGED
|
@@ -4,6 +4,13 @@ BATCH_MAX_SIZE = 8
|
|
| 4 |
BATCH_WINDOW_S = 0.05
|
| 5 |
WARMUP_INTERVAL_S = 300
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
RAG_PROMPT_TEMPLATE_BASE = """\
|
| 8 |
<role>
|
| 9 |
You are a helpful AI study assistant. You provide accurate, well-reasoned educational answers.{subject_line}
|
|
|
|
| 4 |
BATCH_WINDOW_S = 0.05
|
| 5 |
WARMUP_INTERVAL_S = 300
|
| 6 |
|
| 7 |
+
# Web search configuration
|
| 8 |
+
WIKI_TOP_K_RESULTS = 2
|
| 9 |
+
WIKI_DOC_CONTENT_CHARS_MAX = 3000
|
| 10 |
+
ARXIV_TOP_K_RESULTS = 3
|
| 11 |
+
ARXIV_DOC_CONTENT_CHARS_MAX = 2500
|
| 12 |
+
DUCKDUCKGO_DOC_CONTENT_CHARS_MAX = 3000
|
| 13 |
+
|
| 14 |
RAG_PROMPT_TEMPLATE_BASE = """\
|
| 15 |
<role>
|
| 16 |
You are a helpful AI study assistant. You provide accurate, well-reasoned educational answers.{subject_line}
|
rag/rag.py
CHANGED
|
@@ -25,6 +25,11 @@ from .constants import (
|
|
| 25 |
EMBEDDING_DIM,
|
| 26 |
RAG_PROMPT_TEMPLATE_BASE,
|
| 27 |
CHAT_TITLE_PROMPT_TEMPLATE,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
from .schemas import SearchInput, EmbeddingJob
|
| 30 |
|
|
@@ -159,14 +164,16 @@ def get_groq_llm():
|
|
| 159 |
|
| 160 |
# ββ Web Search Tools βββββββββββββββββββββββββββββββββββ
|
| 161 |
|
| 162 |
-
def web_search_tools(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
tools = []
|
| 165 |
-
|
| 166 |
-
# Target ~16,500 chars total across all tools
|
| 167 |
-
wiki_k = 2; wiki_chars = 3000 # 6,000 total
|
| 168 |
-
arxiv_k = 3; arxiv_chars = 2500 # 7,500 total
|
| 169 |
-
duck_chars = 3000 # 3,000 total
|
| 170 |
|
| 171 |
try:
|
| 172 |
wiki_api = WikipediaAPIWrapper(top_k_results=wiki_k, doc_content_chars_max=wiki_chars)
|
|
|
|
| 25 |
EMBEDDING_DIM,
|
| 26 |
RAG_PROMPT_TEMPLATE_BASE,
|
| 27 |
CHAT_TITLE_PROMPT_TEMPLATE,
|
| 28 |
+
WIKI_TOP_K_RESULTS,
|
| 29 |
+
WIKI_DOC_CONTENT_CHARS_MAX,
|
| 30 |
+
ARXIV_TOP_K_RESULTS,
|
| 31 |
+
ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 32 |
+
DUCKDUCKGO_DOC_CONTENT_CHARS_MAX,
|
| 33 |
)
|
| 34 |
from .schemas import SearchInput, EmbeddingJob
|
| 35 |
|
|
|
|
| 164 |
|
| 165 |
# ββ Web Search Tools βββββββββββββββββββββββββββββββββββ
|
| 166 |
|
| 167 |
+
def web_search_tools(
|
| 168 |
+
has_material: bool = False,
|
| 169 |
+
wiki_k: int = WIKI_TOP_K_RESULTS,
|
| 170 |
+
wiki_chars: int = WIKI_DOC_CONTENT_CHARS_MAX,
|
| 171 |
+
arxiv_k: int = ARXIV_TOP_K_RESULTS,
|
| 172 |
+
arxiv_chars: int = ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 173 |
+
duck_chars: int = DUCKDUCKGO_DOC_CONTENT_CHARS_MAX,
|
| 174 |
+
):
|
| 175 |
|
| 176 |
tools = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
try:
|
| 179 |
wiki_api = WikipediaAPIWrapper(top_k_results=wiki_k, doc_content_chars_max=wiki_chars)
|
summary_generator/constants.py
CHANGED
|
@@ -1,7 +1,14 @@
|
|
| 1 |
from langchain.prompts import PromptTemplate
|
| 2 |
|
| 3 |
MAX_INPUT_CHARS = 15000
|
| 4 |
-
MAX_COMBINED_TEXT_LEN =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
SUMMARIZER_PROMPT_TEMPLATE = PromptTemplate(
|
| 7 |
input_variables=["input"],
|
|
|
|
| 1 |
from langchain.prompts import PromptTemplate
|
| 2 |
|
| 3 |
MAX_INPUT_CHARS = 15000
|
| 4 |
+
MAX_COMBINED_TEXT_LEN = 160000
|
| 5 |
+
|
| 6 |
+
# Web search configuration
|
| 7 |
+
WIKI_TOP_K_RESULTS = 2
|
| 8 |
+
WIKI_DOC_CONTENT_CHARS_MAX = 40000
|
| 9 |
+
|
| 10 |
+
ARXIV_TOP_K_RESULTS = 3
|
| 11 |
+
ARXIV_DOC_CONTENT_CHARS_MAX = 25000
|
| 12 |
|
| 13 |
SUMMARIZER_PROMPT_TEMPLATE = PromptTemplate(
|
| 14 |
input_variables=["input"],
|
summary_generator/routes.py
CHANGED
|
@@ -44,7 +44,8 @@ async def generate_summary(
|
|
| 44 |
raise HTTPException(400, "No text chunks found in this material")
|
| 45 |
combined = "\n".join(c["content"] for c in chunks_list)
|
| 46 |
if len(combined) > MAX_COMBINED_TEXT_LEN:
|
| 47 |
-
|
|
|
|
| 48 |
summary = await loop.run_in_executor(None, summarizer, combined)
|
| 49 |
|
| 50 |
elapsed = time.time() - start
|
|
|
|
| 44 |
raise HTTPException(400, "No text chunks found in this material")
|
| 45 |
combined = "\n".join(c["content"] for c in chunks_list)
|
| 46 |
if len(combined) > MAX_COMBINED_TEXT_LEN:
|
| 47 |
+
half_len = MAX_COMBINED_TEXT_LEN // 2
|
| 48 |
+
combined = combined[:half_len] + combined[-half_len:]
|
| 49 |
summary = await loop.run_in_executor(None, summarizer, combined)
|
| 50 |
|
| 51 |
elapsed = time.time() - start
|
summary_generator/summary.py
CHANGED
|
@@ -2,7 +2,14 @@ import re
|
|
| 2 |
import logging
|
| 3 |
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 4 |
from src.rag.rag import get_llm
|
| 5 |
-
from .constants import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
|
@@ -67,7 +74,10 @@ def web_summarizer(topic: str) -> str:
|
|
| 67 |
all_content = []
|
| 68 |
|
| 69 |
try:
|
| 70 |
-
wiki_api = WikipediaAPIWrapper(
|
|
|
|
|
|
|
|
|
|
| 71 |
wiki_content = wiki_api.run(topic)
|
| 72 |
if wiki_content and wiki_content.strip():
|
| 73 |
all_content.append(f"--- Wikipedia ---\n{wiki_content}")
|
|
@@ -75,7 +85,10 @@ def web_summarizer(topic: str) -> str:
|
|
| 75 |
logger.warning(f"Wikipedia search for '{topic}' failed: {e}")
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
arxiv_api = ArxivAPIWrapper(
|
|
|
|
|
|
|
|
|
|
| 79 |
arxiv_content = arxiv_api.run(topic)
|
| 80 |
if arxiv_content and arxiv_content.strip():
|
| 81 |
all_content.append(f"--- Arxiv ---\n{arxiv_content}")
|
|
|
|
| 2 |
import logging
|
| 3 |
from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
|
| 4 |
from src.rag.rag import get_llm
|
| 5 |
+
from .constants import (
|
| 6 |
+
SUMMARIZER_PROMPT_TEMPLATE,
|
| 7 |
+
MAX_INPUT_CHARS,
|
| 8 |
+
WIKI_TOP_K_RESULTS,
|
| 9 |
+
WIKI_DOC_CONTENT_CHARS_MAX,
|
| 10 |
+
ARXIV_TOP_K_RESULTS,
|
| 11 |
+
ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 12 |
+
)
|
| 13 |
|
| 14 |
logger = logging.getLogger(__name__)
|
| 15 |
|
|
|
|
| 74 |
all_content = []
|
| 75 |
|
| 76 |
try:
|
| 77 |
+
wiki_api = WikipediaAPIWrapper(
|
| 78 |
+
top_k_results=WIKI_TOP_K_RESULTS,
|
| 79 |
+
doc_content_chars_max=WIKI_DOC_CONTENT_CHARS_MAX,
|
| 80 |
+
)
|
| 81 |
wiki_content = wiki_api.run(topic)
|
| 82 |
if wiki_content and wiki_content.strip():
|
| 83 |
all_content.append(f"--- Wikipedia ---\n{wiki_content}")
|
|
|
|
| 85 |
logger.warning(f"Wikipedia search for '{topic}' failed: {e}")
|
| 86 |
|
| 87 |
try:
|
| 88 |
+
arxiv_api = ArxivAPIWrapper(
|
| 89 |
+
top_k_results=ARXIV_TOP_K_RESULTS,
|
| 90 |
+
doc_content_chars_max=ARXIV_DOC_CONTENT_CHARS_MAX,
|
| 91 |
+
)
|
| 92 |
arxiv_content = arxiv_api.run(topic)
|
| 93 |
if arxiv_content and arxiv_content.strip():
|
| 94 |
all_content.append(f"--- Arxiv ---\n{arxiv_content}")
|