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 #82 -- Handle bad request errors and feed them back to the user #96

Merged
Show file tree
Hide file tree
Changes from 3 commits
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
25 changes: 16 additions & 9 deletions sam/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,22 @@ async def add_message(
purpose="assistants",
)
file_ids.append(new_file.id)
await client.beta.threads.messages.create(
thread_id=thread_id,
content=content,
role=Roles.USER,
attachments=[
{"file_id": file_id, "tools": [{"type": "file_search"}]}
for file_id in file_ids
],
)

try:
await client.beta.threads.messages.create(
thread_id=thread_id,
content=content,
role=Roles.USER,
attachments=[
{"file_id": file_id, "tools": [{"type": "file_search"}]}
for file_id in file_ids
],
)
except openai.BadRequestError as e:
error = OSError()
error.strerror = e.body["message"]
raise error
herrbenesch marked this conversation as resolved.
Show resolved Hide resolved

return bool(file_ids), voice_prompt


Expand Down
19 changes: 14 additions & 5 deletions sam/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,20 @@ async def handle_message(event: {str, Any}, say: AsyncSay):
redis.from_url(config.REDIS_URL) as redis_client,
redis_client.lock(thread_id, timeout=10 * 60, thread_local=False),
): # 10 minutes
has_attachments, has_audio = await bot.add_message(
thread_id=thread_id,
content=text,
files=files,
)
try:
has_attachments, has_audio = await bot.add_message(
thread_id=thread_id,
content=text,
files=files,
)
except OSError as e:
# Only relevant for direct messages
if channel_type == "im":
await say(
channel=channel_id,
text=f"I'm sorry, I can't process this. {e.strerror}",
)
return
herrbenesch marked this conversation as resolved.
Show resolved Hide resolved

# we need to release the lock before starting a new run
if (
Expand Down
11 changes: 11 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import mock

import pytest
from openai import BadRequestError
from openai.types.beta.threads import (
ImageFile,
ImageFileContentBlock,
Expand Down Expand Up @@ -76,6 +77,16 @@ async def test_add_message__audio(client):
)


@pytest.mark.asyncio
async def test_add_message__bad_request(client):
client.beta.threads.messages.create.side_effect = BadRequestError(
response=mock.Mock(), message="Bad request", body={"message": "Bad Request"}
)
with pytest.raises(OSError) as e:
await bot.add_message("thread-1", "", [])
assert e.value.strerror == "Bad Request"


@pytest.mark.asyncio
async def test_call_tools__value_error(client):
run = mock.MagicMock()
Expand Down
45 changes: 45 additions & 0 deletions tests/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,51 @@ async def test_handle_message__subtype_changed(caplog):
assert "Ignoring `message_changed` event" in caplog.text


@pytest.mark.asyncio
async def test_handle_message__bad_request(caplog, monkeypatch):
event = {
"channel": "channel-1",
"client_msg_id": "client-msg-1",
"channel_type": "im",
"user": "user-1",
"text": "Hello",
"files": [
{
"url_private": "https://example.com/file.png",
"name": "file.png",
}
],
}

urlopen = mock.AsyncMock()
urlopen.__enter__().read.return_value = b"Hello"
side_effect = OSError()
side_effect.strerror = "Bad request"
add_message = mock.AsyncMock(side_effect=side_effect)
monkeypatch.setattr("urllib.request.urlopen", lambda *args, **kwargs: urlopen)
monkeypatch.setattr(bot, "add_message", add_message)
monkeypatch.setattr(
"sam.bot.get_thread_id", mock.AsyncMock(return_value="thread-1")
)
get_bot_user_id = mock.AsyncMock(return_value="bot-1")
monkeypatch.setattr(slack, "get_bot_user_id", get_bot_user_id)
send_response = mock.AsyncMock()
monkeypatch.setattr(slack, "send_response", send_response)
say = mock.AsyncMock()

with caplog.at_level(logging.ERROR):
await slack.handle_message(event, say)

assert add_message.called
assert add_message.call_args == mock.call(
thread_id="thread-1", content="Hello", files=[("file.png", b"Hello")]
)
assert say.call_args == mock.call(
channel="channel-1", text="I'm sorry, I can't process this. Bad request"
)
send_response.assert_not_called()


def test_get_user_profile(monkeypatch):
client = mock.MagicMock()
client.users_profile_get.return_value = {
Expand Down