fix(pptx): 논문 검색어 한국어/문장형을 영어 키워드로 변환 후 검색 — PubMed가 한글 쿼리에 암·방광 등 무관 논문을 반환하던 문제
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea9f1fdb45
commit
9b793784c5
2 files changed
+48
-4
No files matched your search
@@ -4,6 +4,7 @@ import fs from 'fs';
|
||||
import { getConfig, getUserWorkspace } from '../../config/config';
|
||||
import { getOllamaClient } from '../../agents/ollama-client';
|
||||
import { filterImageMarkdown } from '../guards/link-validator';
|
||||
import { toPaperSearchQuery } from '../../tools/query-translate.js';
|
||||
import { getToolRegistry } from '../../tools/registry.js';
|
||||
|
||||
function isPathInsideDir(base: string, target: string): boolean {
|
||||
@@ -393,7 +394,7 @@ app.post('/api/pptx/read-pdf', requireGatewayAuth, async (req: express.Request,
|
||||
|
||||
app.get('/api/pubmed/search', requireGatewayAuth, async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const query = String(req.query.q || req.query.query || '').trim();
|
||||
const query = await toPaperSearchQuery(String(req.query.q || req.query.query || '').trim());
|
||||
const maxResults = Math.min(Number(req.query.max || 20), 50);
|
||||
const minYear = req.query.min_year ? Number(req.query.min_year) : undefined;
|
||||
const sort = req.query.sort === 'date' ? 'date' : 'relevance';
|
||||
@@ -408,7 +409,7 @@ app.get('/api/pubmed/search', requireGatewayAuth, async (req: express.Request, r
|
||||
|
||||
app.get('/api/openalex/search', requireGatewayAuth, async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const query = String(req.query.q || req.query.query || '').trim();
|
||||
const query = await toPaperSearchQuery(String(req.query.q || req.query.query || '').trim());
|
||||
const maxResults = Math.min(Number(req.query.max || 20), 50);
|
||||
const minYear = req.query.min_year ? Number(req.query.min_year) : undefined;
|
||||
const sort = req.query.sort === 'date' ? 'publication_date' : 'relevance';
|
||||
@@ -423,7 +424,7 @@ app.get('/api/openalex/search', requireGatewayAuth, async (req: express.Request,
|
||||
|
||||
app.get('/api/semantic/search', requireGatewayAuth, async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const query = String(req.query.q || req.query.query || '').trim();
|
||||
const query = await toPaperSearchQuery(String(req.query.q || req.query.query || '').trim());
|
||||
const maxResults = Math.min(Number(req.query.max || 20), 50);
|
||||
const minYear = req.query.min_year ? Number(req.query.min_year) : undefined;
|
||||
if (!query) { res.status(400).json({ error: 'q parameter required' }); return; }
|
||||
@@ -437,7 +438,7 @@ app.get('/api/semantic/search', requireGatewayAuth, async (req: express.Request,
|
||||
|
||||
app.get('/api/europepmc/search', requireGatewayAuth, async (req: express.Request, res: express.Response) => {
|
||||
try {
|
||||
const query = String(req.query.q || req.query.query || '').trim();
|
||||
const query = await toPaperSearchQuery(String(req.query.q || req.query.query || '').trim());
|
||||
const maxResults = Math.min(Number(req.query.max || 20), 50);
|
||||
const minYear = req.query.min_year ? Number(req.query.min_year) : undefined;
|
||||
const sort = req.query.sort === 'date' ? 'P_PDATE_D desc' : 'RELEVANCE';
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import { getOllamaConfig } from './web.js';
|
||||
|
||||
// Paper-search backends (PubMed, EuropePMC, Semantic Scholar) index English text only.
|
||||
// A Korean query makes PubMed fall back to arbitrary recent papers (cancer, neurology…),
|
||||
// and sentence-style English ("latest methods and materials") drags in generic noise.
|
||||
// Route such queries through the chat model to get concise English search keywords.
|
||||
const cache = new Map<string, string>();
|
||||
|
||||
export async function toPaperSearchQuery(query: string): Promise<string> {
|
||||
const q = query.trim();
|
||||
if (!q) return q;
|
||||
const hasNonAscii = /[^\x00-\x7F]/.test(q);
|
||||
const sentenceLike = q.split(/\s+/).length > 5;
|
||||
// Short ASCII queries and queries with PubMed syntax ([tag], quotes, AND/OR) pass through untouched.
|
||||
if (!hasNonAscii && (!sentenceLike || /[[\]"()]|\b(AND|OR|NOT)\b/.test(q))) return q;
|
||||
const hit = cache.get(q);
|
||||
if (hit) return hit;
|
||||
try {
|
||||
const { endpoint, model } = getOllamaConfig();
|
||||
const res = await fetch(`${endpoint}/api/chat`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
model,
|
||||
think: false,
|
||||
messages: [{
|
||||
role: 'user',
|
||||
content: `Convert this research topic into a concise English keyword query for PubMed/OpenAlex literature search. Use 2-6 specific scientific terms (MeSH-style). Drop filler words like "latest", "recent", "methods", "overview", "and". No boolean operators, no quotes. Output ONLY the keywords on one line.\n\nTopic: ${q}`,
|
||||
}],
|
||||
stream: false,
|
||||
}),
|
||||
signal: AbortSignal.timeout(20_000),
|
||||
});
|
||||
if (!res.ok) return q;
|
||||
const data: any = await res.json();
|
||||
const out = String(data.message?.content || '').split('\n')[0].replace(/^["'`]+|["'`]+$/g, '').trim();
|
||||
if (!out || out.length > 200) return q;
|
||||
cache.set(q, out);
|
||||
return out;
|
||||
} catch {
|
||||
return q;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user