7db45447 by Janis

Merge branch '28859_integrate_llm_chatbot_plus' into 'master'

opensource

See merge request !81
2 parents 34d94fa0 7e30965a
......@@ -28,9 +28,12 @@ function App() {
const [chatLogTurbo, setChatLogTurbo] = useState(defaultChatLogTurbo);
//
const [chatLogOpenSource, setChatLogOpenSource] = useState([]);
function clearChat(){
setChatLog([]);
setChatLogTurbo(defaultChatLogTurbo);
setChatLogOpenSource([]);
setChatInput("");
setStartedInteraction(false);
}
......@@ -94,12 +97,20 @@ function App() {
setChatLog(prevChatLog => [...prevChatLog, userMessage]);
var messages = chatLogNew.map((message) => { if(message.user !== 'me') return message.message }).join("\n")
if(currentModel == GPTTurbo || currentModel == GPTTurbo0301) {
if(currentModel === GPTTurbo || currentModel === GPTTurbo0301) {
// "gpt-3.5-turbo"
let chatLogTurboNew = [...chatLogTurbo, { role: "user", content: chatInput }];
setChatLogTurbo(chatLogTurboNew);
messages = JSON.stringify(chatLogTurboNew);
}
if(currentModel === "openchat_3.5-GPTQ" || currentModel === "zephyr-7B-beta-GPTQ") {
// "gpt-3.5-turbo"
let chatLogOpenSourceNew = [...chatLogOpenSource, { role: "user", content: chatInput }];
setChatLogOpenSource(chatLogOpenSourceNew);
messages = JSON.stringify(chatLogOpenSourceNew);
}
let intervalId = startInterval();
try {
const response = await fetch(process.env.REACT_APP_SERVER_URL + "/api", {
......@@ -117,6 +128,7 @@ function App() {
const parsedData = data.message.trim();
// "gpt-3.5-turbo"
let chatLogTurboNew = chatLogTurbo;
let chatLogOpenSourceNew = chatLogOpenSource;
if(data.success === false) {
setChatLog(prevChatLog => {
const lastMsg = prevChatLog[prevChatLog.length - 2];
......@@ -129,7 +141,11 @@ function App() {
}
chatLogTurboNew.push({ role: "user", content: userModifiedInput });
chatLogTurboNew.push({ role: "assistant", content: parsedData });
chatLogOpenSourceNew.push({ role: "user", content: userModifiedInput });
chatLogOpenSourceNew.push({ role: "assistant", content: parsedData });
setChatLogTurbo(chatLogTurboNew);
setChatLogOpenSource(chatLogOpenSourceNew);
//
clearInterval(intervalId);
const programmingKeywords = ['code', 'application', 'controller', 'rails' , 'PHP', 'java', 'javascript', 'script', 'console', 'python', 'programming', 'table'];
......
......@@ -33,7 +33,7 @@ const SideMenu = ({
{models && models.length ? (
models.map((model, index) => (
<option key={model.id} value={model.id}>
{model.id}
{model.id} {model.beta ? "(beta)" : ""}
</option>
))
) : (
......
OPENAI_API_ORG=
OPENAI_API_KEY=
OPENSOURCE_MODELS="openchat_3.5-GPTQ,zephyr-7B-beta-GPTQ"
OPENSOURCE_ENDPOINTS={"openchat_3.5-GPTQ": "https://openchat.llm.ai-pro.org/v1", "zephyr-7B-beta-GPTQ": "https://zephyr.llm.ai-pro.org/v1"}
\ No newline at end of file
......
......@@ -75,8 +75,13 @@ app.use(rateLimiter)
app.post('/api', async (req, res) => {
const { message, currentModel, temperature } = req.body;
if(currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") {
runGPTTurbo(req,res);
if (currentModel == "gpt-3.5-turbo" || currentModel == "gpt-3.5-turbo-0301") {
runGPTTurbo(req, res);
return;
}
if (currentModel == "openchat_3.5-GPTQ" || currentModel == "zephyr-7B-beta-GPTQ") {
runOpensource(req, res);
return;
}
......@@ -87,10 +92,10 @@ app.post('/api', async (req, res) => {
}
let query_prompt = `${greetingPrompt}\n${message}`;
str_length = req.body.message.split(' ').length;
if (str_length>=800){
if (str_length >= 800) {
arr_body = req.body.message.split("\n");
if (arr_body.length>=4){
var i = arr_body.length-2
if (arr_body.length >= 4) {
var i = arr_body.length - 2
while (i--) {
arr_body.splice(i, 1);
}
......@@ -101,7 +106,7 @@ app.post('/api', async (req, res) => {
input: query_prompt
}, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } });
if(moderation.data.results[0].flagged) {
if (moderation.data.results[0].flagged) {
res.json({
success: false,
message: "I'm sorry, but I can't assist with that. We want everyone to use our tool safely and responsibly.\nIf you have any other questions or need advice on a different topic, feel free to ask."
......@@ -142,11 +147,11 @@ app.post('/api', async (req, res) => {
})
} catch (e) {
let error_msg = e.response.data.error.message ? e.response.data.error.message : '';
if (error_msg.indexOf('maximum context length')>=0){
if (error_msg.indexOf('maximum context length') >= 0) {
res.json({
message: "The output for your prompt is too long for us to process. Please reduce your prompt and try again.",
})
}else{
} else {
console.log(e.response);
}
} finally {
......@@ -164,7 +169,7 @@ async function runGPTTurbo(req, res) {
input: query_prompt
}, { headers: { 'content-type': 'application/json', 'Authorization': `Bearer ${process.env.OPENAI_API_KEY}` } });
if(moderation.data.results[0].flagged) {
if (moderation.data.results[0].flagged) {
res.json({
success: false,
message: "I'm sorry, but I can't assist with that. We want everyone to use our tool safely and responsibly.\nIf you have any other questions or need advice on a different topic, feel free to ask."
......@@ -182,9 +187,9 @@ async function runGPTTurbo(req, res) {
input = response.data.choices[0].message.content
} catch (e) {
let error_msg = e.response.data.error.message ? e.response.data.error.message : '';
if (error_msg.indexOf('maximum context length')>=0){
if (error_msg.indexOf('maximum context length') >= 0) {
input = "The output for your prompt is too long for us to process. Please reduce your prompt and try again.";
}else{
} else {
console.log(e.response);
}
} finally {
......@@ -216,11 +221,84 @@ async function runGPTTurbo(req, res) {
}
}
const get_endpoint_api_url = (currentModel) => {
const OPENSOURCE_ENDPOINTS = process.env.OPENSOURCE_ENDPOINTS;
const endpoints = JSON.parse(OPENSOURCE_ENDPOINTS);
const endpoint_api_url = endpoints?.[currentModel];
return endpoint_api_url
}
async function runOpensource(req, res) {
const { message, currentModel, temperature } = req.body;
var input = '';
const message_history = JSON.parse(message);
const query_prompt = message_history.length ? message_history[message_history.length - 1].content : "";
try {
const endpoint_api_url = get_endpoint_api_url(currentModel);
console.log('endpoint_api_url', endpoint_api_url);
const response = await axios.post(endpoint_api_url + '/chat/completions', {
messages: JSON.parse(message),
temperature
}, {
headers: {
'Content-Type': 'application/json',
// 'Authorization': `Bearer ${process.env.OPENSOURCE_API_KEY}`
},
});
console.log(" zephyr response", response.data.choices[0])
input = response.data.choices[0].message.content
} catch (e) {
let error_msg = e.response.data.error.message ? e.response.data.error.message : '';
if (error_msg.indexOf('maximum context length') >= 0) {
input = "The output for your prompt is too long for us to process. Please reduce your prompt and try again.";
} else {
console.log(e.response);
}
} finally {
let usage = {};
let enc = null;
try {
enc = encodingForModel('gpt-3.5-turbo');
usage.prompt_tokens = (enc.encode(query_prompt)).length;
usage.completion_tokens = (enc.encode(input)).length;
usage.total_tokens = usage.prompt_tokens + usage.completion_tokens;
} catch (e) {
console.log('Error encoding prompt text', e);
}
res.json({
prompt: JSON.parse(message),
usage: usage,
message: anchorme({
input,
options: {
attributes: {
target: "_blank"
},
}
})
});
return;
}
}
// Get Models Route
app.get('/models', async (req, res) => {
const response = await openai.listEngines();
const models = response.data;
const opensource_models = process.env.OPENSOURCE_MODELS ? process.env.OPENSOURCE_MODELS.split(',') : [];
opensource_models.forEach((model) => {
models.data.push({
id: model,
beta: true,
});
})
res.json({
models: response.data
models
})
});
......
......@@ -10,11 +10,15 @@
"license": "ISC",
"dependencies": {
"anchorme": "^2.1.2",
"axios": "^1.5.1",
"body-parser": "^1.20.1",
"cookie": "0.5.0",
"cookie-parser": "1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-rate-limit": "^6.7.0",
"js-tiktoken": "1.0.7",
"morgan": "^1.10.0",
"openai": "^3.2.0"
}
......@@ -47,13 +51,34 @@
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
},
"node_modules/axios": {
"version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"version": "1.6.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz",
"integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==",
"dependencies": {
"follow-redirects": "^1.14.8"
"follow-redirects": "^1.15.0",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/base64-js": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
......@@ -151,6 +176,26 @@
"node": ">= 0.6"
}
},
"node_modules/cookie-parser": {
"version": "1.4.6",
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.6.tgz",
"integrity": "sha512-z3IzaNjdwUC2olLIB5/ITd0/setiaFMLYiZJle7xg5Fe9KWAceil7xszYfHHBtDFYLSgJduS2Ty0P1uJdPDJeA==",
"dependencies": {
"cookie": "0.4.1",
"cookie-signature": "1.0.6"
},
"engines": {
"node": ">= 0.8.0"
}
},
"node_modules/cookie-parser/node_modules/cookie": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.4.1.tgz",
"integrity": "sha512-ZwrFkGJxUR3EIoXtO+yVE69Eb7KlixbaeAWfBQB9vVsNn/o+Yw69gBWSSDK825hQNdN+wF8zELf3dFNl/kxkUA==",
"engines": {
"node": ">= 0.6"
}
},
"node_modules/cookie-signature": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
......@@ -431,6 +476,14 @@
"node": ">= 0.10"
}
},
"node_modules/js-tiktoken": {
"version": "1.0.7",
"resolved": "https://registry.npmjs.org/js-tiktoken/-/js-tiktoken-1.0.7.tgz",
"integrity": "sha512-biba8u/clw7iesNEWLOLwrNGoBP2lA+hTaBLs/D45pJdUPFXyxD6nhcDVtADChghv4GgyAiMKYMiRx7x6h7Biw==",
"dependencies": {
"base64-js": "^1.5.1"
}
},
"node_modules/media-typer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
......@@ -565,6 +618,14 @@
"form-data": "^4.0.0"
}
},
"node_modules/openai/node_modules/axios": {
"version": "0.26.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz",
"integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==",
"dependencies": {
"follow-redirects": "^1.14.8"
}
},
"node_modules/parseurl": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
......@@ -590,6 +651,11 @@
"node": ">= 0.10"
}
},
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/qs": {
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!