-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor code to handle chat completion and update chat messages and …
…storage in real-time
- Loading branch information
1 parent
a48fbb3
commit b73cc3b
Showing
5 changed files
with
113 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,68 @@ | ||
import { AvailableModels, Mode } from "../config/settings" | ||
import { ChatOpenAI } from "langchain/chat_models/openai" | ||
import { useStorage } from "./useStorage" | ||
import { useCurrentChat, Role } from "./useCurrentChat" | ||
import { useMemo } from "react" | ||
import { AIMessage, HumanMessage, SystemMessage } from "langchain/dist/schema" | ||
import { useState } from "react" | ||
|
||
interface UseChatCompletionProps { | ||
model: AvailableModels | ||
apiKey: string | ||
mode: Mode | ||
systemPrompt: string | ||
chatId: string | ||
} | ||
|
||
|
||
type Role = "user" | "assistant" | "system" | ||
|
||
type Message = { | ||
role: Role | ||
message: string | ||
timestamp: number | ||
} | ||
|
||
export const useChatCompletion = ({ | ||
model, | ||
apiKey, | ||
mode, | ||
systemPrompt, | ||
chatId, | ||
}: UseChatCompletionProps) => { | ||
const chat = new ChatOpenAI({ | ||
const { messages, updateAssistantMessage, updateStoredMessages, clearMessages } = useCurrentChat(chatId) | ||
const [generating, setGenerating] = useState(false) | ||
|
||
const controller = new AbortController(); | ||
|
||
const chat = useMemo(() => new ChatOpenAI({ | ||
streaming: true, | ||
}); | ||
}), []) | ||
|
||
|
||
|
||
const submitQuery = async (query: string) => { | ||
const previousMessages = messages.map((msg) => { | ||
switch (msg.role) { | ||
case Role.ASSISTANT: | ||
return new AIMessage(msg.message) | ||
case Role.SYSTEM: | ||
return new SystemMessage(msg.message) | ||
case Role.USER: | ||
return new HumanMessage(msg.message) | ||
} | ||
}) | ||
setGenerating(true) | ||
const response = await chat.call([...previousMessages, new HumanMessage(query)], { | ||
signal: controller.signal, | ||
callbacks: [ | ||
{ | ||
handleLLMNewToken: updateAssistantMessage | ||
}, | ||
], | ||
}); | ||
setGenerating(false) | ||
updateStoredMessages() | ||
return response.content | ||
} | ||
|
||
const cancelRequest = () => { | ||
controller.abort() | ||
setGenerating(false) | ||
} | ||
|
||
const [messages, setMessages] = useStorage() | ||
|
||
return { messages, submitQuery, generating, cancelRequest, clearMessages } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useEffect } from "react" | ||
import { useState } from "react" | ||
import { useStorage } from "./useStorage" | ||
|
||
|
||
export enum Role { | ||
"USER", | ||
"ASSISTANT", | ||
"SYSTEM" | ||
} | ||
|
||
export type Message = { | ||
role: Role | ||
message: string | ||
timestamp: number | ||
} | ||
|
||
export const useCurrentChat = (chatId: string) => { | ||
const [storedMessages, setStoredMessages] = useStorage<Message[]>(`CHAT-${chatId}`, []) | ||
const [messages, setMessages] = useState<Message[]>(storedMessages) // we don't directly update storedMessages for performance reasons | ||
|
||
const updateAssistantMessage = (chunk: string) => { | ||
setMessages(messages => { | ||
const lastMessage = messages[messages.length - 1] | ||
lastMessage.message += chunk | ||
return [...messages] | ||
}) | ||
} | ||
|
||
const addNewMessage = (role: Role, message: string) => { | ||
const newMessage: Message = { | ||
role, | ||
message, | ||
timestamp: Date.now(), | ||
} | ||
setMessages([...messages, newMessage]) | ||
} | ||
|
||
const updateStoredMessages = () => { | ||
setStoredMessages(messages) | ||
} | ||
|
||
const clearMessages = () => { | ||
setMessages([]) | ||
updateStoredMessages() | ||
} | ||
|
||
return { | ||
messages, | ||
updateAssistantMessage, | ||
addNewMessage, | ||
updateStoredMessages, | ||
clearMessages | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters