-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix conflict and correct package version
- Loading branch information
Showing
8 changed files
with
188 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
azure-identity==1.15.0 | ||
Flask==2.3.2 | ||
Flask==3.0.2 | ||
openai==1.6.1 | ||
azure-storage-blob==12.19.0 | ||
python-dotenv==1.0.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import os | ||
|
||
from unittest.mock import Mock | ||
from unittest.mock import patch | ||
|
||
from code.app.app import app | ||
|
||
|
||
class TestConfig: | ||
def test_returns_correct_config(self): | ||
response = app.test_client().get("/api/config") | ||
|
||
assert response.status_code == 200 | ||
assert response.json == {"azureSpeechKey": None, "azureSpeechRegion": None} | ||
|
||
|
||
class TestCoversationCustom: | ||
def setup_method(self): | ||
self.orchestrator_config = {"strategy": "langchain"} | ||
self.messages = [ | ||
{ | ||
"content": '{"citations": [], "intent": "A question?"}', | ||
"end_turn": False, | ||
"role": "tool", | ||
}, | ||
{"content": "An answer", "end_turn": True, "role": "assistant"}, | ||
] | ||
self.openai_model = "some-model" | ||
self.body = { | ||
"conversation_id": "123", | ||
"messages": [ | ||
{"role": "user", "content": "Hello"}, | ||
{"role": "assistant", "content": "Hi, how can I help?"}, | ||
{"role": "user", "content": "What is the meaning of life?"}, | ||
], | ||
} | ||
|
||
@patch("code.app.app.get_message_orchestrator") | ||
@patch("code.app.app.get_orchestrator_config") | ||
def test_converstation_custom_returns_correct_response( | ||
self, get_orchestrator_config_mock, get_message_orchestrator_mock | ||
): | ||
# given | ||
get_orchestrator_config_mock.return_value = self.orchestrator_config | ||
|
||
message_orchestrator_mock = Mock() | ||
message_orchestrator_mock.handle_message.return_value = self.messages | ||
get_message_orchestrator_mock.return_value = message_orchestrator_mock | ||
|
||
os.environ["AZURE_OPENAI_MODEL"] = self.openai_model | ||
|
||
# when | ||
response = app.test_client().post( | ||
"/api/conversation/custom", | ||
headers={"content-type": "application/json"}, | ||
json=self.body, | ||
) | ||
|
||
# then | ||
assert response.status_code == 200 | ||
assert response.json == { | ||
"choices": [{"messages": self.messages}], | ||
"created": "response.created", | ||
"id": "response.id", | ||
"model": self.openai_model, | ||
"object": "response.object", | ||
} | ||
|
||
@patch("code.app.app.get_message_orchestrator") | ||
@patch("code.app.app.get_orchestrator_config") | ||
def test_converstation_custom_calls_message_orchestrator_correctly( | ||
self, get_orchestrator_config_mock, get_message_orchestrator_mock | ||
): | ||
# given | ||
get_orchestrator_config_mock.return_value = self.orchestrator_config | ||
|
||
message_orchestrator_mock = Mock() | ||
message_orchestrator_mock.handle_message.return_value = self.messages | ||
get_message_orchestrator_mock.return_value = message_orchestrator_mock | ||
|
||
os.environ["AZURE_OPENAI_MODEL"] = self.openai_model | ||
|
||
# when | ||
app.test_client().post( | ||
"/api/conversation/custom", | ||
headers={"content-type": "application/json"}, | ||
json=self.body, | ||
) | ||
|
||
# then | ||
message_orchestrator_mock.handle_message.assert_called_once_with( | ||
user_message=self.body["messages"][-1]["content"], | ||
chat_history=self.body["messages"][:-1], | ||
conversation_id=self.body["conversation_id"], | ||
orchestrator=self.orchestrator_config, | ||
) | ||
|
||
@patch("code.app.app.get_orchestrator_config") | ||
def test_converstation_custom_returns_error_resonse_on_exception( | ||
self, get_orchestrator_config_mock | ||
): | ||
# given | ||
get_orchestrator_config_mock.side_effect = Exception("An error occurred") | ||
|
||
# when | ||
response = app.test_client().post( | ||
"/api/conversation/custom", | ||
headers={"content-type": "application/json"}, | ||
json=self.body, | ||
) | ||
|
||
# then | ||
assert response.status_code == 500 | ||
assert response.json == { | ||
"error": "Exception in /api/conversation/custom. See log for more details." | ||
} | ||
|
||
@patch("code.app.app.get_message_orchestrator") | ||
@patch("code.app.app.get_orchestrator_config") | ||
def test_converstation_custom_allows_multiple_messages_from_user( | ||
self, get_orchestrator_config_mock, get_message_orchestrator_mock | ||
): | ||
"""This can happen if there was an error getting a response from the assistant for the previous user message.""" | ||
|
||
# given | ||
get_orchestrator_config_mock.return_value = self.orchestrator_config | ||
|
||
message_orchestrator_mock = Mock() | ||
message_orchestrator_mock.handle_message.return_value = self.messages | ||
get_message_orchestrator_mock.return_value = message_orchestrator_mock | ||
|
||
os.environ["AZURE_OPENAI_MODEL"] = self.openai_model | ||
|
||
body = { | ||
"conversation_id": "123", | ||
"messages": [ | ||
{"role": "user", "content": "Hello"}, | ||
{"role": "assistant", "content": "Hi, how can I help?"}, | ||
{"role": "user", "content": "What is the meaning of life?"}, | ||
{ | ||
"role": "user", | ||
"content": "Please, what is the meaning of life?", | ||
}, | ||
], | ||
} | ||
|
||
# when | ||
response = app.test_client().post( | ||
"/api/conversation/custom", | ||
headers={"content-type": "application/json"}, | ||
json=body, | ||
) | ||
|
||
# then | ||
assert response.status_code == 200 | ||
message_orchestrator_mock.handle_message.assert_called_once_with( | ||
user_message=body["messages"][-1]["content"], | ||
chat_history=body["messages"][:-1], | ||
conversation_id=body["conversation_id"], | ||
orchestrator=self.orchestrator_config, | ||
) |