mirror of
https://github.com/bitbaum/botsmann.git
synced 2026-12-04 04:27:47 +01:00
lib/nlp.ts's processQuery() made a hardcoded direct fetch to api.openai.com with a single model (gpt-3.5-turbo) and no fallback. OPENAI_API_KEY is not configured in production, so this path always threw before even reaching the network -- silently dead code on every call from app/api/products/search/route.ts since deployment, papered over only by the pre-existing basic-search fallback. Replace the hardcoded OpenAI call with generateWithBestProvider from lib/llm-client.ts, the same Ollama -> Groq -> OpenRouter chain every other AI call path in this repo already uses. Preserves processQuery's signature and its existing graceful-degradation contract (malformed JSON or a fully unavailable chain still falls back to a basic keyword search instead of throwing to the route). Found by this session's fleet-wide AI-reliability audit, which has been fixing this exact "single-vendor bypass, no fallback" shape across the fleet. Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { NLPResult } from '@/types/products';
|
|
import { generateWithBestProvider } from '@/lib/llm-client';
|
|
|
|
/**
|
|
* Converts a one-word shopping query into structured search parameters.
|
|
*
|
|
* Routed through `generateWithBestProvider` (Ollama -> Groq -> OpenRouter),
|
|
* not a hardcoded OpenAI call: this used to `fetch` `api.openai.com` directly
|
|
* with a single model and no fallback, which meant it was dead code the
|
|
* moment `OPENAI_API_KEY` was unset (it always is in production) or OpenAI
|
|
* had an outage. Any failure -- provider unavailable, malformed JSON, no
|
|
* content -- degrades to a basic keyword search rather than surfacing an
|
|
* error, exactly as before.
|
|
*/
|
|
export async function processQuery(query: string): Promise<NLPResult> {
|
|
try {
|
|
const { content } = await generateWithBestProvider([
|
|
{
|
|
role: 'system',
|
|
content: `Convert one-word shopping queries into structured product search parameters.
|
|
Return a JSON object with 'category' and 'attributes'.
|
|
Example: For "laptop", return:
|
|
{
|
|
"category": "electronics/computers",
|
|
"attributes": {
|
|
"type": "laptop",
|
|
"minRam": "8GB",
|
|
"minStorage": "256GB"
|
|
}
|
|
}`,
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: query,
|
|
},
|
|
]);
|
|
|
|
if (!content) {
|
|
throw new Error('Invalid response from LLM provider');
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(content);
|
|
return {
|
|
category: parsed.category || 'general',
|
|
attributes: parsed.attributes || {},
|
|
};
|
|
} catch {
|
|
// Non-critical: error handled via fallback
|
|
return {
|
|
category: 'general',
|
|
attributes: { query },
|
|
};
|
|
}
|
|
} catch {
|
|
// Fallback to basic search
|
|
return {
|
|
category: 'general',
|
|
attributes: { query },
|
|
};
|
|
}
|
|
}
|