LibreChat/api/server/routes/ask/askGoogle.js

157 lines
4.7 KiB
JavaScript
Raw Normal View History

Feat: PaLM 2 (#262) * feat(api): add googleapis package to package.json feat(api): add reqDemo.js file to make a request to Google Cloud AI Platform API to get a response from a chatbot model. * feat: add PaLM2 support * feat(conversationPreset.js): add support for topP and topK for google endpoint feat(askGoogle.js): add support for topP and topK for google endpoint feat(ask/index.js): add google endpoint feat(endpoints.js): add google endpoint feat(MessageHeader.jsx): add support for modelLabel for google endpoint feat(PresetItem.jsx): add support for modelLabel for google endpoint feat(HoverButtons.jsx): add support for google endpoint feat(createPayload.ts): add google endpoint feat(types.ts): add google endpoint feat(store/endpoints.js): add google endpoint feat(cleanupPreset.js): add support for topP and topK for google endpoint feat(getDefaultConversation.js): add support for topP and topK for google endpoint feat(handleSubmit.js): add support for topP and topK for google endpoint * fix: messages payload * refactor(GoogleClient.js): set maxContextTokens based on isTextModel value feat(GoogleClient.js): add delay option to TextStream constructor feat(getIcon.jsx): add support for google endpoint and PaLM2 model label * feat: palm frontend changes * feat(askGoogle.js): set default example to empty input and output feat(Examples.jsx): add ability to add and remove examples refactor(Settings.jsx): remove examples from props and setOption function style(GoogleOptions): remove unnecessary whitespace after Settings2 import feat(GoogleOptions): add addExample and removeExample functions to manage examples fix(cleanupPreset): set default example to [{ input: '', output: ''}] fix(getDefaultConversation): set default example to [{ input: '', output: ''}] fix(handleSubmit): set default example to [{ input: '', output: ''}] * style(client): adjust height of settings and examples components to 350px fix(client): fix path to palm.png image in getIcon.jsx file * style(EndpointOptionsPopover.jsx, Examples.jsx, Settings.jsx): improve button styles and update input placeholders * feat (palm): finalize examples on the frontend * feat(GoogleClient.js): filter out empty examples in options feat(GoogleClient.js): add support for promptPrefix in buildPayload method feat(GoogleClient.js): add support for examples in buildPayload method feat(conversationPreset.js): add maxOutputTokens field to conversation preset schema feat(presetSchema.js): add examples field to preset schema feat(askGoogle.js): add support for examples and promptPrefix in endpointOption feat(EditPresetDialog.jsx): add Examples component for Google endpoint feat(EditPresetDialog.jsx): add button to show/hide Examples component feat(EditPresetDialog.jsx): add functionality to add, remove, and edit examples in Examples component feat(EndpointOptionsDialog.jsx): change endpoint name to PaLM for Google endpoint feat(Settings.jsx): add maxHeight prop to limit height of Settings component in EditPresetDialog and EndpointOptionsDialog fix(Settings.jsx): add examples prop to ChatGPTBrowser component fix(EndpointItem.jsx): add alternate name for google endpoint fix(MessageHeader.jsx): change title for google endpoint to PaLM feat(endpoints.js): add google endpoint to endpointsConfig fix(cleanupPreset.js): add missing comma in examples array * chore: change endpoint order * feat(PaLM 2): complete for testing * fix(PaLM): handle blocked messages
2023-05-13 16:29:06 -04:00
const express = require('express');
const router = express.Router();
const { titleConvo } = require('../../../app/');
const GoogleClient = require('../../../app/google/GoogleClient');
const { saveMessage, getConvoTitle, saveConvo, getConvo } = require('../../../models');
const { handleError, sendMessage, createOnProgress } = require('./handlers');
const requireJwtAuth = require('../../../middleware/requireJwtAuth');
router.post('/', requireJwtAuth, async (req, res) => {
const { endpoint, text, parentMessageId, conversationId } = req.body;
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
if (endpoint !== 'google') return handleError(res, { text: 'Illegal request' });
// build endpoint option
const endpointOption = {
examples: req.body?.examples ?? [{ input: { content: '' }, output: { content: '' } }],
promptPrefix: req.body?.promptPrefix ?? null,
token: req.body?.token ?? null,
modelOptions: {
model: req.body?.model ?? 'chat-bison',
modelLabel: req.body?.modelLabel ?? null,
temperature: req.body?.temperature ?? 0.2,
maxOutputTokens: req.body?.maxOutputTokens ?? 1024,
topP: req.body?.topP ?? 0.95,
topK: req.body?.topK ?? 40
}
};
const availableModels = ['chat-bison', 'text-bison'];
if (availableModels.find(model => model === endpointOption.modelOptions.model) === undefined) {
return handleError(res, { text: `Illegal request: model` });
}
// eslint-disable-next-line no-use-before-define
return await ask({
text,
endpointOption,
conversationId,
parentMessageId,
req,
res
});
});
const ask = async ({ text, endpointOption, parentMessageId = null, conversationId, req, res }) => {
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'
});
let userMessage;
let userMessageId;
let responseMessageId;
let lastSavedTimestamp = 0;
try {
const getIds = (data) => {
userMessage = data.userMessage;
userMessageId = userMessage.messageId;
responseMessageId = data.responseMessageId;
if (!conversationId) {
conversationId = data.conversationId;
}
};
const { onProgress: progressCallback } = createOnProgress({
onProgress: ({ text: partialText }) => {
const currentTimestamp = Date.now();
if (currentTimestamp - lastSavedTimestamp > 500) {
lastSavedTimestamp = currentTimestamp;
saveMessage({
messageId: responseMessageId,
sender: 'PaLM2',
conversationId,
parentMessageId: userMessageId,
text: partialText,
unfinished: true,
cancelled: false,
error: false
});
}
}
});
const abortController = new AbortController();
let key;
if (endpointOption.token) {
key = JSON.parse(endpointOption.token);
delete endpointOption.token;
console.log('Using service account key provided by User for PaLM models');
}
try {
if (!key) {
key = require('../../../data/auth.json');
Feat: PaLM 2 (#262) * feat(api): add googleapis package to package.json feat(api): add reqDemo.js file to make a request to Google Cloud AI Platform API to get a response from a chatbot model. * feat: add PaLM2 support * feat(conversationPreset.js): add support for topP and topK for google endpoint feat(askGoogle.js): add support for topP and topK for google endpoint feat(ask/index.js): add google endpoint feat(endpoints.js): add google endpoint feat(MessageHeader.jsx): add support for modelLabel for google endpoint feat(PresetItem.jsx): add support for modelLabel for google endpoint feat(HoverButtons.jsx): add support for google endpoint feat(createPayload.ts): add google endpoint feat(types.ts): add google endpoint feat(store/endpoints.js): add google endpoint feat(cleanupPreset.js): add support for topP and topK for google endpoint feat(getDefaultConversation.js): add support for topP and topK for google endpoint feat(handleSubmit.js): add support for topP and topK for google endpoint * fix: messages payload * refactor(GoogleClient.js): set maxContextTokens based on isTextModel value feat(GoogleClient.js): add delay option to TextStream constructor feat(getIcon.jsx): add support for google endpoint and PaLM2 model label * feat: palm frontend changes * feat(askGoogle.js): set default example to empty input and output feat(Examples.jsx): add ability to add and remove examples refactor(Settings.jsx): remove examples from props and setOption function style(GoogleOptions): remove unnecessary whitespace after Settings2 import feat(GoogleOptions): add addExample and removeExample functions to manage examples fix(cleanupPreset): set default example to [{ input: '', output: ''}] fix(getDefaultConversation): set default example to [{ input: '', output: ''}] fix(handleSubmit): set default example to [{ input: '', output: ''}] * style(client): adjust height of settings and examples components to 350px fix(client): fix path to palm.png image in getIcon.jsx file * style(EndpointOptionsPopover.jsx, Examples.jsx, Settings.jsx): improve button styles and update input placeholders * feat (palm): finalize examples on the frontend * feat(GoogleClient.js): filter out empty examples in options feat(GoogleClient.js): add support for promptPrefix in buildPayload method feat(GoogleClient.js): add support for examples in buildPayload method feat(conversationPreset.js): add maxOutputTokens field to conversation preset schema feat(presetSchema.js): add examples field to preset schema feat(askGoogle.js): add support for examples and promptPrefix in endpointOption feat(EditPresetDialog.jsx): add Examples component for Google endpoint feat(EditPresetDialog.jsx): add button to show/hide Examples component feat(EditPresetDialog.jsx): add functionality to add, remove, and edit examples in Examples component feat(EndpointOptionsDialog.jsx): change endpoint name to PaLM for Google endpoint feat(Settings.jsx): add maxHeight prop to limit height of Settings component in EditPresetDialog and EndpointOptionsDialog fix(Settings.jsx): add examples prop to ChatGPTBrowser component fix(EndpointItem.jsx): add alternate name for google endpoint fix(MessageHeader.jsx): change title for google endpoint to PaLM feat(endpoints.js): add google endpoint to endpointsConfig fix(cleanupPreset.js): add missing comma in examples array * chore: change endpoint order * feat(PaLM 2): complete for testing * fix(PaLM): handle blocked messages
2023-05-13 16:29:06 -04:00
}
} catch (e) {
console.log("No 'auth.json' file (service account key) found in /api/data/ for PaLM models");
}
const clientOptions = {
// debug: true, // for testing
reverseProxyUrl: process.env.GOOGLE_REVERSE_PROXY || null,
proxy: process.env.PROXY || null,
...endpointOption
};
const client = new GoogleClient(key, clientOptions);
let response = await client.sendMessage(text, {
getIds,
user: req.user.id,
parentMessageId,
conversationId,
onProgress: progressCallback.call(null, { res, text, parentMessageId: userMessageId }),
abortController
});
await saveMessage(response);
sendMessage(res, {
title: await getConvoTitle(req.user.id, conversationId),
final: true,
conversation: await getConvo(req.user.id, conversationId),
requestMessage: userMessage,
responseMessage: response
});
res.end();
if (parentMessageId == '00000000-0000-0000-0000-000000000000') {
const title = await titleConvo({ text, response });
await saveConvo(req.user.id, {
conversationId: conversationId,
title
});
}
} catch (error) {
console.error(error);
const errorMessage = {
messageId: responseMessageId,
sender: 'PaLM2',
conversationId,
parentMessageId,
unfinished: false,
cancelled: false,
error: true,
text: error.message
};
await saveMessage(errorMessage);
handleError(res, errorMessage);
}
};
module.exports = router;