-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (68 loc) · 2.41 KB
/
app.js
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
require('dotenv').config();
const Groq = require("groq-sdk");
const { Bot, InlineKeyboard } = require("grammy");
const groq = new Groq({ apiKey: process.env.GROQ_API_KEY });
const bot = new Bot(process.env.TELEGRAM_BOT_TOKEN);
let userLanguageSettings = {};
bot.command('start', async (ctx) => {
const keyboard = new InlineKeyboard()
.text('🇬🇧 English', 'lang_en')
.text('🇩🇪 German', 'lang_de');
await ctx.reply('Please select the language that you want to practice:', {
reply_markup: keyboard,
});
});
bot.command('change', async (ctx) => {
const keyboard = new InlineKeyboard()
.text('🇬🇧 English', 'lang_en')
.text('🇩🇪 German', 'lang_de');
await ctx.reply('Please select the language that you want to practice:', {
reply_markup: keyboard,
});
});
bot.callbackQuery('lang_en', async (ctx) => {
await ctx.editMessageText('You have chosen to practice English.');
userLanguageSettings[ctx.from.id] = process.env.EN_PROMPT;
});
bot.callbackQuery('lang_de', async (ctx) => {
await ctx.editMessageText('Sie haben sich für die Praxis Deutsch entschieden.');
userLanguageSettings[ctx.from.id] = process.env.DE_PROMPT;
});
async function getGroqResponse(query, userId) {
const systemPrompt = userLanguageSettings[userId] || process.env.EN_PROMPT; // Varsayılan dil olarak İngilizce
try {
const completion = await groq.chat.completions.create({
"messages": [
{
"role": "system",
"content": systemPrompt
},
{
"role": "user",
"content": query
}
],
"model": "llama3-8b-8192",
"temperature": 0.85,
"max_tokens": 1140,
"top_p": 1,
"stream": true,
"stop": null
});
let finalResponse = '';
for await (const chunk of completion) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
finalResponse += content;
}
}
return finalResponse || 'Can\'t resolve it.';
} catch (error) {
console.error(error);
}
}
bot.on("message:text", async (ctx) => {
const response = await getGroqResponse(ctx.message.text, ctx.from.id);
ctx.reply(response);
});
bot.start();