test_anthrophic.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. from unittest.mock import 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="claude-instant-1", token_usage=False)
  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, anthropic_llm.config)
  18. def test_get_messages(anthropic_llm):
  19. prompt = "Test Prompt"
  20. system_prompt = "Test System Prompt"
  21. messages = anthropic_llm._get_messages(prompt, system_prompt)
  22. assert messages == [
  23. SystemMessage(content="Test System Prompt", additional_kwargs={}),
  24. HumanMessage(content="Test Prompt", additional_kwargs={}, example=False),
  25. ]
  26. def test_get_llm_model_answer_with_token_usage(anthropic_llm):
  27. test_config = BaseLlmConfig(
  28. temperature=anthropic_llm.config.temperature, model=anthropic_llm.config.model, token_usage=True
  29. )
  30. anthropic_llm.config = test_config
  31. with patch.object(
  32. AnthropicLlm, "_get_answer", return_value=("Test Response", {"input_tokens": 1, "output_tokens": 2})
  33. ) as mock_method:
  34. prompt = "Test Prompt"
  35. response, token_info = anthropic_llm.get_llm_model_answer(prompt)
  36. assert response == "Test Response"
  37. assert token_info == {
  38. "prompt_tokens": 1,
  39. "completion_tokens": 2,
  40. "total_tokens": 3,
  41. "total_cost": 1.265e-05,
  42. "cost_currency": "USD",
  43. }
  44. mock_method.assert_called_once_with(prompt, anthropic_llm.config)