初始提交:CodingRoom 机器人与 AI 平台
This commit is contained in:
35
services/geminiService.ts
Normal file
35
services/geminiService.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
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 "机器人大脑目前正在充电中,请稍后再试哦!";
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user