Untitled
unknown
python
a year ago
1.7 kB
4
Indexable
Never
import requests import json import datetime from time import sleep openai_org_id = "org-xxx" openai_api_key = "sk-xxx" users = json.loads(requests.get(url="https://api.openai.com/v1/organizations/" + openai_org_id + "/users", headers={ "method": "GET", "authority": "api.openai.com", "scheme": "https", "path": "/v1/organizations/" + openai_org_id + "/users", "authorization": "Bearer " + openai_api_key }, proxies={"https": "http://127.0.0.1:7890"}).text) print(users) summary = {} for user_data in users['members']['data']: user_name = user_data['user']['name'] user_id = user_data['user']['id'] summary[user_name] = {"id": user_id} print(user_name, user_id) date = datetime.date(2023, 9, 6) end_date = datetime.date(2023, 10, 12) while date <= end_date: for user_name, user_data in summary.items(): user_id = user_data['id'] user_usage = json.loads(requests.get(url="https://api.openai.com/v1/usage?date={}&user_public_id={}".format(date.strftime("%Y-%m-%d"), user_id), headers={ "method": "GET", "authority": "api.openai.com", "authorization": "Bearer " + openai_api_key, "openai-organization": openai_org_id }, proxies={"https": "http://127.0.0.1:7890"}).text) print(user_usage) for req_data in user_usage['data']: if req_data['snapshot_id'] not in user_data: user_data[req_data['snapshot_id']] = {"input": 0, "output": 0} user_data[req_data['snapshot_id']]['input'] += req_data['n_context_tokens_total'] user_data[req_data['snapshot_id']]['output'] += req_data['n_generated_tokens_total'] date += datetime.timedelta(days=1) print(summary) sleep(60)