Update api_usage.py
Browse files- api_usage.py +51 -0
api_usage.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
from awsLib import bedrock_model_available, bedrock_send_fake_form, send_signed_request_bedrock, get_service_cost_and_usage
|
|
|
|
| 2 |
import requests
|
| 3 |
import json
|
| 4 |
import os
|
|
@@ -744,6 +745,56 @@ def check_key_or_limits(key):
|
|
| 744 |
balance = (prompt_tokens_limit * price_prompt) + (completion_tokens_limit * price_completion)
|
| 745 |
count+=1
|
| 746 |
return balance, models
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 747 |
|
| 748 |
if __name__ == "__main__":
|
| 749 |
key = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 1 |
from awsLib import bedrock_model_available, bedrock_send_fake_form, send_signed_request_bedrock, get_service_cost_and_usage
|
| 2 |
+
from vertexLib import *
|
| 3 |
import requests
|
| 4 |
import json
|
| 5 |
import os
|
|
|
|
| 745 |
balance = (prompt_tokens_limit * price_prompt) + (completion_tokens_limit * price_completion)
|
| 746 |
count+=1
|
| 747 |
return balance, models
|
| 748 |
+
|
| 749 |
+
async def check_gcp_anthropic(key):
|
| 750 |
+
status = False
|
| 751 |
+
project_id, client_email, private_key = key.split(':')
|
| 752 |
+
private_key = private_key.encode('latin1').decode('unicode_escape')
|
| 753 |
+
|
| 754 |
+
access_token_info = get_access_token(project_id, client_email, private_key)
|
| 755 |
+
if not access_token_info[0]:
|
| 756 |
+
return status, access_token_info[1], None
|
| 757 |
+
|
| 758 |
+
access_token = access_token_info[1]
|
| 759 |
+
|
| 760 |
+
# https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#regions
|
| 761 |
+
regions = ['us-east5', 'europe-west1', 'us-central1', 'europe-west4', 'asia-southeast1']
|
| 762 |
+
|
| 763 |
+
models = {
|
| 764 |
+
'claude-3-5-sonnet@20240620': [],
|
| 765 |
+
'claude-3-opus@20240229': [],
|
| 766 |
+
'claude-3-haiku@20240307': [],
|
| 767 |
+
'claude-3-sonnet@20240229': [],
|
| 768 |
+
}
|
| 769 |
+
|
| 770 |
+
payload = json.dumps({
|
| 771 |
+
"anthropic_version": "vertex-2023-10-16",
|
| 772 |
+
"messages": [{"role": "user", "content": ""}],
|
| 773 |
+
"max_tokens": 0,
|
| 774 |
+
})
|
| 775 |
+
|
| 776 |
+
async with aiohttp.ClientSession() as session:
|
| 777 |
+
tasks = []
|
| 778 |
+
|
| 779 |
+
async def send_gcp_wrap(region, model):
|
| 780 |
+
return region, model, await send_gcp_request(session, project_id, access_token, payload, region, model)
|
| 781 |
+
|
| 782 |
+
for region in regions:
|
| 783 |
+
for model in models:
|
| 784 |
+
tasks.append(send_gcp_wrap(region, model))
|
| 785 |
+
results = await asyncio.gather(*tasks)
|
| 786 |
+
for region, model_name, msg in results:
|
| 787 |
+
try:
|
| 788 |
+
err_msg = msg[0].get('error', '').get('message', '')
|
| 789 |
+
except:
|
| 790 |
+
err_msg = msg.get('error', '').get('message', '')
|
| 791 |
+
if 'messages.0' in err_msg or 'many requests' in err_msg:
|
| 792 |
+
if not status:
|
| 793 |
+
status = True
|
| 794 |
+
models[model_name].append(region)
|
| 795 |
+
#else:
|
| 796 |
+
#models[model_name].append(f'{region}: {err_msg}')
|
| 797 |
+
return status, "", models
|
| 798 |
|
| 799 |
if __name__ == "__main__":
|
| 800 |
key = os.getenv("OPENAI_API_KEY")
|