Update api_usage.py
Browse files- api_usage.py +60 -21
api_usage.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
import os
|
|
@@ -9,7 +10,6 @@ import botocore.exceptions
|
|
| 9 |
import concurrent.futures
|
| 10 |
import asyncio, aiohttp
|
| 11 |
import aiohttp
|
| 12 |
-
from awsLib import bedrock_model_available,bedrock_send_fake_form
|
| 13 |
|
| 14 |
BASE_URL = 'https://api.openai.com/v1'
|
| 15 |
GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4o", "gpt-4-turbo"]
|
|
@@ -538,11 +538,12 @@ async def check_key_aws_availability(key):
|
|
| 538 |
iam_user_change_password = True
|
| 539 |
if policy['PolicyName'] == 'AmazonBedrockFullAccess':
|
| 540 |
aws_bedrock_full_access = True
|
| 541 |
-
|
| 542 |
-
|
|
|
|
| 543 |
cost = check_aws_billing(session)
|
| 544 |
|
| 545 |
-
return True, username[0], root, admin, quarantine, iam_full_access, iam_user_change_password, aws_bedrock_full_access, enable_region, cost
|
| 546 |
|
| 547 |
def check_username(session):
|
| 548 |
try:
|
|
@@ -569,8 +570,10 @@ def is_model_working(form_info, model_info):
|
|
| 569 |
auth_status = model_info['authorizationStatus']
|
| 570 |
entitlementAvai = model_info['entitlementAvailability']
|
| 571 |
|
| 572 |
-
if 'formData' in form_status and agreement_status == 'AVAILABLE' and
|
| 573 |
-
|
|
|
|
|
|
|
| 574 |
if agreement_status == "ERROR":
|
| 575 |
return model_info['agreementAvailability']['errorMessage']
|
| 576 |
return "No"
|
|
@@ -583,13 +586,16 @@ async def get_model_status(session, key, secret, region, model_name, form_info):
|
|
| 583 |
model_status = is_model_working(form_info, model_info)
|
| 584 |
if model_status == "Yes":
|
| 585 |
return region, model_name, ""
|
|
|
|
|
|
|
| 586 |
elif model_status == "No":
|
| 587 |
return None, model_name, ""
|
| 588 |
else:
|
| 589 |
return None, model_name, model_status
|
| 590 |
|
| 591 |
-
async def check_bedrock_claude_status(key, secret):
|
| 592 |
-
|
|
|
|
| 593 |
|
| 594 |
models = {
|
| 595 |
"claude-v2": [],
|
|
@@ -598,20 +604,32 @@ async def check_bedrock_claude_status(key, secret):
|
|
| 598 |
"claude-3-opus-20240229-v1:0": [],
|
| 599 |
"claude-3-5-sonnet-20240620-v1:0": []
|
| 600 |
}
|
| 601 |
-
|
| 602 |
-
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
-
|
| 608 |
-
|
| 609 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 610 |
models[model_name].append(region)
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
|
|
|
|
| 615 |
return models
|
| 616 |
|
| 617 |
def check_aws_billing(session):
|
|
@@ -629,6 +647,27 @@ def check_aws_billing(session):
|
|
| 629 |
except botocore.exceptions.ClientError as error:
|
| 630 |
return error.response['Error']['Message']
|
| 631 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 632 |
def check_key_or_availability(key):
|
| 633 |
url = "https://openrouter.ai/api/v1/auth/key"
|
| 634 |
headers = {'Authorization': f'Bearer {key}'}
|
|
|
|
| 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
|
|
|
|
| 10 |
import concurrent.futures
|
| 11 |
import asyncio, aiohttp
|
| 12 |
import aiohttp
|
|
|
|
| 13 |
|
| 14 |
BASE_URL = 'https://api.openai.com/v1'
|
| 15 |
GPT_TYPES = ["gpt-3.5-turbo", "gpt-4", "gpt-4-32k", "gpt-4-32k-0314", "gpt-4o", "gpt-4-turbo"]
|
|
|
|
| 538 |
iam_user_change_password = True
|
| 539 |
if policy['PolicyName'] == 'AmazonBedrockFullAccess':
|
| 540 |
aws_bedrock_full_access = True
|
| 541 |
+
async with aiohttp.ClientSession() as async_session:
|
| 542 |
+
enable_region = await check_bedrock_claude_status(async_session, access_id, access_secret)
|
| 543 |
+
models_billing = await check_model_billing(async_session, access_id, access_secret)
|
| 544 |
cost = check_aws_billing(session)
|
| 545 |
|
| 546 |
+
return True, username[0], root, admin, quarantine, iam_full_access, iam_user_change_password, aws_bedrock_full_access, enable_region, models_billing, cost
|
| 547 |
|
| 548 |
def check_username(session):
|
| 549 |
try:
|
|
|
|
| 570 |
auth_status = model_info['authorizationStatus']
|
| 571 |
entitlementAvai = model_info['entitlementAvailability']
|
| 572 |
|
| 573 |
+
if 'formData' in form_status and agreement_status == 'AVAILABLE' and entitlementAvai == 'AVAILABLE':
|
| 574 |
+
if auth_status == 'AUTHORIZED':
|
| 575 |
+
return "Yes"
|
| 576 |
+
return "Maybe"
|
| 577 |
if agreement_status == "ERROR":
|
| 578 |
return model_info['agreementAvailability']['errorMessage']
|
| 579 |
return "No"
|
|
|
|
| 586 |
model_status = is_model_working(form_info, model_info)
|
| 587 |
if model_status == "Yes":
|
| 588 |
return region, model_name, ""
|
| 589 |
+
elif model_status == "Maybe":
|
| 590 |
+
return region, model_name, "Maybe"
|
| 591 |
elif model_status == "No":
|
| 592 |
return None, model_name, ""
|
| 593 |
else:
|
| 594 |
return None, model_name, model_status
|
| 595 |
|
| 596 |
+
async def check_bedrock_claude_status(session, key, secret):
|
| 597 |
+
# currently these regions aren't "gated" nor having only "low context" models
|
| 598 |
+
regions = ['us-east-1', 'us-west-2', 'eu-central-1', 'eu-west-3', 'ap-northeast-1', 'ap-southeast-2']
|
| 599 |
|
| 600 |
models = {
|
| 601 |
"claude-v2": [],
|
|
|
|
| 604 |
"claude-3-opus-20240229-v1:0": [],
|
| 605 |
"claude-3-5-sonnet-20240620-v1:0": []
|
| 606 |
}
|
| 607 |
+
|
| 608 |
+
payload = json.dumps({
|
| 609 |
+
"max_tokens": 0,
|
| 610 |
+
"messages": [{"role": "user", "content": ""}],
|
| 611 |
+
"anthropic_version": "bedrock-2023-05-31"
|
| 612 |
+
})
|
| 613 |
+
|
| 614 |
+
tasks = []
|
| 615 |
+
form_info = await bedrock_send_fake_form(session, key, secret, "us-east-1", "")
|
| 616 |
+
for region in regions:
|
| 617 |
+
for model in models:
|
| 618 |
+
tasks.append(get_model_status(session, key, secret, region, model, form_info))
|
| 619 |
+
results = await asyncio.gather(*tasks)
|
| 620 |
+
for region, model_name, msg in results:
|
| 621 |
+
if region and model_name:
|
| 622 |
+
if msg == "Maybe":
|
| 623 |
+
invoke_info = await send_signed_request_bedrock(session, payload, f"anthropic.{model_name}", key, secret, region)
|
| 624 |
+
if 'messages.0' in invoke_info.get('message'):
|
| 625 |
+
models[model_name].append(f'{region}: might be Unavailable if disable')
|
| 626 |
+
else:
|
| 627 |
models[model_name].append(region)
|
| 628 |
+
elif form_info.get('message') == "Operation not allowed" and "Operation not allowed" not in models[model_name]:
|
| 629 |
+
models[model_name].append('Operation not allowed')
|
| 630 |
+
elif msg and msg not in models[model_name]:
|
| 631 |
+
models[model_name].append(msg)
|
| 632 |
+
|
| 633 |
return models
|
| 634 |
|
| 635 |
def check_aws_billing(session):
|
|
|
|
| 647 |
except botocore.exceptions.ClientError as error:
|
| 648 |
return error.response['Error']['Message']
|
| 649 |
|
| 650 |
+
async def check_model_billing(session, key, secret):
|
| 651 |
+
services = {
|
| 652 |
+
'Claude (Amazon Bedrock Edition)': 'Claude 2',
|
| 653 |
+
'Claude 3 Haiku (Amazon Bedrock Edition)': 'Claude 3 Haiku',
|
| 654 |
+
'Claude 3 Sonnet (Amazon Bedrock Edition)': 'Claude 3 Sonnet',
|
| 655 |
+
'Claude 3 Opus (Amazon Bedrock Edition)': 'Claude 3 Opus',
|
| 656 |
+
'Claude 3.5 Sonnet (Amazon Bedrock Edition)': 'Claude 3.5 Sonnet'
|
| 657 |
+
}
|
| 658 |
+
costs = {}
|
| 659 |
+
|
| 660 |
+
cost_info = await asyncio.gather(*(get_service_cost_and_usage(session, key, secret, service) for service in services))
|
| 661 |
+
for cost_and_usage, model in cost_info:
|
| 662 |
+
USD = 0
|
| 663 |
+
try:
|
| 664 |
+
for result in cost_and_usage["ResultsByTime"]:
|
| 665 |
+
USD+=float(result["Total"]["BlendedCost"]["Amount"])
|
| 666 |
+
costs[f'{services[model]} ({cost_and_usage["ResultsByTime"][0]["Total"]["BlendedCost"]["Unit"]})'] = USD
|
| 667 |
+
except:
|
| 668 |
+
costs[services[model]] = USD
|
| 669 |
+
return costs
|
| 670 |
+
|
| 671 |
def check_key_or_availability(key):
|
| 672 |
url = "https://openrouter.ai/api/v1/auth/key"
|
| 673 |
headers = {'Authorization': f'Bearer {key}'}
|