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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
import aiohttp
import asyncio
import json
import gconfig
try:
from config import *
except:
gconfig.copy_config()
finally:
from config import *
async def make_request(url, headers, payload):
async with aiohttp.ClientSession(headers=headers) as session:
async with session.post(url, json=payload) as response:
if payload.get("stream") == True:
async for line in response.content:
line = line.decode("utf-8").strip()
if line.startswith("data:"):
data = line[5:].strip()
if data == "DONE":
break
else:
# data = await response.text()
yield data
else:
response_data = b""
async for chunk in response.content.iter_chunks():
response_data += chunk[0]
yield response_data.decode()
async def completions(
url,
api_key,
model,
messages,
stream=False,
max_tokens=MAX_TOKENS,
extra=None
):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"messages": messages,
"model": model,
"stream": stream,
"max_tokens": max_tokens,
}
if extra:
payload = payload | extra
if stream:
async for data in make_request(url+"/chat/completions", headers, payload):
try:
yield json.loads(data)
except:
yield data
else:
async for data in make_request(url+"/chat/completions", headers, payload):
try:
yield json.loads(data)
except:
yield data
async def responses(
url,
api_key,
model,
input,
stream=False,
max_tokens=MAX_TOKENS,
extra=None
):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"input": input,
"stream": stream,
"max_tokens": max_tokens,
}
if extra:
payload = payload | extra
if stream:
async for data in make_request(url+"/chat/completions", headers, payload):
try:
yield json.loads(data)
except:
yield data
else:
async for data in make_request(url+"/chat/completions", headers, payload):
try:
yield json.loads(data)
except:
yield data
|