Spaces:
Sleeping
Sleeping
Update GPTSimple.py
Browse files- GPTSimple.py +101 -100
GPTSimple.py
CHANGED
|
@@ -1,101 +1,102 @@
|
|
| 1 |
-
import requests
|
| 2 |
-
import json
|
| 3 |
-
base_urls = {'deepinfra':"https://api.deepinfra.com/v1/openai/chat/completions", "openai":"https://api.openai.com/v1/chat/completions"}
|
| 4 |
-
def print_token(token):
|
| 5 |
-
if token.token == None:
|
| 6 |
-
print()
|
| 7 |
-
else:
|
| 8 |
-
print(token.token, end="", flush=True)
|
| 9 |
-
def get_direct_output(history, model, api_key, stream = False, base_url="openai"):
|
| 10 |
-
if base_url in base_urls:
|
| 11 |
-
url = base_urls[base_url]
|
| 12 |
-
else:
|
| 13 |
-
url = base_url
|
| 14 |
-
headers = {
|
| 15 |
-
"Content-Type": "application/json",
|
| 16 |
-
"Authorization": f"Bearer {api_key}"
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
-
data = {
|
| 20 |
-
"model": model,
|
| 21 |
-
"stream":stream,
|
| 22 |
-
"messages": history
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
self.
|
| 35 |
-
self.
|
| 36 |
-
self.
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
self.
|
| 40 |
-
self.
|
| 41 |
-
self.
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
self.
|
| 61 |
-
self.
|
| 62 |
-
self.
|
| 63 |
-
self.
|
| 64 |
-
self.
|
| 65 |
-
self.
|
| 66 |
-
self.
|
| 67 |
-
self.
|
| 68 |
-
self.
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
self.
|
| 74 |
-
self.
|
| 75 |
-
self.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
out.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
| 101 |
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
base_urls = {'deepinfra':"https://api.deepinfra.com/v1/openai/chat/completions", "openai":"https://api.openai.com/v1/chat/completions"}
|
| 4 |
+
def print_token(token):
|
| 5 |
+
if token.token == None:
|
| 6 |
+
print()
|
| 7 |
+
else:
|
| 8 |
+
print(token.token, end="", flush=True)
|
| 9 |
+
def get_direct_output(history, model, api_key, stream = False, base_url="openai"):
|
| 10 |
+
if base_url in base_urls:
|
| 11 |
+
url = base_urls[base_url]
|
| 12 |
+
else:
|
| 13 |
+
url = base_url
|
| 14 |
+
headers = {
|
| 15 |
+
"Content-Type": "application/json",
|
| 16 |
+
"Authorization": f"Bearer {api_key}"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
data = {
|
| 20 |
+
"model": model,
|
| 21 |
+
"stream":stream,
|
| 22 |
+
"messages": history,
|
| 23 |
+
"max_tokens": 1000000
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
response = requests.post(url, json=data, headers=headers, stream=stream)
|
| 27 |
+
if stream:
|
| 28 |
+
return response
|
| 29 |
+
return response.json()
|
| 30 |
+
class conversation:
|
| 31 |
+
class token:
|
| 32 |
+
def __init__(self, line):
|
| 33 |
+
if line['choices'][0]['finish_reason'] == "stop":
|
| 34 |
+
self.token = None
|
| 35 |
+
self.model = line["model"]
|
| 36 |
+
self.message = {'role':'assistant','content':None}
|
| 37 |
+
self.response = line
|
| 38 |
+
else:
|
| 39 |
+
self.token = line["choices"][0]['delta']['content']
|
| 40 |
+
self.model = line["model"]
|
| 41 |
+
self.message = line["choices"][0]['delta']
|
| 42 |
+
self.response = line
|
| 43 |
+
def streamingResponse(self, lines, invis):
|
| 44 |
+
message = ""
|
| 45 |
+
iters = lines.iter_lines(decode_unicode=True)
|
| 46 |
+
for line in iters:
|
| 47 |
+
if 'data: ' not in line:
|
| 48 |
+
continue
|
| 49 |
+
line_js = json.loads(line.split('data: ')[1])
|
| 50 |
+
if line_js['choices'][0]['finish_reason'] == "stop":
|
| 51 |
+
if not invis:
|
| 52 |
+
self.history.append({'role':'assistant', 'content':message})
|
| 53 |
+
yield self.token(line_js)
|
| 54 |
+
break
|
| 55 |
+
token = self.token(line_js)
|
| 56 |
+
message += token.token
|
| 57 |
+
yield token
|
| 58 |
+
class response:
|
| 59 |
+
def __init__(self, json):
|
| 60 |
+
self.response = json
|
| 61 |
+
self.model = json['model']
|
| 62 |
+
self.id = json['id']
|
| 63 |
+
self.choices = json['choices']
|
| 64 |
+
self.text = json['choices'][0]['message']['content']
|
| 65 |
+
self.message = json['choices'][0]['message']
|
| 66 |
+
self.usage = json['usage']
|
| 67 |
+
self.prompt_tokens = json['usage']['prompt_tokens']
|
| 68 |
+
self.output_tokens = json['usage']['completion_tokens']
|
| 69 |
+
self.total_tokens = json['usage']['total_tokens']
|
| 70 |
+
def __init__(self, api_key='', model='gpt-3.5-turbo', history=None, system_prompt="You are a helpful assistant", base_url="openai"):
|
| 71 |
+
if base_url.lower() == "deepinfra" and model == "gpt-3.5-turbo":
|
| 72 |
+
model = "meta-llama/Llama-2-70b-chat-hf"
|
| 73 |
+
self.base_url = base_url.lower()
|
| 74 |
+
self.api_key = api_key
|
| 75 |
+
self.model = model
|
| 76 |
+
self.history = [{'role':'system',"content":system_prompt}]
|
| 77 |
+
if history is not None:
|
| 78 |
+
self.history = history
|
| 79 |
+
def generate(self, invisible=False, stream=False):
|
| 80 |
+
if stream:
|
| 81 |
+
res = self.streamingResponse(get_direct_output(self.history, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
|
| 82 |
+
else:
|
| 83 |
+
res = self.response(get_direct_output(self.history, self.model, self.api_key, base_url=self.base_url))
|
| 84 |
+
if not invisible:
|
| 85 |
+
self.history.append(res.message)
|
| 86 |
+
return res
|
| 87 |
+
def ask(self, message, invisible=False, stream=False):
|
| 88 |
+
|
| 89 |
+
if invisible:
|
| 90 |
+
out = self.history.copy()
|
| 91 |
+
out.append({"role":"user", "content":message})
|
| 92 |
+
else:
|
| 93 |
+
self.history.append({"role":"user", "content":message})
|
| 94 |
+
out = self.history
|
| 95 |
+
if stream:
|
| 96 |
+
res = self.streamingResponse(get_direct_output(out, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
|
| 97 |
+
else:
|
| 98 |
+
res = self.response(get_direct_output(out, self.model, self.api_key, base_url=self.base_url))
|
| 99 |
+
if not invisible:
|
| 100 |
+
self.history.append(res.message)
|
| 101 |
+
return res
|
| 102 |
|