-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeminiService.ts
More file actions
93 lines (78 loc) · 3.17 KB
/
Copy pathgeminiService.ts
File metadata and controls
93 lines (78 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { GoogleGenAI, Type, Schema } from "@google/genai";
// --- Initialization ---
const getAiClient = () => new GoogleGenAI({ apiKey: process.env.API_KEY });
// --- Core Research Analysis (Thinking Mode) ---
export const analyzeResearchPaper = async (text: string): Promise<any> => {
const ai = getAiClient();
const schema: Schema = {
type: Type.OBJECT,
properties: {
slides: {
type: Type.ARRAY,
items: {
type: Type.OBJECT,
properties: {
title: { type: Type.STRING },
content: { type: Type.ARRAY, items: { type: Type.STRING } },
notes: { type: Type.STRING, description: "Speaker notes for this slide." }
},
required: ["title", "content"]
}
},
summary: { type: Type.STRING }
},
required: ["slides", "summary"]
};
const response = await ai.models.generateContent({
model: "gemini-3-pro-preview",
contents: `Analyze the following research text and structure it into a compelling 5-7 slide presentation.
Focus on the narrative arc: Problem, Methodology, Key Findings, Implications.
Text: ${text.substring(0, 30000)}`,
config: {
responseMimeType: "application/json",
responseSchema: schema,
thinkingConfig: { thinkingBudget: 16000 },
}
});
return JSON.parse(response.text || "{}");
};
// --- Mind Map Generation ---
export const generateMindMapCode = async (text: string): Promise<string> => {
const ai = getAiClient();
// Strict prompt for simple tree structure
const prompt = `Create a Mermaid.js diagram code that visualizes the hierarchy of this research.
Use 'graph TD' (Top-Down).
Keep node labels short (max 4 words).
Max 10-12 nodes total.
Do NOT use parentheses () or brackets [] inside node text strings as they break syntax, use simple text only.
Return ONLY the raw mermaid code. No markdown code blocks.`;
const response = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: `${prompt}
Text: ${text.substring(0, 20000)}`
});
let code = response.text || "";
// Heavy cleaning to prevent syntax errors
code = code.replace(/```mermaid/g, '').replace(/```/g, '').trim();
// Fix common syntax issues
code = code.replace(/\(/g, '').replace(/\)/g, ''); // Remove parentheses from labels to prevent syntax break
// Ensure it starts with graph TD
if (!code.startsWith('graph TD') && !code.startsWith('graph')) {
code = 'graph TD\n' + code;
}
return code;
};
// --- Chat (Gemini 3 Pro) ---
export const sendChatMessage = async (history: {role: string, parts: {text: string}[]}[], message: string, context: string) => {
const ai = getAiClient();
const chat = ai.chats.create({
model: 'gemini-3-pro-preview',
history: [
{ role: 'user', parts: [{ text: `System Context: You are a research assistant. Use the following paper content as your primary knowledge base: \n\n${context}\n\nRule: Answer questions clearly. Use **bold** for key terms or emphasis.` }] },
{ role: 'model', parts: [{ text: "Understood." }] },
...history
]
});
const result = await chat.sendMessage({ message });
return result.text;
};