-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
60 lines (43 loc) · 1.67 KB
/
main.js
File metadata and controls
60 lines (43 loc) · 1.67 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { generateEmbeddings, chatCompletion } from "./utils/openai.js";
import { queryVectors, upsertVectors } from "./utils/pinecone.js";
import "./style.css";
const filePath = "./documents/lectures-sm.txt";
const form = document.querySelector("form");
const input = document.querySelector("input");
const chatReply = document.querySelector("#chat-reply");
// Submit form
form.addEventListener("submit", async function (e) {
e.preventDefault();
//pass prompt details to chatReply function
main(input.value);
input.values = '';
});
async function main(prompt) {
try {
chatReply.innerHTML = "Thinking...!";
// Generate embeddings from query
const queryEmbeddings = await generateEmbeddings(prompt);
//console.log(queryEmbeddings);
// query cosine similar vector embeddings based on query
const contexts = await queryVectors(queryEmbeddings[0].values);
console.log(contexts);
//
const chatResponse = await chatCompletion(prompt, contexts, [], 0.5, 0.2); // Set temperature to 0.5, frequency penalty to 0.2
console.log(chatResponse);
chatReply.innerHTML = `<p>${reply}</p>`
} catch (error) {
console.log("Error: ", error);
chatReply.innerHTML = `<p>Sorry, some error occured! Please try again!</p>`
}
}
// write vectors into Pinecone index
async function upsert() {
try {
const embeddingsData = await generateEmbeddings(filePath);
console.log(embeddingsData);
await upsertVectors(embeddingsData);
} catch (error) {
console.log("Error: ", error);
}
}
//upsert()