mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-13 18:27:40 +02:00
feat: Introduce bulk AI product analysis, integrating with OpenRouter via a new API and UI.
This commit is contained in:
1 parent
04e6ed70f6
commit
3de16d15d7
5 files changed
+194
-106
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import { AnalysisEngine } from "../business/analysis-engine";
|
||||
import { ProductAnalysisInput, AnalysisResult } from "../models/ai-analysis.types";
|
||||
|
||||
const OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions";
|
||||
|
||||
export interface OpenRouterConfig {
|
||||
apiKey: string;
|
||||
model: string;
|
||||
}
|
||||
|
||||
export class OpenRouterClient {
|
||||
constructor(private config: OpenRouterConfig) { }
|
||||
|
||||
async analyzeProduct(p: ProductAnalysisInput): Promise<AnalysisResult> {
|
||||
const response = await fetch(OPENROUTER_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.config.apiKey}`,
|
||||
"Content-Type": "application/json",
|
||||
"HTTP-Referer": "https://collectflow.app",
|
||||
"X-Title": "CollectFlow AI Copilot",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: this.config.model,
|
||||
messages: [
|
||||
{ role: "system", content: AnalysisEngine.generateSystemPrompt() },
|
||||
{ role: "user", content: AnalysisEngine.generateUserMessage(p) },
|
||||
],
|
||||
max_tokens: 150,
|
||||
temperature: 0.3,
|
||||
}),
|
||||
});
|
||||
|
||||
if (response.status === 429) {
|
||||
throw new Error("rate_limited");
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const err = await response.text();
|
||||
throw new Error(`OpenRouter error: ${err}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
const content = data.choices?.[0]?.message?.content ?? "";
|
||||
|
||||
return {
|
||||
insight: content,
|
||||
codein: p.codein,
|
||||
recommandation: AnalysisEngine.extractRecommendation(content)
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user