Skip to content
Snippets Groups Projects
Commit 7d0aaae2 authored by Arash Mosharraf's avatar Arash Mosharraf
Browse files

fixed the test not getting 87% coverage

parent 287126d5
Branches
Tags
No related merge requests found
......@@ -23,11 +23,11 @@ class AzureOpenAILLM(BaseLLM):
api_version="2023-07-01-preview",
):
if name is None:
name = os.getenv("OPENAI_CHAT_MODEL_NAME", "gpt-35-turbo")
name = os.getenv("OPENAI_CHAT_MODEL_NAME", "gpt-3.5-turbo")
super().__init__(name=name)
api_key = openai_api_key or os.getenv("AZURE_OPENAI_API_KEY")
if api_key is None:
raise ValueError("OpenAI API key cannot be 'None'.")
raise ValueError("AzureOpenAI API key cannot be 'None'.")
azure_endpoint = azure_endpoint or os.getenv("AZURE_OPENAI_ENDPOINT")
if azure_endpoint is None:
raise ValueError("Azure endpoint API key cannot be 'None'.")
......@@ -36,13 +36,13 @@ class AzureOpenAILLM(BaseLLM):
api_key=api_key, azure_endpoint=azure_endpoint, api_version=api_version
)
except Exception as e:
raise ValueError(f"OpenAI API client failed to initialize. Error: {e}")
raise ValueError(f"AzureOpenAI API client failed to initialize. Error: {e}")
self.temperature = temperature
self.max_tokens = max_tokens
def __call__(self, messages: List[Message]) -> str:
if self.client is None:
raise ValueError("OpenAI client is not initialized.")
raise ValueError("AzureOpenAI client is not initialized.")
try:
completion = self.client.chat.completions.create(
model=self.name,
......
......@@ -6,8 +6,8 @@ from semantic_router.schema import Message
@pytest.fixture
def azure_openai_llm(mocker):
mocker.patch("azureopenai.Client")
return AzureOpenAILLM(openai_api_key="test_api_key")
mocker.patch("openai.Client")
return AzureOpenAILLM(openai_api_key="test_api_key", azure_endpoint="test_endpoint")
class TestOpenAILLM:
......@@ -25,12 +25,19 @@ class TestOpenAILLM:
with pytest.raises(ValueError) as _:
AzureOpenAILLM()
# def test_azure_openai_llm_init_without_azure_endpoint(self, mocker):
# mocker.patch("os.getenv", side_effect=[None, "fake-api-key"])
# with pytest.raises(ValueError) as e:
# AzureOpenAILLM(openai_api_key="test_api_key")
# assert "Azure endpoint API key cannot be 'None'." in str(e.value)
def test_azure_openai_llm_init_without_azure_endpoint(self, mocker):
mocker.patch("os.getenv", side_effect=[None, "fake-api-key"]) # Simulate missing Azure endpoint
mocker.patch("os.getenv", side_effect=lambda key, default=None: {"OPENAI_CHAT_MODEL_NAME": "test-model-name"}.get(key, default))
with pytest.raises(ValueError) as e:
AzureOpenAILLM(openai_api_key="test_api_key")
assert "Azure endpoint API key cannot be 'None'" in str(e.value)
def test_azure_openai_llm_call_uninitialized_client(self, azure_openai_llm):
# Set the client to None to simulate an uninitialized client
azure_openai_llm.client = None
......@@ -52,7 +59,7 @@ class TestOpenAILLM:
def test_azure_openai_llm_temperature_max_tokens_initialization(self):
test_temperature = 0.5
test_max_tokens = 100
azure_llm = AzureOpenAILLM(openai_api_key="test_api_key", temperature=test_temperature, max_tokens=test_max_tokens)
azure_llm = AzureOpenAILLM(openai_api_key="test_api_key",azure_endpoint="test_endpoint", temperature=test_temperature, max_tokens=test_max_tokens)
assert azure_llm.temperature == test_temperature, "Temperature not set correctly"
assert azure_llm.max_tokens == test_max_tokens, "Max tokens not set correctly"
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment