session start works
This commit is contained in:
51
backend/src/services/LLMService.ts
Normal file
51
backend/src/services/LLMService.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
|
||||
|
||||
interface DesireSet {
|
||||
wants: string[];
|
||||
accepts: string[];
|
||||
noGoes: string[];
|
||||
}
|
||||
|
||||
export class LLMService {
|
||||
private genAI: GoogleGenerativeAI;
|
||||
private model: GenerativeModel;
|
||||
|
||||
constructor(apiKey: string) {
|
||||
this.genAI = new GoogleGenerativeAI(apiKey);
|
||||
this.model = this.genAI.getGenerativeModel({ model: "gemini-pro" });
|
||||
}
|
||||
|
||||
async analyzeDesires(desireSets: DesireSet[]): Promise<Record<string, string>> {
|
||||
const allDesires: string[] = [];
|
||||
desireSets.forEach(set => {
|
||||
allDesires.push(...set.wants, ...set.accepts, ...set.noGoes);
|
||||
});
|
||||
|
||||
const uniqueDesires = Array.from(new Set(allDesires.filter(d => d.trim() !== '')));
|
||||
|
||||
if (uniqueDesires.length === 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const prompt = `
|
||||
You are an AI assistant that groups similar desires. Given a list of desires, identify semantically equivalent or very similar items and group them under a single, concise canonical name. Return the output as a JSON object where keys are the original desire strings and values are their canonical group names.
|
||||
|
||||
Example:
|
||||
Input: ["go for a walk", "walking", "stroll in the park", "eat pizza", "pizza for dinner"]
|
||||
Output: {"go for a walk": "Go for a walk", "walking": "Go for a walk", "stroll in the park": "Go for a walk", "eat pizza": "Eat pizza", "pizza for dinner": "Eat pizza"}
|
||||
|
||||
Here is the list of desires to group:
|
||||
${JSON.stringify(uniqueDesires)}
|
||||
`;
|
||||
|
||||
try {
|
||||
const result = await this.model.generateContent(prompt);
|
||||
const response = result.response;
|
||||
const text = response.text();
|
||||
return JSON.parse(text);
|
||||
} catch (error) {
|
||||
console.error("Error calling Gemini API:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user