Spaces:
Configuration error
Configuration error
File size: 8,194 Bytes
447ebeb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
import datetime
import json
import os
import sys
import unittest
from unittest.mock import ANY, MagicMock, patch
sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system-path
from litellm.integrations.athina import AthinaLogger
class TestAthinaLogger(unittest.TestCase):
def setUp(self):
# Set up environment variables for testing
self.env_patcher = patch.dict(
"os.environ",
{
"ATHINA_API_KEY": "test-api-key",
"ATHINA_BASE_URL": "https://test.athina.ai",
},
)
self.env_patcher.start()
self.logger = AthinaLogger()
# Setup common test variables
self.start_time = datetime.datetime(2023, 1, 1, 12, 0, 0)
self.end_time = datetime.datetime(2023, 1, 1, 12, 0, 1)
self.print_verbose = MagicMock()
def tearDown(self):
self.env_patcher.stop()
def test_init(self):
"""Test the initialization of AthinaLogger"""
self.assertEqual(self.logger.athina_api_key, "test-api-key")
self.assertEqual(
self.logger.athina_logging_url,
"https://test.athina.ai/api/v1/log/inference",
)
self.assertEqual(
self.logger.headers,
{"athina-api-key": "test-api-key", "Content-Type": "application/json"},
)
@patch("litellm.module_level_client.post")
def test_log_event_success(self, mock_post):
"""Test successful logging of an event"""
# Setup mock response
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.text = "Success"
mock_post.return_value = mock_response
# Create test data
kwargs = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}],
"stream": False,
"litellm_params": {
"metadata": {
"environment": "test-environment",
"prompt_slug": "test-prompt",
"customer_id": "test-customer",
"customer_user_id": "test-user",
"session_id": "test-session",
"external_reference_id": "test-ext-ref",
"context": "test-context",
"expected_response": "test-expected",
"user_query": "test-query",
"tags": ["test-tag"],
"user_feedback": "test-feedback",
"model_options": {"test-opt": "test-val"},
"custom_attributes": {"test-attr": "test-val"},
}
},
}
response_obj = MagicMock()
response_obj.model_dump.return_value = {
"id": "resp-123",
"choices": [{"message": {"content": "Hi there"}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
}
# Call the method
self.logger.log_event(
kwargs, response_obj, self.start_time, self.end_time, self.print_verbose
)
# Verify the results
mock_post.assert_called_once()
call_args = mock_post.call_args
self.assertEqual(call_args[0][0], "https://test.athina.ai/api/v1/log/inference")
self.assertEqual(call_args[1]["headers"], self.logger.headers)
# Parse and verify the sent data
sent_data = json.loads(call_args[1]["data"])
self.assertEqual(sent_data["language_model_id"], "gpt-4")
self.assertEqual(sent_data["prompt"], kwargs["messages"])
self.assertEqual(sent_data["prompt_tokens"], 10)
self.assertEqual(sent_data["completion_tokens"], 5)
self.assertEqual(sent_data["total_tokens"], 15)
self.assertEqual(sent_data["response_time"], 1000) # 1 second = 1000ms
self.assertEqual(sent_data["customer_id"], "test-customer")
self.assertEqual(sent_data["session_id"], "test-session")
self.assertEqual(sent_data["environment"], "test-environment")
self.assertEqual(sent_data["prompt_slug"], "test-prompt")
self.assertEqual(sent_data["external_reference_id"], "test-ext-ref")
self.assertEqual(sent_data["context"], "test-context")
self.assertEqual(sent_data["expected_response"], "test-expected")
self.assertEqual(sent_data["user_query"], "test-query")
self.assertEqual(sent_data["tags"], ["test-tag"])
self.assertEqual(sent_data["user_feedback"], "test-feedback")
self.assertEqual(sent_data["model_options"], {"test-opt": "test-val"})
self.assertEqual(sent_data["custom_attributes"], {"test-attr": "test-val"})
# Verify the print_verbose was called
self.print_verbose.assert_called_once_with("Athina Logger Succeeded - Success")
@patch("litellm.module_level_client.post")
def test_log_event_error_response(self, mock_post):
"""Test handling of error response from the API"""
# Setup mock error response
mock_response = MagicMock()
mock_response.status_code = 400
mock_response.text = "Bad Request"
mock_post.return_value = mock_response
# Create test data
kwargs = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}],
"stream": False,
}
response_obj = MagicMock()
response_obj.model_dump.return_value = {
"id": "resp-123",
"choices": [{"message": {"content": "Hi there"}}],
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
}
# Call the method
self.logger.log_event(
kwargs, response_obj, self.start_time, self.end_time, self.print_verbose
)
# Verify print_verbose was called with error message
self.print_verbose.assert_called_once_with(
"Athina Logger Error - Bad Request, 400"
)
@patch("litellm.module_level_client.post")
def test_log_event_exception(self, mock_post):
"""Test handling of exceptions during logging"""
# Setup mock to raise exception
mock_post.side_effect = Exception("Test exception")
# Create test data
kwargs = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "Hello"}],
"stream": False,
}
response_obj = MagicMock()
response_obj.model_dump.return_value = {}
# Call the method
self.logger.log_event(
kwargs, response_obj, self.start_time, self.end_time, self.print_verbose
)
# Verify print_verbose was called with exception info
self.print_verbose.assert_called_once()
self.assertIn(
"Athina Logger Error - Test exception", self.print_verbose.call_args[0][0]
)
@patch("litellm.module_level_client.post")
def test_log_event_with_tools(self, mock_post):
"""Test logging with tools/functions data"""
# Setup mock response
mock_response = MagicMock()
mock_response.status_code = 200
mock_post.return_value = mock_response
# Create test data with tools
kwargs = {
"model": "gpt-4",
"messages": [{"role": "user", "content": "What's the weather?"}],
"stream": False,
"optional_params": {
"tools": [{"type": "function", "function": {"name": "get_weather"}}]
},
}
response_obj = MagicMock()
response_obj.model_dump.return_value = {
"id": "resp-123",
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
}
# Call the method
self.logger.log_event(
kwargs, response_obj, self.start_time, self.end_time, self.print_verbose
)
# Verify the results
sent_data = json.loads(mock_post.call_args[1]["data"])
self.assertEqual(
sent_data["tools"],
[{"type": "function", "function": {"name": "get_weather"}}],
)
if __name__ == "__main__":
unittest.main()
|