2024-05-09 15:40:36 +02:00
|
|
|
# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai>
|
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-01-05 15:48:28 +01:00
|
|
|
import os
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from openai import OpenAIError
|
|
|
|
|
2024-05-15 10:00:38 +02:00
|
|
|
from haystack import Pipeline
|
2024-01-05 15:48:28 +01:00
|
|
|
from haystack.components.generators.chat import AzureOpenAIChatGenerator
|
2024-01-26 16:00:02 +01:00
|
|
|
from haystack.components.generators.utils import print_streaming_chunk
|
2025-01-23 10:24:04 +01:00
|
|
|
from haystack.dataclasses import ChatMessage, ToolCall
|
|
|
|
from haystack.tools.tool import Tool
|
2024-02-05 13:17:01 +01:00
|
|
|
from haystack.utils.auth import Secret
|
2024-01-05 15:48:28 +01:00
|
|
|
|
|
|
|
|
2025-01-23 10:24:04 +01:00
|
|
|
@pytest.fixture
|
|
|
|
def tools():
|
|
|
|
tool_parameters = {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]}
|
|
|
|
tool = Tool(
|
|
|
|
name="weather",
|
|
|
|
description="useful to determine the weather in a given location",
|
|
|
|
parameters=tool_parameters,
|
|
|
|
function=lambda x: x,
|
|
|
|
)
|
|
|
|
|
|
|
|
return [tool]
|
|
|
|
|
|
|
|
|
|
|
|
class TestAzureOpenAIChatGenerator:
|
2024-02-05 13:17:01 +01:00
|
|
|
def test_init_default(self, monkeypatch):
|
|
|
|
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
|
|
|
|
component = AzureOpenAIChatGenerator(azure_endpoint="some-non-existing-endpoint")
|
2024-01-05 15:48:28 +01:00
|
|
|
assert component.client.api_key == "test-api-key"
|
2024-09-17 10:36:42 +02:00
|
|
|
assert component.azure_deployment == "gpt-4o-mini"
|
2024-01-05 15:48:28 +01:00
|
|
|
assert component.streaming_callback is None
|
|
|
|
assert not component.generation_kwargs
|
|
|
|
|
|
|
|
def test_init_fail_wo_api_key(self, monkeypatch):
|
|
|
|
monkeypatch.delenv("AZURE_OPENAI_API_KEY", raising=False)
|
2024-02-05 13:17:01 +01:00
|
|
|
monkeypatch.delenv("AZURE_OPENAI_AD_TOKEN", raising=False)
|
2024-01-05 15:48:28 +01:00
|
|
|
with pytest.raises(OpenAIError):
|
|
|
|
AzureOpenAIChatGenerator(azure_endpoint="some-non-existing-endpoint")
|
|
|
|
|
2025-01-23 10:24:04 +01:00
|
|
|
def test_init_with_parameters(self, tools):
|
2024-01-05 15:48:28 +01:00
|
|
|
component = AzureOpenAIChatGenerator(
|
2024-02-05 13:17:01 +01:00
|
|
|
api_key=Secret.from_token("test-api-key"),
|
2024-01-05 15:48:28 +01:00
|
|
|
azure_endpoint="some-non-existing-endpoint",
|
2024-01-26 16:00:02 +01:00
|
|
|
streaming_callback=print_streaming_chunk,
|
2024-01-05 15:48:28 +01:00
|
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
2025-01-23 10:24:04 +01:00
|
|
|
tools=tools,
|
|
|
|
tools_strict=True,
|
2024-01-05 15:48:28 +01:00
|
|
|
)
|
|
|
|
assert component.client.api_key == "test-api-key"
|
2024-09-17 10:36:42 +02:00
|
|
|
assert component.azure_deployment == "gpt-4o-mini"
|
2024-01-26 16:00:02 +01:00
|
|
|
assert component.streaming_callback is print_streaming_chunk
|
2024-01-05 15:48:28 +01:00
|
|
|
assert component.generation_kwargs == {"max_tokens": 10, "some_test_param": "test-params"}
|
2025-01-23 10:24:04 +01:00
|
|
|
assert component.tools == tools
|
|
|
|
assert component.tools_strict
|
2024-01-05 15:48:28 +01:00
|
|
|
|
2024-02-05 13:17:01 +01:00
|
|
|
def test_to_dict_default(self, monkeypatch):
|
|
|
|
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
|
|
|
|
component = AzureOpenAIChatGenerator(azure_endpoint="some-non-existing-endpoint")
|
2024-01-05 15:48:28 +01:00
|
|
|
data = component.to_dict()
|
|
|
|
assert data == {
|
|
|
|
"type": "haystack.components.generators.chat.azure.AzureOpenAIChatGenerator",
|
|
|
|
"init_parameters": {
|
2024-02-05 13:17:01 +01:00
|
|
|
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
|
|
|
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
2024-01-05 15:48:28 +01:00
|
|
|
"api_version": "2023-05-15",
|
|
|
|
"azure_endpoint": "some-non-existing-endpoint",
|
2024-09-17 10:36:42 +02:00
|
|
|
"azure_deployment": "gpt-4o-mini",
|
2024-01-05 15:48:28 +01:00
|
|
|
"organization": None,
|
|
|
|
"streaming_callback": None,
|
|
|
|
"generation_kwargs": {},
|
2024-07-09 02:04:51 +05:30
|
|
|
"timeout": 30.0,
|
|
|
|
"max_retries": 5,
|
2024-09-10 03:29:56 -07:00
|
|
|
"default_headers": {},
|
2025-01-23 10:24:04 +01:00
|
|
|
"tools": None,
|
|
|
|
"tools_strict": False,
|
2024-01-05 15:48:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-02-05 13:17:01 +01:00
|
|
|
def test_to_dict_with_parameters(self, monkeypatch):
|
|
|
|
monkeypatch.setenv("ENV_VAR", "test-api-key")
|
2024-01-05 15:48:28 +01:00
|
|
|
component = AzureOpenAIChatGenerator(
|
2024-02-05 13:17:01 +01:00
|
|
|
api_key=Secret.from_env_var("ENV_VAR", strict=False),
|
|
|
|
azure_ad_token=Secret.from_env_var("ENV_VAR1", strict=False),
|
2024-01-05 15:48:28 +01:00
|
|
|
azure_endpoint="some-non-existing-endpoint",
|
2024-05-23 16:28:24 +02:00
|
|
|
timeout=2.5,
|
2024-07-09 02:04:51 +05:30
|
|
|
max_retries=10,
|
2024-01-05 15:48:28 +01:00
|
|
|
generation_kwargs={"max_tokens": 10, "some_test_param": "test-params"},
|
|
|
|
)
|
|
|
|
data = component.to_dict()
|
|
|
|
assert data == {
|
|
|
|
"type": "haystack.components.generators.chat.azure.AzureOpenAIChatGenerator",
|
|
|
|
"init_parameters": {
|
2024-02-05 13:17:01 +01:00
|
|
|
"api_key": {"env_vars": ["ENV_VAR"], "strict": False, "type": "env_var"},
|
|
|
|
"azure_ad_token": {"env_vars": ["ENV_VAR1"], "strict": False, "type": "env_var"},
|
2024-01-05 15:48:28 +01:00
|
|
|
"api_version": "2023-05-15",
|
|
|
|
"azure_endpoint": "some-non-existing-endpoint",
|
2024-09-17 10:36:42 +02:00
|
|
|
"azure_deployment": "gpt-4o-mini",
|
2024-01-05 15:48:28 +01:00
|
|
|
"organization": None,
|
|
|
|
"streaming_callback": None,
|
2024-05-23 16:28:24 +02:00
|
|
|
"timeout": 2.5,
|
2024-07-09 02:04:51 +05:30
|
|
|
"max_retries": 10,
|
2024-01-05 15:48:28 +01:00
|
|
|
"generation_kwargs": {"max_tokens": 10, "some_test_param": "test-params"},
|
2025-01-23 10:24:04 +01:00
|
|
|
"tools": None,
|
|
|
|
"tools_strict": False,
|
2024-09-10 03:29:56 -07:00
|
|
|
"default_headers": {},
|
2024-01-05 15:48:28 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2025-01-23 10:24:04 +01:00
|
|
|
def test_from_dict(self, monkeypatch):
|
|
|
|
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
|
|
|
|
monkeypatch.setenv("AZURE_OPENAI_AD_TOKEN", "test-ad-token")
|
|
|
|
data = {
|
|
|
|
"type": "haystack.components.generators.chat.azure.AzureOpenAIChatGenerator",
|
|
|
|
"init_parameters": {
|
|
|
|
"api_key": {"env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False, "type": "env_var"},
|
|
|
|
"azure_ad_token": {"env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False, "type": "env_var"},
|
|
|
|
"api_version": "2023-05-15",
|
|
|
|
"azure_endpoint": "some-non-existing-endpoint",
|
|
|
|
"azure_deployment": "gpt-4o-mini",
|
|
|
|
"organization": None,
|
|
|
|
"streaming_callback": None,
|
|
|
|
"generation_kwargs": {},
|
|
|
|
"timeout": 30.0,
|
|
|
|
"max_retries": 5,
|
|
|
|
"default_headers": {},
|
|
|
|
"tools": [
|
|
|
|
{
|
|
|
|
"type": "haystack.tools.tool.Tool",
|
|
|
|
"data": {
|
|
|
|
"description": "description",
|
|
|
|
"function": "builtins.print",
|
|
|
|
"name": "name",
|
|
|
|
"parameters": {"x": {"type": "string"}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"tools_strict": False,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
generator = AzureOpenAIChatGenerator.from_dict(data)
|
|
|
|
assert isinstance(generator, AzureOpenAIChatGenerator)
|
|
|
|
|
|
|
|
assert generator.api_key == Secret.from_env_var("AZURE_OPENAI_API_KEY", strict=False)
|
|
|
|
assert generator.azure_ad_token == Secret.from_env_var("AZURE_OPENAI_AD_TOKEN", strict=False)
|
|
|
|
assert generator.api_version == "2023-05-15"
|
|
|
|
assert generator.azure_endpoint == "some-non-existing-endpoint"
|
|
|
|
assert generator.azure_deployment == "gpt-4o-mini"
|
|
|
|
assert generator.organization is None
|
|
|
|
assert generator.streaming_callback is None
|
|
|
|
assert generator.generation_kwargs == {}
|
|
|
|
assert generator.timeout == 30.0
|
|
|
|
assert generator.max_retries == 5
|
|
|
|
assert generator.default_headers == {}
|
|
|
|
assert generator.tools == [
|
|
|
|
Tool(name="name", description="description", parameters={"x": {"type": "string"}}, function=print)
|
|
|
|
]
|
|
|
|
assert generator.tools_strict == False
|
|
|
|
|
2024-05-15 10:00:38 +02:00
|
|
|
def test_pipeline_serialization_deserialization(self, tmp_path, monkeypatch):
|
|
|
|
monkeypatch.setenv("AZURE_OPENAI_API_KEY", "test-api-key")
|
|
|
|
generator = AzureOpenAIChatGenerator(azure_endpoint="some-non-existing-endpoint")
|
|
|
|
p = Pipeline()
|
|
|
|
p.add_component(instance=generator, name="generator")
|
2025-01-23 10:24:04 +01:00
|
|
|
|
|
|
|
assert p.to_dict() == {
|
|
|
|
"metadata": {},
|
|
|
|
"max_runs_per_component": 100,
|
|
|
|
"components": {
|
|
|
|
"generator": {
|
|
|
|
"type": "haystack.components.generators.chat.azure.AzureOpenAIChatGenerator",
|
|
|
|
"init_parameters": {
|
|
|
|
"azure_endpoint": "some-non-existing-endpoint",
|
|
|
|
"azure_deployment": "gpt-4o-mini",
|
|
|
|
"organization": None,
|
|
|
|
"api_version": "2023-05-15",
|
|
|
|
"streaming_callback": None,
|
|
|
|
"generation_kwargs": {},
|
|
|
|
"timeout": 30.0,
|
|
|
|
"max_retries": 5,
|
|
|
|
"api_key": {"type": "env_var", "env_vars": ["AZURE_OPENAI_API_KEY"], "strict": False},
|
|
|
|
"azure_ad_token": {"type": "env_var", "env_vars": ["AZURE_OPENAI_AD_TOKEN"], "strict": False},
|
|
|
|
"default_headers": {},
|
|
|
|
"tools": None,
|
|
|
|
"tools_strict": False,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"connections": [],
|
|
|
|
}
|
2024-05-15 10:00:38 +02:00
|
|
|
p_str = p.dumps()
|
|
|
|
q = Pipeline.loads(p_str)
|
|
|
|
assert p.to_dict() == q.to_dict(), "Pipeline serialization/deserialization w/ AzureOpenAIChatGenerator failed."
|
|
|
|
|
2024-01-05 15:48:28 +01:00
|
|
|
@pytest.mark.integration
|
|
|
|
@pytest.mark.skipif(
|
2024-09-10 03:29:56 -07:00
|
|
|
not os.environ.get("AZURE_OPENAI_API_KEY", None) or not os.environ.get("AZURE_OPENAI_ENDPOINT", None),
|
2024-01-05 15:48:28 +01:00
|
|
|
reason=(
|
|
|
|
"Please export env variables called AZURE_OPENAI_API_KEY containing "
|
|
|
|
"the Azure OpenAI key, AZURE_OPENAI_ENDPOINT containing "
|
|
|
|
"the Azure OpenAI endpoint URL to run this test."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_live_run(self):
|
|
|
|
chat_messages = [ChatMessage.from_user("What's the capital of France")]
|
|
|
|
component = AzureOpenAIChatGenerator(organization="HaystackCI")
|
|
|
|
results = component.run(chat_messages)
|
|
|
|
assert len(results["replies"]) == 1
|
|
|
|
message: ChatMessage = results["replies"][0]
|
2024-11-28 11:16:07 +01:00
|
|
|
assert "Paris" in message.text
|
2024-09-17 10:36:42 +02:00
|
|
|
assert "gpt-4o-mini" in message.meta["model"]
|
2024-01-05 15:48:28 +01:00
|
|
|
assert message.meta["finish_reason"] == "stop"
|
|
|
|
|
2025-01-23 10:24:04 +01:00
|
|
|
@pytest.mark.integration
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
not os.environ.get("AZURE_OPENAI_API_KEY", None) or not os.environ.get("AZURE_OPENAI_ENDPOINT", None),
|
|
|
|
reason=(
|
|
|
|
"Please export env variables called AZURE_OPENAI_API_KEY containing "
|
|
|
|
"the Azure OpenAI key, AZURE_OPENAI_ENDPOINT containing "
|
|
|
|
"the Azure OpenAI endpoint URL to run this test."
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_live_run_with_tools(self, tools):
|
|
|
|
chat_messages = [ChatMessage.from_user("What's the weather like in Paris?")]
|
|
|
|
component = AzureOpenAIChatGenerator(organization="HaystackCI", tools=tools)
|
|
|
|
results = component.run(chat_messages)
|
|
|
|
assert len(results["replies"]) == 1
|
|
|
|
message = results["replies"][0]
|
|
|
|
|
|
|
|
assert not message.texts
|
|
|
|
assert not message.text
|
|
|
|
assert message.tool_calls
|
|
|
|
tool_call = message.tool_call
|
|
|
|
assert isinstance(tool_call, ToolCall)
|
|
|
|
assert tool_call.tool_name == "weather"
|
|
|
|
assert tool_call.arguments == {"city": "Paris"}
|
|
|
|
assert message.meta["finish_reason"] == "tool_calls"
|
|
|
|
|
2024-01-05 15:48:28 +01:00
|
|
|
# additional tests intentionally omitted as they are covered by test_openai.py
|