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 IndexError when collating chat history #195

Merged
merged 13 commits into from
Feb 8, 2024

Conversation

adamdougal
Copy link
Collaborator

Purpose

This fixes a bug which causes the exception:

ERROR:root:Exception in /api/conversation/custom | list index out of range
Traceback (most recent call last):
  File "/workspaces/chat-with-your-data-solution-accelerator/code/app/app.py", line 283, in conversation_custom
    chat_history.append((user_assistant_messages[i]['content'],user_assistant_messages[i+1]['content']))

This is caused when there has been an error providing a response, and the latest message in the history is from a user, rather than the assitant. Our code assumes a user message is always followed by an assistant message.

This change removes that assumption and explitely retreives the role for each message when collating the chat history.

Required by #114

Does this introduce a breaking change?

[ ] Yes
[x] No

Pull Request Type

What kind of change does this Pull Request introduce?

[x] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

This fixes a bug which causes the exception:
```
ERROR:root:Exception in /api/conversation/custom | list index out of range
Traceback (most recent call last):
  File "/workspaces/chat-with-your-data-solution-accelerator/code/app/app.py", line 283, in conversation_custom
    chat_history.append((user_assistant_messages[i]['content'],user_assistant_messages[i+1]['content']))
```

This is caused when there has been an error providing a response, and
the latest message in the history is from a user, rather than the
assitant. Our code assumes a user message is always followed by an
assistant message.

This change removes that assumption and explitely retreives the role for
each message when collating the chat history.

Required by Azure-Samples#114
@ross-p-smith
Copy link
Collaborator

I hate asking this - but is there a test that we can put around this?

@adamdougal
Copy link
Collaborator Author

@ross-p-smith test added!

Copy link
Collaborator

@ross-p-smith ross-p-smith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💖

@ross-p-smith ross-p-smith added this pull request to the merge queue Feb 8, 2024
Merged via the queue into Azure-Samples:main with commit 4756cd1 Feb 8, 2024
3 checks passed
@adamdougal adamdougal deleted the bugfix/message-index branch February 9, 2024 09:08
eduardogch pushed a commit to devopsdale/chat-with-your-data-solution-accelerator that referenced this pull request Apr 30, 2024
* Fix IndexError when collating chat history

This fixes a bug which causes the exception:
```
ERROR:root:Exception in /api/conversation/custom | list index out of range
Traceback (most recent call last):
  File "/workspaces/chat-with-your-data-solution-accelerator/code/app/app.py", line 283, in conversation_custom
    chat_history.append((user_assistant_messages[i]['content'],user_assistant_messages[i+1]['content']))
```

This is caused when there has been an error providing a response, and
the latest message in the history is from a user, rather than the
assitant. Our code assumes a user message is always followed by an
assistant message.

This change removes that assumption and explitely retreives the role for
each message when collating the chat history.

Required by Azure-Samples#114

* Add python formatter to dev container

* Add tests for conversation custom

- Extract some elements to dedicated function to allow mocking

* Switch to black formatter to align with precommit hook

* Add test to cover error scenario when message index is out of range

* Add dependencies required for running app tests

---------

Co-authored-by: Ross Smith <ross-p-smith@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants