-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat6.js
209 lines (170 loc) · 6.56 KB
/
chat6.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import pkg from "whatsapp-web.js";
import qrcode from "qrcode-terminal";
import puppeteer from "puppeteer";
import dotenv from "dotenv";
import fs from "fs/promises";
import { MemoryVectorStore } from "langchain/vectorstores/memory";
import { createStuffDocumentsChain } from "langchain/chains/combine_documents";
import { ChatPromptTemplate, MessagesPlaceholder,
} from "@langchain/core/prompts";
import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
import { ChatGoogleGenerativeAI } from "@langchain/google-genai";
import { Document } from "langchain/document";
import { GoogleGenerativeAIEmbeddings } from "@langchain/google-genai";
import { TaskType } from "@google/generative-ai";
import { createRetrievalChain } from "langchain/chains/retrieval";
import { createHistoryAwareRetriever } from "langchain/chains/history_aware_retriever";
// Load environment variables
dotenv.config();
// Set up Google service account credentials
const keyPath = "key.json";
process.env.GOOGLE_APPLICATION_CREDENTIALS = keyPath;
const { Client, LocalAuth } = pkg;
// Initialize the AI model
const model = new ChatGoogleGenerativeAI({
apiKey: process.env.API_KEY,
model: "gemini-1.5-flash",
temperature: 0.0,
});
async function initializeVectorStore() {
// Load synthetic data asynchronously
const syntheticData = await fs.readFile("synthetic_data.txt", "utf-8");
console.log("Loaded synthetic data:", syntheticData);
// Ensure syntheticData is not empty
if (!syntheticData) {
throw new Error("Synthetic data is empty");
}
const docs = new Document({
pageContent: syntheticData,
metadata: {},
id: "1",
});
console.log("Loaded docs:", docs);
// Split data into smaller chunks
const textSplitter = new RecursiveCharacterTextSplitter({
chunkSize: 1000,
chunkOverlap: 200,
separators: ["\n\n", "\n", " ", ""],
});
const documents = await textSplitter.splitDocuments([docs]);
// give each document an id
documents.forEach((doc, i) => {
doc.id = `doc-${i}`;
});
console.log("Loaded documents:", documents);
// Initialize embeddings
const embeddings = new GoogleGenerativeAIEmbeddings({
model: "text-embedding-004",
taskType: TaskType.RETRIEVAL_DOCUMENT,
title: "Document title",
apiKey: process.env.API_KEY,
});
// Create a vector store from the documents.
//const vectorStore = await HNSWLib.fromDocuments(docs, new OpenAIEmbeddings());
// Create vector store and retriever
const vectorStore = await MemoryVectorStore.fromDocuments(
documents,
embeddings
);
return vectorStore.asRetriever();
}
async function initializeRAGChain(retriever) {
// Define the system template
const systemTemplate = `
##Tentang
Kamu adalah customer service sebuah program beasiswa dari Stargan Mitra Teknologi bernama program Stargan Bisnis Digital, Inovasi, dan Kewirausahaan dengan nama Rai.
##Tugas
Tugas kamu adalah menjawab pertanyaan terkait mata kuliah. Kamu hanya menjawab dalam maksimum 1 paragraf saja dengan bahasa Indonesia yang sopan dan ramah tanpa emoticon.
##Panggilan
Selalu panggil dengan "Kak" atau "Kakak" atau "Juragan" atau "Agan" dan hindari memanggil dengan sebutan "Anda".
##Batasan
Jawab hanya yang kamu tahu saja.
Tanpa menyebutkan informasi pribadi atau data sensitif.
kamu dapat meminta pertanyaan lebih spesifik jika diperlukan. tapi jangan terlalu banyak bertanya.
Arahkan mereka untuk kontak ke [email protected] jika terdapat kendala.
##Rekomendasi
Kamu juga dapat memberikan rekomendasi mata kuliah dari data yang kamu punya jika mereka menanyakan rekomendasi yang diambil.
kamu dapat meminta pertanyaan lebih spesifik jika diperlukan. Tanyakan dulu mengenai kenginan profesi dia
kamu dapat bertanya tentang ketertarikan di bidangnya,
kamu dapat bertanya tentang batasan jumlah mata kuliah yang bisa diambil.
Kemudian cocokkan dengan data yang kamu punya. Rekomendasikan setidaknya 5 mata kuliah.
##Call to Action
Arahkan untuk segera mendaftar ke program Stargan Bisnis Digital, Inovasi, dan Kewirausahaan di starganteknologi.com dan hubungi [email protected] jika terdapat kendala.
{context}
`;
const prompt = ChatPromptTemplate.fromMessages([
["system", systemTemplate],
new MessagesPlaceholder("chat_history"),
["human", "{input}"],
]);
// Create the question-answering chain
const questionAnswerChain = await createStuffDocumentsChain({
llm: model,
prompt,
});
// Create the RAG chain
return createRetrievalChain({
retriever,
combineDocsChain: questionAnswerChain,
});
}
async function handleChat(ragChain, inputMessage) {
try {
const relevantData = await ragChain.invoke({ input: inputMessage });
console.log("Input:", inputMessage);
console.log("Relevant data:", relevantData);
if (relevantData && relevantData.answer) {
console.log("Response:", relevantData.answer);
return relevantData.answer;
}
return "Maaf, saya tidak memiliki jawaban untuk pertanyaan tersebut.";
} catch (error) {
console.error("Error in handleChat:", error);
return "Maaf, terjadi kesalahan saat memproses permintaan Anda.";
}
}
async function main() {
const retriever = await initializeVectorStore();
const ragChain = await initializeRAGChain(retriever);
const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
executablePath: puppeteer.executablePath(),
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox"],
},
});
client.on("qr", (qr) => {
console.log("Scan this QR code with your WhatsApp to log in:");
qrcode.generate(qr, { small: true });
});
client.on("authenticated", () =>
console.log("WhatsApp client authenticated!")
);
client.on("ready", () => console.log("WhatsApp client is ready!"));
client.on("message", async (msg) => {
console.log(`Message from ${msg.from}: ${msg.body}`);
console.log(msg.id);
const replyText = await (async () => {
if (msg.body === "!ping") return "pong";
if (msg.body.startsWith("!echo ")) return msg.body.slice(6);
if (msg.body.startsWith("!q ")) {
const question = msg.body.slice(3);
return await handleChat(ragChain, question);
}
return null;
})();
if (replyText) {
await msg.reply(replyText);
}
});
client.on("auth_failure", (err) => {
console.error("Authentication failure:", err);
});
client.on("disconnected", (reason) => {
console.log("WhatsApp client disconnected:", reason);
process.exit();
});
client.initialize();
}
main().catch((err) => console.error("Error in main:", err));