wangrongsheng commited on
Commit
757bf4e
1 Parent(s): 0f6900b

Delete optimizeOpenAI.py

Browse files
Files changed (1) hide show
  1. optimizeOpenAI.py +0 -227
optimizeOpenAI.py DELETED
@@ -1,227 +0,0 @@
1
- """
2
- A simple wrapper for the official ChatGPT API
3
- """
4
- import json
5
- import os
6
- import threading
7
- import time
8
- import requests
9
- import tiktoken
10
- from typing import Generator
11
- from queue import PriorityQueue as PQ
12
- import json
13
- import os
14
- import time
15
- ENCODER = tiktoken.get_encoding("gpt2")
16
- class chatPaper:
17
- """
18
- Official ChatGPT API
19
- """
20
- def __init__(
21
- self,
22
- api_keys: list,
23
- proxy = None,
24
- api_proxy = None,
25
- max_tokens: int = 4000,
26
- temperature: float = 0.5,
27
- top_p: float = 1.0,
28
- model_name: str = "gpt-3.5-turbo",
29
- reply_count: int = 1,
30
- system_prompt = "You are ChatPaper, A paper reading bot",
31
- lastAPICallTime = time.time()-100,
32
- apiTimeInterval = 20,
33
- ) -> None:
34
- self.model_name = model_name
35
- self.system_prompt = system_prompt
36
- self.apiTimeInterval = apiTimeInterval
37
- self.session = requests.Session()
38
- self.api_keys = PQ()
39
- for key in api_keys:
40
- self.api_keys.put((lastAPICallTime,key))
41
- self.proxy = proxy
42
- if self.proxy:
43
- proxies = {
44
- "http": self.proxy,
45
- "https": self.proxy,
46
- }
47
- self.session.proxies = proxies
48
- self.max_tokens = max_tokens
49
- self.temperature = temperature
50
- self.top_p = top_p
51
- self.reply_count = reply_count
52
- self.decrease_step = 250
53
- self.conversation = {}
54
- if self.token_str(self.system_prompt) > self.max_tokens:
55
- raise Exception("System prompt is too long")
56
- self.lock = threading.Lock()
57
-
58
- def get_api_key(self):
59
- with self.lock:
60
- apiKey = self.api_keys.get()
61
- delay = self._calculate_delay(apiKey)
62
- time.sleep(delay)
63
- self.api_keys.put((time.time(), apiKey[1]))
64
- return apiKey[1]
65
-
66
- def _calculate_delay(self, apiKey):
67
- elapsed_time = time.time() - apiKey[0]
68
- if elapsed_time < self.apiTimeInterval:
69
- return self.apiTimeInterval - elapsed_time
70
- else:
71
- return 0
72
-
73
- def add_to_conversation(self, message: str, role: str, convo_id: str = "default"):
74
- if(convo_id not in self.conversation):
75
- self.reset(convo_id)
76
- self.conversation[convo_id].append({"role": role, "content": message})
77
-
78
- def __truncate_conversation(self, convo_id: str = "default"):
79
- """
80
- Truncate the conversation
81
- """
82
- last_dialog = self.conversation[convo_id][-1]
83
- query = str(last_dialog['content'])
84
- if(len(ENCODER.encode(str(query)))>self.max_tokens):
85
- query = query[:int(1.5*self.max_tokens)]
86
- while(len(ENCODER.encode(str(query)))>self.max_tokens):
87
- query = query[:self.decrease_step]
88
- self.conversation[convo_id] = self.conversation[convo_id][:-1]
89
- full_conversation = "\n".join([x["content"] for x in self.conversation[convo_id]],)
90
- if len(ENCODER.encode(full_conversation)) > self.max_tokens:
91
- self.conversation_summary(convo_id=convo_id)
92
- last_dialog['content'] = query
93
- self.conversation[convo_id].append(last_dialog)
94
- while True:
95
- full_conversation = ""
96
- for x in self.conversation[convo_id]:
97
- #full_conversation = str(x["content"] + "\n")
98
- full_conversation = "{} \n".format(x["content"])
99
- if (len(ENCODER.encode(full_conversation)) > self.max_tokens):
100
- self.conversation[convo_id][-1] = self.conversation[convo_id][-1][:-self.decrease_step]
101
- else:
102
- break
103
-
104
- def ask_stream(
105
- self,
106
- prompt: str,
107
- role: str = "user",
108
- convo_id: str = "default",
109
- **kwargs,
110
- ) -> Generator:
111
- if convo_id not in self.conversation:
112
- self.reset(convo_id=convo_id)
113
- self.add_to_conversation(prompt, "user", convo_id=convo_id)
114
- self.__truncate_conversation(convo_id=convo_id)
115
- apiKey = self.get_api_key()
116
- response = self.session.post(
117
- "https://api.openai.com/v1/chat/completions",
118
- headers={"Authorization": f"Bearer {kwargs.get('api_key', apiKey)}"},
119
- json={
120
- "model": self.model_name,
121
- "messages": self.conversation[convo_id],
122
- "stream": True,
123
- # kwargs
124
- "temperature": kwargs.get("temperature", self.temperature),
125
- "top_p": kwargs.get("top_p", self.top_p),
126
- "n": kwargs.get("n", self.reply_count),
127
- "user": role,
128
- },
129
- stream=True,
130
- )
131
- if response.status_code != 200:
132
- raise Exception(
133
- f"Error: {response.status_code} {response.reason} {response.text}",
134
- )
135
- for line in response.iter_lines():
136
- if not line:
137
- continue
138
- # Remove "data: "
139
- line = line.decode("utf-8")[6:]
140
- if line == "[DONE]":
141
- break
142
- resp: dict = json.loads(line)
143
- choices = resp.get("choices")
144
- if not choices:
145
- continue
146
- delta = choices[0].get("delta")
147
- if not delta:
148
- continue
149
- if "content" in delta:
150
- content = delta["content"]
151
- yield content
152
- def ask(self, prompt: str, role: str = "user", convo_id: str = "default", **kwargs):
153
- """
154
- Non-streaming ask
155
- """
156
- response = self.ask_stream(
157
- prompt=prompt,
158
- role=role,
159
- convo_id=convo_id,
160
- **kwargs,
161
- )
162
- full_response: str = "".join(response)
163
- self.add_to_conversation(full_response, role, convo_id=convo_id)
164
- usage_token = self.token_str(prompt)
165
- com_token = self.token_str(full_response)
166
- total_token = self.token_cost(convo_id=convo_id)
167
- return full_response, usage_token, com_token, total_token
168
-
169
- def check_api_available(self):
170
- response = self.session.post(
171
- "https://api.openai.com/v1/chat/completions",
172
- headers={"Authorization": f"Bearer {self.get_api_key()}"},
173
- json={
174
- "model": self.engine,
175
- "messages": [{"role": "system", "content": "You are a helpful assistant."},{"role": "user", "content": "print A"}],
176
- "stream": True,
177
- # kwargs
178
- "temperature": self.temperature,
179
- "top_p": self.top_p,
180
- "n": self.reply_count,
181
- "user": "user",
182
- },
183
- stream=True,
184
- )
185
- if response.status_code == 200:
186
- return True
187
- else:
188
- return False
189
- def reset(self, convo_id: str = "default", system_prompt = None):
190
- """
191
- Reset the conversation
192
- """
193
- self.conversation[convo_id] = [
194
- {"role": "system", "content": str(system_prompt or self.system_prompt)},
195
- ]
196
- def conversation_summary(self, convo_id: str = "default"):
197
- input = ""
198
- role = ""
199
- for conv in self.conversation[convo_id]:
200
- if (conv["role"]=='user'):
201
- role = 'User'
202
- else:
203
- role = 'ChatGpt'
204
- input+=role+' : '+conv['content']+'\n'
205
- prompt = "Your goal is to summarize the provided conversation in English. Your summary should be concise and focus on the key information to facilitate better dialogue for the large language model.Ensure that you include all necessary details and relevant information while still reducing the length of the conversation as much as possible. Your summary should be clear and easily understandable for the ChatGpt model providing a comprehensive and concise summary of the conversation."
206
- if(self.token_str(str(input)+prompt)>self.max_tokens):
207
- input = input[self.token_str(str(input))-self.max_tokens:]
208
- while self.token_str(str(input)+prompt)>self.max_tokens:
209
- input = input[self.decrease_step:]
210
- prompt = prompt.replace("{conversation}", input)
211
- self.reset(convo_id='conversationSummary')
212
- response = self.ask(prompt,convo_id='conversationSummary')
213
- while self.token_str(str(response))>self.max_tokens:
214
- response = response[:-self.decrease_step]
215
- self.reset(convo_id='conversationSummary',system_prompt='Summariaze our diaglog')
216
- self.conversation[convo_id] = [
217
- {"role": "system", "content": self.system_prompt},
218
- {"role": "user", "content": "Summariaze our diaglog"},
219
- {"role": 'assistant', "content": response},
220
- ]
221
- return self.conversation[convo_id]
222
- def token_cost(self,convo_id: str = "default"):
223
- return len(ENCODER.encode("\n".join([x["content"] for x in self.conversation[convo_id]])))
224
- def token_str(self,content:str):
225
- return len(ENCODER.encode(content))
226
- def main():
227
- return