(Admin UI) - maintain history on chat UI (#7351)

* ui fix - allow searching model list + fix bug on filtering

* qa fix - use correct provider name for azure_text

* ui wrap content onto next line

* ui fix - allow selecting current UI session when logging in

* ui session budgets

* ui show provider models on wildcard models

* test provider name appears in model list

* ui fix auto scroll on chat ui tab

* ui - maintain chat history
This commit is contained in:
Ishaan Jaff 2024-12-21 14:25:35 -08:00 committed by GitHub
parent 56d9427fdb
commit 237b6ba635
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -25,6 +25,7 @@ import {
import { message, Select } from "antd"; import { message, Select } from "antd";
import { modelAvailableCall } from "./networking"; import { modelAvailableCall } from "./networking";
import openai from "openai"; import openai from "openai";
import { ChatCompletionMessageParam } from "openai/resources/chat/completions";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { Typography } from "antd"; import { Typography } from "antd";
@ -36,15 +37,15 @@ interface ChatUIProps {
} }
async function generateModelResponse( async function generateModelResponse(
inputMessage: string, chatHistory: { role: string; content: string }[],
updateUI: (chunk: string) => void, updateUI: (chunk: string) => void,
selectedModel: string, selectedModel: string,
accessToken: string accessToken: string
) { ) {
// base url should be the current base_url // base url should be the current base_url
const isLocal = process.env.NODE_ENV === "development"; const isLocal = process.env.NODE_ENV === "development";
if (isLocal != true) { if (isLocal !== true) {
console.log = function() {}; console.log = function () {};
} }
console.log("isLocal:", isLocal); console.log("isLocal:", isLocal);
const proxyBaseUrl = isLocal const proxyBaseUrl = isLocal
@ -60,12 +61,7 @@ async function generateModelResponse(
const response = await client.chat.completions.create({ const response = await client.chat.completions.create({
model: selectedModel, model: selectedModel,
stream: true, stream: true,
messages: [ messages: chatHistory as ChatCompletionMessageParam[],
{
role: "user",
content: inputMessage,
},
],
}); });
for await (const chunk of response) { for await (const chunk of response) {
@ -88,7 +84,7 @@ const ChatUI: React.FC<ChatUIProps> = ({
const [apiKeySource, setApiKeySource] = useState<'session' | 'custom'>('session'); const [apiKeySource, setApiKeySource] = useState<'session' | 'custom'>('session');
const [apiKey, setApiKey] = useState(""); const [apiKey, setApiKey] = useState("");
const [inputMessage, setInputMessage] = useState(""); const [inputMessage, setInputMessage] = useState("");
const [chatHistory, setChatHistory] = useState<any[]>([]); const [chatHistory, setChatHistory] = useState<{ role: string; content: string }[]>([]);
const [selectedModel, setSelectedModel] = useState<string | undefined>( const [selectedModel, setSelectedModel] = useState<string | undefined>(
undefined undefined
); );
@ -183,15 +179,17 @@ const ChatUI: React.FC<ChatUIProps> = ({
return; return;
} }
setChatHistory((prevHistory) => [
...prevHistory, const newUserMessage = { role: "user", content: inputMessage };
{ role: "user", content: inputMessage },
]); const updatedChatHistory = [...chatHistory, newUserMessage];
setChatHistory(updatedChatHistory);
try { try {
if (selectedModel) { if (selectedModel) {
await generateModelResponse( await generateModelResponse(
inputMessage, updatedChatHistory,
(chunk) => updateUI("assistant", chunk), (chunk) => updateUI("assistant", chunk),
selectedModel, selectedModel,
effectiveApiKey effectiveApiKey
@ -205,6 +203,11 @@ const ChatUI: React.FC<ChatUIProps> = ({
setInputMessage(""); setInputMessage("");
}; };
const clearChatHistory = () => {
setChatHistory([]);
message.success("Chat history cleared.");
};
if (userRole && userRole === "Admin Viewer") { if (userRole && userRole === "Admin Viewer") {
const { Title, Paragraph } = Typography; const { Title, Paragraph } = Typography;
return ( return (
@ -229,47 +232,51 @@ const ChatUI: React.FC<ChatUIProps> = ({
<TabList> <TabList>
<Tab>Chat</Tab> <Tab>Chat</Tab>
</TabList> </TabList>
<TabPanels> <TabPanels>
<TabPanel> <TabPanel>
<div className="sm:max-w-2xl"> <div className="sm:max-w-2xl">
<Grid numItems={2}> <Grid numItems={2}>
<Col> <Col>
<Text>API Key Source</Text> <Text>API Key Source</Text>
<Select <Select
defaultValue="session" defaultValue="session"
style={{ width: "100%" }} style={{ width: "100%" }}
onChange={(value) => setApiKeySource(value as "session" | "custom")} onChange={(value) => setApiKeySource(value as "session" | "custom")}
options={[ options={[
{ value: 'session', label: 'Current UI Session' }, { value: 'session', label: 'Current UI Session' },
{ value: 'custom', label: 'Virtual Key' }, { value: 'custom', label: 'Virtual Key' },
]} ]}
/> />
{apiKeySource === 'custom' && ( {apiKeySource === 'custom' && (
<TextInput <TextInput
className="mt-2" className="mt-2"
placeholder="Enter custom API key" placeholder="Enter custom API key"
type="password" type="password"
onValueChange={setApiKey} onValueChange={setApiKey}
value={apiKey} value={apiKey}
/> />
)} )}
</Col> </Col>
<Col className="mx-2"> <Col className="mx-2">
<Text>Select Model:</Text> <Text>Select Model:</Text>
<Select
placeholder="Select a Model"
onChange={onChange}
options={modelInfo}
style={{ width: "350px" }}
showSearch={true}
/>
</Col>
</Grid>
<Select {/* Clear Chat Button */}
placeholder="Select a Model" <Button
onChange={onChange} onClick={clearChatHistory}
options={modelInfo} className="mt-4"
style={{ width: "350px" }} >
showSearch={true} Clear Chat
/> </Button>
</Col> </div>
</Grid>
</div>
<Table <Table
className="mt-5" className="mt-5"
style={{ style={{
@ -315,7 +322,7 @@ const ChatUI: React.FC<ChatUIProps> = ({
type="text" type="text"
value={inputMessage} value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)} onChange={(e) => setInputMessage(e.target.value)}
onKeyDown={handleKeyDown} // Add this line onKeyDown={handleKeyDown}
placeholder="Type your message..." placeholder="Type your message..."
/> />
<Button <Button