aicmo 0.0.1__tar.gz → 0.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {aicmo-0.0.1 → aicmo-0.0.3}/PKG-INFO +1 -1
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo/__init__.py +41 -22
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo.egg-info/PKG-INFO +1 -1
- {aicmo-0.0.1 → aicmo-0.0.3}/setup.py +1 -1
- {aicmo-0.0.1 → aicmo-0.0.3}/README.md +0 -0
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo.egg-info/SOURCES.txt +0 -0
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo.egg-info/dependency_links.txt +0 -0
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo.egg-info/requires.txt +0 -0
- {aicmo-0.0.1 → aicmo-0.0.3}/aicmo.egg-info/top_level.txt +0 -0
- {aicmo-0.0.1 → aicmo-0.0.3}/setup.cfg +0 -0
|
@@ -23,6 +23,7 @@ class AICMOClient:
|
|
|
23
23
|
ts_host: str=None,
|
|
24
24
|
ts_port: int=None,
|
|
25
25
|
ts_api_key: str=None,
|
|
26
|
+
use_openrouter: bool=True
|
|
26
27
|
) -> None:
|
|
27
28
|
"""
|
|
28
29
|
Initialize the AICMOClient with AWS credentials and OpenAI model.
|
|
@@ -44,9 +45,15 @@ class AICMOClient:
|
|
|
44
45
|
else:
|
|
45
46
|
self.aws_s3_bucket = self.secret_dict.get('AWS_S3_BUCKET', None)
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
48
|
+
if use_openrouter:
|
|
49
|
+
# OPENROUTER_API_KEY
|
|
50
|
+
# OPENROUTER_BASE_URL
|
|
51
|
+
openai_dict = {x:self.secret_dict[y] for x,y in (("api_key", "OPENROUTER_API_KEY"), ("base_url", "OPENROUTER_BASE_URL")) if self.secret_dict.get(y, None)}
|
|
52
|
+
self.openai_client = OpenAI(**openai_dict)
|
|
53
|
+
else:
|
|
54
|
+
# Initialize OpenAI client
|
|
55
|
+
openai_dict = {x:self.secret_dict[y] for x,y in (("api_key", "OPENAI_API_KEY"), ("organization", "OPENAI_ORG_KEY")) if self.secret_dict.get(y, None)}
|
|
56
|
+
self.openai_client = OpenAI(**openai_dict)
|
|
50
57
|
|
|
51
58
|
# Initialize ScrapingBee client
|
|
52
59
|
self.SCRAPINGBEE_API_KEY = self.secret_dict.get('SCRAPINGBEE_API_KEY', None)
|
|
@@ -75,7 +82,10 @@ class AICMOClient:
|
|
|
75
82
|
self.ts_client = None
|
|
76
83
|
|
|
77
84
|
# Initialize OpenAI model
|
|
78
|
-
|
|
85
|
+
if use_openrouter:
|
|
86
|
+
self.OPENAI_MODEL = self.secret_dict.get('OPENROUTER_MODEL', None)
|
|
87
|
+
else:
|
|
88
|
+
self.OPENAI_MODEL = self.secret_dict.get('OPENAI_MODEL', None)
|
|
79
89
|
|
|
80
90
|
# Initialize Costing for per APIs
|
|
81
91
|
self.COST = json.loads(self.secret_dict['COST'])
|
|
@@ -188,25 +198,34 @@ class AICMOClient:
|
|
|
188
198
|
self,
|
|
189
199
|
data: types.chat.chat_completion.ChatCompletion,
|
|
190
200
|
model: str,
|
|
191
|
-
tokens: dict
|
|
201
|
+
tokens: dict,
|
|
202
|
+
use_openrouter: bool=True
|
|
192
203
|
) -> dict:
|
|
193
204
|
"""
|
|
194
205
|
Calculate the token usage and cost.
|
|
195
206
|
"""
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
tokens[
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
207
|
+
if use_openrouter:
|
|
208
|
+
tokens['total_cost'] += data.usage.cost
|
|
209
|
+
tokens['input_cost'] += data.usage.cost_details['upstream_inference_prompt_cost']
|
|
210
|
+
tokens['output_cost'] += data.usage.cost_details['upstream_inference_completions_cost']
|
|
211
|
+
tokens["prompt_tokens"] += data.usage.prompt_tokens
|
|
212
|
+
tokens["completion_tokens"] += data.usage.completion_tokens
|
|
213
|
+
tokens["total_tokens"] += data.usage.total_tokens
|
|
214
|
+
else:
|
|
215
|
+
prompt_tokens = data.usage.prompt_tokens
|
|
216
|
+
completion_tokens = data.usage.completion_tokens
|
|
217
|
+
cost = self.COST.get('openai', {}).get("texts", {}).get(model, {})
|
|
218
|
+
openai_input_cost = cost.get('input', None)
|
|
219
|
+
openai_output_cost = cost.get('output', None)
|
|
220
|
+
if openai_input_cost and openai_output_cost:
|
|
221
|
+
tokens['input_cost'] += prompt_tokens * openai_input_cost
|
|
222
|
+
tokens['output_cost'] += completion_tokens * openai_output_cost
|
|
223
|
+
tokens['total_cost'] = round(tokens['input_cost'] + tokens['output_cost'], 4)
|
|
224
|
+
tokens['prompt_tokens'] += prompt_tokens
|
|
225
|
+
tokens['completion_tokens'] += completion_tokens
|
|
226
|
+
tokens['total_tokens'] += prompt_tokens + completion_tokens
|
|
227
|
+
tokens['openai_input_cost'] = openai_input_cost
|
|
228
|
+
tokens['openai_output_cost'] = openai_output_cost
|
|
210
229
|
return tokens
|
|
211
230
|
|
|
212
231
|
def s3_upload_pickle(
|
|
@@ -300,9 +319,9 @@ class AICMOClient:
|
|
|
300
319
|
"text": {
|
|
301
320
|
"type": "mrkdwn",
|
|
302
321
|
"text": (
|
|
303
|
-
f"Event ID: {event['event_id']}"
|
|
304
|
-
f"User ID: {event['user_id']}"
|
|
305
|
-
f"{tb}"
|
|
322
|
+
f"Event ID: {event['event_id']}\n"
|
|
323
|
+
f"User ID: {event['user_id']}\n"
|
|
324
|
+
f"Error:\n{tb}\n"
|
|
306
325
|
)
|
|
307
326
|
}
|
|
308
327
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|