test_azure_openai.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from langchain.schema import HumanMessage, SystemMessage
  4. from embedchain.config import BaseLlmConfig
  5. from embedchain.llm.azure_openai import AzureOpenAILlm
  6. @pytest.fixture
  7. def azure_openai_llm():
  8. config = BaseLlmConfig(
  9. deployment_name="azure_deployment",
  10. temperature=0.7,
  11. model="gpt-3.5-turbo",
  12. max_tokens=50,
  13. system_prompt="System Prompt",
  14. )
  15. return AzureOpenAILlm(config)
  16. def test_get_llm_model_answer(azure_openai_llm):
  17. with patch.object(AzureOpenAILlm, "_get_answer", return_value="Test Response") as mock_method:
  18. prompt = "Test Prompt"
  19. response = azure_openai_llm.get_llm_model_answer(prompt)
  20. assert response == "Test Response"
  21. mock_method.assert_called_once_with(prompt=prompt, config=azure_openai_llm.config)
  22. def test_get_answer(azure_openai_llm):
  23. with patch("langchain_community.chat_models.AzureChatOpenAI") as mock_chat:
  24. mock_chat_instance = mock_chat.return_value
  25. mock_chat_instance.return_value = MagicMock(content="Test Response")
  26. prompt = "Test Prompt"
  27. response = azure_openai_llm._get_answer(prompt, azure_openai_llm.config)
  28. assert response == "Test Response"
  29. mock_chat.assert_called_once_with(
  30. deployment_name=azure_openai_llm.config.deployment_name,
  31. openai_api_version="2023-05-15",
  32. model_name=azure_openai_llm.config.model or "gpt-3.5-turbo",
  33. temperature=azure_openai_llm.config.temperature,
  34. max_tokens=azure_openai_llm.config.max_tokens,
  35. streaming=azure_openai_llm.config.stream,
  36. )
  37. mock_chat_instance.assert_called_once_with(
  38. azure_openai_llm._get_messages(prompt, system_prompt=azure_openai_llm.config.system_prompt)
  39. )
  40. def test_get_messages(azure_openai_llm):
  41. prompt = "Test Prompt"
  42. system_prompt = "Test System Prompt"
  43. messages = azure_openai_llm._get_messages(prompt, system_prompt)
  44. assert messages == [
  45. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  46. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  47. ]
  48. def test_when_no_deployment_name_provided():
  49. config = BaseLlmConfig(temperature=0.7, model="gpt-3.5-turbo", max_tokens=50, system_prompt="System Prompt")
  50. with pytest.raises(ValueError):
  51. llm = AzureOpenAILlm(config)
  52. llm.get_llm_model_answer("Test Prompt")