Files
codingroom/services/geminiService.ts
2026-01-28 00:19:52 +08:00

36 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { GoogleGenAI } from "@google/genai";
import { Message } from "../types";
const SYSTEM_INSTRUCTION = `你是 CodingRoom.cn 机器人 AI 助手。
你是一名亲切的机器人专家,专门为学习者解答机器人、编程和 AI 的奥秘。
请用通俗易懂、生动有趣的语言回答问题(多用比喻,比如把代码比作魔法,把传感器比作眼睛)。
在提到我们时请使用“CodingRoom.cn”或“我们”。
你的回复必须使用中文。
如果用户问到 C++ 入门题,请给予鼓励并给出简单清晰的提示。`;
export const getRoboticsChatResponse = async (history: Message[], userInput: string) => {
const ai = new GoogleGenAI({ apiKey: process.env.API_KEY });
try {
const response = await ai.models.generateContent({
model: 'gemini-3-pro-preview',
contents: [
...history.map(m => ({ role: m.role, parts: [{ text: m.text }] })),
{ role: 'user', parts: [{ text: userInput }] }
],
config: {
systemInstruction: SYSTEM_INSTRUCTION,
temperature: 0.8,
topK: 40,
topP: 0.95,
},
});
return response.text || "抱歉,我的机器人脑袋刚才短路了,请再说一遍?";
} catch (error) {
console.error("Gemini API Error:", error);
return "机器人大脑目前正在充电中,请稍后再试哦!";
}
};