test_vertex_ai.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import pytest
  2. from unittest.mock import MagicMock, patch
  3. from embedchain.llm.vertex_ai import VertexAiLlm
  4. from embedchain.config import BaseLlmConfig
  5. from langchain.schema import HumanMessage, SystemMessage
  6. @pytest.fixture
  7. def vertexai_llm():
  8. config = BaseLlmConfig(temperature=0.6, model="vertexai_model", system_prompt="System Prompt")
  9. return VertexAiLlm(config)
  10. def test_get_llm_model_answer(vertexai_llm):
  11. with patch.object(VertexAiLlm, "_get_answer", return_value="Test Response") as mock_method:
  12. prompt = "Test Prompt"
  13. response = vertexai_llm.get_llm_model_answer(prompt)
  14. assert response == "Test Response"
  15. mock_method.assert_called_once_with(prompt=prompt, config=vertexai_llm.config)
  16. def test_get_answer_with_warning(vertexai_llm, caplog):
  17. with patch("langchain.chat_models.ChatVertexAI") as mock_chat:
  18. mock_chat_instance = mock_chat.return_value
  19. mock_chat_instance.return_value = MagicMock(content="Test Response")
  20. prompt = "Test Prompt"
  21. config = vertexai_llm.config
  22. config.top_p = 0.5
  23. response = vertexai_llm._get_answer(prompt, config)
  24. assert response == "Test Response"
  25. mock_chat.assert_called_once_with(temperature=config.temperature, model=config.model)
  26. assert "Config option `top_p` is not supported by this model." in caplog.text
  27. def test_get_answer_no_warning(vertexai_llm, caplog):
  28. with patch("langchain.chat_models.ChatVertexAI") as mock_chat:
  29. mock_chat_instance = mock_chat.return_value
  30. mock_chat_instance.return_value = MagicMock(content="Test Response")
  31. prompt = "Test Prompt"
  32. config = vertexai_llm.config
  33. config.top_p = 1.0
  34. response = vertexai_llm._get_answer(prompt, config)
  35. assert response == "Test Response"
  36. mock_chat.assert_called_once_with(temperature=config.temperature, model=config.model)
  37. assert "Config option `top_p` is not supported by this model." not in caplog.text
  38. def test_get_messages(vertexai_llm):
  39. prompt = "Test Prompt"
  40. system_prompt = "Test System Prompt"
  41. messages = vertexai_llm._get_messages(prompt, system_prompt)
  42. assert messages == [
  43. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  44. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  45. ]