test_azure_openai.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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_get_answer_top_p_is_provided(azure_openai_llm, caplog):
  49. with patch("langchain_community.chat_models.AzureChatOpenAI") as mock_chat:
  50. mock_chat_instance = mock_chat.return_value
  51. mock_chat_instance.return_value = MagicMock(content="Test Response")
  52. prompt = "Test Prompt"
  53. config = azure_openai_llm.config
  54. config.top_p = 0.5
  55. response = azure_openai_llm._get_answer(prompt, config)
  56. assert response == "Test Response"
  57. mock_chat.assert_called_once_with(
  58. deployment_name=config.deployment_name,
  59. openai_api_version="2023-05-15",
  60. model_name=config.model or "gpt-3.5-turbo",
  61. temperature=config.temperature,
  62. max_tokens=config.max_tokens,
  63. streaming=config.stream,
  64. )
  65. mock_chat_instance.assert_called_once_with(
  66. azure_openai_llm._get_messages(prompt, system_prompt=config.system_prompt)
  67. )
  68. assert "Config option `top_p` is not supported by this model." in caplog.text
  69. def test_when_no_deployment_name_provided():
  70. config = BaseLlmConfig(temperature=0.7, model="gpt-3.5-turbo", max_tokens=50, system_prompt="System Prompt")
  71. with pytest.raises(ValueError):
  72. llm = AzureOpenAILlm(config)
  73. llm.get_llm_model_answer("Test Prompt")