test_antrophic.py 2.4 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.anthropic import AnthropicLlm
  6. @pytest.fixture
  7. def anthropic_llm():
  8. config = BaseLlmConfig(temperature=0.5, model="gpt2")
  9. return AnthropicLlm(config)
  10. def test_get_llm_model_answer(anthropic_llm):
  11. with patch.object(AnthropicLlm, "_get_answer", return_value="Test Response") as mock_method:
  12. prompt = "Test Prompt"
  13. response = anthropic_llm.get_llm_model_answer(prompt)
  14. assert response == "Test Response"
  15. mock_method.assert_called_once_with(prompt=prompt, config=anthropic_llm.config)
  16. def test_get_answer(anthropic_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 = anthropic_llm._get_answer(prompt, anthropic_llm.config)
  22. assert response == "Test Response"
  23. mock_chat.assert_called_once_with(
  24. temperature=anthropic_llm.config.temperature, model=anthropic_llm.config.model
  25. )
  26. mock_chat_instance.assert_called_once_with(
  27. anthropic_llm._get_messages(prompt, system_prompt=anthropic_llm.config.system_prompt)
  28. )
  29. def test_get_messages(anthropic_llm):
  30. prompt = "Test Prompt"
  31. system_prompt = "Test System Prompt"
  32. messages = anthropic_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(anthropic_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 = anthropic_llm.config
  43. config.max_tokens = 500
  44. response = anthropic_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