test_anthrophic.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. ]