2023-12-19 13:27:41 +01:00
|
|
|
import json
|
2024-02-22 14:03:50 +01:00
|
|
|
from unittest.mock import MagicMock, Mock, patch, PropertyMock
|
2024-02-09 16:03:27 +01:00
|
|
|
|
2024-02-15 19:33:34 +01:00
|
|
|
import pytest
|
2023-12-19 13:27:41 +01:00
|
|
|
from openapi3 import OpenAPI
|
2024-01-18 16:49:48 +01:00
|
|
|
from openapi3.schemas import Model
|
2024-02-15 19:33:34 +01:00
|
|
|
|
2023-12-19 13:27:41 +01:00
|
|
|
from haystack.components.connectors import OpenAPIServiceConnector
|
2023-12-28 17:29:47 +01:00
|
|
|
from haystack.dataclasses import ChatMessage
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def openapi_service_mock():
|
|
|
|
return MagicMock(spec=OpenAPI)
|
|
|
|
|
|
|
|
|
|
|
|
class TestOpenAPIServiceConnector:
|
|
|
|
@pytest.fixture
|
2024-02-09 16:03:27 +01:00
|
|
|
def connector(self):
|
|
|
|
return OpenAPIServiceConnector()
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
def test_parse_message_invalid_json(self, connector):
|
|
|
|
# Test invalid JSON content
|
|
|
|
with pytest.raises(ValueError):
|
2023-12-28 17:29:47 +01:00
|
|
|
connector._parse_message(ChatMessage.from_assistant("invalid json"))
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
def test_parse_valid_json_message(self):
|
|
|
|
connector = OpenAPIServiceConnector()
|
|
|
|
|
|
|
|
# The content format here is OpenAI function calling descriptor
|
|
|
|
content = (
|
2023-12-28 17:29:47 +01:00
|
|
|
'[{"function":{"name": "compare_branches","arguments": "{\\n \\"parameters\\": {\\n '
|
2023-12-19 13:27:41 +01:00
|
|
|
' \\"basehead\\": \\"main...openapi_container_v5\\",\\n '
|
2023-12-28 17:29:47 +01:00
|
|
|
' \\"owner\\": \\"deepset-ai\\",\\n \\"repo\\": \\"haystack\\"\\n }\\n}"}, "type": "function"}]'
|
2023-12-19 13:27:41 +01:00
|
|
|
)
|
2023-12-28 17:29:47 +01:00
|
|
|
descriptors = connector._parse_message(ChatMessage.from_assistant(content))
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
# Assert that the descriptor contains the expected method name and arguments
|
2023-12-28 17:29:47 +01:00
|
|
|
assert descriptors[0]["name"] == "compare_branches"
|
|
|
|
assert descriptors[0]["arguments"]["parameters"] == {
|
2023-12-19 13:27:41 +01:00
|
|
|
"basehead": "main...openapi_container_v5",
|
|
|
|
"owner": "deepset-ai",
|
|
|
|
"repo": "haystack",
|
|
|
|
}
|
|
|
|
# but not the requestBody
|
2023-12-28 17:29:47 +01:00
|
|
|
assert "requestBody" not in descriptors[0]["arguments"]
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
# The content format here is OpenAI function calling descriptor
|
2023-12-28 17:29:47 +01:00
|
|
|
content = '[{"function": {"name": "search","arguments": "{\\n \\"requestBody\\": {\\n \\"q\\": \\"haystack\\"\\n }\\n}"}, "type": "function"}]'
|
|
|
|
descriptors = connector._parse_message(ChatMessage.from_assistant(content))
|
|
|
|
assert descriptors[0]["name"] == "search"
|
|
|
|
assert descriptors[0]["arguments"]["requestBody"] == {"q": "haystack"}
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
# but not the parameters
|
2023-12-28 17:29:47 +01:00
|
|
|
assert "parameters" not in descriptors[0]["arguments"]
|
2023-12-19 13:27:41 +01:00
|
|
|
|
|
|
|
def test_parse_message_missing_fields(self, connector):
|
|
|
|
# Test JSON content with missing fields
|
|
|
|
with pytest.raises(ValueError):
|
2023-12-28 17:29:47 +01:00
|
|
|
connector._parse_message(ChatMessage.from_assistant('[{"function": {"name": "test_method"}}]'))
|
2023-12-19 13:27:41 +01:00
|
|
|
|
2024-02-09 16:03:27 +01:00
|
|
|
def test_authenticate_service_missing_authentication_token(self, connector, openapi_service_mock):
|
2024-02-22 14:03:50 +01:00
|
|
|
security_schemes_dict = {
|
|
|
|
"components": {"securitySchemes": {"apiKey": {"in": "header", "name": "x-api-key", "type": "apiKey"}}}
|
|
|
|
}
|
|
|
|
openapi_service_mock.raw_element = security_schemes_dict
|
|
|
|
|
2023-12-19 13:27:41 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
connector._authenticate_service(openapi_service_mock)
|
|
|
|
|
2024-02-09 16:03:27 +01:00
|
|
|
def test_authenticate_service_having_authentication_token(self, connector, openapi_service_mock):
|
2024-02-22 14:03:50 +01:00
|
|
|
security_schemes_dict = {
|
|
|
|
"components": {"securitySchemes": {"apiKey": {"in": "header", "name": "x-api-key", "type": "apiKey"}}}
|
|
|
|
}
|
|
|
|
openapi_service_mock.raw_element = security_schemes_dict
|
|
|
|
openapi_service_mock.components.securitySchemes.raw_element = {
|
|
|
|
"apiKey": {"in": "header", "name": "x-api-key", "type": "apiKey"}
|
|
|
|
}
|
2024-02-09 16:03:27 +01:00
|
|
|
connector._authenticate_service(openapi_service_mock, "some_fake_token")
|
|
|
|
|
|
|
|
def test_authenticate_service_having_authentication_dict(self, connector, openapi_service_mock):
|
2024-02-22 14:03:50 +01:00
|
|
|
security_schemes_dict = {
|
|
|
|
"components": {"securitySchemes": {"apiKey": {"in": "header", "name": "x-api-key", "type": "apiKey"}}}
|
|
|
|
}
|
|
|
|
openapi_service_mock.raw_element = security_schemes_dict
|
|
|
|
openapi_service_mock.components.securitySchemes.raw_element = {
|
|
|
|
"apiKey": {"in": "header", "name": "x-api-key", "type": "apiKey"}
|
|
|
|
}
|
2024-02-09 16:03:27 +01:00
|
|
|
connector._authenticate_service(openapi_service_mock, {"apiKey": "some_fake_token"})
|
|
|
|
|
|
|
|
def test_authenticate_service_having_authentication_dict_but_unsupported_auth(
|
|
|
|
self, connector, openapi_service_mock
|
|
|
|
):
|
2024-02-22 14:03:50 +01:00
|
|
|
security_schemes_dict = {"components": {"securitySchemes": {"oauth2": {"type": "oauth2"}}}}
|
|
|
|
openapi_service_mock.raw_element = security_schemes_dict
|
|
|
|
openapi_service_mock.components.securitySchemes.raw_element = {"oauth2": {"type": "oauth2"}}
|
2024-02-09 16:03:27 +01:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
connector._authenticate_service(openapi_service_mock, {"apiKey": "some_fake_token"})
|
|
|
|
|
2024-01-18 16:49:48 +01:00
|
|
|
def test_for_internal_raw_data_field(self):
|
|
|
|
# see https://github.com/deepset-ai/haystack/pull/6772 for details
|
|
|
|
model = Model(data={}, schema={})
|
|
|
|
assert hasattr(model, "_raw_data"), (
|
|
|
|
"openapi3 changed. Model should have a _raw_data field, we rely on it in OpenAPIServiceConnector"
|
|
|
|
" to get the raw data from the service response"
|
|
|
|
)
|
2024-02-09 16:03:27 +01:00
|
|
|
|
2024-02-20 11:54:51 +01:00
|
|
|
@patch("haystack.components.connectors.openapi_service.OpenAPI")
|
|
|
|
def test_run(self, openapi_mock, test_files_path):
|
|
|
|
connector = OpenAPIServiceConnector()
|
|
|
|
spec_path = test_files_path / "json" / "github_compare_branch_openapi_spec.json"
|
|
|
|
spec = json.loads((spec_path).read_text())
|
|
|
|
|
|
|
|
mock_message = json.dumps(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": "call_NJr1NBz2Th7iUWJpRIJZoJIA",
|
|
|
|
"function": {
|
2024-02-22 14:03:50 +01:00
|
|
|
"arguments": '{"basehead": "main...some_branch", "owner": "deepset-ai", "repo": "haystack"}',
|
2024-02-20 11:54:51 +01:00
|
|
|
"name": "compare_branches",
|
|
|
|
},
|
|
|
|
"type": "function",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
messages = [ChatMessage.from_assistant(mock_message)]
|
2024-02-22 14:03:50 +01:00
|
|
|
call_compare_branches = Mock(return_value=Mock(_raw_data="some_data"))
|
|
|
|
call_compare_branches.operation.__self__ = Mock()
|
|
|
|
call_compare_branches.operation.__self__.raw_element = {
|
|
|
|
"parameters": [{"name": "basehead"}, {"name": "owner"}, {"name": "repo"}]
|
|
|
|
}
|
2024-02-20 11:54:51 +01:00
|
|
|
mock_service = Mock(
|
2024-02-22 14:03:50 +01:00
|
|
|
call_compare_branches=call_compare_branches,
|
2024-02-20 11:54:51 +01:00
|
|
|
components=Mock(securitySchemes=Mock(raw_element={"apikey": {"type": "apiKey"}})),
|
|
|
|
)
|
|
|
|
openapi_mock.return_value = mock_service
|
2024-02-09 16:03:27 +01:00
|
|
|
|
2024-02-22 14:03:50 +01:00
|
|
|
connector.run(messages=messages, service_openapi_spec=spec, service_credentials="fake_key")
|
2024-02-09 16:03:27 +01:00
|
|
|
|
2024-02-20 11:54:51 +01:00
|
|
|
openapi_mock.assert_called_once_with(spec)
|
|
|
|
mock_service.authenticate.assert_called_once_with("apikey", "fake_key")
|
|
|
|
mock_service.call_compare_branches.assert_called_once_with(
|
|
|
|
parameters={"basehead": "main...some_branch", "owner": "deepset-ai", "repo": "haystack"}
|
|
|
|
)
|
2024-02-09 16:03:27 +01:00
|
|
|
|
2024-02-22 14:03:50 +01:00
|
|
|
@patch("haystack.components.connectors.openapi_service.OpenAPI")
|
|
|
|
def test_run_with_mix_params_request_body(self, openapi_mock, test_files_path):
|
|
|
|
connector = OpenAPIServiceConnector()
|
|
|
|
spec_path = test_files_path / "yaml" / "openapi_greeting_service.yml"
|
|
|
|
with open(spec_path, "r") as file:
|
|
|
|
spec = json.loads(file.read())
|
|
|
|
mock_message = json.dumps(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"id": "call_NJr1NBz2Th7iUWJpRIJZoJIA",
|
|
|
|
"function": {"arguments": '{"name": "John", "message": "Hello"}', "name": "greet"},
|
|
|
|
"type": "function",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
)
|
|
|
|
call_greet = Mock(return_value=Mock(_raw_data="Hello, John"))
|
|
|
|
call_greet.operation.__self__ = Mock()
|
|
|
|
call_greet.operation.__self__.raw_element = {
|
|
|
|
"parameters": [{"name": "name"}],
|
|
|
|
"requestBody": {
|
|
|
|
"content": {"application/json": {"schema": {"properties": {"message": {"type": "string"}}}}}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
mock_service = Mock(call_greet=call_greet)
|
|
|
|
mock_service.raw_element = {}
|
|
|
|
openapi_mock.return_value = mock_service
|
|
|
|
|
|
|
|
messages = [ChatMessage.from_assistant(mock_message)]
|
|
|
|
result = connector.run(messages=messages, service_openapi_spec=spec)
|
|
|
|
response = json.loads(result["service_response"][0].content)
|
|
|
|
assert response == "Hello, John"
|