-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
182 lines (123 loc) · 4.74 KB
/
index.js
File metadata and controls
182 lines (123 loc) · 4.74 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
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
require('dotenv').config();
require('fs')
const { Configuration, OpenAIApi } = require("openai");
const { Client, GatewayIntentBits, SlashCommandSubcommandBuilder } = require('discord.js');
const { readFileSync, fstat, writeFileSync } = require('fs');
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const client = new Client({ intents: [GatewayIntentBits.Guilds,GatewayIntentBits.GuildMessages,GatewayIntentBits.MessageContent] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
const prefix = `You are a Q&A bot named "Sup Port" and answer questions regarding the Minecraft Mod "Valkyrien Skies 2" which is about building Airships. The official Valkyrien Skies Website is "https://www.valkyrienskies.org/". The download website for "Valkyrien Skies 2" is "https://www.curseforge.com/minecraft/mc-mods/valkyrien-skies". This is only the download for Valkyrien Skies 2, not for Clockwork or Takeoff or similar. The wiki Website is "https://wiki.valkyrienskies.org/wiki/Main_Page" and the faq website is "https://wiki.valkyrienskies.org/wiki/FAQ".
Answer the question as truthfully as possible using the provided text, and if the answer is not contained within the text below, say "I don't know". Do not include unnecessary details
Context: Make sure you are using the latest version of Valkyrien Skies 2 and of your mod loader. \n\r`
client.on('messageCreate', async msg => {
console.log(msg.channel.parentId)
if (msg.channel.parentId == "1071387850022584320" && msg.author.bot == false) {
const ConJSON = JSON.parse(readFileSync("Context.json"))
const Ordered = await order_document_sections_by_query_similarity(msg.content, Embeds)
console.log(Ordered)
let Context = ""
for (x in Ordered) {
const EmbOrdered = Ordered[x][1]
const Points = Ordered[x][0]
if (Points < 0.26) {
break
}
for (x of ConJSON) {
if (x.Header == EmbOrdered) {
Context+=x.Content+"\n\r"
break
}
}
}
if (Context != "") {
await msg.channel.sendTyping()
console.log(msg.content)
const pref = prefix + Context + "\r\nQuestion: "+msg.channel.name+". " +msg.content +"\r\nAnswer:"
console.log(pref)
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: pref,
temperature: 0.05,
max_tokens: 300
});
msg.reply(completion.data.choices[0].text)
}
}
});
MODEL_NAME = "davinci"
DOC_EMBEDDINGS_MODEL = "text-search-"+MODEL_NAME+"-doc-001"
QUERY_EMBEDDINGS_MODEL = "text-search-"+MODEL_NAME+"-query-001"
async function get_embedding(text, model){
const result = await openai.createEmbedding({
"model": model,
"input": text
})
return result["data"]["data"][0]["embedding"]
}
function get_doc_embedding(text){
return get_embedding(text, DOC_EMBEDDINGS_MODEL)
}
function get_query_embedding(text){
return get_embedding(text, QUERY_EMBEDDINGS_MODEL)
}
async function compute_doc_embeddings(df) {
let Dic = {}
for(x in df) {
Dic[x] = await get_doc_embedding(df[x]["Content"])
}
return Dic
}
function load_embeddings(EmbeddingPath) {
df = JSON.parse(readFileSync(EmbeddingPath))
let Dic = {}
for (x in df) {
const object = df[x]
Dic[ object["Header"] ] = object["Vectors"]
}
return Dic
}
async function create_embeddings(df) {
const docEmb = await compute_doc_embeddings(df)
Final = []
for (x in docEmb) {
Final[x] = {
"Header":df[x]["Header"],
"Vectors":docEmb[x]
}
}
return Final
}
function vector_similarity(x, y) {
return x.map((a, i) => x[i] * y[i]).reduce((m, n) => m + n);
}
async function order_document_sections_by_query_similarity(query, contexts){
const query_embedding = await get_query_embedding(query)
let document_similarities = []
for (const [key, value] of Object.entries(contexts)) {
document_similarities.push( [vector_similarity(query_embedding, value),key] )
}
document_similarities.sort(function(a,b) {
return b[0]-a[0]
})
/*sorted([
(vector_similarity(query_embedding, doc_embedding), doc_index) for doc_index, doc_embedding in contexts.items()
], reverse=True)
*/
return document_similarities
}
const data = JSON.parse(readFileSync("Context.json"))
const Embeds = load_embeddings("ContextEmbedding.json")
// Creates a "ContextEmbedding.json" file"
/*
create_embeddings(data).then(Embs=>{
Jfile = JSON.stringify(Embs)
writeFileSync("ContextEmbedding.json",Jfile)
console.log("done")
})
*/
client.login(process.env.DISCORD_TOKEN);