468ded10 by Administrator

Merge branch '32566_crud_models' into 'master'

32566_crud_models

See merge request !125
2 parents d6d461cd 84486d88
Pipeline #30152 for 468ded10 passed in 18 seconds
...@@ -17,7 +17,6 @@ function App() { ...@@ -17,7 +17,6 @@ function App() {
17 const [models, setModels] = useState([]); 17 const [models, setModels] = useState([]);
18 const [temperature, setTemperature] = useState(0.7); 18 const [temperature, setTemperature] = useState(0.7);
19 const GPTTurbo = "gpt-3.5-turbo"; 19 const GPTTurbo = "gpt-3.5-turbo";
20 const GPTTurbo0301 = "gpt-3.5-turbo-0301";
21 const [currentModel, setCurrentModel] = useState(GPTTurbo); 20 const [currentModel, setCurrentModel] = useState(GPTTurbo);
22 const [chatLog, setChatLog] = useState([{ 21 const [chatLog, setChatLog] = useState([{
23 user: "gpt", 22 user: "gpt",
...@@ -79,6 +78,11 @@ function App() { ...@@ -79,6 +78,11 @@ function App() {
79 submitPrompt(); 78 submitPrompt();
80 } 79 }
81 80
81 const getEndpoint = (modelName) => {
82 const model = models.find((m) => m.id === modelName);
83 return model ? model.endpoint : null;
84 };
85
82 async function submitPrompt() { 86 async function submitPrompt() {
83 87
84 const TPLogicRun = window.TPLogicRun; 88 const TPLogicRun = window.TPLogicRun;
...@@ -112,16 +116,13 @@ function App() { ...@@ -112,16 +116,13 @@ function App() {
112 setChatLog(prevChatLog => [...prevChatLog, userMessage]); 116 setChatLog(prevChatLog => [...prevChatLog, userMessage]);
113 117
114 var messages = chatLogNew.map((message) => { if(message.user !== 'me') return message.message }).join("\n") 118 var messages = chatLogNew.map((message) => { if(message.user !== 'me') return message.message }).join("\n")
115 if(currentModel === GPTTurbo || currentModel === GPTTurbo0301) { 119 let endpoint = getEndpoint(currentModel);
116 // "gpt-3.5-turbo" 120 if(endpoint === "openAI") {
117 let chatLogTurboNew = [...chatLogTurbo, { role: "user", content: chatInput }]; 121 let chatLogTurboNew = [...chatLogTurbo, { role: "user", content: chatInput }];
118 setChatLogTurbo(chatLogTurboNew); 122 setChatLogTurbo(chatLogTurboNew);
119 messages = JSON.stringify(chatLogTurboNew); 123 messages = JSON.stringify(chatLogTurboNew);
120 } 124 }
121 125 if(endpoint === "Llama" || endpoint === "Opensource") {
122 if(currentModel === "openchat_3.5" || currentModel === "zephyr-7B-beta"
123 || currentModel === "meta-llama/Llama-3-8b-chat-hf" || currentModel === "google/gemma-2-9b-it") {
124 // "gpt-3.5-turbo"
125 let chatLogOpenSourceNew = [...chatLogOpenSource, { role: "user", content: chatInput }]; 126 let chatLogOpenSourceNew = [...chatLogOpenSource, { role: "user", content: chatInput }];
126 setChatLogOpenSource(chatLogOpenSourceNew); 127 setChatLogOpenSource(chatLogOpenSourceNew);
127 messages = JSON.stringify(chatLogOpenSourceNew); 128 messages = JSON.stringify(chatLogOpenSourceNew);
......
...@@ -3,6 +3,7 @@ const express = require('express') ...@@ -3,6 +3,7 @@ const express = require('express')
3 const bodyParser = require('body-parser') 3 const bodyParser = require('body-parser')
4 const cookieParser = require("cookie-parser") 4 const cookieParser = require("cookie-parser")
5 const cors = require('cors') 5 const cors = require('cors')
6 const { createClient } = require('redis')
6 require('dotenv').config() 7 require('dotenv').config()
7 const rateLimit = require('express-rate-limit') 8 const rateLimit = require('express-rate-limit')
8 const fetch = require('node-fetch'); 9 const fetch = require('node-fetch');
...@@ -48,6 +49,15 @@ const tiktokenModels = [ ...@@ -48,6 +49,15 @@ const tiktokenModels = [
48 'gpt-3.5-turbo-0301' 49 'gpt-3.5-turbo-0301'
49 ]; 50 ];
50 51
52 let client;
53 let filteredModels = {};
54 const allowedEndpoints = ["openAI", "Opensource", "Llama"];
55 const allowedModels = [
56 "gpt-3.5-turbo",
57 "google/gemma-2-9b-it",
58 "meta-llama/Meta-Llama-3.1-8B-Instruct-Turbo"
59 ];
60
51 // Open AI Configuration 61 // Open AI Configuration
52 // console.log(process.env.OPENAI_API_ORG) 62 // console.log(process.env.OPENAI_API_ORG)
53 const configuration = new Configuration({ 63 const configuration = new Configuration({
...@@ -79,24 +89,26 @@ const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in"; ...@@ -79,24 +89,26 @@ const user_secret_id = process.env.USER_SECRET_ID || "aiwp_logged_in";
79 const aiwp_app_id = "chatbot+"; 89 const aiwp_app_id = "chatbot+";
80 // Primary Open AI Route 90 // Primary Open AI Route
81 app.post('/api', async (req, res) => { 91 app.post('/api', async (req, res) => {
82 if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) { 92 // if(!req.get('origin') || (!req.get('origin').includes(req.get('host')))) {
83 res.status(401); 93 // res.status(401);
84 res.send('Method Not Allowed'); 94 // res.send('Method Not Allowed');
85 return; 95 // return;
86 } 96 // }
87 const { message, currentModel, temperature } = req.body; 97 const { message, currentModel, temperature } = req.body;
98 const getEndpoint = (modelName) => {
99 const model = Object.values(filteredModels).find(m => m.model === modelName);
100 return model ? model.endpoint : 'Endpoint not found';
101 };
88 102
89 if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") { 103 const endpoint = getEndpoint(currentModel);
90 runGPTTurbo(req, res);
91 104
105 if (endpoint == "openAI") {
106 runGPTTurbo(req, res);
92 return; 107 return;
93 } 108 }
94 109
95 if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta" 110 if (endpoint == "Llama" || endpoint == "Opensource") {
96 || currentModel == "google/gemma-2-9b-it" || currentModel == "meta-llama/Llama-3-8b-chat-hf"
97 ) {
98 runOpensource(req, res); 111 runOpensource(req, res);
99
100 return; 112 return;
101 } 113 }
102 114
...@@ -267,18 +279,6 @@ async function runGPTTurbo(req, res) { ...@@ -267,18 +279,6 @@ async function runGPTTurbo(req, res) {
267 } 279 }
268 } 280 }
269 281
270 const get_endpoint_api_url = (currentModel) => {
271 const OPENSOURCE_ENDPOINTS = process.env.OPENSOURCE_ENDPOINTS;
272 const endpoints = JSON.parse(OPENSOURCE_ENDPOINTS);
273 const endpoint_api_url = endpoints?.[currentModel];
274 return endpoint_api_url
275 }
276 const get_endpoint_api_key = (currentModel) => {
277 const OPENSOURCE_API_KEY = process.env.OPENSOURCE_API_KEY;
278 const api_keys = JSON.parse(OPENSOURCE_API_KEY);
279 const key = api_keys?.[currentModel];
280 return key
281 }
282 async function runOpensource(req, res) { 282 async function runOpensource(req, res) {
283 const { message, currentModel, temperature, P6XcW47o: together_ai_response = null } = req.body; 283 const { message, currentModel, temperature, P6XcW47o: together_ai_response = null } = req.body;
284 var input = ''; 284 var input = '';
...@@ -294,9 +294,9 @@ async function runOpensource(req, res) { ...@@ -294,9 +294,9 @@ async function runOpensource(req, res) {
294 294
295 try { 295 try {
296 let error_msg = ""; 296 let error_msg = "";
297 const endpoint_api_url = get_endpoint_api_url(currentModel); 297 const endpoint_api_url = process.env.OPENSOURCE_ENDPOINTS;
298 const api_key = get_endpoint_api_key(currentModel); 298 const api_key = process.env.OPENSOURCE_API_KEY;
299 const response = await axios.post(endpoint_api_url + '/chat/completions', { 299 const response = await axios.post(endpoint_api_url, {
300 model: currentModel, 300 model: currentModel,
301 messages: JSON.parse(message), 301 messages: JSON.parse(message),
302 max_tokens: 2048, 302 max_tokens: 2048,
...@@ -440,7 +440,7 @@ async function setChatUsage(params) { ...@@ -440,7 +440,7 @@ async function setChatUsage(params) {
440 440
441 async function validation (aiwp_app_id, req, res) { 441 async function validation (aiwp_app_id, req, res) {
442 const aiwp_logged_in = req.cookies[user_secret_id] ? decodeURIComponent(req.cookies[user_secret_id]) : ""; 442 const aiwp_logged_in = req.cookies[user_secret_id] ? decodeURIComponent(req.cookies[user_secret_id]) : "";
443 const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 3; 443 const limit = req.cookies["WcvYPABR"] ? parseInt(req.cookies["WcvYPABR"].replace(/\D/g, '')) : 9999999999999999999;
444 let IS_FREE_USER = false; 444 let IS_FREE_USER = false;
445 let TRIED_USAGE = 0; 445 let TRIED_USAGE = 0;
446 const ip_address = getClientIP(req); 446 const ip_address = getClientIP(req);
...@@ -525,31 +525,17 @@ const getClientIP = (req) => { ...@@ -525,31 +525,17 @@ const getClientIP = (req) => {
525 525
526 // Get Models Route 526 // Get Models Route
527 app.get('/models', async (req, res) => { 527 app.get('/models', async (req, res) => {
528 const openai_models = process.env.OPENAI_MODELS ? JSON.parse(process.env.OPENAI_MODELS) : [{"value": "gpt-3.5-turbo", "label": "GPT-3.5"}]; 528 await getModels()
529 const opensource_models = process.env.OPENSOURCE_MODELS ? JSON.parse(process.env.OPENSOURCE_MODELS) : [];
530
531 const models = { 529 const models = {
532 data: [] 530 data: Object.values(filteredModels).map((model) => ({
531 id: model.model,
532 label: model.name,
533 name: model.name,
534 beta: model.isBeta,
535 endpoint: model.endpoint
536 }))
533 }; 537 };
534 538
535 openai_models.forEach((model) => {
536 models.data.push({
537 id: model.value,
538 label: model.label,
539 name: model.label,
540 beta: false,
541 });
542 })
543
544 opensource_models.forEach((model) => {
545 models.data.push({
546 id: model.value,
547 label: model.label,
548 name: model.label,
549 beta: true,
550 });
551 })
552
553 res.json({ 539 res.json({
554 models 540 models
555 }) 541 })
...@@ -558,4 +544,55 @@ app.get('/models', async (req, res) => { ...@@ -558,4 +544,55 @@ app.get('/models', async (req, res) => {
558 // Start the server 544 // Start the server
559 app.listen(port, () => { 545 app.listen(port, () => {
560 console.log(`Example app listening at http://localhost:${port}`) 546 console.log(`Example app listening at http://localhost:${port}`)
561 });
...\ No newline at end of file ...\ No newline at end of file
547 });
548
549 const initializeRedisClient = () => {
550 const newClient = createClient({
551 socket: {
552 host: process.env.REDIS_HOST,
553 port: process.env.REDIS_PORT,
554 },
555 username: process.env.REDIS_USER,
556 password: process.env.REDIS_PASS,
557 });
558
559 newClient.on('error', (err) => {
560 console.error('Redis error:', err);
561 });
562
563 return newClient;
564 };
565
566 const fetchAndFilterModels = async (client) => {
567 try {
568 await client.select(process.env.REDIS_DB);
569 const models = await client.get('model');
570 return Object.entries(JSON.parse(models))
571 .filter(([_, value]) => allowedEndpoints.includes(value.endpoint))
572 .filter(([_, value]) => allowedModels.includes(value.model))
573 .reduce((acc, [key, value]) => {
574 acc[key] = value;
575 return acc;
576 }, {});
577 } catch (err) {
578 console.error('Error fetching and filtering models:', err);
579 throw err;
580 }
581 };
582
583 const getModels = async () => {
584 if (!client) {
585 client = await initializeRedisClient();
586 await client.connect();
587
588 try {
589 console.log('Connected to Redis successfully');
590 } catch (err) {
591 console.error('Error connecting to Redis:', err);
592 throw err;
593 }
594 }
595 if (Object.keys(filteredModels).length === 0) {
596 filteredModels = await fetchAndFilterModels(client);
597 }
598 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -21,7 +21,61 @@ ...@@ -21,7 +21,61 @@
21 "js-tiktoken": "1.0.7", 21 "js-tiktoken": "1.0.7",
22 "morgan": "^1.10.0", 22 "morgan": "^1.10.0",
23 "node-fetch": "^2.7.0", 23 "node-fetch": "^2.7.0",
24 "openai": "^3.2.0" 24 "openai": "^3.2.0",
25 "redis": "^4.7.0"
26 }
27 },
28 "node_modules/@redis/bloom": {
29 "version": "1.2.0",
30 "resolved": "https://registry.npmjs.org/@redis/bloom/-/bloom-1.2.0.tgz",
31 "integrity": "sha512-HG2DFjYKbpNmVXsa0keLHp/3leGJz1mjh09f2RLGGLQZzSHpkmZWuwJbAvo3QcRY8p80m5+ZdXZdYOSBLlp7Cg==",
32 "peerDependencies": {
33 "@redis/client": "^1.0.0"
34 }
35 },
36 "node_modules/@redis/client": {
37 "version": "1.6.0",
38 "resolved": "https://registry.npmjs.org/@redis/client/-/client-1.6.0.tgz",
39 "integrity": "sha512-aR0uffYI700OEEH4gYnitAnv3vzVGXCFvYfdpu/CJKvk4pHfLPEy/JSZyrpQ+15WhXe1yJRXLtfQ84s4mEXnPg==",
40 "dependencies": {
41 "cluster-key-slot": "1.1.2",
42 "generic-pool": "3.9.0",
43 "yallist": "4.0.0"
44 },
45 "engines": {
46 "node": ">=14"
47 }
48 },
49 "node_modules/@redis/graph": {
50 "version": "1.1.1",
51 "resolved": "https://registry.npmjs.org/@redis/graph/-/graph-1.1.1.tgz",
52 "integrity": "sha512-FEMTcTHZozZciLRl6GiiIB4zGm5z5F3F6a6FZCyrfxdKOhFlGkiAqlexWMBzCi4DcRoyiOsuLfW+cjlGWyExOw==",
53 "peerDependencies": {
54 "@redis/client": "^1.0.0"
55 }
56 },
57 "node_modules/@redis/json": {
58 "version": "1.0.7",
59 "resolved": "https://registry.npmjs.org/@redis/json/-/json-1.0.7.tgz",
60 "integrity": "sha512-6UyXfjVaTBTJtKNG4/9Z8PSpKE6XgSyEb8iwaqDcy+uKrd/DGYHTWkUdnQDyzm727V7p21WUMhsqz5oy65kPcQ==",
61 "peerDependencies": {
62 "@redis/client": "^1.0.0"
63 }
64 },
65 "node_modules/@redis/search": {
66 "version": "1.2.0",
67 "resolved": "https://registry.npmjs.org/@redis/search/-/search-1.2.0.tgz",
68 "integrity": "sha512-tYoDBbtqOVigEDMAcTGsRlMycIIjwMCgD8eR2t0NANeQmgK/lvxNAvYyb6bZDD4frHRhIHkJu2TBRvB0ERkOmw==",
69 "peerDependencies": {
70 "@redis/client": "^1.0.0"
71 }
72 },
73 "node_modules/@redis/time-series": {
74 "version": "1.1.0",
75 "resolved": "https://registry.npmjs.org/@redis/time-series/-/time-series-1.1.0.tgz",
76 "integrity": "sha512-c1Q99M5ljsIuc4YdaCwfUEXsofakb9c8+Zse2qxTadu8TalLXuAESzLvFAvNVbkmSlvlzIQOLpBCmWI9wTOt+g==",
77 "peerDependencies": {
78 "@redis/client": "^1.0.0"
25 } 79 }
26 }, 80 },
27 "node_modules/accepts": { 81 "node_modules/accepts": {
...@@ -145,6 +199,14 @@ ...@@ -145,6 +199,14 @@
145 "url": "https://github.com/sponsors/ljharb" 199 "url": "https://github.com/sponsors/ljharb"
146 } 200 }
147 }, 201 },
202 "node_modules/cluster-key-slot": {
203 "version": "1.1.2",
204 "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz",
205 "integrity": "sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA==",
206 "engines": {
207 "node": ">=0.10.0"
208 }
209 },
148 "node_modules/combined-stream": { 210 "node_modules/combined-stream": {
149 "version": "1.0.8", 211 "version": "1.0.8",
150 "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", 212 "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
...@@ -458,6 +520,14 @@ ...@@ -458,6 +520,14 @@
458 "url": "https://github.com/sponsors/ljharb" 520 "url": "https://github.com/sponsors/ljharb"
459 } 521 }
460 }, 522 },
523 "node_modules/generic-pool": {
524 "version": "3.9.0",
525 "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.9.0.tgz",
526 "integrity": "sha512-hymDOu5B53XvN4QT9dBmZxPX4CWhBPPLguTZ9MMFeFa/Kg0xWVfylOVNlJji/E7yTZWFd/q9GO5TxDLq156D7g==",
527 "engines": {
528 "node": ">= 4"
529 }
530 },
461 "node_modules/get-intrinsic": { 531 "node_modules/get-intrinsic": {
462 "version": "1.2.4", 532 "version": "1.2.4",
463 "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", 533 "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz",
...@@ -805,6 +875,22 @@ ...@@ -805,6 +875,22 @@
805 "node": ">= 0.8" 875 "node": ">= 0.8"
806 } 876 }
807 }, 877 },
878 "node_modules/redis": {
879 "version": "4.7.0",
880 "resolved": "https://registry.npmjs.org/redis/-/redis-4.7.0.tgz",
881 "integrity": "sha512-zvmkHEAdGMn+hMRXuMBtu4Vo5P6rHQjLoHftu+lBqq8ZTA3RCVC/WzD790bkKKiNFp7d5/9PcSD19fJyyRvOdQ==",
882 "workspaces": [
883 "./packages/*"
884 ],
885 "dependencies": {
886 "@redis/bloom": "1.2.0",
887 "@redis/client": "1.6.0",
888 "@redis/graph": "1.1.1",
889 "@redis/json": "1.0.7",
890 "@redis/search": "1.2.0",
891 "@redis/time-series": "1.1.0"
892 }
893 },
808 "node_modules/safe-buffer": { 894 "node_modules/safe-buffer": {
809 "version": "5.2.1", 895 "version": "5.2.1",
810 "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", 896 "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
...@@ -979,6 +1065,11 @@ ...@@ -979,6 +1065,11 @@
979 "tr46": "~0.0.3", 1065 "tr46": "~0.0.3",
980 "webidl-conversions": "^3.0.0" 1066 "webidl-conversions": "^3.0.0"
981 } 1067 }
1068 },
1069 "node_modules/yallist": {
1070 "version": "4.0.0",
1071 "resolved": "https://registry.npmjs.org/yallist/-/yallist-4.0.0.tgz",
1072 "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
982 } 1073 }
983 } 1074 }
984 } 1075 }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
22 "js-tiktoken": "1.0.7", 22 "js-tiktoken": "1.0.7",
23 "morgan": "^1.10.0", 23 "morgan": "^1.10.0",
24 "node-fetch": "^2.7.0", 24 "node-fetch": "^2.7.0",
25 "openai": "^3.2.0" 25 "openai": "^3.2.0",
26 "redis": "^4.7.0"
26 } 27 }
27 } 28 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!