SFP commited on
Commit
eee5054
1 Parent(s): 51fc08b

Update GPTSimple.py

Browse files
Files changed (1) hide show
  1. 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
- 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
 
 
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