Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicate role inside messages cause some model to refuse to answer #3006

Merged
merged 4 commits into from
Jun 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update
  • Loading branch information
namchuai committed Jun 10, 2024
commit ba1543b962339c9b14d65eced0ef5441798d0e87
22 changes: 14 additions & 8 deletions web/hooks/useSendChatMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 96,24 @@ export default function useSendChatMessage() {
messages: ChatCompletionMessage[]
): ChatCompletionMessage[] => {
const stack = new Stack<ChatCompletionMessage>()
for (const msg of messages) {
for (const message of messages) {
if (stack.isEmpty()) {
stack.push(msg)
stack.push(message)
continue
}
const peekMsg = stack.peek()

if (msg.role === peekMsg.role) {
stack.pop()
const topMessage = stack.peek()

if (message.role === topMessage.role) {
// add an empty message
stack.push({
role:
topMessage.role === ChatCompletionRole.User
? ChatCompletionRole.Assistant
: ChatCompletionRole.User,
content: '',
})
}

stack.push(msg)
stack.push(message)
}

return stack.reverseOutput()
Expand Down
Loading