Update geminiService.ts
This commit is contained in:
@@ -1,5 +1,30 @@
|
|||||||
import { GoogleGenAI } from "@google/genai";
|
import { GoogleGenAI } from "@google/genai";
|
||||||
import { KBArticle, Ticket, TicketStatus } from "../types";
|
import { AiProvider, KBArticle, Ticket, TicketStatus } from "../types";
|
||||||
|
|
||||||
|
// --- OPENROUTER / OPENAI COMPATIBLE FETCH ---
|
||||||
|
async function callOpenRouter(apiKey: string, model: string, messages: any[]) {
|
||||||
|
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Authorization": `Bearer ${apiKey}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"HTTP-Referer": window.location.origin, // Required by OpenRouter
|
||||||
|
"X-Title": "OmniSupport AI" // Optional
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: model || "openai/gpt-3.5-turbo",
|
||||||
|
messages: messages
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const err = await response.text();
|
||||||
|
throw new Error(`OpenRouter API Error: ${err}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.choices[0]?.message?.content || "";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Agent 1: Customer Support Chat
|
* Agent 1: Customer Support Chat
|
||||||
@@ -9,15 +34,14 @@ export const getSupportResponse = async (
|
|||||||
apiKey: string,
|
apiKey: string,
|
||||||
userQuery: string,
|
userQuery: string,
|
||||||
chatHistory: string[],
|
chatHistory: string[],
|
||||||
knowledgeBase: KBArticle[]
|
knowledgeBase: KBArticle[],
|
||||||
|
provider: AiProvider = AiProvider.GEMINI,
|
||||||
|
model: string = 'gemini-3-flash-preview'
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
if (!apiKey) {
|
if (!apiKey) {
|
||||||
return "L'assistente AI non è configurato (API Key mancante). Contatta l'amministratore.";
|
return "L'assistente AI non è configurato (API Key mancante). Contatta l'amministratore.";
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const ai = new GoogleGenAI({ apiKey });
|
|
||||||
|
|
||||||
// Prepare Context from KB
|
// Prepare Context from KB
|
||||||
const kbContext = knowledgeBase.map(a => {
|
const kbContext = knowledgeBase.map(a => {
|
||||||
if (a.type === 'url') {
|
if (a.type === 'url') {
|
||||||
@@ -26,7 +50,7 @@ export const getSupportResponse = async (
|
|||||||
return `Articolo [${a.category}]: ${a.title}\nContenuto: ${a.content}`;
|
return `Articolo [${a.category}]: ${a.title}\nContenuto: ${a.content}`;
|
||||||
}).join('\n\n');
|
}).join('\n\n');
|
||||||
|
|
||||||
const systemInstruction = `
|
const systemInstructionText = `
|
||||||
Sei "OmniSupport AI", un assistente clienti virtuale globale.
|
Sei "OmniSupport AI", un assistente clienti virtuale globale.
|
||||||
|
|
||||||
IL TUO COMPITO:
|
IL TUO COMPITO:
|
||||||
@@ -46,22 +70,38 @@ export const getSupportResponse = async (
|
|||||||
4. Sii cortese, professionale e sintetico.
|
4. Sii cortese, professionale e sintetico.
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (provider === AiProvider.OPENROUTER || provider === AiProvider.OPENAI || provider === AiProvider.DEEPSEEK) {
|
||||||
|
// Logic for OpenRouter/OpenAI compatible APIs
|
||||||
|
const messages = [
|
||||||
|
{ role: "system", content: systemInstructionText },
|
||||||
|
...chatHistory.map(msg => ({ role: "user", content: msg })),
|
||||||
|
{ role: "user", content: userQuery }
|
||||||
|
];
|
||||||
|
|
||||||
|
const response = await callOpenRouter(apiKey, model, messages);
|
||||||
|
return response || "Mi dispiace, non riesco a generare una risposta al momento.";
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Default to Google Gemini
|
||||||
|
const ai = new GoogleGenAI({ apiKey });
|
||||||
const response = await ai.models.generateContent({
|
const response = await ai.models.generateContent({
|
||||||
model: 'gemini-3-flash-preview',
|
model: model,
|
||||||
contents: [
|
contents: [
|
||||||
...chatHistory.map(msg => ({ role: 'user', parts: [{ text: msg }] })),
|
...chatHistory.map(msg => ({ role: 'user', parts: [{ text: msg }] })),
|
||||||
{ role: 'user', parts: [{ text: userQuery }] }
|
{ role: 'user', parts: [{ text: userQuery }] }
|
||||||
],
|
],
|
||||||
config: {
|
config: {
|
||||||
systemInstruction: systemInstruction,
|
systemInstruction: systemInstructionText,
|
||||||
temperature: 0.3,
|
temperature: 0.3,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return response.text || "Mi dispiace, non riesco a generare una risposta al momento.";
|
return response.text || "Mi dispiace, non riesco a generare una risposta al momento.";
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Gemini Error:", error);
|
console.error("AI Service Error:", error);
|
||||||
return "Si è verificato un errore nel servizio AI (Verifica API Key o connessione).";
|
return "Si è verificato un errore nel servizio AI (Verifica API Key, Provider o connessione).";
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,13 +111,12 @@ export const getSupportResponse = async (
|
|||||||
export const generateNewKBArticle = async (
|
export const generateNewKBArticle = async (
|
||||||
apiKey: string,
|
apiKey: string,
|
||||||
resolvedTickets: Ticket[],
|
resolvedTickets: Ticket[],
|
||||||
existingArticles: KBArticle[]
|
existingArticles: KBArticle[],
|
||||||
|
provider: AiProvider = AiProvider.GEMINI,
|
||||||
|
model: string = 'gemini-3-pro-preview'
|
||||||
): Promise<{ title: string; content: string; category: string } | null> => {
|
): Promise<{ title: string; content: string; category: string } | null> => {
|
||||||
if (!apiKey) return null;
|
if (!apiKey) return null;
|
||||||
|
|
||||||
try {
|
|
||||||
const ai = new GoogleGenAI({ apiKey });
|
|
||||||
|
|
||||||
// Filter only resolved tickets
|
// Filter only resolved tickets
|
||||||
const relevantTickets = resolvedTickets.filter(t => t.status === TicketStatus.RESOLVED);
|
const relevantTickets = resolvedTickets.filter(t => t.status === TicketStatus.RESOLVED);
|
||||||
|
|
||||||
@@ -91,7 +130,7 @@ export const generateNewKBArticle = async (
|
|||||||
|
|
||||||
const existingTitles = existingArticles.map(a => a.title).join(', ');
|
const existingTitles = existingArticles.map(a => a.title).join(', ');
|
||||||
|
|
||||||
const prompt = `
|
const systemPrompt = `
|
||||||
Sei un Knowledge Manager AI esperto.
|
Sei un Knowledge Manager AI esperto.
|
||||||
Analizza i seguenti ticket risolti e confrontali con gli articoli esistenti nella Knowledge Base.
|
Analizza i seguenti ticket risolti e confrontali con gli articoli esistenti nella Knowledge Base.
|
||||||
|
|
||||||
@@ -103,7 +142,7 @@ export const generateNewKBArticle = async (
|
|||||||
OBIETTIVO:
|
OBIETTIVO:
|
||||||
1. Identifica un problema ricorrente o una soluzione tecnica presente nei ticket risolti MA NON coperta dagli articoli esistenti.
|
1. Identifica un problema ricorrente o una soluzione tecnica presente nei ticket risolti MA NON coperta dagli articoli esistenti.
|
||||||
2. Se trovi una lacuna, scrivi un NUOVO articolo di Knowledge Base per colmarla.
|
2. Se trovi una lacuna, scrivi un NUOVO articolo di Knowledge Base per colmarla.
|
||||||
3. Restituisci il risultato ESCLUSIVAMENTE in formato JSON.
|
3. Restituisci il risultato ESCLUSIVAMENTE in formato JSON valido. Non aggiungere markdown code blocks.
|
||||||
|
|
||||||
SCHEMA JSON RICHIESTO:
|
SCHEMA JSON RICHIESTO:
|
||||||
{
|
{
|
||||||
@@ -114,18 +153,29 @@ export const generateNewKBArticle = async (
|
|||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let rawText = "";
|
||||||
|
|
||||||
|
if (provider === AiProvider.OPENROUTER || provider === AiProvider.OPENAI || provider === AiProvider.DEEPSEEK) {
|
||||||
|
const messages = [{ role: "system", content: systemPrompt }];
|
||||||
|
rawText = await callOpenRouter(apiKey, model, messages);
|
||||||
|
} else {
|
||||||
|
const ai = new GoogleGenAI({ apiKey });
|
||||||
const response = await ai.models.generateContent({
|
const response = await ai.models.generateContent({
|
||||||
model: 'gemini-3-pro-preview',
|
model: model,
|
||||||
contents: prompt,
|
contents: systemPrompt,
|
||||||
config: {
|
config: { responseMimeType: "application/json" }
|
||||||
responseMimeType: "application/json"
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
rawText = response.text || "";
|
||||||
|
}
|
||||||
|
|
||||||
const text = response.text;
|
if (!rawText) return null;
|
||||||
if (!text) return null;
|
|
||||||
|
|
||||||
const result = JSON.parse(text);
|
// Clean markdown if present (sometimes models add ```json ... ```)
|
||||||
|
// We escape the backticks in the regex to ensure safety in all environments
|
||||||
|
const cleanedText = rawText.replace(/\`\`\`json/g, '').replace(/\`\`\`/g, '').trim();
|
||||||
|
|
||||||
|
const result = JSON.parse(cleanedText);
|
||||||
if (result.foundGap) {
|
if (result.foundGap) {
|
||||||
return {
|
return {
|
||||||
title: result.title,
|
title: result.title,
|
||||||
@@ -134,6 +184,7 @@ export const generateNewKBArticle = async (
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Knowledge Agent Error:", error);
|
console.error("Knowledge Agent Error:", error);
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user