test_anthrophic.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os
  2. from unittest.mock import MagicMock, patch
  3. import pytest
  4. from langchain.schema import HumanMessage, SystemMessage
  5. from embedchain.config import BaseLlmConfig
  6. from embedchain.llm.anthropic import AnthropicLlm
  7. @pytest.fixture
  8. def anthropic_llm():
  9. os.environ["ANTHROPIC_API_KEY"] = "test_api_key"
  10. config = BaseLlmConfig(temperature=0.5, model="gpt2")
  11. return AnthropicLlm(config)
  12. def test_get_llm_model_answer(anthropic_llm):
  13. with patch.object(AnthropicLlm, "_get_answer", return_value="Test Response") as mock_method:
  14. prompt = "Test Prompt"
  15. response = anthropic_llm.get_llm_model_answer(prompt)
  16. assert response == "Test Response"
  17. mock_method.assert_called_once_with(prompt=prompt, config=anthropic_llm.config)
  18. def test_get_answer(anthropic_llm):
  19. with patch("langchain_community.chat_models.ChatAnthropic") as mock_chat:
  20. mock_chat_instance = mock_chat.return_value
  21. mock_chat_instance.return_value = MagicMock(content="Test Response")
  22. prompt = "Test Prompt"
  23. response = anthropic_llm._get_answer(prompt, anthropic_llm.config)
  24. assert response == "Test Response"
  25. mock_chat.assert_called_once_with(
  26. anthropic_api_key="test_api_key",
  27. temperature=anthropic_llm.config.temperature,
  28. model=anthropic_llm.config.model,
  29. )
  30. mock_chat_instance.assert_called_once_with(
  31. anthropic_llm._get_messages(prompt, system_prompt=anthropic_llm.config.system_prompt)
  32. )
  33. def test_get_messages(anthropic_llm):
  34. prompt = "Test Prompt"
  35. system_prompt = "Test System Prompt"
  36. messages = anthropic_llm._get_messages(prompt, system_prompt)
  37. assert messages == [
  38. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  39. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  40. ]
  41. def test_get_answer_max_tokens_is_provided(anthropic_llm, caplog):
  42. with patch("langchain_community.chat_models.ChatAnthropic") as mock_chat:
  43. mock_chat_instance = mock_chat.return_value
  44. mock_chat_instance.return_value = MagicMock(content="Test Response")
  45. prompt = "Test Prompt"
  46. config = anthropic_llm.config
  47. config.max_tokens = 500
  48. response = anthropic_llm._get_answer(prompt, config)
  49. assert response == "Test Response"
  50. mock_chat.assert_called_once_with(
  51. anthropic_api_key="test_api_key", temperature=config.temperature, model=config.model
  52. )
  53. assert "Config option `max_tokens` is not supported by this model." in caplog.text