tokenator 0.1.11__py3-none-any.whl → 0.1.12__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,7 +14,9 @@ logger = logging.getLogger(__name__)
14
14
 
15
15
 
16
16
  class BaseOpenAIWrapper(BaseWrapper):
17
- provider = "openai"
17
+ def __init__(self, client, db_path=None, provider: str = "openai"):
18
+ super().__init__(client, db_path)
19
+ self.provider = provider
18
20
 
19
21
  def _process_response_usage(
20
22
  self, response: ResponseType
@@ -134,6 +136,7 @@ class AsyncOpenAIWrapper(BaseOpenAIWrapper):
134
136
  def tokenator_openai(
135
137
  client: OpenAI,
136
138
  db_path: Optional[str] = None,
139
+ provider: str = "openai",
137
140
  ) -> OpenAIWrapper: ...
138
141
 
139
142
 
@@ -141,23 +144,26 @@ def tokenator_openai(
141
144
  def tokenator_openai(
142
145
  client: AsyncOpenAI,
143
146
  db_path: Optional[str] = None,
147
+ provider: str = "openai",
144
148
  ) -> AsyncOpenAIWrapper: ...
145
149
 
146
150
 
147
151
  def tokenator_openai(
148
152
  client: Union[OpenAI, AsyncOpenAI],
149
153
  db_path: Optional[str] = None,
154
+ provider: str = "openai",
150
155
  ) -> Union[OpenAIWrapper, AsyncOpenAIWrapper]:
151
156
  """Create a token-tracking wrapper for an OpenAI client.
152
157
 
153
158
  Args:
154
159
  client: OpenAI or AsyncOpenAI client instance
155
160
  db_path: Optional path to SQLite database for token tracking
161
+ provider: Provider name, defaults to "openai"
156
162
  """
157
163
  if isinstance(client, OpenAI):
158
- return OpenAIWrapper(client=client, db_path=db_path)
164
+ return OpenAIWrapper(client=client, db_path=db_path, provider=provider)
159
165
 
160
166
  if isinstance(client, AsyncOpenAI):
161
- return AsyncOpenAIWrapper(client=client, db_path=db_path)
167
+ return AsyncOpenAIWrapper(client=client, db_path=db_path, provider=provider)
162
168
 
163
169
  raise ValueError("Client must be an instance of OpenAI or AsyncOpenAI")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: tokenator
3
- Version: 0.1.11
3
+ Version: 0.1.12
4
4
  Summary: Token usage tracking wrapper for LLMs
5
5
  License: MIT
6
6
  Author: Ujjwal Maheshwari
@@ -171,6 +171,66 @@ print(usage.last_execution().model_dump_json(indent=4))
171
171
  }
172
172
  """
173
173
  ```
174
+
175
+ ### xAI
176
+
177
+ You can use xAI models through the `openai` SDK and track usage using `provider` parameter in `tokenator`.
178
+
179
+ ```python
180
+ from openai import OpenAI
181
+ from tokenator import tokenator_openai
182
+
183
+ xai_client = OpenAI(
184
+ api_key=os.getenv("XAI_API_KEY"),
185
+ base_url="https://api.x.ai/v1"
186
+ )
187
+
188
+ # Wrap it with Tokenator
189
+ client = tokenator_openai(client, db_path=temp_db, provider="xai")
190
+
191
+ # Use it exactly like the OpenAI client but with xAI models
192
+ response = client.chat.completions.create(
193
+ model="grok-2-latest",
194
+ messages=[{"role": "user", "content": "Hello!"}]
195
+ )
196
+
197
+ print(response)
198
+
199
+ print(usage.last_execution())
200
+ ```
201
+
202
+ ### Other AI model providers through openai SDKs
203
+
204
+ Today, a variety of AI companies have made their APIs compatible to the `openai` SDK.
205
+ You can track usage of any such AI models using `tokenator`'s `provider` parameter.
206
+
207
+ For example, let's see how we can track usage of `perplexity` tokens.
208
+
209
+ ```python
210
+ from openai import OpenAI
211
+ from tokenator import tokenator_openai
212
+
213
+ xai_client = OpenAI(
214
+ api_key=os.getenv("PERPLEXITY_API_KEY"),
215
+ base_url="https://api.perplexity.ai"
216
+ )
217
+
218
+ # Wrap it with Tokenator
219
+ client = tokenator_openai(client, db_path=temp_db, provider="perplexity")
220
+
221
+ # Use it exactly like the OpenAI client but with xAI models
222
+ response = client.chat.completions.create(
223
+ model="grok-2-latest",
224
+ messages=[{"role": "user", "content": "Hello!"}]
225
+ )
226
+
227
+ print(response)
228
+
229
+ print(usage.last_execution())
230
+
231
+ print(usage.provider("perplexity"))
232
+ ```
233
+
174
234
  ---
175
235
 
176
236
  Most importantly, none of your data is ever sent to any server.
@@ -8,12 +8,12 @@ tokenator/migrations/script.py.mako,sha256=nJL-tbLQE0Qy4P9S4r4ntNAcikPtoFUlvXe6x
8
8
  tokenator/migrations/versions/f6f1f2437513_initial_migration.py,sha256=4cveHkwSxs-hxOPCm81YfvGZTkJJ2ClAFmyL98-1VCo,1910
9
9
  tokenator/migrations.py,sha256=YAf9gZmDzAq36PWWXPtdUQoJFYPXtIDzflC79H6gcJg,1114
10
10
  tokenator/models.py,sha256=MhYwCvmqposUNDRxFZNAVnzCqBTHxNL3Hp0MNFXM5ck,1201
11
- tokenator/openai/client_openai.py,sha256=Umfxha3BhBFU_JebPjyuaUZEZuPqJWQo1xTCuAy3R24,5691
11
+ tokenator/openai/client_openai.py,sha256=Ffa3ujLh5PuPe1W8KSISGH3NonZ_AC6ZpKhO6kTupTU,5996
12
12
  tokenator/openai/stream_interceptors.py,sha256=ez1MnjRZW_rEalv2SIPAvrU9oMD6OJoD9vht-057fDM,5243
13
13
  tokenator/schemas.py,sha256=Ye8hqZlrm3Gh2FyvOVX-hWCpKynWxS58QQRQMfDtIAQ,2114
14
14
  tokenator/usage.py,sha256=eTWfcRrTLop-30FmwHpi7_GwCJxU6Qfji374hG1Qptw,8476
15
15
  tokenator/utils.py,sha256=xg9l2GV1yJL1BlxKL1r8CboABWDslf3G5rGQEJSjFrE,1973
16
- tokenator-0.1.11.dist-info/LICENSE,sha256=wdG-B6-ODk8RQ4jq5uXSn0w1UWTzCH_MMyvh7AwtGns,1074
17
- tokenator-0.1.11.dist-info/METADATA,sha256=bXOp-V9bD3dI3mM5lSv1MruAo00RL-2bOZ0kjQUmTOQ,4446
18
- tokenator-0.1.11.dist-info/WHEEL,sha256=RaoafKOydTQ7I_I3JTrPCg6kUmTgtm4BornzOqyEfJ8,88
19
- tokenator-0.1.11.dist-info/RECORD,,
16
+ tokenator-0.1.12.dist-info/LICENSE,sha256=wdG-B6-ODk8RQ4jq5uXSn0w1UWTzCH_MMyvh7AwtGns,1074
17
+ tokenator-0.1.12.dist-info/METADATA,sha256=VdJVlwESY2_QbiterwI1lH9dng4r4WwWYd6MwXlT9V4,5969
18
+ tokenator-0.1.12.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
19
+ tokenator-0.1.12.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.0.0
2
+ Generator: poetry-core 2.0.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any