test_vertex_ai.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from unittest.mock import MagicMock, patch
  2. import pytest
  3. from langchain.schema import HumanMessage, SystemMessage
  4. from langchain_google_vertexai import ChatVertexAI
  5. from embedchain.config import BaseLlmConfig
  6. from embedchain.llm.vertex_ai import VertexAILlm
  7. @pytest.fixture
  8. def vertexai_llm():
  9. config = BaseLlmConfig(temperature=0.6, model="chat-bison")
  10. return VertexAILlm(config)
  11. def test_get_llm_model_answer(vertexai_llm):
  12. with patch.object(VertexAILlm, "_get_answer", return_value="Test Response") as mock_method:
  13. prompt = "Test Prompt"
  14. response = vertexai_llm.get_llm_model_answer(prompt)
  15. assert response == "Test Response"
  16. mock_method.assert_called_once_with(prompt=prompt, config=vertexai_llm.config)
  17. @pytest.mark.skip(
  18. reason="Requires mocking of Google Console Auth. Revisit later since don't want to block users right now."
  19. )
  20. def test_get_answer(vertexai_llm, caplog):
  21. with patch.object(ChatVertexAI, "invoke", return_value=MagicMock(content="Test Response")) as mock_method:
  22. config = vertexai_llm.config
  23. prompt = "Test Prompt"
  24. messages = vertexai_llm._get_messages(prompt)
  25. response = vertexai_llm._get_answer(prompt, config)
  26. mock_method.assert_called_once_with(messages)
  27. assert response == "Test Response" # Assertion corrected
  28. assert "Config option `top_p` is not supported by this model." not in caplog.text
  29. def test_get_messages(vertexai_llm):
  30. prompt = "Test Prompt"
  31. system_prompt = "Test System Prompt"
  32. messages = vertexai_llm._get_messages(prompt, system_prompt)
  33. assert messages == [
  34. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  35. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  36. ]