66c9f735 by Bryan Batac

- Added Redis

- Modifed the getmodel implementation
1 parent d6d461cd
...@@ -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,10 @@ const tiktokenModels = [ ...@@ -48,6 +49,10 @@ 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
51 // Open AI Configuration 56 // Open AI Configuration
52 // console.log(process.env.OPENAI_API_ORG) 57 // console.log(process.env.OPENAI_API_ORG)
53 const configuration = new Configuration({ 58 const configuration = new Configuration({
...@@ -85,18 +90,20 @@ app.post('/api', async (req, res) => { ...@@ -85,18 +90,20 @@ app.post('/api', async (req, res) => {
85 return; 90 return;
86 } 91 }
87 const { message, currentModel, temperature } = req.body; 92 const { message, currentModel, temperature } = req.body;
93 const getEndpoint = (modelName) => {
94 const model = Object.values(filteredModels).find(m => m.model === modelName);
95 return model ? model.endpoint : 'Endpoint not found';
96 };
88 97
89 if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") { 98 const endpoint = getEndpoint(currentModel);
90 runGPTTurbo(req, res);
91 99
100 if (endpoint == "openAI") {
101 runGPTTurbo(req, res);
92 return; 102 return;
93 } 103 }
94 104
95 if (currentModel == "openchat_3.5" || currentModel == "zephyr-7B-beta" 105 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); 106 runOpensource(req, res);
99
100 return; 107 return;
101 } 108 }
102 109
...@@ -267,18 +274,6 @@ async function runGPTTurbo(req, res) { ...@@ -267,18 +274,6 @@ async function runGPTTurbo(req, res) {
267 } 274 }
268 } 275 }
269 276
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) { 277 async function runOpensource(req, res) {
283 const { message, currentModel, temperature, P6XcW47o: together_ai_response = null } = req.body; 278 const { message, currentModel, temperature, P6XcW47o: together_ai_response = null } = req.body;
284 var input = ''; 279 var input = '';
...@@ -294,9 +289,9 @@ async function runOpensource(req, res) { ...@@ -294,9 +289,9 @@ async function runOpensource(req, res) {
294 289
295 try { 290 try {
296 let error_msg = ""; 291 let error_msg = "";
297 const endpoint_api_url = get_endpoint_api_url(currentModel); 292 const endpoint_api_url = process.env.OPENSOURCE_ENDPOINTS;
298 const api_key = get_endpoint_api_key(currentModel); 293 const api_key = OPENSOURCE_API_KEY;
299 const response = await axios.post(endpoint_api_url + '/chat/completions', { 294 const response = await axios.post(endpoint_api_url, {
300 model: currentModel, 295 model: currentModel,
301 messages: JSON.parse(message), 296 messages: JSON.parse(message),
302 max_tokens: 2048, 297 max_tokens: 2048,
...@@ -525,31 +520,17 @@ const getClientIP = (req) => { ...@@ -525,31 +520,17 @@ const getClientIP = (req) => {
525 520
526 // Get Models Route 521 // Get Models Route
527 app.get('/models', async (req, res) => { 522 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"}]; 523 await getModels()
529 const opensource_models = process.env.OPENSOURCE_MODELS ? JSON.parse(process.env.OPENSOURCE_MODELS) : [];
530
531 const models = { 524 const models = {
532 data: [] 525 data: Object.values(filteredModels).map((model) => ({
526 id: model.model,
527 label: model.name,
528 name: model.name,
529 beta: model.isBeta,
530 endpoint: model.endpoint
531 }))
533 }; 532 };
534 533
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({ 534 res.json({
554 models 535 models
555 }) 536 })
...@@ -558,4 +539,53 @@ app.get('/models', async (req, res) => { ...@@ -558,4 +539,53 @@ app.get('/models', async (req, res) => {
558 // Start the server 539 // Start the server
559 app.listen(port, () => { 540 app.listen(port, () => {
560 console.log(`Example app listening at http://localhost:${port}`) 541 console.log(`Example app listening at http://localhost:${port}`)
561 });
...\ No newline at end of file ...\ No newline at end of file
542 });
543
544 const initializeRedisClient = () => {
545 const newClient = createClient({
546 socket: {
547 host: process.env.REDIS_HOST,
548 port: process.env.REDIS_PORT,
549 },
550 username: process.env.REDIS_USER,
551 password: process.env.REDIS_PASS,
552 });
553
554 newClient.on('error', (err) => {
555 console.error('Redis error:', err);
556 });
557
558 return newClient;
559 };
560
561 const fetchAndFilterModels = async (client) => {
562 try {
563 await client.select(process.env.REDIS_DB);
564 const models = await client.get('model');
565
566 return Object.entries(JSON.parse(models))
567 .filter(([_, value]) => allowedEndpoints.includes(value.endpoint))
568 .reduce((acc, [key, value]) => {
569 acc[key] = value;
570 return acc;
571 }, {});
572 } catch (err) {
573 console.error('Error fetching and filtering models:', err);
574 throw err;
575 }
576 };
577
578 const getModels = async () => {
579 if (!client) {
580 client = initializeRedisClient();
581
582 try {
583 await client.connect();
584 filteredModels = await fetchAndFilterModels(client);
585 console.log('Connected to Redis successfully');
586 } catch (err) {
587 console.error('Error connecting to Redis:', err);
588 throw err;
589 }
590 }
591 };
...\ 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!