Spaces:
Running
Running
perf: optimize token usage and context window for K2 engine (10k context, 8k response)
Browse files
app/services/analysis_service.py
CHANGED
|
@@ -102,12 +102,13 @@ class AnalysisService:
|
|
| 102 |
|
| 103 |
if remote_data:
|
| 104 |
p_info = remote_data[0]
|
|
|
|
| 105 |
paper = ResearchPaper(
|
| 106 |
project_id=project_id,
|
| 107 |
remote_id=pid,
|
| 108 |
title=p_info.get("title", "Unknown"),
|
| 109 |
authors=", ".join(p_info.get("authors", [])),
|
| 110 |
-
summary=
|
| 111 |
publication_year=datetime.now().year # Fallback
|
| 112 |
)
|
| 113 |
db.add(paper)
|
|
@@ -135,7 +136,7 @@ class AnalysisService:
|
|
| 135 |
title=paper.title,
|
| 136 |
authors=author_list,
|
| 137 |
abstract=paper.summary or "",
|
| 138 |
-
content=paper.summary or "",
|
| 139 |
document_type=dtype,
|
| 140 |
url=paper.pdf_path or ""
|
| 141 |
))
|
|
|
|
| 102 |
|
| 103 |
if remote_data:
|
| 104 |
p_info = remote_data[0]
|
| 105 |
+
snippet = p_info.get("content", p_info.get("summary", ""))[:10000]
|
| 106 |
paper = ResearchPaper(
|
| 107 |
project_id=project_id,
|
| 108 |
remote_id=pid,
|
| 109 |
title=p_info.get("title", "Unknown"),
|
| 110 |
authors=", ".join(p_info.get("authors", [])),
|
| 111 |
+
summary=snippet,
|
| 112 |
publication_year=datetime.now().year # Fallback
|
| 113 |
)
|
| 114 |
db.add(paper)
|
|
|
|
| 136 |
title=paper.title,
|
| 137 |
authors=author_list,
|
| 138 |
abstract=paper.summary or "",
|
| 139 |
+
content=(paper.summary or "")[:10000],
|
| 140 |
document_type=dtype,
|
| 141 |
url=paper.pdf_path or ""
|
| 142 |
))
|
app/services/k2_think_engine.py
CHANGED
|
@@ -62,14 +62,14 @@ class K2ThinkEngine:
|
|
| 62 |
openai_api_key=settings.K2_THINK_API_KEY,
|
| 63 |
openai_api_base=settings.K2_THINK_API_URL,
|
| 64 |
temperature=0.7,
|
| 65 |
-
max_tokens=
|
| 66 |
max_retries=3
|
| 67 |
)
|
| 68 |
|
| 69 |
# 2. Préparation du contexte documentaire
|
| 70 |
context_parts = []
|
| 71 |
for doc in request.documents:
|
| 72 |
-
snippet = doc.content[:
|
| 73 |
first_author = doc.authors[0].split()[-1] if doc.authors else "Unknown"
|
| 74 |
year = "n.d."
|
| 75 |
citation_key = f"({first_author}, {year})"
|
|
@@ -87,6 +87,7 @@ class K2ThinkEngine:
|
|
| 87 |
# 4. Définition du Prompt Système
|
| 88 |
system_template = """You are the K2 Think V2 Scientific Co-Investigator.
|
| 89 |
Your primary directive is MULTI-DOCUMENT REASONING and KNOWLEDGE SYNTHESIS.
|
|
|
|
| 90 |
|
| 91 |
IMPORTANT: Your internal reasoning (thoughts) should be contained within <think></think> tags.
|
| 92 |
|
|
|
|
| 62 |
openai_api_key=settings.K2_THINK_API_KEY,
|
| 63 |
openai_api_base=settings.K2_THINK_API_URL,
|
| 64 |
temperature=0.7,
|
| 65 |
+
max_tokens=8192,
|
| 66 |
max_retries=3
|
| 67 |
)
|
| 68 |
|
| 69 |
# 2. Préparation du contexte documentaire
|
| 70 |
context_parts = []
|
| 71 |
for doc in request.documents:
|
| 72 |
+
snippet = doc.content[:10000]
|
| 73 |
first_author = doc.authors[0].split()[-1] if doc.authors else "Unknown"
|
| 74 |
year = "n.d."
|
| 75 |
citation_key = f"({first_author}, {year})"
|
|
|
|
| 87 |
# 4. Définition du Prompt Système
|
| 88 |
system_template = """You are the K2 Think V2 Scientific Co-Investigator.
|
| 89 |
Your primary directive is MULTI-DOCUMENT REASONING and KNOWLEDGE SYNTHESIS.
|
| 90 |
+
Keep your reasoning process efficient and focused. Proceed to the [RESULT] JSON block as soon as you have synthesized the core findings.
|
| 91 |
|
| 92 |
IMPORTANT: Your internal reasoning (thoughts) should be contained within <think></think> tags.
|
| 93 |
|