Skip to content

Commit

Permalink
fix: add back normalize message function
Browse files Browse the repository at this point in the history
Signed-off-by: James <[email protected]>
  • Loading branch information
namchuai committed Aug 2, 2024
1 parent 68714ee commit 8a80547
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions web/hooks/useSendMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import { inferenceErrorAtom } from '@/screens/HubScreen2/components/InferenceErr
import { showWarningMultipleModelModalAtom } from '@/screens/HubScreen2/components/WarningMultipleModelModal'
import { concurrentModelWarningThreshold } from '@/screens/Settings/MyModels/ModelItem'

import { Stack } from '@/utils/Stack'

import useCortex from './useCortex'

import useEngineInit from './useEngineInit'
Expand All @@ -47,28 +49,29 @@ import {
updateThreadTitleAtom,
} from '@/helpers/atoms/Thread.atom'

// TODO: NamH add this back
// const normalizeMessages = (messages: Message[]): Message[] => {
// const stack = new Stack<Message>()
// for (const message of messages) {
// if (stack.isEmpty()) {
// stack.push(message)
// continue
// }
// const topMessage = stack.peek()

// if (message.role === topMessage.role) {
// // add an empty message
// stack.push({
// role: topMessage.role === 'user' ? 'assistant' : 'user',
// content: '.', // some model requires not empty message
// })
// }
// stack.push(message)
// }

// return stack.reverseOutput()
// }
const normalizeMessages = (
messages: ChatCompletionMessageParam[]
): ChatCompletionMessageParam[] => {
const stack = new Stack<ChatCompletionMessageParam>()
for (const message of messages) {
if (stack.isEmpty()) {
stack.push(message)
continue
}
const topMessage = stack.peek()

if (message.role === topMessage.role) {
// add an empty message
stack.push({
role: topMessage.role === 'user' ? 'assistant' : 'user',
content: '.', // some model requires not empty message
})
}
stack.push(message)
}

return stack.reverseOutput()
}

const useSendMessage = () => {
const createMessage = useMessageCreateMutation()
Expand Down Expand Up @@ -285,7 +288,7 @@ const useSendMessage = () => {
content: activeThread!.assistants[0].instructions ?? '',
}

const messages: ChatCompletionMessageParam[] = currentMessages
let messages: ChatCompletionMessageParam[] = currentMessages
.map((msg) => {
switch (msg.role) {
case 'user':
Expand All @@ -305,7 +308,7 @@ const useSendMessage = () => {
})
.filter((msg) => msg != null) as ChatCompletionMessageParam[]
messages.unshift(systemMessage)

messages = normalizeMessages(messages)
const modelOptions: Record<string, string | number> = {}
if (selectedModel!.frequency_penalty) {
modelOptions.frequency_penalty = selectedModel!.frequency_penalty
Expand Down Expand Up @@ -540,7 +543,7 @@ const useSendMessage = () => {
content: activeThread!.assistants[0].instructions ?? '',
}

const messages: ChatCompletionMessageParam[] = currentMessages
let messages: ChatCompletionMessageParam[] = currentMessages
.map((msg) => {
switch (msg.role) {
case 'user':
Expand All @@ -564,6 +567,7 @@ const useSendMessage = () => {
content: message,
})
messages.unshift(systemMessage)
messages = normalizeMessages(messages)
const modelOptions: Record<string, string | number> = {}
if (selectedModel!.frequency_penalty) {
modelOptions.frequency_penalty = selectedModel!.frequency_penalty
Expand Down

0 comments on commit 8a80547

Please sign in to comment.