Skip to content

Instantly share code, notes, and snippets.

@ogabrielguerra
Created April 15, 2024 18:23
Show Gist options
  • Save ogabrielguerra/a6e586f39f63bc833ed3c79f9a537aee to your computer and use it in GitHub Desktop.
Save ogabrielguerra/a6e586f39f63bc833ed3c79f9a537aee to your computer and use it in GitHub Desktop.
Refactor
// old
const handleSubmit = async (event) => {
event.preventDefault()
setIsLoading(true)
var userMessage:ChatMessageFormat = {
...message,
id: crypto.randomUUID()
}
var searchResult = await Promise.all([runSearches(message.content, selectedCollection)])
console.log("Search Result: ", searchResult)
var formattedPrompt:string = basePrompt.replace("{context}", searchResult[0]).replace("{question}", message.content)
var payloadMessage:Object = {
"role": "user",
"content": formattedPrompt
}
currentMessages.push(userMessage)
payloadMessages.push(payloadMessage)
var inferencePayload = {
messages: [{
messages: payloadMessages
}],
model: selectedModel
}
setCurrentMessages(currentMessages)
setPayloadMessages(payloadMessages)
generateProxyOAIInference(inferencePayload).then(response => {
var assistantMessage:ChatMessageFormat = {
content: response.data.content,
id: crypto.randomUUID(),
role: "assistant"
}
var assistantResponseMessage:Object = {
"role": "assistant",
"content": response.data.content
}
currentMessages.push(assistantMessage)
payloadMessages.push(assistantResponseMessage)
setCurrentMessages(currentMessages)
setPayloadMessages(payloadMessages)
setIsLoading(false)
console.log(selectedModel)
}).catch(error => {
console.log(error)
setIsLoading(false)
})
}
//new
const processPrompt = async (event: React.ChangeEvent<HTMLInputElement>) => {
event.preventDefault()
setIsLoading(true)
currentMessages.push(userMessage(message))
setCurrentMessages([...currentMessages])
disabledRagMessages.push(payloadMessage(message))
setDisabledRagMessages([...disabledRagMessages])
try {
const response = await loadInferences.loadAll(
inferencePayload(disabledRagMessages, defaultModel));
currentMessages.push(assistantMessage(response))
setCurrentMessages([...currentMessages])
disabledRagMessages.push(assistantMessageResponse(response))
setDisabledRagMessages([...disabledRagMessages])
setIsLoading(false)
} catch (error) {
console.log(error)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment