Skip to content

Commit 95121b8

Browse files
authored
test: aimlapi - add unit tests (#3184)
1 parent 106b880 commit 95121b8

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

integrations/aimlapi/tests/test_aimlapi_chat_generator.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,74 @@ def test_serde_in_pipeline(self, monkeypatch):
538538
assert loaded_generator.tools[0].description == generator.tools[0].description
539539
assert loaded_generator.tools[0].parameters == generator.tools[0].parameters
540540

541+
def test_prepare_api_call_with_tools(self, tools, monkeypatch):
542+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
543+
component = AIMLAPIChatGenerator(tools=tools)
544+
args = component._prepare_api_call(messages=[ChatMessage.from_user("hi")])
545+
546+
assert args["tools"] == [
547+
{
548+
"type": "function",
549+
"function": {
550+
"name": "weather",
551+
"description": "useful to determine the weather in a given location",
552+
"parameters": {
553+
"type": "object",
554+
"properties": {"city": {"type": "string"}},
555+
"required": ["city"],
556+
},
557+
},
558+
}
559+
]
560+
assert "strict" not in args["tools"][0]["function"]
561+
assert args["tools"][0]["function"]["parameters"].get("additionalProperties") is None
562+
563+
def test_prepare_api_call_with_tools_strict(self, tools, monkeypatch):
564+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
565+
component = AIMLAPIChatGenerator(tools=tools)
566+
args = component._prepare_api_call(messages=[ChatMessage.from_user("hi")], tools_strict=True)
567+
568+
function_spec = args["tools"][0]["function"]
569+
assert function_spec["strict"] is True
570+
assert function_spec["parameters"]["additionalProperties"] is False
571+
572+
def test_prepare_api_call_streaming_multiple_responses_raises(self, monkeypatch):
573+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
574+
component = AIMLAPIChatGenerator()
575+
with pytest.raises(ValueError, match="Cannot stream multiple responses"):
576+
component._prepare_api_call(
577+
messages=[ChatMessage.from_user("hi")],
578+
streaming_callback=print_streaming_chunk,
579+
generation_kwargs={"n": 2},
580+
)
581+
582+
def test_prepare_api_call_with_response_format_non_streaming(self, monkeypatch):
583+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
584+
component = AIMLAPIChatGenerator()
585+
response_format = {"type": "json_object"}
586+
args = component._prepare_api_call(
587+
messages=[ChatMessage.from_user("hi")],
588+
generation_kwargs={"response_format": response_format},
589+
)
590+
591+
assert args["response_format"] == response_format
592+
assert args["openai_endpoint"] == "parse"
593+
assert "stream" not in args
594+
595+
def test_prepare_api_call_with_response_format_streaming(self, monkeypatch):
596+
monkeypatch.setenv("AIMLAPI_API_KEY", "fake-api-key")
597+
component = AIMLAPIChatGenerator()
598+
response_format = {"type": "json_object"}
599+
args = component._prepare_api_call(
600+
messages=[ChatMessage.from_user("hi")],
601+
streaming_callback=print_streaming_chunk,
602+
generation_kwargs={"response_format": response_format},
603+
)
604+
605+
assert args["response_format"] == response_format
606+
assert args["openai_endpoint"] == "create"
607+
assert args["stream"] is True
608+
541609

542610
class TestChatCompletionChunkConversion:
543611
def test_handle_stream_response(self):

0 commit comments

Comments
 (0)