test_azure_openai.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_openai.AzureChatOpenAI") as mock_chat:
  24. mock_chat_instance = mock_chat.return_value
  25. mock_chat_instance.invoke.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="2024-02-01",
  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. def test_get_messages(azure_openai_llm):
  38. prompt = "Test Prompt"
  39. system_prompt = "Test System Prompt"
  40. messages = azure_openai_llm._get_messages(prompt, system_prompt)
  41. assert messages == [
  42. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  43. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  44. ]
  45. def test_when_no_deployment_name_provided():
  46. config = BaseLlmConfig(temperature=0.7, model="gpt-3.5-turbo", max_tokens=50, system_prompt="System Prompt")
  47. with pytest.raises(ValueError):
  48. llm = AzureOpenAILlm(config)
  49. llm.get_llm_model_answer("Test Prompt")
  50. def test_with_api_version():
  51. config = BaseLlmConfig(
  52. deployment_name="azure_deployment",
  53. temperature=0.7,
  54. model="gpt-3.5-turbo",
  55. max_tokens=50,
  56. system_prompt="System Prompt",
  57. api_version="2024-02-01",
  58. )
  59. with patch("langchain_openai.AzureChatOpenAI") as mock_chat:
  60. llm = AzureOpenAILlm(config)
  61. llm.get_llm_model_answer("Test Prompt")
  62. mock_chat.assert_called_once_with(
  63. deployment_name="azure_deployment",
  64. openai_api_version="2024-02-01",
  65. model_name="gpt-3.5-turbo",
  66. temperature=0.7,
  67. max_tokens=50,
  68. streaming=False,
  69. )