LibreChat/client/src/components/Input/index.jsx

194 lines
6.6 KiB
React
Raw Normal View History

2023-04-06 11:30:25 +09:00
import React, { useEffect, useRef, useState } from 'react';
import { useRecoilValue, useRecoilState } from 'recoil';
import SubmitButton from './SubmitButton';
2023-03-31 23:15:38 +08:00
import OpenAIOptions from './OpenAIOptions';
import ChatGPTOptions from './ChatGPTOptions';
import BingAIOptions from './BingAIOptions';
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
import GoogleOptions from './GoogleOptions';
// import BingStyles from './BingStyles';
2023-04-08 00:14:15 +08:00
import NewConversationMenu from './NewConversationMenu';
2023-04-06 11:30:25 +09:00
import AdjustToneButton from './AdjustToneButton';
import Footer from './Footer';
import TextareaAutosize from 'react-textarea-autosize';
import { useMessageHandler } from '../../utils/handleSubmit';
import store from '~/store';
export default function TextChat({ isSearchView = false }) {
const inputRef = useRef(null);
const isComposing = useRef(false);
const conversation = useRecoilValue(store.conversation);
const latestMessage = useRecoilValue(store.latestMessage);
2023-03-29 11:25:27 -04:00
const [text, setText] = useRecoilState(store.text);
// const [text, setText] = useState('');
const endpointsConfig = useRecoilValue(store.endpointsConfig);
const isSubmitting = useRecoilValue(store.isSubmitting);
// TODO: do we need this?
const disabled = false;
const { ask, stopGenerating } = useMessageHandler();
// const bingStylesRef = useRef(null);
2023-04-06 11:30:25 +09:00
const [showBingToneSetting, setShowBingToneSetting] = useState(false);
const isNotAppendable = (latestMessage?.unfinished & !isSubmitting) || latestMessage?.error;
// auto focus to input, when enter a conversation.
useEffect(() => {
if (conversation?.conversationId !== 'search') inputRef.current?.focus();
// setText('');
}, [conversation?.conversationId]);
// // controls the height of Bing tone style tabs
// useEffect(() => {
// if (!inputRef.current) {
// return; // wait for the ref to be available
// }
// const resizeObserver = new ResizeObserver(() => {
// const newHeight = inputRef.current.clientHeight;
// if (newHeight >= 24) {
// // 24 is the default height of the input
2023-04-06 11:30:25 +09:00
// bingStylesRef.current.style.bottom = 15 + newHeight + 'px';
// }
// });
// resizeObserver.observe(inputRef.current);
// return () => resizeObserver.disconnect();
// }, [inputRef]);
const submitMessage = () => {
ask({ text });
setText('');
};
const handleStopGenerating = e => {
e.preventDefault();
stopGenerating();
};
const handleKeyDown = e => {
if (e.key === 'Enter' && isSubmitting) {
return;
}
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
}
if (e.key === 'Enter' && !e.shiftKey && !isComposing?.current) {
submitMessage();
}
};
const handleKeyUp = e => {
if (e.keyCode === 8 && e.target.value.trim() === '') {
setText(e.target.value);
}
if (e.key === 'Enter' && e.shiftKey) {
return console.log('Enter + Shift');
}
if (isSubmitting) {
return;
}
};
const handleCompositionStart = () => {
isComposing.current = true;
};
const handleCompositionEnd = () => {
isComposing.current = false;
};
const changeHandler = e => {
const { value } = e.target;
setText(value);
};
const getPlaceholderText = () => {
if (isSearchView) {
return 'Click a message title to open its conversation.';
}
if (disabled) {
return 'Choose another model or customize GPT again';
}
if (isNotAppendable) {
return 'Edit your message or Regenerate.';
}
return '';
};
2023-04-06 11:30:25 +09:00
const handleBingToneSetting = () => {
setShowBingToneSetting(show => !show);
};
if (isSearchView) return <></>;
return (
<>
<div className="fixed bottom-0 left-0 w-full md:absolute">
<div className="relative py-2 md:mb-[-16px] md:py-4 lg:mb-[-32px]">
2023-04-05 16:27:11 +08:00
<span className="flex w-full flex-col items-center justify-center gap-0 md:order-none md:m-auto md:gap-2">
<OpenAIOptions />
<ChatGPTOptions />
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
<GoogleOptions />
2023-04-06 11:30:25 +09:00
<BingAIOptions show={showBingToneSetting} />
</span>
</div>
<div className="input-panel md:bg-vert-light-gradient dark:md:bg-vert-dark-gradient relative w-full border-t bg-white py-2 dark:border-white/20 dark:bg-gray-800 md:border-t-0 md:border-transparent md:bg-transparent md:dark:border-transparent md:dark:bg-transparent">
<form className="stretch mx-2 flex flex-row gap-3 last:mb-2 md:pt-2 md:last:mb-6 lg:mx-auto lg:max-w-3xl lg:pt-6">
<div className="relative flex h-full flex-1 md:flex-col">
<div
2023-04-08 00:14:15 +08:00
className={`relative flex flex-grow flex-row rounded-md border border-black/10 ${
disabled ? 'bg-gray-100' : 'bg-white'
} py-2 shadow-[0_0_10px_rgba(0,0,0,0.10)] dark:border-gray-900/50 ${
disabled ? 'dark:bg-gray-900' : 'dark:bg-gray-700'
} dark:text-white dark:shadow-[0_0_15px_rgba(0,0,0,0.10)] md:py-3 md:pl-4`}
>
<NewConversationMenu />
<TextareaAutosize
tabIndex="0"
autoFocus
ref={inputRef}
// style={{maxHeight: '200px', height: '24px', overflowY: 'hidden'}}
rows="1"
value={disabled || isNotAppendable ? '' : text}
onKeyUp={handleKeyUp}
onKeyDown={handleKeyDown}
onChange={changeHandler}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
placeholder={getPlaceholderText()}
disabled={disabled || isNotAppendable}
2023-04-08 00:14:15 +08:00
className="m-0 flex h-auto max-h-52 flex-1 resize-none overflow-auto border-0 bg-transparent p-0 pl-2 pr-12 leading-6 placeholder:text-sm placeholder:text-gray-600 focus:outline-none focus:ring-0 focus-visible:ring-0 dark:bg-transparent dark:placeholder:text-gray-500 md:pl-2"
/>
<SubmitButton
submitMessage={submitMessage}
handleStopGenerating={handleStopGenerating}
disabled={disabled || isNotAppendable}
isSubmitting={isSubmitting}
endpointsConfig={endpointsConfig}
endpoint={conversation?.endpoint}
/>
2023-04-06 11:30:25 +09:00
{latestMessage && conversation?.jailbreak && conversation.endpoint === 'bingAI' ? (
<AdjustToneButton onClick={handleBingToneSetting} />
2023-04-06 11:30:25 +09:00
) : null}
</div>
</div>
</form>
<Footer />
</div>
</div>
</>
);
}