Space-Face commited on
Commit
4ce9873
1 Parent(s): 5aa253f

Upload GPTSimple.py

Browse files
Files changed (1) hide show
  1. GPTSimple.py +101 -0
GPTSimple.py ADDED
@@ -0,0 +1,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
+ }
24
+
25
+ response = requests.post(url, json=data, headers=headers, stream=stream)
26
+ if stream:
27
+ return response
28
+ return response.json()
29
+ class conversation:
30
+ class token:
31
+ def __init__(self, line):
32
+ if line['choices'][0]['finish_reason'] == "stop":
33
+ self.token = None
34
+ self.model = line["model"]
35
+ self.message = {'role':'assistant','content':None}
36
+ self.response = line
37
+ else:
38
+ self.token = line["choices"][0]['delta']['content']
39
+ self.model = line["model"]
40
+ self.message = line["choices"][0]['delta']
41
+ self.response = line
42
+ def streamingResponse(self, lines, invis):
43
+ message = ""
44
+ iters = lines.iter_lines(decode_unicode=True)
45
+ for line in iters:
46
+ if 'data: ' not in line:
47
+ continue
48
+ line_js = json.loads(line.split('data: ')[1])
49
+ if line_js['choices'][0]['finish_reason'] == "stop":
50
+ if not invis:
51
+ self.history.append({'role':'assistant', 'content':message})
52
+ yield self.token(line_js)
53
+ break
54
+ token = self.token(line_js)
55
+ message += token.token
56
+ yield token
57
+ class response:
58
+ def __init__(self, json):
59
+ self.response = json
60
+ self.model = json['model']
61
+ self.id = json['id']
62
+ self.choices = json['choices']
63
+ self.text = json['choices'][0]['message']['content']
64
+ self.message = json['choices'][0]['message']
65
+ self.usage = json['usage']
66
+ self.prompt_tokens = json['usage']['prompt_tokens']
67
+ self.output_tokens = json['usage']['completion_tokens']
68
+ self.total_tokens = json['usage']['total_tokens']
69
+ def __init__(self, api_key='', model='gpt-3.5-turbo', history=None, system_prompt="You are a helpful assistant", base_url="openai"):
70
+ if base_url.lower() == "deepinfra" and model == "gpt-3.5-turbo":
71
+ model = "meta-llama/Llama-2-70b-chat-hf"
72
+ self.base_url = base_url.lower()
73
+ self.api_key = api_key
74
+ self.model = model
75
+ self.history = [{'role':'system',"content":system_prompt}]
76
+ if history is not None:
77
+ self.history = history
78
+ def generate(self, invisible=False, stream=False):
79
+ if stream:
80
+ res = self.streamingResponse(get_direct_output(self.history, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
81
+ else:
82
+ res = self.response(get_direct_output(self.history, self.model, self.api_key, base_url=self.base_url))
83
+ if not invisible:
84
+ self.history.append(res.message)
85
+ return res
86
+ def ask(self, message, invisible=False, stream=False):
87
+
88
+ if invisible:
89
+ out = self.history.copy()
90
+ out.append({"role":"user", "content":message})
91
+ else:
92
+ self.history.append({"role":"user", "content":message})
93
+ out = self.history
94
+ if stream:
95
+ res = self.streamingResponse(get_direct_output(out, self.model, self.api_key, stream=True, base_url=self.base_url), invisible)
96
+ else:
97
+ res = self.response(get_direct_output(out, self.model, self.api_key, base_url=self.base_url))
98
+ if not invisible:
99
+ self.history.append(res.message)
100
+ return res
101
+