Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modulerization #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 30 additions & 96 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ dotenv.config();
import express from "express";
import multer from "multer";
import cors from "cors";
import OpenAI from "openai";
import { v4 as uuidv4 } from "uuid";
import { uploadToS3 } from "./uploads3.js";
import { PORT, PROMPT } from "./constant.js";
import { client } from "./MongoDbsetup.js";
import { openAiVisionApi } from "./openAIVisionAPi.js";
import { openAiEmbeddings } from "./openAiEmbeddingsApi.js";
import { vectorQuerySchema } from "./vectorQuerySchema.js";
const corsOptions = {
origin: "http://localhost:3000",
};
const app = express();
const upload = multer(); // Using multer's default memory storage
app.use(cors(corsOptions)); // This will enable all CORS requests. For production, configure this properly.
const openai = new OpenAI({
organization: process.env.OPENAI_ORGANISATION,
apiKey: process.env.OPENAI_API_KEY,
});
// Create a MongoClient with a MongoClientOptions object to set the Stable API version
app.post("/upload", upload.array("images"), async (req, res) => {
if (!req.files || (Array.isArray(req.files) && req.files.length === 0)) {
Expand All @@ -28,77 +26,36 @@ app.post("/upload", upload.array("images"), async (req, res) => {
try {
const results = await Promise.all(uploadPromises);
const allPromise = results.map(async (url) => {
const response = await openai.chat.completions.create({
model: "gpt-4-vision-preview",
temperature: 0,
messages: [
{
role: "system",
content: "the system only speaks in JSON. Do not generate output that isn’t in properly formatted JSON.",
},
{
role: "user",
content: [
{
type: "text",
text: PROMPT,
},
{
type: "image_url",
image_url: {
url: url,
},
},
],
},
],
});
const response = await openAiVisionApi(url, PROMPT);
const stringArray = response.choices[0].message.content.split("json");
const jsonString = stringArray.join("");
const trimmedString = jsonString.replace(/`|\n/g, "");
const jsonData = JSON.parse(trimmedString);
return { jsonData, url, id: uuidv4() };
});
const data = await Promise.all(allPromise)
.then((results) => {
// Process API call results
return results;
})
.catch((error) => {
console.error("Error:", error);
});
const data = await Promise.all(allPromise);
if (data) {
try {
await client.connect();
console.log("Connected successfully to MongoDB");
const database = client.db("Cluster0"); // Replace with your database name
const collection = database.collection("image_metadata");
const allPromises = data.map(async (item) => {
const data = await openai.embeddings.create({
model: "text-embedding-3-small",
input: item.jsonData.description,
encoding_format: "float",
});
const document = {
subjects: item.jsonData.subjects,
attributes: item.jsonData.attributes,
themes: item.jsonData.themes,
contexts: item.jsonData.contexts,
description: item.jsonData.description,
image_url: item.url,
id: item.id,
embeddings: data.data[0].embedding,
};
console.log(document.description);
return collection.insertOne(document);
});
const insertedRecords = await Promise.all(allPromises);
console.log("records inserted");
res.status(200).send("data inserted");
}
catch (error) {
console.log("Error", error);
}
await client.connect();
console.log("Connected successfully to MongoDB");
const database = client.db("Cluster0"); // Replace with your database name
const collection = database.collection("image_metadata");
const allPromises = data.map(async (item) => {
const data = await openAiEmbeddings(item.jsonData.description);
const document = {
subjects: item.jsonData.subjects,
attributes: item.jsonData.attributes,
themes: item.jsonData.themes,
contexts: item.jsonData.contexts,
description: item.jsonData.description,
image_url: item.url,
id: item.id,
embeddings: data.data[0].embedding,
};
return collection.insertOne(document);
});
const insertedRecords = await Promise.all(allPromises);
console.log("records inserted");
res.status(200).send("data inserted");
}
}
catch (error) {
Expand All @@ -113,34 +70,11 @@ app.get("/search", async (req, res) => {
const database = client.db("Cluster0"); // Replace with your database name
const collection = database.collection("image_metadata");
const queryString = req.query.query;
const queryVector = await openai.embeddings.create({
model: "text-embedding-3-small",
input: queryString,
encoding_format: "float",
});
const queryVector = await openAiEmbeddings(queryString);
const query = queryVector.data[0].embedding;
const agg = [
{
$vectorSearch: {
index: "PlotSemanticSearch",
path: "embeddings",
queryVector: query,
numCandidates: 100,
limit: 100,
},
},
{
$project: {
_id: 0,
image_url: 1,
description: 1,
score: {
$meta: "vectorSearchScore",
},
},
},
];
const result = await collection.aggregate(agg).toArray();
const result = await collection
.aggregate(vectorQuerySchema(query))
.toArray();
const scoreThreshold = 65;
res.status(200).send({
data: result
Expand Down
1 change: 1 addition & 0 deletions dist/openAIVisionAPi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const openAiVisionApi: (url: string, prompt: string) => Promise<import("openai/resources").ChatCompletion>;
28 changes: 28 additions & 0 deletions dist/openAIVisionAPi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import openai from "./openAiSetup";
export const openAiVisionApi = async (url, prompt) => {
return await openai.chat.completions.create({
model: "gpt-4-vision-preview",
temperature: 0,
messages: [
{
role: "system",
content: "the system only speaks in JSON. Do not generate output that isn’t in properly formatted JSON.",
},
{
role: "user",
content: [
{
type: "text",
text: prompt,
},
{
type: "image_url",
image_url: {
url: url,
},
},
],
},
],
});
};
1 change: 1 addition & 0 deletions dist/openAiEmbeddingsApi.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export declare const openAiEmbeddings: (text: string) => Promise<import("openai/resources").CreateEmbeddingResponse>;
8 changes: 8 additions & 0 deletions dist/openAiEmbeddingsApi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import openai from "./openAiSetup";
export const openAiEmbeddings = async (text) => {
return await openai.embeddings.create({
model: "text-embedding-3-small",
input: text,
encoding_format: "float",
});
};
3 changes: 3 additions & 0 deletions dist/openAiSetup.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import OpenAI from "openai";
declare const openai: OpenAI;
export default openai;
6 changes: 6 additions & 0 deletions dist/openAiSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import OpenAI from "openai";
const openai = new OpenAI({
organization: process.env.OPENAI_ORGANISATION,
apiKey: process.env.OPENAI_API_KEY,
});
export default openai;
20 changes: 20 additions & 0 deletions dist/vectorQuerySchema.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export declare const vectorQuerySchema: (query: number[]) => ({
$vectorSearch: {
index: string;
path: string;
queryVector: number[];
numCandidates: number;
limit: number;
};
$project?: undefined;
} | {
$project: {
_id: number;
image_url: number;
description: number;
score: {
$meta: string;
};
};
$vectorSearch?: undefined;
})[];
24 changes: 24 additions & 0 deletions dist/vectorQuerySchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export const vectorQuerySchema = (query) => {
const agg = [
{
$vectorSearch: {
index: "PlotSemanticSearch",
path: "embeddings",
queryVector: query,
numCandidates: 100,
limit: 100,
},
},
{
$project: {
_id: 0,
image_url: 1,
description: 1,
score: {
$meta: "vectorSearchScore",
},
},
},
];
return agg;
};
Loading