Skip to content

Commit

Permalink
feat: created endpoints to fetch categories
Browse files Browse the repository at this point in the history
  • Loading branch information
sametcodes committed May 9, 2023
1 parent 3de398a commit dc6c749
Show file tree
Hide file tree
Showing 46 changed files with 1,761 additions and 4,710 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 5 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
NEXT_PUBLIC_SITE_URL = xxxxxxxxxxxxxxxxxxxxxxx
OPENAI_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
OPENAI_ORGANIZATION="xxxxxxx"
PINECONE_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
PINECONE_ENVIRONMENT="xxxxxxxxxxxx"
PINECONE_INDEX_NAME="xxxx"
3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

38 changes: 3 additions & 35 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,35 +1,3 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
node_modules
.env
static
5 changes: 1 addition & 4 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.yarn
.next
dist
node_modules
package.json
.github
public/assets
.github
29 changes: 29 additions & 0 deletions build/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.env = void 0;
var env_nextjs_1 = require("@t3-oss/env-nextjs");
var zod_1 = require("zod");
var dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
exports.env = (0, env_nextjs_1.createEnv)({
server: {
OPENAI_API_KEY: zod_1.z.string().nonempty(),
PINECONE_API_KEY: zod_1.z.string().nonempty(),
PINECONE_ENVIRONMENT: zod_1.z.string().nonempty(),
PINECONE_INDEX_NAME: zod_1.z.string().nonempty(),
OPENAI_ORGANIZATION: zod_1.z.string().nonempty(),
PORT: zod_1.z.string().nonempty().default("6006"),
},
client: {},
runtimeEnv: {
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
OPENAI_ORGANIZATION: process.env.OPENAI_ORGANIZATION,
PINECONE_API_KEY: process.env.PINECONE_API_KEY,
PINECONE_ENVIRONMENT: process.env.PINECONE_ENVIRONMENT,
PINECONE_INDEX_NAME: process.env.PINECONE_INDEX_NAME,
PORT: process.env.PORT,
}
});
25 changes: 25 additions & 0 deletions build/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var express_1 = __importDefault(require("express"));
var env_1 = require("./env");
var routes_1 = __importDefault(require("./routes"));
var app = (0, express_1.default)();
app.use(express_1.default.json());
routes_1.default.forEach(function (route) {
app[route.method].apply(app, __spreadArray(__spreadArray([route.path], route.middlewares, false), [route.handler], false));
});
app.listen(env_1.env.PORT, function () {
console.log("Server running on port ".concat(env_1.env.PORT));
});
47 changes: 47 additions & 0 deletions build/lib/openai/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.produceKeywords = exports.createEmbedding = void 0;
var env_1 = require("../../env");
var node_fetch_1 = __importDefault(require("node-fetch"));
var createEmbedding = function (_a) {
var input = _a.input;
var raw = JSON.stringify({
"model": "text-embedding-ada-002",
"input": input
});
return (0, node_fetch_1.default)("https://api.openai.com/v1/embeddings", {
method: 'POST',
headers: {
"Authorization": "Bearer ".concat(env_1.env.OPENAI_API_KEY),
"Content-Type": "application/json"
},
body: raw,
redirect: 'follow'
}).then(function (response) { return response.json(); })
.catch(function (error) { return console.log('error', error); });
};
exports.createEmbedding = createEmbedding;
var produceKeywords = function (_a) {
var input = _a.input, lang = _a.lang, model = _a.model;
var raw = JSON.stringify({
prompt: "".concat(input, "\n Extract 3-level categories from this product name FOR SHOPIFY CATEGORY TAXONOMY. Return only your output. Split levels with > character. Output language: ").concat(lang.toUpperCase()),
temperature: 0.2,
max_tokens: 100,
top_p: 1,
frequency_penalty: 0.8,
presence_penalty: 0,
});
return (0, node_fetch_1.default)("https://api.openai.com/v1/engines/".concat(model, "/completions"), {
method: 'POST',
headers: {
"Authorization": "Bearer ".concat(env_1.env.OPENAI_API_KEY),
"Content-Type": "application/json"
},
body: raw
}).then(function (response) { return response.json(); })
.catch(function (error) { return console.log('error', error); });
};
exports.produceKeywords = produceKeywords;
114 changes: 114 additions & 0 deletions build/lib/pinecone/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteNamespace = exports.createCategoryVector = exports.searchInVectors = void 0;
var env_1 = require("../../env");
var pinecone_1 = require("@pinecone-database/pinecone");
var openai_1 = require("../openai");
var pinecone = new pinecone_1.PineconeClient();
var client = pinecone.init({
environment: env_1.env.PINECONE_ENVIRONMENT,
apiKey: env_1.env.PINECONE_API_KEY
});
var index = "taxonomy";
var searchInVectors = function (namespace, vector) { return __awaiter(void 0, void 0, void 0, function () {
var pineconeIndex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client];
case 1:
_a.sent();
pineconeIndex = pinecone.Index(index);
return [2 /*return*/, pineconeIndex.query({
queryRequest: {
namespace: namespace,
topK: 10,
vector: vector,
includeMetadata: true
}
})];
}
});
}); };
exports.searchInVectors = searchInVectors;
var createCategoryVector = function (namespace, id, name) { return __awaiter(void 0, void 0, void 0, function () {
var response, upsertRequest, pineconeIndex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client];
case 1:
_a.sent();
return [4 /*yield*/, (0, openai_1.createEmbedding)({ input: name })];
case 2:
response = _a.sent();
if (!(response === null || response === void 0 ? void 0 : response.data)) {
return [2 /*return*/, null];
}
upsertRequest = {
namespace: namespace,
vectors: [{
id: String(namespace + "_" + id),
metadata: {
id: String(id),
categoryName: name
},
values: response.data[0].embedding
}],
};
pineconeIndex = pinecone.Index(index);
return [2 /*return*/, pineconeIndex.upsertRaw({ upsertRequest: upsertRequest })];
}
});
}); };
exports.createCategoryVector = createCategoryVector;
var deleteNamespace = function (namespace) { return __awaiter(void 0, void 0, void 0, function () {
var pineconeIndex;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, client];
case 1:
_a.sent();
pineconeIndex = pinecone.Index(index);
return [2 /*return*/, pineconeIndex.delete1({
namespace: namespace,
deleteAll: true,
})];
}
});
}); };
exports.deleteNamespace = deleteNamespace;
62 changes: 62 additions & 0 deletions build/lib/shopify/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCategoryTaxonomy = void 0;
var node_fetch_1 = __importDefault(require("node-fetch"));
var getCategoryTaxonomy = function (params) { return __awaiter(void 0, void 0, void 0, function () {
var _a, lang, data;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = (params || {}).lang, lang = _a === void 0 ? "en" : _a;
return [4 /*yield*/, (0, node_fetch_1.default)("https://help.shopify.com/txt/product_taxonomy/".concat(lang, ".txt"))
.then(function (response) { return response.text(); })];
case 1:
data = _b.sent();
return [2 /*return*/, data.split("\n")
.map(function (line) {
var _a = line.split(" - "), categoryId = _a[0], categoryName = _a[1];
return { categoryId: categoryId, categoryName: categoryName };
}).slice(1)];
}
});
}); };
exports.getCategoryTaxonomy = getCategoryTaxonomy;
21 changes: 21 additions & 0 deletions build/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var resolvers_1 = require("./resolvers");
var multer_1 = __importDefault(require("multer"));
var storage = multer_1.default.memoryStorage();
var upload = (0, multer_1.default)({ storage: storage });
var routes = [{
path: '/category/:platform',
method: 'get',
middlewares: [],
handler: resolvers_1.queryCategory
}, {
path: '/category/:platform',
method: 'post',
middlewares: [upload.single("file")],
handler: resolvers_1.createVectors
}];
exports.default = routes;
Loading

0 comments on commit dc6c749

Please sign in to comment.