Skip to content

Commit

Permalink
[Fix] Check for empty tool message before setMessages (#385)
Browse files Browse the repository at this point in the history
Co-authored-by: Ian Seabock (Centific Technologies Inc) <v-ianseabock@microsoft.com>
  • Loading branch information
iseabock and Ian Seabock (Centific Technologies Inc) authored Nov 17, 2023
1 parent 60e6b3e commit 056939b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ def update_conversation():
## then write it to the conversation history in cosmos
messages = request.json["messages"]
if len(messages) > 0 and messages[-1]['role'] == "assistant":
if len(messages) > 1 and messages[-2] != {} and messages[-2]['role'] == "tool":
if len(messages) > 1 and messages[-2].get('role', None) == "tool":
# write the tool message first
cosmos_conversation_client.create_message(
conversation_id=conversation_id,
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,15 +341,19 @@ const Chat = () => {
abortFuncs.current = abortFuncs.current.filter(a => a !== abortController);
return;
}
resultConversation.messages.push(toolMessage, assistantMessage)
isEmpty(toolMessage) ?
resultConversation.messages.push(assistantMessage) :
resultConversation.messages.push(toolMessage, assistantMessage)
}else{
resultConversation = {
id: result.history_metadata.conversation_id,
title: result.history_metadata.title,
messages: [userMessage],
date: result.history_metadata.date
}
resultConversation.messages.push(toolMessage, assistantMessage)
isEmpty(toolMessage) ?
resultConversation.messages.push(assistantMessage) :
resultConversation.messages.push(toolMessage, assistantMessage)
}
if(!resultConversation){
setIsLoading(false);
Expand All @@ -358,7 +362,9 @@ const Chat = () => {
return;
}
appStateContext?.dispatch({ type: 'UPDATE_CURRENT_CHAT', payload: resultConversation });
setMessages([...messages, toolMessage, assistantMessage]);
isEmpty(toolMessage) ?
setMessages([...messages, assistantMessage]) :
setMessages([...messages, toolMessage, assistantMessage]);
}

} catch ( e ) {
Expand Down
Loading

0 comments on commit 056939b

Please sign in to comment.