-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathbotEngine.ts
More file actions
36 lines (30 loc) · 1.31 KB
/
botEngine.ts
File metadata and controls
36 lines (30 loc) · 1.31 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
import { BotPersona } from "./botPersonas.ts";
import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY as string);
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash"} );
interface chat {
sender: string,
text: string,
};
export const generateBotReply = async (persona: BotPersona, chatHistory: Messages[]): Promise<string> => {
try{
const historyText = chatHistory.map((msg) => `${msg.sender}: ${msg.text}`).join("\n");
const prompt = `
System Instructions: ${persona.systemLayer}
Task: Read the chat history below and participate in the conversation.
- If the conversation is empty, start a new topic relevant to your persona.
- If people are talking, reply naturally.
- Keep your reply short (under 2 sentences).
- Do NOT start your message with your name.
Chat History: ${historyText}
Your Reply:
`;
const result = await model.generateContent(prompt);
const response = result.response;
const text = response.text();
return text.trim();
}catch(error) {
console.log(`Bot engine error: ${error}`);
throw new Error(`While generating bot reply`);
}
};