mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
feat: feat: new endpoint-style submit
This commit is contained in:
parent
089ca5f120
commit
adcc021c9e
22 changed files with 566 additions and 478 deletions
|
|
@ -1,8 +1,22 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
|
|
||||||
const askBing = async ({ text, onProgress, convo }) => {
|
const askBing = async ({
|
||||||
|
text,
|
||||||
|
parentMessageId,
|
||||||
|
conversationId,
|
||||||
|
jailbreak,
|
||||||
|
jailbreakConversationId,
|
||||||
|
conversationSignature,
|
||||||
|
clientId,
|
||||||
|
invocationId,
|
||||||
|
toneStyle,
|
||||||
|
onProgress
|
||||||
|
}) => {
|
||||||
const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api');
|
const { BingAIClient } = await import('@waylaidwanderer/chatgpt-api');
|
||||||
|
const store = {
|
||||||
|
store: new KeyvFile({ filename: './data/cache.json' })
|
||||||
|
};
|
||||||
|
|
||||||
const bingAIClient = new BingAIClient({
|
const bingAIClient = new BingAIClient({
|
||||||
// "_U" cookie from bing.com
|
// "_U" cookie from bing.com
|
||||||
|
|
@ -10,23 +24,25 @@ const askBing = async ({ text, onProgress, convo }) => {
|
||||||
// If the above doesn't work, provide all your cookies as a string instead
|
// If the above doesn't work, provide all your cookies as a string instead
|
||||||
// cookies: '',
|
// cookies: '',
|
||||||
debug: false,
|
debug: false,
|
||||||
cache: { store: new KeyvFile({ filename: './data/cache.json' }) },
|
cache: store,
|
||||||
proxy: process.env.PROXY || null
|
proxy: process.env.PROXY || null
|
||||||
});
|
});
|
||||||
|
|
||||||
let options = { onProgress };
|
let options = {
|
||||||
if (convo) {
|
jailbreakConversationId: jailbreakConversationId || jailbreak,
|
||||||
options = { ...options, ...convo };
|
parentMessageId,
|
||||||
}
|
conversationId,
|
||||||
|
conversationSignature,
|
||||||
|
clientId,
|
||||||
|
invocationId,
|
||||||
|
toneStyle,
|
||||||
|
onProgress
|
||||||
|
};
|
||||||
|
|
||||||
if (options?.jailbreakConversationId == 'false') {
|
if (options?.jailbreakConversationId == 'false') {
|
||||||
options.jailbreakConversationId = false;
|
options.jailbreakConversationId = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (convo.toneStyle) {
|
|
||||||
options.toneStyle = convo.toneStyle;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('bing options', options);
|
console.log('bing options', options);
|
||||||
|
|
||||||
const res = await bingAIClient.sendMessage(text, options);
|
const res = await bingAIClient.sendMessage(text, options);
|
||||||
|
|
|
||||||
|
|
@ -1,40 +1,45 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
const set = new Set(["gpt-4", "text-davinci-002-render", "text-davinci-002-render-paid", "text-davinci-002-render-sha"]);
|
// const set = new Set([
|
||||||
|
// 'gpt-4',
|
||||||
|
// 'text-davinci-002-render',
|
||||||
|
// 'text-davinci-002-render-paid',
|
||||||
|
// 'text-davinci-002-render-sha'
|
||||||
|
// ]);
|
||||||
|
|
||||||
const clientOptions = {
|
const browserClient = async ({
|
||||||
// Warning: This will expose your access token to a third party. Consider the risks before using this.
|
text,
|
||||||
reverseProxyUrl: 'https://bypass.duti.tech/api/conversation',
|
parentMessageId,
|
||||||
// Access token from https://chat.openai.com/api/auth/session
|
conversationId,
|
||||||
accessToken: process.env.CHATGPT_TOKEN,
|
model,
|
||||||
// debug: true
|
onProgress,
|
||||||
proxy: process.env.PROXY || null,
|
abortController
|
||||||
};
|
}) => {
|
||||||
|
|
||||||
// You can check which models you have access to by opening DevTools and going to the Network tab.
|
|
||||||
// Refresh the page and look at the response body for https://chat.openai.com/backend-api/models.
|
|
||||||
if (set.has(process.env.BROWSER_MODEL)) {
|
|
||||||
clientOptions.model = process.env.BROWSER_MODEL;
|
|
||||||
}
|
|
||||||
|
|
||||||
const browserClient = async ({ text, onProgress, convo, abortController }) => {
|
|
||||||
const { ChatGPTBrowserClient } = await import('@waylaidwanderer/chatgpt-api');
|
const { ChatGPTBrowserClient } = await import('@waylaidwanderer/chatgpt-api');
|
||||||
|
|
||||||
const store = {
|
const store = {
|
||||||
store: new KeyvFile({ filename: './data/cache.json' })
|
store: new KeyvFile({ filename: './data/cache.json' })
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const clientOptions = {
|
||||||
|
// Warning: This will expose your access token to a third party. Consider the risks before using this.
|
||||||
|
reverseProxyUrl: 'https://bypass.duti.tech/api/conversation',
|
||||||
|
// Access token from https://chat.openai.com/api/auth/session
|
||||||
|
accessToken: process.env.CHATGPT_TOKEN,
|
||||||
|
model,
|
||||||
|
// debug: true
|
||||||
|
proxy: process.env.PROXY || null
|
||||||
|
};
|
||||||
|
|
||||||
const client = new ChatGPTBrowserClient(clientOptions, store);
|
const client = new ChatGPTBrowserClient(clientOptions, store);
|
||||||
let options = { onProgress, abortController };
|
let options = { onProgress, abortController };
|
||||||
|
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
if (!!parentMessageId && !!conversationId) {
|
||||||
options = { ...options, ...convo };
|
options = { ...options, parentMessageId, conversationId };
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('gptBrowser options', options, clientOptions);
|
// console.log('gptBrowser options', options, clientOptions);
|
||||||
|
|
||||||
/* will error if given a convoId at the start */
|
if (parentMessageId === '00000000-0000-0000-0000-000000000000') {
|
||||||
if (convo.parentMessageId.startsWith('0000')) {
|
|
||||||
delete options.conversationId;
|
delete options.conversationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,43 @@
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const { KeyvFile } = require('keyv-file');
|
const { KeyvFile } = require('keyv-file');
|
||||||
const set = new Set(['gpt-4', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301']);
|
// const set = new Set(['gpt-4', 'text-davinci-003', 'gpt-3.5-turbo', 'gpt-3.5-turbo-0301']);
|
||||||
|
|
||||||
const clientOptions = {
|
const askClient = async ({
|
||||||
modelOptions: {
|
text,
|
||||||
model: 'gpt-3.5-turbo'
|
parentMessageId,
|
||||||
},
|
conversationId,
|
||||||
proxy: process.env.PROXY || null,
|
model,
|
||||||
debug: false
|
chatGptLabel,
|
||||||
};
|
promptPrefix,
|
||||||
|
temperature,
|
||||||
if (set.has(process.env.DEFAULT_API_GPT)) {
|
top_p,
|
||||||
clientOptions.modelOptions.model = process.env.DEFAULT_API_GPT;
|
presence_penalty,
|
||||||
}
|
onProgress,
|
||||||
|
abortController
|
||||||
const askClient = async ({ text, onProgress, convo, abortController }) => {
|
}) => {
|
||||||
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
||||||
const store = {
|
const store = {
|
||||||
store: new KeyvFile({ filename: './data/cache.json' })
|
store: new KeyvFile({ filename: './data/cache.json' })
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const clientOptions = {
|
||||||
|
modelOptions: {
|
||||||
|
model: model,
|
||||||
|
temperature,
|
||||||
|
top_p,
|
||||||
|
presence_penalty
|
||||||
|
},
|
||||||
|
chatGptLabel,
|
||||||
|
promptPrefix,
|
||||||
|
proxy: process.env.PROXY || null,
|
||||||
|
debug: false
|
||||||
|
};
|
||||||
|
|
||||||
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
||||||
let options = { onProgress, abortController };
|
let options = { onProgress, abortController };
|
||||||
|
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
if (!!parentMessageId && !!conversationId) {
|
||||||
options = { ...options, ...convo };
|
options = { ...options, parentMessageId, conversationId };
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = await client.sendMessage(text, options);
|
const res = await client.sendMessage(text, options);
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
require('dotenv').config();
|
|
||||||
const { KeyvFile } = require('keyv-file');
|
|
||||||
|
|
||||||
const clientOptions = {
|
|
||||||
modelOptions: {
|
|
||||||
model: 'gpt-3.5-turbo'
|
|
||||||
},
|
|
||||||
proxy: process.env.PROXY || null,
|
|
||||||
debug: false
|
|
||||||
};
|
|
||||||
|
|
||||||
const customClient = async ({ text, onProgress, convo, promptPrefix, chatGptLabel, abortController }) => {
|
|
||||||
const ChatGPTClient = (await import('@waylaidwanderer/chatgpt-api')).default;
|
|
||||||
const store = {
|
|
||||||
store: new KeyvFile({ filename: './data/cache.json' })
|
|
||||||
};
|
|
||||||
|
|
||||||
clientOptions.chatGptLabel = chatGptLabel;
|
|
||||||
|
|
||||||
if (promptPrefix?.length > 0) {
|
|
||||||
clientOptions.promptPrefix = promptPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
const client = new ChatGPTClient(process.env.OPENAI_KEY, clientOptions, store);
|
|
||||||
|
|
||||||
let options = { onProgress, abortController };
|
|
||||||
if (!!convo.parentMessageId && !!convo.conversationId) {
|
|
||||||
options = { ...options, ...convo };
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await client.sendMessage(text, options);
|
|
||||||
return res;
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = customClient;
|
|
||||||
|
|
@ -2,7 +2,6 @@ const { askClient } = require('./clients/chatgpt-client');
|
||||||
const { browserClient } = require('./clients/chatgpt-browser');
|
const { browserClient } = require('./clients/chatgpt-browser');
|
||||||
const { askBing } = require('./clients/bingai');
|
const { askBing } = require('./clients/bingai');
|
||||||
const { askSydney } = require('./clients/sydney');
|
const { askSydney } = require('./clients/sydney');
|
||||||
const customClient = require('./clients/chatgpt-custom');
|
|
||||||
const titleConvo = require('./titleConvo');
|
const titleConvo = require('./titleConvo');
|
||||||
const getCitations = require('../lib/parse/getCitations');
|
const getCitations = require('../lib/parse/getCitations');
|
||||||
const citeText = require('../lib/parse/citeText');
|
const citeText = require('../lib/parse/citeText');
|
||||||
|
|
@ -10,10 +9,9 @@ const citeText = require('../lib/parse/citeText');
|
||||||
module.exports = {
|
module.exports = {
|
||||||
askClient,
|
askClient,
|
||||||
browserClient,
|
browserClient,
|
||||||
customClient,
|
|
||||||
askBing,
|
askBing,
|
||||||
askSydney,
|
askSydney,
|
||||||
titleConvo,
|
titleConvo,
|
||||||
getCitations,
|
getCitations,
|
||||||
citeText,
|
citeText
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ const proxyEnvToAxiosProxy = proxyString => {
|
||||||
return proxyConfig;
|
return proxyConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
const titleConvo = async ({ model, text, response }) => {
|
const titleConvo = async ({ endpoint, text, response }) => {
|
||||||
let title = 'New Chat';
|
let title = 'New Chat';
|
||||||
const messages = [
|
const messages = [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -27,11 +27,6 @@ module.exports = {
|
||||||
if (!update.jailbreakConversationId) {
|
if (!update.jailbreakConversationId) {
|
||||||
update.jailbreakConversationId = null;
|
update.jailbreakConversationId = null;
|
||||||
}
|
}
|
||||||
if (update.model !== 'chatgptCustom' && update.chatGptLabel && update.promptPrefix) {
|
|
||||||
console.log('Validation error: resetting chatgptCustom fields', update);
|
|
||||||
update.chatGptLabel = null;
|
|
||||||
update.promptPrefix = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return await Conversation.findOneAndUpdate(
|
return await Conversation.findOneAndUpdate(
|
||||||
{ conversationId: conversationId, user },
|
{ conversationId: conversationId, user },
|
||||||
|
|
@ -149,10 +144,10 @@ module.exports = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
deleteConvos: async (user, filter) => {
|
deleteConvos: async (user, filter) => {
|
||||||
let toRemove = await Conversation.find({...filter, user}).select('conversationId')
|
let toRemove = await Conversation.find({ ...filter, user }).select('conversationId');
|
||||||
const ids = toRemove.map(instance => instance.conversationId);
|
const ids = toRemove.map(instance => instance.conversationId);
|
||||||
let deleteCount = await Conversation.deleteMany({...filter, user}).exec();
|
let deleteCount = await Conversation.deleteMany({ ...filter, user }).exec();
|
||||||
deleteCount.messages = await deleteMessages({conversationId: {$in: ids}});
|
deleteCount.messages = await deleteMessages({ conversationId: { $in: ids } });
|
||||||
return deleteCount;
|
return deleteCount;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -60,12 +60,13 @@ const projectPath = path.join(__dirname, '..', '..', 'client');
|
||||||
app.use('/api/prompts', routes.authenticatedOr401, routes.prompts);
|
app.use('/api/prompts', routes.authenticatedOr401, routes.prompts);
|
||||||
app.use('/auth', routes.auth);
|
app.use('/auth', routes.auth);
|
||||||
|
|
||||||
app.get('/api/models', function (req, res) {
|
app.get('/api/endpoints', function (req, res) {
|
||||||
const hasOpenAI = !!process.env.OPENAI_KEY;
|
const azureOpenAI = !!process.env.AZURE_OPENAI_KEY;
|
||||||
const hasChatGpt = !!process.env.CHATGPT_TOKEN;
|
const openAI = !!process.env.OPENAI_KEY;
|
||||||
const hasBing = !!process.env.BING_TOKEN;
|
const bingAI = !!process.env.BING_TOKEN;
|
||||||
|
const chatGPTBrowser = !!process.env.CHATGPT_TOKEN;
|
||||||
|
|
||||||
res.send(JSON.stringify({ hasOpenAI, hasChatGpt, hasBing }));
|
res.send(JSON.stringify({ azureOpenAI, openAI, bingAI, chatGPTBrowser }));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get('/*', routes.authenticatedOrRedirect, function (req, res) {
|
app.get('/*', routes.authenticatedOrRedirect, function (req, res) {
|
||||||
|
|
|
||||||
|
|
@ -1,148 +1,13 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const crypto = require('crypto');
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const askBing = require('./askBing');
|
// const askAzureOpenAI = require('./askAzureOpenAI';)
|
||||||
const askSydney = require('./askSydney');
|
const askOpenAI = require('./askOpenAI');
|
||||||
const { titleConvo, askClient, browserClient, customClient } = require('../../app/');
|
const askBingAI = require('./askBingAI');
|
||||||
const { saveMessage, getConvoTitle, saveConvo, updateConvo } = require('../../models');
|
const askChatGPTBrowser = require('./askChatGPTBrowser');
|
||||||
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
|
||||||
|
|
||||||
router.use('/bing', askBing);
|
// router.use('/azureOpenAI', askAzureOpenAI);
|
||||||
router.use('/sydney', askSydney);
|
router.use('/openAI', askOpenAI);
|
||||||
|
router.use('/bingAI', askBingAI);
|
||||||
router.post('/', async (req, res) => {
|
router.use('/chatGPTBrowser', askChatGPTBrowser);
|
||||||
const {
|
|
||||||
model,
|
|
||||||
text,
|
|
||||||
overrideParentMessageId = null,
|
|
||||||
parentMessageId,
|
|
||||||
conversationId: oldConversationId,
|
|
||||||
...convo
|
|
||||||
} = req.body;
|
|
||||||
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
|
|
||||||
|
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
|
||||||
const userMessageId = crypto.randomUUID();
|
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
|
||||||
const userMessage = {
|
|
||||||
messageId: userMessageId,
|
|
||||||
sender: 'User',
|
|
||||||
text,
|
|
||||||
parentMessageId: userParentMessageId,
|
|
||||||
conversationId,
|
|
||||||
isCreatedByUser: true
|
|
||||||
};
|
|
||||||
console.log('ask log', {
|
|
||||||
model,
|
|
||||||
...userMessage,
|
|
||||||
...convo
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!overrideParentMessageId) {
|
|
||||||
await saveMessage(userMessage);
|
|
||||||
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
|
|
||||||
}
|
|
||||||
|
|
||||||
return await ask({ userMessage, model, convo, preSendRequest: true, overrideParentMessageId, req, res });
|
|
||||||
});
|
|
||||||
|
|
||||||
const ask = async ({
|
|
||||||
userMessage,
|
|
||||||
overrideParentMessageId = null,
|
|
||||||
model,
|
|
||||||
convo,
|
|
||||||
preSendRequest = true,
|
|
||||||
req,
|
|
||||||
res
|
|
||||||
}) => {
|
|
||||||
const {
|
|
||||||
text,
|
|
||||||
parentMessageId: userParentMessageId,
|
|
||||||
conversationId,
|
|
||||||
messageId: userMessageId
|
|
||||||
} = userMessage;
|
|
||||||
|
|
||||||
const client = model === 'chatgpt' ? askClient : model === 'chatgptCustom' ? customClient : browserClient;
|
|
||||||
|
|
||||||
res.writeHead(200, {
|
|
||||||
Connection: 'keep-alive',
|
|
||||||
'Content-Type': 'text/event-stream',
|
|
||||||
'Cache-Control': 'no-cache, no-transform',
|
|
||||||
'Access-Control-Allow-Origin': '*',
|
|
||||||
'X-Accel-Buffering': 'no'
|
|
||||||
});
|
|
||||||
|
|
||||||
if (preSendRequest) sendMessage(res, { message: userMessage, created: true });
|
|
||||||
|
|
||||||
try {
|
|
||||||
const progressCallback = createOnProgress();
|
|
||||||
const abortController = new AbortController();
|
|
||||||
res.on('close', () => abortController.abort());
|
|
||||||
let gptResponse = await client({
|
|
||||||
text,
|
|
||||||
onProgress: progressCallback.call(null, model, { res, text }),
|
|
||||||
convo: { parentMessageId: userParentMessageId, conversationId, ...convo },
|
|
||||||
...convo,
|
|
||||||
abortController
|
|
||||||
});
|
|
||||||
|
|
||||||
gptResponse.text = gptResponse.response;
|
|
||||||
console.log('CLIENT RESPONSE', gptResponse);
|
|
||||||
|
|
||||||
if (!gptResponse.parentMessageId) {
|
|
||||||
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
|
||||||
delete gptResponse.response;
|
|
||||||
}
|
|
||||||
|
|
||||||
gptResponse.sender = model === 'chatgptCustom' ? convo.chatGptLabel : model;
|
|
||||||
gptResponse.model = model;
|
|
||||||
gptResponse.text = await handleText(gptResponse);
|
|
||||||
if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
|
||||||
gptResponse.chatGptLabel = convo.chatGptLabel;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (convo.promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
|
||||||
gptResponse.promptPrefix = convo.promptPrefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
|
||||||
|
|
||||||
if (model === 'chatgptBrowser' && userParentMessageId.startsWith('000')) {
|
|
||||||
await saveMessage({ ...userMessage, conversationId: gptResponse.conversationId });
|
|
||||||
}
|
|
||||||
|
|
||||||
await saveMessage(gptResponse);
|
|
||||||
await updateConvo(req?.session?.user?.username, {
|
|
||||||
...gptResponse,
|
|
||||||
oldConvoId: model === 'chatgptBrowser' && conversationId
|
|
||||||
});
|
|
||||||
sendMessage(res, {
|
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
|
||||||
final: true,
|
|
||||||
requestMessage: userMessage,
|
|
||||||
responseMessage: gptResponse
|
|
||||||
});
|
|
||||||
res.end();
|
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
|
||||||
const title = await titleConvo({ model, text, response: gptResponse });
|
|
||||||
await updateConvo(req?.session?.user?.username, {
|
|
||||||
conversationId: model === 'chatgptBrowser' ? gptResponse.conversationId : conversationId,
|
|
||||||
title
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
const errorMessage = {
|
|
||||||
messageId: crypto.randomUUID(),
|
|
||||||
sender: model,
|
|
||||||
conversationId,
|
|
||||||
parentMessageId: overrideParentMessageId || userMessageId,
|
|
||||||
error: true,
|
|
||||||
text: error.message
|
|
||||||
};
|
|
||||||
await saveMessage(errorMessage);
|
|
||||||
handleError(res, errorMessage);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,26 @@
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { titleConvo, askBing } = require('../../app/');
|
const { titleConvo, askBing } = require('../../app');
|
||||||
const { saveBingMessage, getConvoTitle, saveConvo } = require('../../models');
|
const { saveBingMessage, getConvoTitle, saveConvo, getConvo } = require('../../models');
|
||||||
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
|
|
||||||
router.post('/', async (req, res) => {
|
router.post('/', async (req, res) => {
|
||||||
const {
|
const {
|
||||||
model,
|
endpoint,
|
||||||
text,
|
text,
|
||||||
overrideParentMessageId=null,
|
messageId,
|
||||||
|
overrideParentMessageId = null,
|
||||||
parentMessageId,
|
parentMessageId,
|
||||||
conversationId: oldConversationId,
|
conversationId: oldConversationId
|
||||||
...convo
|
|
||||||
} = req.body;
|
} = req.body;
|
||||||
if (text.length === 0) {
|
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
|
||||||
return handleError(res, { text: 'Prompt empty or too short' });
|
if (endpoint !== 'bingAI') return handleError(res, { text: 'Illegal request' });
|
||||||
}
|
|
||||||
|
|
||||||
|
// build user message
|
||||||
const conversationId = oldConversationId || crypto.randomUUID();
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
const isNewConversation = !oldConversationId;
|
const isNewConversation = !oldConversationId;
|
||||||
|
const userMessageId = messageId;
|
||||||
const userMessageId = convo.messageId;
|
|
||||||
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
let userMessage = {
|
let userMessage = {
|
||||||
messageId: userMessageId,
|
messageId: userMessageId,
|
||||||
|
|
@ -32,22 +31,33 @@ router.post('/', async (req, res) => {
|
||||||
isCreatedByUser: true
|
isCreatedByUser: true
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// build endpoint option
|
||||||
|
const endpointOption = {
|
||||||
|
jailbreak: req.body?.jailbreak || false,
|
||||||
|
jailbreakConversationId: req.body?.jailbreakConversationId || null,
|
||||||
|
conversationSignature: req.body?.conversationSignature || null,
|
||||||
|
clientId: req.body?.clientId || null,
|
||||||
|
invocationId: req.body?.invocationId || null,
|
||||||
|
toneStyle: req.body?.toneStyle || 'fast',
|
||||||
|
suggestions: req.body?.suggestions || []
|
||||||
|
};
|
||||||
|
|
||||||
console.log('ask log', {
|
console.log('ask log', {
|
||||||
model,
|
userMessage,
|
||||||
...convo,
|
endpointOption,
|
||||||
...userMessage
|
conversationId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!overrideParentMessageId) {
|
if (!overrideParentMessageId) {
|
||||||
await saveBingMessage(userMessage);
|
await saveBingMessage(userMessage);
|
||||||
await saveConvo(req?.session?.user?.username, { model, ...convo, ...userMessage });
|
await saveConvo(req?.session?.user?.username, { ...userMessage, ...endpointOption, conversationId });
|
||||||
}
|
}
|
||||||
|
|
||||||
return await ask({
|
return await ask({
|
||||||
isNewConversation,
|
isNewConversation,
|
||||||
userMessage,
|
userMessage,
|
||||||
model,
|
endpointOption,
|
||||||
convo,
|
conversationId,
|
||||||
preSendRequest: true,
|
preSendRequest: true,
|
||||||
overrideParentMessageId,
|
overrideParentMessageId,
|
||||||
req,
|
req,
|
||||||
|
|
@ -57,20 +67,15 @@ router.post('/', async (req, res) => {
|
||||||
|
|
||||||
const ask = async ({
|
const ask = async ({
|
||||||
isNewConversation,
|
isNewConversation,
|
||||||
overrideParentMessageId = null,
|
|
||||||
userMessage,
|
userMessage,
|
||||||
model,
|
endpointOption,
|
||||||
convo,
|
conversationId,
|
||||||
preSendRequest = true,
|
preSendRequest = true,
|
||||||
|
overrideParentMessageId = null,
|
||||||
req,
|
req,
|
||||||
res
|
res
|
||||||
}) => {
|
}) => {
|
||||||
let {
|
let { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||||
text,
|
|
||||||
parentMessageId: userParentMessageId,
|
|
||||||
conversationId,
|
|
||||||
messageId: userMessageId
|
|
||||||
} = userMessage;
|
|
||||||
|
|
||||||
res.writeHead(200, {
|
res.writeHead(200, {
|
||||||
Connection: 'keep-alive',
|
Connection: 'keep-alive',
|
||||||
|
|
@ -84,99 +89,82 @@ const ask = async ({
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const progressCallback = createOnProgress();
|
const progressCallback = createOnProgress();
|
||||||
|
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
res.on('close', () => {
|
res.on('close', () => abortController.abort());
|
||||||
console.log('The client has disconnected.');
|
|
||||||
// 执行其他操作
|
|
||||||
abortController.abort();
|
|
||||||
})
|
|
||||||
|
|
||||||
let response = await askBing({
|
let response = await askBing({
|
||||||
text,
|
text,
|
||||||
onProgress: progressCallback.call(null, model, {
|
parentMessageId: userParentMessageId,
|
||||||
|
conversationId,
|
||||||
|
...endpointOption,
|
||||||
|
onProgress: progressCallback.call(null, {
|
||||||
res,
|
res,
|
||||||
text,
|
text,
|
||||||
parentMessageId: overrideParentMessageId || userMessageId
|
parentMessageId: overrideParentMessageId || userMessageId
|
||||||
}),
|
}),
|
||||||
convo: {
|
|
||||||
...convo,
|
|
||||||
parentMessageId: userParentMessageId,
|
|
||||||
conversationId
|
|
||||||
},
|
|
||||||
abortController
|
abortController
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log('BING RESPONSE', response);
|
console.log('BING RESPONSE', response);
|
||||||
// console.dir(response, { depth: null });
|
|
||||||
|
|
||||||
userMessage.conversationSignature =
|
userMessage.conversationSignature =
|
||||||
convo.conversationSignature || response.conversationSignature;
|
endpointOption.conversationSignature || response.conversationSignature;
|
||||||
userMessage.conversationId = response.conversationId || conversationId;
|
userMessage.conversationId = response.conversationId || conversationId;
|
||||||
userMessage.invocationId = response.invocationId;
|
userMessage.invocationId = endpointOption.invocationId;
|
||||||
userMessage.messageId = response.details.requestId || userMessageId;
|
userMessage.messageId = response.details.requestId || userMessageId;
|
||||||
if (!overrideParentMessageId)
|
if (!overrideParentMessageId) await saveBingMessage({ oldMessageId: userMessageId, ...userMessage });
|
||||||
await saveBingMessage({ oldMessageId: userMessageId, ...userMessage });
|
|
||||||
|
|
||||||
// Bing API will not use our conversationId at the first time,
|
// Bing API will not use our conversationId at the first time,
|
||||||
// so change the placeholder conversationId to the real one.
|
// so change the placeholder conversationId to the real one.
|
||||||
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
||||||
// but in this situation, don't change the conversationId, but create new convo.
|
// but in this situation, don't change the conversationId, but create new convo.
|
||||||
if (conversationId != userMessage.conversationId && isNewConversation)
|
if (conversationId != userMessage.conversationId && isNewConversation)
|
||||||
await saveConvo(
|
await saveConvo(req?.session?.user?.username, {
|
||||||
req?.session?.user?.username,
|
conversationId: conversationId,
|
||||||
{
|
newConversationId: userMessage.conversationId
|
||||||
conversationId: conversationId,
|
});
|
||||||
newConversationId: userMessage.conversationId
|
|
||||||
}
|
|
||||||
);
|
|
||||||
conversationId = userMessage.conversationId;
|
conversationId = userMessage.conversationId;
|
||||||
|
|
||||||
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
||||||
// delete response.response;
|
// delete response.response;
|
||||||
// response.id = response.details.messageId;
|
// response.id = response.details.messageId;
|
||||||
response.suggestions =
|
response.suggestions =
|
||||||
response.details.suggestedResponses &&
|
response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text);
|
||||||
response.details.suggestedResponses.map((s) => s.text);
|
response.sender = endpointOption?.jailbreak ? 'Sydney' : 'BingAI';
|
||||||
response.sender = model;
|
|
||||||
// response.final = true;
|
// response.final = true;
|
||||||
|
|
||||||
response.messageId = response.details.messageId;
|
response.messageId = response.details.messageId;
|
||||||
// override the parentMessageId, for the regeneration.
|
// override the parentMessageId, for the regeneration.
|
||||||
response.parentMessageId =
|
response.parentMessageId = overrideParentMessageId || response.details.requestId || userMessageId;
|
||||||
overrideParentMessageId || response.details.requestId || userMessageId;
|
|
||||||
|
|
||||||
response.text = await handleText(response, true);
|
response.text = await handleText(response, true);
|
||||||
await saveBingMessage(response);
|
await saveBingMessage(response);
|
||||||
await saveConvo(req?.session?.user?.username, { model, chatGptLabel: null, promptPrefix: null, ...convo, ...response });
|
await saveConvo(req?.session?.user?.username, {
|
||||||
|
...endpointOption,
|
||||||
|
...response
|
||||||
|
});
|
||||||
|
|
||||||
sendMessage(res, {
|
sendMessage(res, {
|
||||||
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
final: true,
|
final: true,
|
||||||
|
conversation: await getConvo(req?.session?.user?.username, conversationId),
|
||||||
requestMessage: userMessage,
|
requestMessage: userMessage,
|
||||||
responseMessage: response
|
responseMessage: response
|
||||||
});
|
});
|
||||||
res.end();
|
res.end();
|
||||||
|
|
||||||
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
const title = await titleConvo({ model, text, response });
|
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response });
|
||||||
|
|
||||||
await saveConvo(
|
await saveConvo(req?.session?.user?.username, {
|
||||||
req?.session?.user?.username,
|
conversationId: conversationId,
|
||||||
{
|
title
|
||||||
...convo,
|
});
|
||||||
...response,
|
|
||||||
conversationId,
|
|
||||||
title
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
// await deleteMessages({ messageId: userMessageId });
|
|
||||||
const errorMessage = {
|
const errorMessage = {
|
||||||
messageId: crypto.randomUUID(),
|
messageId: crypto.randomUUID(),
|
||||||
sender: model,
|
sender: endpointOption?.jailbreak ? 'Sydney' : 'BingAI',
|
||||||
conversationId,
|
conversationId,
|
||||||
parentMessageId: overrideParentMessageId || userMessageId,
|
parentMessageId: overrideParentMessageId || userMessageId,
|
||||||
error: true,
|
error: true,
|
||||||
|
|
@ -187,4 +175,4 @@ const ask = async ({
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
156
api/server/routes/askChatGPTBrowser.js
Normal file
156
api/server/routes/askChatGPTBrowser.js
Normal file
|
|
@ -0,0 +1,156 @@
|
||||||
|
const express = require('express');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const router = express.Router();
|
||||||
|
const { titleConvo, browserClient } = require('../../app/');
|
||||||
|
const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../models');
|
||||||
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
|
|
||||||
|
router.post('/', async (req, res) => {
|
||||||
|
const {
|
||||||
|
endpoint,
|
||||||
|
text,
|
||||||
|
overrideParentMessageId = null,
|
||||||
|
parentMessageId,
|
||||||
|
conversationId: oldConversationId
|
||||||
|
} = req.body;
|
||||||
|
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
|
||||||
|
if (endpoint !== 'chatGPTBrowser') return handleError(res, { text: 'Illegal request' });
|
||||||
|
|
||||||
|
// build user message
|
||||||
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
|
const userMessageId = crypto.randomUUID();
|
||||||
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
|
const userMessage = {
|
||||||
|
messageId: userMessageId,
|
||||||
|
sender: 'User',
|
||||||
|
text,
|
||||||
|
parentMessageId: userParentMessageId,
|
||||||
|
conversationId,
|
||||||
|
isCreatedByUser: true
|
||||||
|
};
|
||||||
|
|
||||||
|
// build endpoint option
|
||||||
|
const endpointOption = {
|
||||||
|
model: req.body?.model || 'text-davinci-002-render-sha'
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('ask log', {
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!overrideParentMessageId) {
|
||||||
|
await saveMessage(userMessage);
|
||||||
|
await saveConvo(req?.session?.user?.username, { ...userMessage, ...endpointOption, conversationId });
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ask({
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId,
|
||||||
|
preSendRequest: true,
|
||||||
|
overrideParentMessageId,
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const ask = async ({
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId,
|
||||||
|
preSendRequest = true,
|
||||||
|
overrideParentMessageId = null,
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
}) => {
|
||||||
|
const { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||||
|
|
||||||
|
const client = browserClient;
|
||||||
|
|
||||||
|
res.writeHead(200, {
|
||||||
|
Connection: 'keep-alive',
|
||||||
|
'Content-Type': 'text/event-stream',
|
||||||
|
'Cache-Control': 'no-cache, no-transform',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'X-Accel-Buffering': 'no'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (preSendRequest) sendMessage(res, { message: userMessage, created: true });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const progressCallback = createOnProgress();
|
||||||
|
const abortController = new AbortController();
|
||||||
|
res.on('close', () => abortController.abort());
|
||||||
|
let gptResponse = await client({
|
||||||
|
text,
|
||||||
|
parentMessageId: userParentMessageId,
|
||||||
|
conversationId,
|
||||||
|
...endpointOption,
|
||||||
|
onProgress: progressCallback.call(null, { res, text }),
|
||||||
|
abortController
|
||||||
|
});
|
||||||
|
|
||||||
|
gptResponse.text = gptResponse.response;
|
||||||
|
console.log('CLIENT RESPONSE', gptResponse);
|
||||||
|
|
||||||
|
if (!gptResponse.parentMessageId) {
|
||||||
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||||
|
delete gptResponse.response;
|
||||||
|
}
|
||||||
|
|
||||||
|
gptResponse.sender = 'ChatGPT';
|
||||||
|
// gptResponse.model = model;
|
||||||
|
gptResponse.text = await handleText(gptResponse);
|
||||||
|
// if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
||||||
|
// gptResponse.chatGptLabel = convo.chatGptLabel;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (convo.promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
||||||
|
// gptResponse.promptPrefix = convo.promptPrefix;
|
||||||
|
// }
|
||||||
|
|
||||||
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||||
|
|
||||||
|
if (userParentMessageId.startsWith('000')) {
|
||||||
|
await saveMessage({ ...userMessage, conversationId: gptResponse.conversationId });
|
||||||
|
}
|
||||||
|
|
||||||
|
await saveMessage(gptResponse);
|
||||||
|
await updateConvo(req?.session?.user?.username, {
|
||||||
|
...gptResponse,
|
||||||
|
oldConvoId: conversationId
|
||||||
|
});
|
||||||
|
|
||||||
|
sendMessage(res, {
|
||||||
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
final: true,
|
||||||
|
conversation: await getConvo(req?.session?.user?.username, conversationId),
|
||||||
|
requestMessage: userMessage,
|
||||||
|
responseMessage: gptResponse
|
||||||
|
});
|
||||||
|
res.end();
|
||||||
|
|
||||||
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
|
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response: gptResponse });
|
||||||
|
await updateConvo(req?.session?.user?.username, {
|
||||||
|
conversationId: gptResponse.conversationId,
|
||||||
|
title
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = {
|
||||||
|
messageId: crypto.randomUUID(),
|
||||||
|
sender: 'ChatGPT',
|
||||||
|
conversationId,
|
||||||
|
parentMessageId: overrideParentMessageId || userMessageId,
|
||||||
|
error: true,
|
||||||
|
text: error.message
|
||||||
|
};
|
||||||
|
await saveMessage(errorMessage);
|
||||||
|
handleError(res, errorMessage);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
157
api/server/routes/askOpenAI.js
Normal file
157
api/server/routes/askOpenAI.js
Normal file
|
|
@ -0,0 +1,157 @@
|
||||||
|
const express = require('express');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const router = express.Router();
|
||||||
|
const { titleConvo, askClient } = require('../../app/');
|
||||||
|
const { saveMessage, getConvoTitle, saveConvo, updateConvo, getConvo } = require('../../models');
|
||||||
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
||||||
|
|
||||||
|
router.post('/', async (req, res) => {
|
||||||
|
const {
|
||||||
|
endpoint,
|
||||||
|
text,
|
||||||
|
overrideParentMessageId = null,
|
||||||
|
parentMessageId,
|
||||||
|
conversationId: oldConversationId
|
||||||
|
} = req.body;
|
||||||
|
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
|
||||||
|
if (endpoint !== 'openAI') return handleError(res, { text: 'Illegal request' });
|
||||||
|
|
||||||
|
// build user message
|
||||||
|
const conversationId = oldConversationId || crypto.randomUUID();
|
||||||
|
const userMessageId = crypto.randomUUID();
|
||||||
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
||||||
|
const userMessage = {
|
||||||
|
messageId: userMessageId,
|
||||||
|
sender: 'User',
|
||||||
|
text,
|
||||||
|
parentMessageId: userParentMessageId,
|
||||||
|
conversationId,
|
||||||
|
isCreatedByUser: true
|
||||||
|
};
|
||||||
|
|
||||||
|
// build endpoint option
|
||||||
|
const endpointOption = {
|
||||||
|
model: req.body?.model || 'gpt-3.5-turbo',
|
||||||
|
chatGptLabel: req.body?.chatGptLabel || null,
|
||||||
|
promptPrefix: req.body?.promptPrefix || null,
|
||||||
|
temperature: req.body?.temperature || 0.8,
|
||||||
|
top_p: req.body?.top_p || 1,
|
||||||
|
presence_penalty: req.body?.presence_penalty || 1
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('ask log', {
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!overrideParentMessageId) {
|
||||||
|
await saveMessage(userMessage);
|
||||||
|
await saveConvo(req?.session?.user?.username, { ...userMessage, ...endpointOption, conversationId });
|
||||||
|
}
|
||||||
|
|
||||||
|
return await ask({
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId,
|
||||||
|
preSendRequest: true,
|
||||||
|
overrideParentMessageId,
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const ask = async ({
|
||||||
|
userMessage,
|
||||||
|
endpointOption,
|
||||||
|
conversationId,
|
||||||
|
preSendRequest = true,
|
||||||
|
overrideParentMessageId = null,
|
||||||
|
req,
|
||||||
|
res
|
||||||
|
}) => {
|
||||||
|
const { text, parentMessageId: userParentMessageId, messageId: userMessageId } = userMessage;
|
||||||
|
|
||||||
|
const client = askClient;
|
||||||
|
|
||||||
|
res.writeHead(200, {
|
||||||
|
Connection: 'keep-alive',
|
||||||
|
'Content-Type': 'text/event-stream',
|
||||||
|
'Cache-Control': 'no-cache, no-transform',
|
||||||
|
'Access-Control-Allow-Origin': '*',
|
||||||
|
'X-Accel-Buffering': 'no'
|
||||||
|
});
|
||||||
|
|
||||||
|
if (preSendRequest) sendMessage(res, { message: userMessage, created: true });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const progressCallback = createOnProgress();
|
||||||
|
const abortController = new AbortController();
|
||||||
|
res.on('close', () => abortController.abort());
|
||||||
|
let gptResponse = await client({
|
||||||
|
text,
|
||||||
|
parentMessageId: userParentMessageId,
|
||||||
|
conversationId,
|
||||||
|
...endpointOption,
|
||||||
|
onProgress: progressCallback.call(null, { res, text }),
|
||||||
|
abortController
|
||||||
|
});
|
||||||
|
|
||||||
|
gptResponse.text = gptResponse.response;
|
||||||
|
console.log('CLIENT RESPONSE', gptResponse);
|
||||||
|
|
||||||
|
if (!gptResponse.parentMessageId) {
|
||||||
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||||
|
delete gptResponse.response;
|
||||||
|
}
|
||||||
|
|
||||||
|
gptResponse.sender = endpointOption?.chatGptLabel || 'ChatGPT';
|
||||||
|
// gptResponse.model = model;
|
||||||
|
gptResponse.text = await handleText(gptResponse);
|
||||||
|
// if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
||||||
|
// gptResponse.chatGptLabel = convo.chatGptLabel;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (convo.promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
||||||
|
// gptResponse.promptPrefix = convo.promptPrefix;
|
||||||
|
// }
|
||||||
|
|
||||||
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
||||||
|
|
||||||
|
await saveMessage(gptResponse);
|
||||||
|
await updateConvo(req?.session?.user?.username, {
|
||||||
|
...gptResponse,
|
||||||
|
oldConvoId: conversationId
|
||||||
|
});
|
||||||
|
|
||||||
|
sendMessage(res, {
|
||||||
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
||||||
|
final: true,
|
||||||
|
conversation: await getConvo(req?.session?.user?.username, conversationId),
|
||||||
|
requestMessage: userMessage,
|
||||||
|
responseMessage: gptResponse
|
||||||
|
});
|
||||||
|
res.end();
|
||||||
|
|
||||||
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
||||||
|
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response: gptResponse });
|
||||||
|
await updateConvo(req?.session?.user?.username, {
|
||||||
|
conversationId: conversationId,
|
||||||
|
title
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
const errorMessage = {
|
||||||
|
messageId: crypto.randomUUID(),
|
||||||
|
sender: endpointOption?.chatGptLabel || 'ChatGPT',
|
||||||
|
conversationId,
|
||||||
|
parentMessageId: overrideParentMessageId || userMessageId,
|
||||||
|
error: true,
|
||||||
|
text: error.message
|
||||||
|
};
|
||||||
|
await saveMessage(errorMessage);
|
||||||
|
handleError(res, errorMessage);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
|
|
@ -68,9 +68,8 @@ const createOnProgress = () => {
|
||||||
i++;
|
i++;
|
||||||
};
|
};
|
||||||
|
|
||||||
const onProgress = (model, opts) => {
|
const onProgress = opts => {
|
||||||
const bingModels = new Set(['bingai', 'sydney']);
|
return _.partialRight(progressCallback, opts);
|
||||||
return _.partialRight(progressCallback, { ...opts, bing: bingModels.has(model) });
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return onProgress;
|
return onProgress;
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ const router = createBrowserRouter([
|
||||||
const App = () => {
|
const App = () => {
|
||||||
const [user, setUser] = useRecoilState(store.user);
|
const [user, setUser] = useRecoilState(store.user);
|
||||||
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
|
const setIsSearchEnabled = useSetRecoilState(store.isSearchEnabled);
|
||||||
const setModelsFilter = useSetRecoilState(store.modelsFilter);
|
const setEndpointsFilter = useSetRecoilState(store.endpointsFilter);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// fetch if seatch enabled
|
// fetch if seatch enabled
|
||||||
|
|
@ -58,19 +58,12 @@ const App = () => {
|
||||||
|
|
||||||
// fetch models
|
// fetch models
|
||||||
axios
|
axios
|
||||||
.get('/api/models', {
|
.get('/api/endpoints', {
|
||||||
timeout: 1000,
|
timeout: 1000,
|
||||||
withCredentials: true
|
withCredentials: true
|
||||||
})
|
})
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
const filter = {
|
setEndpointsFilter(data);
|
||||||
chatgpt: data?.hasOpenAI,
|
|
||||||
chatgptCustom: data?.hasOpenAI,
|
|
||||||
bingai: data?.hasBing,
|
|
||||||
sydney: data?.hasBing,
|
|
||||||
chatgptBrowser: data?.hasChatGpt
|
|
||||||
};
|
|
||||||
setModelsFilter(filter);
|
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
import React, { useEffect, useRef, useState } from 'react';
|
import { useEffect } from 'react';
|
||||||
import { useRecoilState, useResetRecoilState, useSetRecoilState } from 'recoil';
|
import { useRecoilValue, useResetRecoilState, useSetRecoilState } from 'recoil';
|
||||||
import { SSE } from '~/utils/sse';
|
import { SSE } from '~/utils/sse';
|
||||||
import { useMessageHandler } from '../../utils/handleSubmit';
|
|
||||||
import createPayload from '~/utils/createPayload';
|
import createPayload from '~/utils/createPayload';
|
||||||
|
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
export default function MessageHandler({ messages }) {
|
export default function MessageHandler() {
|
||||||
const [submission, setSubmission] = useRecoilState(store.submission);
|
const submission = useRecoilValue(store.submission);
|
||||||
const [isSubmitting, setIsSubmitting] = useRecoilState(store.isSubmitting);
|
const setIsSubmitting = useSetRecoilState(store.isSubmitting);
|
||||||
const setMessages = useSetRecoilState(store.messages);
|
const setMessages = useSetRecoilState(store.messages);
|
||||||
const setConversation = useSetRecoilState(store.conversation);
|
const setConversation = useSetRecoilState(store.conversation);
|
||||||
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
const resetLatestMessage = useResetRecoilState(store.latestMessage);
|
||||||
|
|
@ -105,10 +104,9 @@ export default function MessageHandler({ messages }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
const finalHandler = (data, submission) => {
|
const finalHandler = (data, submission) => {
|
||||||
const { conversation, messages, message, initialResponse, isRegenerate = false } = submission;
|
const { messages, isRegenerate = false } = submission;
|
||||||
|
|
||||||
const { requestMessage, responseMessage } = data;
|
const { requestMessage, responseMessage, conversation } = data;
|
||||||
const { conversationId } = requestMessage;
|
|
||||||
|
|
||||||
// update the messages
|
// update the messages
|
||||||
if (isRegenerate) setMessages([...messages, responseMessage]);
|
if (isRegenerate) setMessages([...messages, responseMessage]);
|
||||||
|
|
@ -127,66 +125,14 @@ export default function MessageHandler({ messages }) {
|
||||||
}, 5000);
|
}, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { model, chatGptLabel, promptPrefix } = conversation;
|
setConversation(prevState => ({
|
||||||
const isBing = model === 'bingai' || model === 'sydney';
|
...prevState,
|
||||||
|
...conversation
|
||||||
if (!isBing) {
|
}));
|
||||||
const { title } = data;
|
|
||||||
const { conversationId } = responseMessage;
|
|
||||||
setConversation(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
title,
|
|
||||||
conversationId,
|
|
||||||
jailbreakConversationId: null,
|
|
||||||
conversationSignature: null,
|
|
||||||
clientId: null,
|
|
||||||
invocationId: null,
|
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix,
|
|
||||||
latestMessage: null
|
|
||||||
}));
|
|
||||||
} else if (model === 'bingai') {
|
|
||||||
const { title } = data;
|
|
||||||
const { conversationSignature, clientId, conversationId, invocationId } = responseMessage;
|
|
||||||
setConversation(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
title,
|
|
||||||
conversationId,
|
|
||||||
jailbreakConversationId: null,
|
|
||||||
conversationSignature,
|
|
||||||
clientId,
|
|
||||||
invocationId,
|
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix,
|
|
||||||
latestMessage: null
|
|
||||||
}));
|
|
||||||
} else if (model === 'sydney') {
|
|
||||||
const { title } = data;
|
|
||||||
const {
|
|
||||||
jailbreakConversationId,
|
|
||||||
parentMessageId,
|
|
||||||
conversationSignature,
|
|
||||||
clientId,
|
|
||||||
conversationId,
|
|
||||||
invocationId
|
|
||||||
} = responseMessage;
|
|
||||||
setConversation(prevState => ({
|
|
||||||
...prevState,
|
|
||||||
title,
|
|
||||||
conversationId,
|
|
||||||
jailbreakConversationId,
|
|
||||||
conversationSignature,
|
|
||||||
clientId,
|
|
||||||
invocationId,
|
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix,
|
|
||||||
latestMessage: null
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const errorHandler = (data, submission) => {
|
const errorHandler = (data, submission) => {
|
||||||
const { conversation, messages, message, initialResponse, isRegenerate = false } = submission;
|
const { messages, message } = submission;
|
||||||
|
|
||||||
console.log('Error:', data);
|
console.log('Error:', data);
|
||||||
const errorResponse = {
|
const errorResponse = {
|
||||||
|
|
@ -203,7 +149,6 @@ export default function MessageHandler({ messages }) {
|
||||||
if (submission === null) return;
|
if (submission === null) return;
|
||||||
if (Object.keys(submission).length === 0) return;
|
if (Object.keys(submission).length === 0) return;
|
||||||
|
|
||||||
const { messages, initialResponse, isRegenerate = false } = submission;
|
|
||||||
let { message } = submission;
|
let { message } = submission;
|
||||||
|
|
||||||
const { server, payload } = createPayload(submission);
|
const { server, payload } = createPayload(submission);
|
||||||
|
|
@ -224,9 +169,6 @@ export default function MessageHandler({ messages }) {
|
||||||
if (data.created) {
|
if (data.created) {
|
||||||
message = {
|
message = {
|
||||||
...data.message,
|
...data.message,
|
||||||
model: message?.model,
|
|
||||||
chatGptLabel: message?.chatGptLabel,
|
|
||||||
promptPrefix: message?.promptPrefix,
|
|
||||||
overrideParentMessageId: message?.overrideParentMessageId
|
overrideParentMessageId: message?.overrideParentMessageId
|
||||||
};
|
};
|
||||||
createdHandler(data, { ...submission, message });
|
createdHandler(data, { ...submission, message });
|
||||||
|
|
@ -245,7 +187,7 @@ export default function MessageHandler({ messages }) {
|
||||||
|
|
||||||
events.onopen = () => console.log('connection is opened');
|
events.onopen = () => console.log('connection is opened');
|
||||||
|
|
||||||
events.oncancel = e => cancelHandler(latestResponseText, { ...submission, message });
|
events.oncancel = () => cancelHandler(latestResponseText, { ...submission, message });
|
||||||
|
|
||||||
events.onerror = function (e) {
|
events.onerror = function (e) {
|
||||||
console.log('error in opening conn.');
|
console.log('error in opening conn.');
|
||||||
|
|
|
||||||
|
|
@ -97,6 +97,8 @@ export default function Messages({ isSearchView = false }) {
|
||||||
if (model) _title += `: ${model}`;
|
if (model) _title += `: ${model}`;
|
||||||
} else if (endpoint === null) {
|
} else if (endpoint === null) {
|
||||||
null;
|
null;
|
||||||
|
} else {
|
||||||
|
null;
|
||||||
}
|
}
|
||||||
return _title;
|
return _title;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -79,7 +79,7 @@ const useConversation = () => {
|
||||||
if (endpoint === null)
|
if (endpoint === null)
|
||||||
// get the default model
|
// get the default model
|
||||||
conversation = getDefaultConversation({ conversation, availableEndpoints, prevConversation });
|
conversation = getDefaultConversation({ conversation, availableEndpoints, prevConversation });
|
||||||
|
console.log(conversation);
|
||||||
setConversation(conversation);
|
setConversation(conversation);
|
||||||
setMessages(messages);
|
setMessages(messages);
|
||||||
resetLatestMessage();
|
resetLatestMessage();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import conversation from './conversation';
|
import conversation from './conversation';
|
||||||
import conversations from './conversations';
|
import conversations from './conversations';
|
||||||
import models from './models';
|
import models from './models';
|
||||||
|
import endpoints from './endpoints';
|
||||||
import user from './user';
|
import user from './user';
|
||||||
import text from './text';
|
import text from './text';
|
||||||
import submission from './submission';
|
import submission from './submission';
|
||||||
|
|
@ -10,6 +11,7 @@ export default {
|
||||||
...conversation,
|
...conversation,
|
||||||
...conversations,
|
...conversations,
|
||||||
...models,
|
...models,
|
||||||
|
...endpoints,
|
||||||
...user,
|
...user,
|
||||||
text,
|
text,
|
||||||
...submission,
|
...submission,
|
||||||
|
|
|
||||||
|
|
@ -1,55 +1,22 @@
|
||||||
export default function createPayload(submission) {
|
export default function createPayload(submission) {
|
||||||
const { conversation, messages, message, initialResponse, isRegenerate = false } = submission;
|
const { conversation, message, endpointOption } = submission;
|
||||||
|
const { conversationId } = conversation;
|
||||||
|
const { endpoint } = endpointOption;
|
||||||
|
|
||||||
const endpoint = `/api/ask`;
|
const endpointUrlMap = {
|
||||||
const {
|
azureOpenAI: '/api/ask/azureOpenAI',
|
||||||
model,
|
openAI: '/api/ask/openAI',
|
||||||
chatGptLabel,
|
bingAI: '/api/ask/bingAI',
|
||||||
promptPrefix,
|
chatGPTBrowser: '/api/ask/chatGPTBrowser'
|
||||||
jailbreakConversationId,
|
};
|
||||||
conversationId,
|
|
||||||
conversationSignature,
|
const server = endpointUrlMap[endpoint];
|
||||||
clientId,
|
|
||||||
invocationId,
|
|
||||||
toneStyle
|
|
||||||
} = conversation;
|
|
||||||
|
|
||||||
let payload = {
|
let payload = {
|
||||||
...message,
|
...message,
|
||||||
...{
|
...endpointOption,
|
||||||
model,
|
conversationId
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix,
|
|
||||||
conversationId
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// if (!payload.conversationId)
|
|
||||||
// if (convo?.conversationId && convo?.parentMessageId) {
|
|
||||||
// payload = {
|
|
||||||
// ...payload,
|
|
||||||
// conversationId: convo.conversationId,
|
|
||||||
// parentMessageId: convo.parentMessageId || '00000000-0000-0000-0000-000000000000'
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
const isBing = model === 'bingai' || model === 'sydney';
|
|
||||||
if (isBing && !conversationId) {
|
|
||||||
payload.toneStyle = toneStyle || 'fast';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isBing && conversationId) {
|
|
||||||
payload = {
|
|
||||||
...payload,
|
|
||||||
jailbreakConversationId,
|
|
||||||
conversationSignature,
|
|
||||||
clientId,
|
|
||||||
invocationId
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
let server = endpoint;
|
|
||||||
server = model === 'bingai' ? server + '/bing' : server;
|
|
||||||
server = model === 'sydney' ? server + '/sydney' : server;
|
|
||||||
return { server, payload };
|
return { server, payload };
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ const buildDefaultConversation = ({ conversation, endpoint, lastConversationSetu
|
||||||
conversationSignature: lastConversationSetup?.conversationSignature || null,
|
conversationSignature: lastConversationSetup?.conversationSignature || null,
|
||||||
clientId: lastConversationSetup?.clientId || null,
|
clientId: lastConversationSetup?.clientId || null,
|
||||||
invocationId: lastConversationSetup?.invocationId || null,
|
invocationId: lastConversationSetup?.invocationId || null,
|
||||||
toneStyle: lastConversationSetup?.toneStyle || null,
|
toneStyle: lastConversationSetup?.toneStyle || 'fast',
|
||||||
suggestions: lastConversationSetup?.suggestions || []
|
suggestions: lastConversationSetup?.suggestions || []
|
||||||
};
|
};
|
||||||
} else if (endpoint === 'chatGPTBrowser') {
|
} else if (endpoint === 'chatGPTBrowser') {
|
||||||
|
|
@ -33,6 +33,12 @@ const buildDefaultConversation = ({ conversation, endpoint, lastConversationSetu
|
||||||
...conversation,
|
...conversation,
|
||||||
endpoint
|
endpoint
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
console.error(`Unknown endpoint ${endpoint}`);
|
||||||
|
conversation = {
|
||||||
|
...conversation,
|
||||||
|
endpoint: null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return conversation;
|
return conversation;
|
||||||
|
|
|
||||||
|
|
@ -27,10 +27,7 @@ const getIcon = props => {
|
||||||
else if (!isCreatedByUser) {
|
else if (!isCreatedByUser) {
|
||||||
const { endpoint, error } = props;
|
const { endpoint, error } = props;
|
||||||
|
|
||||||
let icon = <GPTIcon size={size * 0.7} />;
|
let icon, bg, name;
|
||||||
let bg = 'grey';
|
|
||||||
let name = 'UNKNOWN';
|
|
||||||
|
|
||||||
if (endpoint === 'azureOpenAI') {
|
if (endpoint === 'azureOpenAI') {
|
||||||
const { chatGptLabel } = props;
|
const { chatGptLabel } = props;
|
||||||
|
|
||||||
|
|
@ -59,6 +56,10 @@ const getIcon = props => {
|
||||||
icon = <GPTIcon size={size * 0.7} />;
|
icon = <GPTIcon size={size * 0.7} />;
|
||||||
bg = `grey`;
|
bg = `grey`;
|
||||||
name = 'N/A';
|
name = 'N/A';
|
||||||
|
} else {
|
||||||
|
icon = <GPTIcon size={size * 0.7} />;
|
||||||
|
bg = `grey`;
|
||||||
|
name = 'UNKNOWN';
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,14 @@
|
||||||
// import resetConvo from './resetConvo';
|
|
||||||
// import { useSelector, useDispatch } from 'react-redux';
|
|
||||||
// import { setNewConvo } from '~/store/convoSlice';
|
|
||||||
// import { setMessages } from '~/store/messageSlice';
|
|
||||||
// import { setSubmitState, setSubmission } from '~/store/submitSlice';
|
|
||||||
// import { setText } from '~/store/textSlice';
|
|
||||||
// import { setError } from '~/store/convoSlice';
|
|
||||||
import { v4 } from 'uuid';
|
import { v4 } from 'uuid';
|
||||||
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil';
|
||||||
|
|
||||||
import store from '~/store';
|
import store from '~/store';
|
||||||
|
|
||||||
const useMessageHandler = () => {
|
const useMessageHandler = () => {
|
||||||
// const dispatch = useDispatch();
|
const currentConversation = useRecoilValue(store.conversation) || {};
|
||||||
// const convo = useSelector((state) => state.convo);
|
|
||||||
// const { initial } = useSelector((state) => state.models);
|
|
||||||
// const { messages } = useSelector((state) => state.messages);
|
|
||||||
// const { model, chatGptLabel, promptPrefix, isSubmitting } = useSelector((state) => state.submit);
|
|
||||||
// const { latestMessage, error } = convo;
|
|
||||||
|
|
||||||
const [currentConversation, setCurrentConversation] = useRecoilState(store.conversation) || {};
|
|
||||||
const setSubmission = useSetRecoilState(store.submission);
|
const setSubmission = useSetRecoilState(store.submission);
|
||||||
const isSubmitting = useRecoilValue(store.isSubmitting);
|
const isSubmitting = useRecoilValue(store.isSubmitting);
|
||||||
|
|
||||||
const latestMessage = useRecoilValue(store.latestMessage);
|
const latestMessage = useRecoilValue(store.latestMessage);
|
||||||
const { error } = currentConversation;
|
|
||||||
|
|
||||||
const [messages, setMessages] = useRecoilState(store.messages);
|
const [messages, setMessages] = useRecoilState(store.messages);
|
||||||
|
|
||||||
|
|
@ -36,16 +21,53 @@ const useMessageHandler = () => {
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine the model to be used
|
// determine the model to be used
|
||||||
const { model = null, chatGptLabel = null, promptPrefix = null } = currentConversation;
|
const { endpoint } = currentConversation;
|
||||||
|
let endpointOption = {};
|
||||||
|
let responseSender = '';
|
||||||
|
if (endpoint === 'azureOpenAI' || endpoint === 'openAI') {
|
||||||
|
endpointOption = {
|
||||||
|
endpoint,
|
||||||
|
model: currentConversation?.model || 'gpt-3.5-turbo',
|
||||||
|
chatGptLabel: currentConversation?.chatGptLabel || null,
|
||||||
|
promptPrefix: currentConversation?.promptPrefix || null,
|
||||||
|
temperature: currentConversation?.temperature || 0.8,
|
||||||
|
top_p: currentConversation?.top_p || 1,
|
||||||
|
presence_penalty: currentConversation?.presence_penalty || 1
|
||||||
|
};
|
||||||
|
responseSender = endpointOption.chatGptLabel || 'ChatGPT';
|
||||||
|
} else if (endpoint === 'bingAI') {
|
||||||
|
endpointOption = {
|
||||||
|
endpoint,
|
||||||
|
jailbreak: currentConversation?.jailbreak || false,
|
||||||
|
jailbreakConversationId: currentConversation?.jailbreakConversationId || null,
|
||||||
|
conversationSignature: currentConversation?.conversationSignature || null,
|
||||||
|
clientId: currentConversation?.clientId || null,
|
||||||
|
invocationId: currentConversation?.invocationId || null,
|
||||||
|
toneStyle: currentConversation?.toneStyle || 'fast',
|
||||||
|
suggestions: currentConversation?.suggestions || []
|
||||||
|
};
|
||||||
|
responseSender = endpointOption.jailbreak ? 'Sydney' : 'BingAI';
|
||||||
|
} else if (endpoint === 'chatGPTBrowser') {
|
||||||
|
endpointOption = {
|
||||||
|
endpoint,
|
||||||
|
model: currentConversation?.model || 'text-davinci-002-render-sha'
|
||||||
|
};
|
||||||
|
responseSender = 'ChatGPT';
|
||||||
|
} else if (endpoint === null) {
|
||||||
|
console.error('No endpoint available');
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
console.error(`Unknown endpoint ${endpoint}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let currentMessages = messages;
|
||||||
|
|
||||||
// construct the query message
|
// construct the query message
|
||||||
// this is not a real messageId, it is used as placeholder before real messageId returned
|
// this is not a real messageId, it is used as placeholder before real messageId returned
|
||||||
text = text.trim();
|
text = text.trim();
|
||||||
const fakeMessageId = v4();
|
const fakeMessageId = v4();
|
||||||
// const isCustomModel = model === 'chatgptCustom' || !initial[model];
|
|
||||||
// const sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
|
||||||
parentMessageId = parentMessageId || latestMessage?.messageId || '00000000-0000-0000-0000-000000000000';
|
parentMessageId = parentMessageId || latestMessage?.messageId || '00000000-0000-0000-0000-000000000000';
|
||||||
let currentMessages = messages;
|
|
||||||
conversationId = conversationId || currentConversation?.conversationId;
|
conversationId = conversationId || currentConversation?.conversationId;
|
||||||
if (conversationId == 'search') {
|
if (conversationId == 'search') {
|
||||||
console.error('cannot send any message under search view!');
|
console.error('cannot send any message under search view!');
|
||||||
|
|
@ -68,7 +90,7 @@ const useMessageHandler = () => {
|
||||||
|
|
||||||
// construct the placeholder response message
|
// construct the placeholder response message
|
||||||
const initialResponse = {
|
const initialResponse = {
|
||||||
sender: chatGptLabel || model,
|
sender: responseSender,
|
||||||
text: '<span className="result-streaming">█</span>',
|
text: '<span className="result-streaming">█</span>',
|
||||||
parentMessageId: isRegenerate ? messageId : fakeMessageId,
|
parentMessageId: isRegenerate ? messageId : fakeMessageId,
|
||||||
messageId: (isRegenerate ? messageId : fakeMessageId) + '_',
|
messageId: (isRegenerate ? messageId : fakeMessageId) + '_',
|
||||||
|
|
@ -79,16 +101,11 @@ const useMessageHandler = () => {
|
||||||
const submission = {
|
const submission = {
|
||||||
conversation: {
|
conversation: {
|
||||||
...currentConversation,
|
...currentConversation,
|
||||||
conversationId,
|
conversationId
|
||||||
model,
|
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix
|
|
||||||
},
|
},
|
||||||
|
endpointOption,
|
||||||
message: {
|
message: {
|
||||||
...currentMsg,
|
...currentMsg,
|
||||||
model,
|
|
||||||
chatGptLabel,
|
|
||||||
promptPrefix,
|
|
||||||
overrideParentMessageId: isRegenerate ? messageId : null
|
overrideParentMessageId: isRegenerate ? messageId : null
|
||||||
},
|
},
|
||||||
messages: currentMessages,
|
messages: currentMessages,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue