test_antrophic.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import pytest
  2. from unittest.mock import MagicMock, patch
  3. from embedchain.llm.antrophic import AntrophicLlm
  4. from embedchain.config import BaseLlmConfig
  5. from langchain.schema import HumanMessage, SystemMessage
  6. @pytest.fixture
  7. def antrophic_llm():
  8. config = BaseLlmConfig(temperature=0.5, model="gpt2")
  9. return AntrophicLlm(config)
  10. def test_get_llm_model_answer(antrophic_llm):
  11. with patch.object(AntrophicLlm, "_get_answer", return_value="Test Response") as mock_method:
  12. prompt = "Test Prompt"
  13. response = antrophic_llm.get_llm_model_answer(prompt)
  14. assert response == "Test Response"
  15. mock_method.assert_called_once_with(prompt=prompt, config=antrophic_llm.config)
  16. def test_get_answer(antrophic_llm):
  17. with patch("langchain.chat_models.ChatAnthropic") 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. response = antrophic_llm._get_answer(prompt, antrophic_llm.config)
  22. assert response == "Test Response"
  23. mock_chat.assert_called_once_with(
  24. temperature=antrophic_llm.config.temperature, model=antrophic_llm.config.model
  25. )
  26. mock_chat_instance.assert_called_once_with(
  27. antrophic_llm._get_messages(prompt, system_prompt=antrophic_llm.config.system_prompt)
  28. )
  29. def test_get_messages(antrophic_llm):
  30. prompt = "Test Prompt"
  31. system_prompt = "Test System Prompt"
  32. messages = antrophic_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. ]
  37. def test_get_answer_max_tokens_is_provided(antrophic_llm, caplog):
  38. with patch("langchain.chat_models.ChatAnthropic") as mock_chat:
  39. mock_chat_instance = mock_chat.return_value
  40. mock_chat_instance.return_value = MagicMock(content="Test Response")
  41. prompt = "Test Prompt"
  42. config = antrophic_llm.config
  43. config.max_tokens = 500
  44. response = antrophic_llm._get_answer(prompt, config)
  45. assert response == "Test Response"
  46. mock_chat.assert_called_once_with(temperature=config.temperature, model=config.model)
  47. assert "Config option `max_tokens` is not supported by this model." in caplog.text