-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·94 lines (68 loc) · 3.85 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
// ####################################################
// Package Initialization
const TelegramBot = require('node-telegram-bot-api');
const { exec } = require('child_process');
require('dotenv').config()
// ####################################################
// Variable Declarations
const TelegramToken = `${process.env.BOTTOKEN}`;
const ElasticAPIKey = `${process.env.ELKAPIKEY}`;
const ChatGPTKey = `${process.env.VTIGPTKEY}`;
const ElasticURL = `${process.env.ELKURL}`;
const ElasticUser = `${process.env.ELKUSER}`;
const ElasticPassword = `${process.env.ELKPASS}`;
const indexName = `${process.env.INDEXNAME}`;
const modelType = `${process.env.MODELTYPE}`;
// ChatGPT API URL
const gptURL = 'https://api.openai.com/v1/chat/completions';
// Telegram Bot Init
const bot = new TelegramBot(TelegramToken, { polling: true });
// Telegram Start Listening on New Message
bot.on('message', async (msg) => {
const chatId = msg.chat.id;
// Remove all return char
const messageText = msg.text.replace(/[^a-zA-Z ]/g, "");
let fullQuery;
let queryBody;
let promptCommand;
if(modelType == "elser"){
queryBody = `"query": { "nested": { "path": "passages", "query": { "text_expansion": { "passages.vector.predicted_value": { "model_id": ".elser_model_2_linux-x86_64", "model_text": "${messageText}" } } }, "inner_hits": { "_source": "false", "fields": [ "passages.text" ]}}}`;
promptCommand = `You are a helpful AI assistant who answers questions using the following supplied context. If you can't answer the question using this context say "I don't know"`;
}else{
queryBody = `"knn": { "field": "passages.vector.predicted_value", "query_vector_builder": { "text_embedding": { "model_id": ".multilingual-e5-small_linux-x86_64", "model_text": "${messageText}" }}, "k": 2, "num_candidates": 5}`;
promptCommand = `Kamu adalah AI Assisten yang hanya menjawab pertanyaan berdasarkan konteks berikut ini. Jika kamu tidak bisa menjawab pertanyaannya menggunakan konteks ini, cukup jawab "Saya tidak tahu"`;
}
fullQuery = `curl -s --location --request POST '${ElasticURL}/${indexName}/_search' -k -u ${ElasticUser}:${ElasticPassword} --header 'Authorization: apiKey ${ElasticAPIKey}' --header 'Content-Type: application/json' -d '{${queryBody}}'`;
// console.log(fullQuery);
// Send search request to Elastic App Search based on received message from Telegram
exec(`${fullQuery}`, (error, stdout, stderr) => {
// Prompt text for ChatGPT
// let prompt = `
// You are a helpful assistant.
// Answer the following question: ${messageText}.
// by using the following text: ${JSON.parse(stdout).hits.hits[0]._source.content}.
// Do not search for answer outside of the given text
// `;
// console.log(JSON.parse(stdout).hits.hits[0]);
// console.log(stdout, error, stderr);
let prompt = `
${promptCommand}
Context: "${JSON.parse(stdout).hits.hits[0]._source.passages[0].text}"
Question: "${messageText}"
`;
// console.log(`${JSON.parse(stdout).hits.hits[0]._source.passages[0].text}`);
// jawab menggunakan teks berikut: ${JSON.parse(stdout).hits.hits[0]._source.content}.
// Remove all double quotes from prompt
prompt = JSON.stringify(prompt.replace(/['"]+/g, ''));
// Send request to ChatGPT
exec(`curl -s ${gptURL} -H "Authorization: Bearer ${ChatGPTKey}" -H "Content-Type: application/json" -d '{ "model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": ${prompt}}] }'`, (error2, stdout2, stderr2) => {
// Convert the response to JSON object
let gptResponse = JSON.parse(stdout2);
console.log(gptResponse);
// Get only the response message
let chatReply = gptResponse.choices[0].message.content;
// Reply Telegram message with the response from ChatGPT
bot.sendMessage(chatId, ''+chatReply);
});
});
});