Skip to content

Commit 1a162a4

Browse files
committed
feat: generate embeddings
1 parent 01bf584 commit 1a162a4

10 files changed

+286
-2
lines changed

.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
PORT=6969
33

44
# OPENAI
5-
# OPENAI_API_KEY=
5+
OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
66

77
# DB (Postgres)
88
DB_CONNECTION_URL=<YOUR_POSTGRES_DB_CONNECTION_URL>

app.js

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import express from "express";
22
import * as dotenv from 'dotenv'
33

44
import { DB } from "./db.js";
5+
import indexRouter from "./routes.js";
56
dotenv.config()
67

78
const app = express();
@@ -10,6 +11,8 @@ const PORT = process.env.PORT || 6969;
1011
app.use(express.json({ limit: '50mb' }));
1112
app.use(express.urlencoded({ extended: true }))
1213

14+
app.use('/api', indexRouter);
15+
1316
app.listen(PORT, async () => {
1417
try {
1518
await DB.authenticate()

controllers/insertContent.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Document } from "../models/document.js";
2+
import { openAI } from "../openai.js";
3+
4+
export const generateEmbeddingsController = async (req, res) => {
5+
try {
6+
const { content } = req?.body; // You can pass the embeddings manually in request body if you don't want to use openai
7+
8+
// Generate embeddings of given text content using openAI's text-embedding-ada-002 embedding model
9+
const embeddingResponse = await openAI.embeddings.create({
10+
input: content,
11+
model: 'text-embedding-ada-002'
12+
});
13+
14+
// Save embeddings in DB
15+
const document = await Document.create({
16+
content,
17+
embeddings: embeddingResponse.data[0].embedding
18+
}, { raw: true });
19+
20+
return res.json({
21+
message: 'Embeddings generate successfully',
22+
data: document
23+
});
24+
} catch (error) {
25+
console.log("🚀 ~ insertContentController ~ error:", error)
26+
return res.status(500).json({ error })
27+
}
28+
}

controllers/vectorSearch.js

Whitespace-only changes.

models/document.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DataTypes } from "sequelize";
2+
import { DB } from "../db.js";
3+
4+
export const Document = DB.define('document', {
5+
content: {
6+
type: DataTypes.STRING
7+
},
8+
embeddings: {
9+
type: DataTypes.JSONB
10+
}
11+
}, {
12+
timestamps: false
13+
})

openai.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { OpenAI } from 'openai'
2+
import * as dotenv from 'dotenv'
3+
dotenv.config();
4+
5+
export const openAI = new OpenAI({
6+
apiKey: process.env.OPENAI_API_KEY
7+
})

0 commit comments

Comments
 (0)