Skip to content

Commit 289fe62

Browse files
author
Naresh Varma
committed
add initial files
1 parent e0876c5 commit 289fe62

12 files changed

+3594
-0
lines changed

Diff for: Photosynthesis

+1
Large diffs are not rendered by default.

Diff for: data_embeddings.json

+1,549
Large diffs are not rendered by default.

Diff for: embed.js

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const { OpenAI } = require('openai');
2+
const BluebirdPromise = require('bluebird');
3+
const request = require('request');
4+
const fs = require('fs');
5+
const { Client } = require('@elastic/elasticsearch')
6+
const client = new Client({
7+
node: 'http://localhost:9200',
8+
})
9+
require("dotenv").config();
10+
11+
const openai = new OpenAI({
12+
apiKey: process.env.OPENAI_SECRET_PAID
13+
});
14+
15+
const arr = [
16+
{
17+
"title": "The Basics of Computer Programming",
18+
"content": "Computer programming involves writing instructions (code) that a computer can interpret and execute. It requires knowledge of programming languages like Python, Java, C++, and more. Programmers use logic, algorithms, and data structures to solve problems and create software applications, games, and websites that power our digital world."
19+
},
20+
{
21+
"title": "The Human Digestive System",
22+
"content": "The human digestive system is a complex series of organs responsible for breaking down food and absorbing nutrients. It includes the mouth, esophagus, stomach, small intestine, large intestine, and various associated glands. Digestion begins in the mouth with the action of enzymes and continues through a carefully orchestrated process that ensures essential nutrients reach the body's cells."
23+
},
24+
{
25+
"title": "Renewable Energy Sources",
26+
"content": "Renewable energy sources are natural resources that can be replenished over time. They include solar energy, wind power, hydropower, geothermal energy, and biomass. Unlike fossil fuels, which are finite, renewable energy sources offer sustainable and environmentally friendly alternatives for meeting our energy needs."
27+
},
28+
{
29+
"title": "The Water Cycle",
30+
"content": "The water cycle, also known as the hydrologic cycle, is the continuous movement of water on, above, and below the surface of the Earth. It involves processes such as evaporation, condensation, precipitation, and runoff. This cycle regulates the distribution of water across the planet, sustaining life and maintaining ecological balance."
31+
},
32+
{
33+
"title": "Photosynthesis in Plants",
34+
"content": "Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy into chemical energy in the form of glucose. This vital process occurs in chloroplasts, specialized organelles containing the pigment chlorophyll. Through photosynthesis, plants play a crucial role in the Earth's ecosystem by producing oxygen and providing a source of food for various organisms."
35+
}
36+
]
37+
38+
async function getEmbeddings(text) {
39+
const embedding = await openai.embeddings.create({
40+
model: "text-embedding-ada-002",
41+
input: text,
42+
});
43+
return embedding;
44+
}
45+
46+
const makeBulkRequestToEls = (data) => new Promise((resolve, reject) => {
47+
const indexName = 'test_embeedings';
48+
client.bulk({
49+
body: data.flatMap(doc => [{ index: { _index: 'test_embeedings' } }, doc])
50+
}).then((body) => {
51+
console.log('Response :', body);
52+
return resolve();
53+
}).catch((err) => {
54+
console.error('getting error while posting data to elastic search :', err);
55+
return reject(err);
56+
});
57+
58+
// request.post({
59+
// url: `http://localhost:9200/${indexName}/_bulk`,
60+
// json: true,
61+
// body: JSON.stringify().join(),
62+
// }, (err, res, body) => {
63+
64+
// });
65+
});
66+
67+
const main = () => new Promise((resolve, reject) => {
68+
BluebirdPromise.mapSeries(arr, (data) => new Promise((resolve, reject) => {
69+
getEmbeddings(data.content)
70+
.then((embedRes) => {
71+
console.log(`Recevied embeedings for ${data.title}`);
72+
console.log(`embeedings length ${embedRes?.data[0]?.embedding.length}`);
73+
if (embedRes) {
74+
data['title-vector'] = embedRes?.data[0]?.embedding;
75+
}
76+
return resolve();
77+
});
78+
}))
79+
.then(() => {
80+
console.log('completed');
81+
return resolve(makeBulkRequestToEls(arr));
82+
})
83+
.catch(err => console.error(err));
84+
})
85+
86+
main();
87+
88+
// console.log(client.bulk);

Diff for: getKnnData.js

+34
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: index.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const OpenAI = require('openai').OpenAI;
2+
require("dotenv").config();
3+
4+
const openai = new OpenAI({
5+
apiKey: process.env.OPENAI_SECRET_PAID
6+
});
7+
8+
const getKeywords = [
9+
{
10+
role: "system",
11+
content: "You will be provided with a block of text, and your task is to extract a list of keywords from it."
12+
},
13+
{
14+
role: "user",
15+
content: ''
16+
}];
17+
18+
const getAnswer = [{
19+
role: "system",
20+
content: "You are my helpful agent, I will give you the content and you have to understand the content and answer my question from the content, Keep the response very short and strightforward\n\ncontent: \"Photosynthesis is the process by which green plants, algae, and some bacteria convert light energy into chemical energy in the form of glucose. This vital process occurs in chloroplasts, specialized organelles containing the pigment chlorophyll. Through photosynthesis, plants play a crucial role in the Earth's ecosystem by producing oxygen and providing a source of food for various organisms. \"\n\n"
21+
},
22+
{
23+
role: "user",
24+
content: ''
25+
}];
26+
27+
export async function main(queryType, query) {
28+
const message = queryType === 'keywords' ? getKeywords : getAnswer;
29+
message[1].content = query;
30+
const completion = await openai.chat.completions.create({
31+
messages: message,
32+
model: "gpt-3.5-turbo",
33+
temperature: 0.5,
34+
max_tokens: 256,
35+
top_p: 1,
36+
});
37+
console.log(completion.choices[0].message);
38+
return completion.choices[0].message.content;
39+
}
40+
41+
// main('khed', 'what is photosythasis');

Diff for: notes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
API key
2+
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
3+
https://platform.openai.com/docs/api-reference/embeddings?lang=node.js

0 commit comments

Comments
 (0)