|
1 | 1 | try: |
| 2 | + import litellm |
2 | 3 | from litellm.integrations.custom_logger import CustomLogger |
3 | 4 | except ImportError: |
4 | 5 | raise ImportError( |
@@ -37,62 +38,85 @@ def __init__( |
37 | 38 | self.token = tinybird_token |
38 | 39 | self.api_url = api_url |
39 | 40 | self.datasource_name = datasource_name |
| 41 | + self.api_version = api_version |
40 | 42 | self.api = Tinybird( |
41 | | - token=self.token, api_url=self.api_url, version=api_version |
| 43 | + token=self.token, api_url=self.api_url, version=self.api_version |
42 | 44 | ) |
43 | 45 | self.async_api = AsyncTinybird( |
44 | | - token=self.token, api_url=self.api_url, version=api_version |
| 46 | + token=self.token, api_url=self.api_url, version=self.api_version |
45 | 47 | ) |
46 | 48 |
|
| 49 | + def _extract_data(self, kwargs, response_obj, start_time, end_time): |
| 50 | + data = { |
| 51 | + "model": kwargs.get("model"), |
| 52 | + "messages": kwargs.get("messages"), |
| 53 | + "user": kwargs.get("user"), |
| 54 | + "start_time": start_time, |
| 55 | + "end_time": end_time, |
| 56 | + "id": kwargs.get("litellm_call_id"), |
| 57 | + "stream": kwargs.get("stream", False), |
| 58 | + "call_type": kwargs.get("call_type", "completion"), |
| 59 | + "provider": kwargs.get("custom_llm_provider"), |
| 60 | + "api_key": kwargs.get("api_key"), |
| 61 | + "log_event_type": kwargs.get("log_event_type"), |
| 62 | + "llm_api_duration_ms": kwargs.get("llm_api_duration_ms"), |
| 63 | + "response_headers": kwargs.get("response_headers", {}), |
| 64 | + "cache_hit": kwargs.get("cache_hit", False), |
| 65 | + "standard_logging_object_id": kwargs.get("standard_logging_object", {}).get("id"), |
| 66 | + "standard_logging_object_status": kwargs.get("standard_logging_object", {}).get("status"), |
| 67 | + "standard_logging_object_response_time": kwargs.get("standard_logging_object", {}).get |
| 68 | + ("response_time"), |
| 69 | + "standard_logging_object_saved_cache_cost": kwargs.get("standard_logging_object", {}).get("saved_cache_cost"), |
| 70 | + "standard_logging_object_hidden_params": kwargs.get("standard_logging_object", {}).get("status"), |
| 71 | + } |
| 72 | + |
| 73 | + # response = litellm.completion(model="gpt-3.5-turbo", messages=messages, metadata={"hello": "world"}) |
| 74 | + litellm_params = kwargs.get("litellm_params", {}) |
| 75 | + data["proxy_metadata"] = litellm_params.get("metadata", {}) |
| 76 | + data["response"] = response_obj.json() |
| 77 | + data["cost"] = litellm.completion_cost(completion_response=response_obj) |
| 78 | + |
| 79 | + data["exception"] = kwargs.get("exception", None) |
| 80 | + data["traceback"] = kwargs.get("traceback_exception", None) |
| 81 | + if isinstance(start_time, datetime) and isinstance(end_time, datetime): |
| 82 | + duration = (end_time - start_time).total_seconds() |
| 83 | + else: |
| 84 | + duration = (end_time - start_time) |
| 85 | + data["duration"] = duration |
| 86 | + return safe_json_dumps(data) |
| 87 | + |
| 88 | +class TinybirdLitellmSyncHandler(TinybirdLitellmHandler): |
| 89 | + def __init__(self, *args, **kwargs): |
| 90 | + super().__init__(*args, **kwargs) |
| 91 | + |
47 | 92 | def log_success_event(self, kwargs, response_obj, start_time, end_time): |
| 93 | + data = self._extract_data(kwargs, response_obj, start_time, end_time) |
48 | 94 | self.api.send( |
49 | 95 | f"events?name={self.datasource_name}", |
50 | | - data=safe_json_dumps( |
51 | | - { |
52 | | - "kwargs": kwargs, |
53 | | - "response_obj": response_obj, |
54 | | - "start_time": start_time, |
55 | | - "end_time": end_time, |
56 | | - } |
57 | | - ), |
| 96 | + data=data |
58 | 97 | ) |
59 | 98 |
|
60 | 99 | def log_failure_event(self, kwargs, response_obj, start_time, end_time): |
| 100 | + data = self._extract_data(kwargs, response_obj, start_time, end_time) |
61 | 101 | self.api.send( |
62 | 102 | f"events?name={self.datasource_name}", |
63 | | - data=safe_json_dumps( |
64 | | - { |
65 | | - "kwargs": kwargs, |
66 | | - "response_obj": response_obj, |
67 | | - "start_time": start_time, |
68 | | - "end_time": end_time, |
69 | | - } |
70 | | - ), |
| 103 | + data=data |
71 | 104 | ) |
72 | 105 |
|
73 | | - #### ASYNC #### - for acompletion/aembeddings |
| 106 | +class TinybirdLitellmAsyncHandler(TinybirdLitellmHandler): |
| 107 | + def __init__(self, *args, **kwargs): |
| 108 | + super().__init__(*args, **kwargs) |
| 109 | + |
74 | 110 | async def async_log_success_event(self, kwargs, response_obj, start_time, end_time): |
| 111 | + data = self._extract_data(kwargs, response_obj, start_time, end_time) |
75 | 112 | await self.async_api.send( |
76 | 113 | f"events?name={self.datasource_name}", |
77 | | - data=safe_json_dumps( |
78 | | - { |
79 | | - "kwargs": kwargs, |
80 | | - "response_obj": response_obj, |
81 | | - "start_time": start_time, |
82 | | - "end_time": end_time, |
83 | | - } |
84 | | - ), |
85 | | - ) |
| 114 | + data=data |
| 115 | + ) |
86 | 116 |
|
87 | 117 | async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time): |
| 118 | + data = self._extract_data(kwargs, response_obj, start_time, end_time) |
88 | 119 | await self.async_api.send( |
89 | 120 | f"events?name={self.datasource_name}", |
90 | | - data=safe_json_dumps( |
91 | | - { |
92 | | - "kwargs": kwargs, |
93 | | - "response_obj": response_obj, |
94 | | - "start_time": start_time, |
95 | | - "end_time": end_time, |
96 | | - } |
97 | | - ), |
| 121 | + data=data |
98 | 122 | ) |
0 commit comments