Skip to content

Commit d070cf9

Browse files
committed
redact api_key and fix cost calculation for custom providers
1 parent 019b9fc commit d070cf9

4 files changed

Lines changed: 49 additions & 30 deletions

File tree

examples/litellm_async.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@ async def main():
2121
messages=[{"role": "user", "content": "Hi 👋 - i'm openai"}],
2222
stream=True,
2323
user="test_user",
24-
metadata={"organization": "tinybird", "environment": "dev", "project": "litellm_test", "chat_id": "1234567890"}
24+
metadata={
25+
"organization": "tinybird",
26+
"environment": "dev",
27+
"project": "litellm_test",
28+
"chat_id": "1234567890",
29+
},
2530
)
2631

2732
async for chunk in response:

examples/litellm_sync.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
messages=[{"role": "user", "content": "Hi 👋 - i'm claude"}],
1919
stream=True,
2020
user="test_user",
21-
metadata={"organization": "tinybird", "environment": "dev", "project": "litellm_test", "chat_id": "1234567890"}
21+
metadata={
22+
"organization": "tinybird",
23+
"environment": "dev",
24+
"project": "litellm_test",
25+
"chat_id": "1234567890",
26+
},
2227
)
2328

2429
for chunk in response:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "tinybird-python-sdk"
7-
version = "0.3.1"
7+
version = "0.3.2"
88
description = "Python SDK for Tinybird"
99
readme = "README.md"
1010
authors = [

tb/litellm/handler.py

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def __init__(
4848
)
4949

5050
def _extract_data(self, kwargs, response_obj, start_time, end_time):
51+
api_key = kwargs.get("api_key")
52+
if api_key and len(api_key) > 8:
53+
api_key = api_key[:4] + "****" + api_key[-4:]
54+
else:
55+
api_key = "****"
56+
5157
data = {
5258
"model": kwargs.get("model"),
5359
"messages": kwargs.get("messages"),
@@ -58,77 +64,80 @@ def _extract_data(self, kwargs, response_obj, start_time, end_time):
5864
"stream": kwargs.get("stream", False),
5965
"call_type": kwargs.get("call_type", "completion"),
6066
"provider": kwargs.get("custom_llm_provider"),
61-
"api_key": kwargs.get("api_key"),
6267
"log_event_type": kwargs.get("log_event_type"),
6368
"llm_api_duration_ms": kwargs.get("llm_api_duration_ms"),
6469
"response_headers": kwargs.get("response_headers", {}),
6570
"cache_hit": kwargs.get("cache_hit", False),
66-
"standard_logging_object_id": kwargs.get("standard_logging_object", {}).get("id"),
67-
"standard_logging_object_status": kwargs.get("standard_logging_object", {}).get("status"),
68-
"standard_logging_object_response_time": kwargs.get("standard_logging_object", {}).get
69-
("response_time"),
70-
"standard_logging_object_saved_cache_cost": kwargs.get("standard_logging_object", {}).get("saved_cache_cost"),
71-
"standard_logging_object_hidden_params": kwargs.get("standard_logging_object", {}).get("status"),
71+
"standard_logging_object_id": kwargs.get("standard_logging_object", {}).get(
72+
"id"
73+
),
74+
"standard_logging_object_status": kwargs.get(
75+
"standard_logging_object", {}
76+
).get("status"),
77+
"standard_logging_object_response_time": kwargs.get(
78+
"standard_logging_object", {}
79+
).get("response_time"),
80+
"standard_logging_object_saved_cache_cost": kwargs.get(
81+
"standard_logging_object", {}
82+
).get("saved_cache_cost"),
83+
"standard_logging_object_hidden_params": kwargs.get(
84+
"standard_logging_object", {}
85+
).get("status"),
86+
"api_key": api_key,
7287
}
73-
88+
7489
# response = litellm.completion(model="gpt-3.5-turbo", messages=messages, metadata={"hello": "world"})
7590
litellm_params = kwargs.get("litellm_params", {})
7691
data["proxy_metadata"] = litellm_params.get("metadata", {})
7792
data["response"] = response_obj.json()
78-
data["cost"] = litellm.completion_cost(completion_response=response_obj)
93+
data["cost"] = litellm.completion_cost(
94+
completion_response=response_obj,
95+
custom_llm_provider=kwargs.get("custom_llm_provider"),
96+
)
7997

8098
data["exception"] = kwargs.get("exception", None)
8199
data["traceback"] = kwargs.get("traceback_exception", None)
82100
if isinstance(start_time, datetime) and isinstance(end_time, datetime):
83101
duration = (end_time - start_time).total_seconds()
84102
else:
85-
duration = (end_time - start_time)
103+
duration = end_time - start_time
86104
data["duration"] = duration
87105
return safe_json_dumps(data)
88-
106+
107+
89108
class TinybirdLitellmSyncHandler(TinybirdLitellmHandler):
90109
def __init__(self, *args, **kwargs):
91110
super().__init__(*args, **kwargs)
92111

93112
def log_success_event(self, kwargs, response_obj, start_time, end_time):
94113
try:
95114
data = self._extract_data(kwargs, response_obj, start_time, end_time)
96-
self.api.send(
97-
f"events?name={self.datasource_name}",
98-
data=data
99-
)
115+
self.api.send(f"events?name={self.datasource_name}", data=data)
100116
except Exception as e:
101117
logging.error(f"Error logging success event: {e}")
118+
102119
def log_failure_event(self, kwargs, response_obj, start_time, end_time):
103120
try:
104121
data = self._extract_data(kwargs, response_obj, start_time, end_time)
105-
self.api.send(
106-
f"events?name={self.datasource_name}",
107-
data=data
108-
)
122+
self.api.send(f"events?name={self.datasource_name}", data=data)
109123
except Exception as e:
110124
logging.error(f"Error logging failure event: {e}")
111125

126+
112127
class TinybirdLitellmAsyncHandler(TinybirdLitellmHandler):
113128
def __init__(self, *args, **kwargs):
114129
super().__init__(*args, **kwargs)
115130

116131
async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):
117132
try:
118133
data = self._extract_data(kwargs, response_obj, start_time, end_time)
119-
await self.async_api.send(
120-
f"events?name={self.datasource_name}",
121-
data=data
122-
)
134+
await self.async_api.send(f"events?name={self.datasource_name}", data=data)
123135
except Exception as e:
124136
logging.error(f"Error logging success event: {e}")
125137

126138
async def async_log_failure_event(self, kwargs, response_obj, start_time, end_time):
127139
try:
128140
data = self._extract_data(kwargs, response_obj, start_time, end_time)
129-
await self.async_api.send(
130-
f"events?name={self.datasource_name}",
131-
data=data
132-
)
141+
await self.async_api.send(f"events?name={self.datasource_name}", data=data)
133142
except Exception as e:
134143
logging.error(f"Error logging failure event: {e}")

0 commit comments

Comments
 (0)