test_vertex_ai.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.vertex_ai import VertexAILlm
  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_community.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_community.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. ]