-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
143 lines (121 loc) · 4.13 KB
/
Copy pathserver.ts
File metadata and controls
143 lines (121 loc) · 4.13 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI, Modality } from "@google/genai";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const PORT = 3000;
app.use(express.json({ limit: "10mb" }));
// Initialize Gemini Client
const ai = new GoogleGenAI({
apiKey: process.env.GEMINI_API_KEY || "",
httpOptions: {
headers: {
"User-Agent": "aistudio-build",
},
},
});
// API Routes
// 1. Text to Speech API (using gemini-3.1-flash-tts-preview)
app.post("/api/tts", async (req, res) => {
try {
const { text, voice = "Kore", promptPrefix } = req.body;
if (!text || typeof text !== "string") {
return res.status(400).json({ error: "Text string is required" });
}
if (!process.env.GEMINI_API_KEY) {
return res.status(500).json({ error: "GEMINI_API_KEY is missing on server" });
}
// Standard instruction for clear nursing terminology reading
let finalPrompt = text;
if (promptPrefix) {
finalPrompt = `${promptPrefix}: ${text}`;
}
const response = await ai.models.generateContent({
model: "gemini-3.1-flash-tts-preview",
contents: [{ parts: [{ text: finalPrompt }] }],
config: {
responseModalities: [Modality.AUDIO],
speechConfig: {
voiceConfig: {
prebuiltVoiceConfig: { voiceName: voice },
},
},
},
});
const candidate = response.candidates?.[0];
const audioPart = candidate?.content?.parts?.find((p: any) => p.inlineData?.data);
const base64Audio = audioPart?.inlineData?.data;
if (!base64Audio) {
return res.status(500).json({ error: "Failed to generate audio from Gemini TTS" });
}
return res.json({
audio: base64Audio,
mimeType: audioPart?.inlineData?.mimeType || "audio/pcm",
});
} catch (error: any) {
console.error("TTS API Error:", error);
return res.status(500).json({
error: error.message || "Failed to call Gemini TTS API",
});
}
});
// 2. AI Nursing Care Tutor API (using gemini-3.6-flash)
app.post("/api/tutor", async (req, res) => {
try {
const { prompt, conversationHistory = [] } = req.body;
if (!prompt) {
return res.status(400).json({ error: "Prompt is required" });
}
if (!process.env.GEMINI_API_KEY) {
return res.status(500).json({ error: "GEMINI_API_KEY is missing on server" });
}
const systemInstruction = `Anda adalah 'Kaigo Sensei' (Tutor AI Ujian Keperawatan 特定技能1号 Tokutei Ginou Kaigo 2024 Ver 4.0).
Tugas Anda adalah membantu calon pekerja migran Indonesia memahami materi keperawatan Jepang (Kaigo Text Ver 4.0).
Gunakan bahasa Indonesia yang jelas, ramah, dan komunikatif, disertai istilah bahasa Jepang (Kanji, Hiragana, Romaji) beserta artinya.
Berikan penjelasan ringkas, contoh kasus di panti jompo/fasilitas lansia (Kaigo Shisetsu), dan petunjuk tips lulus ujian CBT Kaigo.`;
const contents = [
...conversationHistory.map((msg: any) => ({
role: msg.role === "user" ? "user" : "model",
parts: [{ text: msg.content }],
})),
{ role: "user", parts: [{ text: prompt }] },
];
const response = await ai.models.generateContent({
model: "gemini-3.6-flash",
contents,
config: {
systemInstruction,
temperature: 0.7,
},
});
return res.json({
reply: response.text,
});
} catch (error: any) {
console.error("AI Tutor Error:", error);
return res.status(500).json({
error: error.message || "Failed to call Gemini Tutor API",
});
}
});
async function startServer() {
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();