aicmo 0.0.1__py3-none-any.whl → 0.0.3__py3-none-any.whl

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/__init__.py CHANGED
@@ -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
- # Initialize OpenAI client
48
- 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)}
49
- self.openai_client = OpenAI(**openai_dict)
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
- self.OPENAI_MODEL = self.secret_dict.get('OPENAI_MODEL', None)
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
- prompt_tokens = data.usage.prompt_tokens
197
- completion_tokens = data.usage.completion_tokens
198
- cost = self.COST.get('openai', {}).get("texts", {}).get(model, {})
199
- openai_input_cost = cost.get('input', None)
200
- openai_output_cost = cost.get('output', None)
201
- if openai_input_cost and openai_output_cost:
202
- tokens['input_cost'] += prompt_tokens * openai_input_cost
203
- tokens['output_cost'] += completion_tokens * openai_output_cost
204
- tokens['total_cost'] = round(tokens['input_cost'] + tokens['output_cost'], 4)
205
- tokens['prompt_tokens'] += prompt_tokens
206
- tokens['completion_tokens'] += completion_tokens
207
- tokens['total_tokens'] += prompt_tokens + completion_tokens
208
- tokens['openai_input_cost'] = openai_input_cost
209
- tokens['openai_output_cost'] = openai_output_cost
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
  }
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aicmo
3
- Version: 0.0.1
3
+ Version: 0.0.3
4
4
  Summary: A package for using aicmo functions and tools
5
5
  Author: Jayr Castro
6
6
  Author-email: jayrcastro.py@gmail.com
@@ -0,0 +1,5 @@
1
+ aicmo/__init__.py,sha256=mpBZRf4wGqigidr8cLuMW2Z20sWmAhmf3Sduv4zpK5Y,34310
2
+ aicmo-0.0.3.dist-info/METADATA,sha256=R28YKWI7HOuJuJiJPwPOkObVQ4PFK4HpkRocsDT0p7M,1187
3
+ aicmo-0.0.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
4
+ aicmo-0.0.3.dist-info/top_level.txt,sha256=uqMJv53xrmaqWkkPb9xI38tTYnSuPoplqyCzyKygG60,6
5
+ aicmo-0.0.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.3.1)
2
+ Generator: setuptools (80.10.2)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,5 +0,0 @@
1
- aicmo/__init__.py,sha256=mgPNc3BeqwTi0JvQknoBKwn6HOpk1Sb-ktiCdWlYsYc,33208
2
- aicmo-0.0.1.dist-info/METADATA,sha256=xwzhMlJ16IQepkdEmt22XIbY91MYO8vUeqA8LteHu_Y,1187
3
- aicmo-0.0.1.dist-info/WHEEL,sha256=0CuiUZ_p9E4cD6NyLD6UG80LBXYyiSYZOKDm5lp32xk,91
4
- aicmo-0.0.1.dist-info/top_level.txt,sha256=uqMJv53xrmaqWkkPb9xI38tTYnSuPoplqyCzyKygG60,6
5
- aicmo-0.0.1.dist-info/RECORD,,