-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenai_async.py
40 lines (35 loc) · 1.71 KB
/
openai_async.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import httpx
from config import PROXY
def _send_to_openai(endpoint_url: str, method='post'):
async def send_to_openai(api_key: str, timeout: float, payload: dict) -> httpx.Response:
"""
Send a request to openai.
:param api_key: your api key
:param timeout: timeout in seconds
:param payload: the request body, as detailed here: https://beta.openai.com/docs/api-reference
"""
async with httpx.AsyncClient(proxies=PROXY) as client:
if method == 'post':
r = await client.post(
url=endpoint_url,
json=payload,
headers={"content_type": "application/json", "Authorization": f"Bearer {api_key}"},
timeout=timeout,
)
return r.json()
else:
r = await client.get(
url=endpoint_url,
params=payload,
headers={"content_type": "application/json", "Authorization": f"Bearer {api_key}"},
timeout=timeout,
)
return r.json()
return send_to_openai
complete = _send_to_openai("https://api.openai.com/v1/completions")
generate_img = _send_to_openai("https://api.openai.com/v1/images/generations")
embeddings = _send_to_openai("https://api.openai.com/v1/embeddings")
chat_complete = _send_to_openai("https://api.openai.com/v1/chat/completions")
credit_grants = _send_to_openai("https://api.openai.com/dashboard/billing/credit_grants", "get")
subscription = _send_to_openai("https://api.openai.com/v1/dashboard/billing/subscription", "get")
usage = _send_to_openai("https://api.openai.com/v1/dashboard/billing/usage", "get")