|
5 | 5 | # https://github.com/FlyingFathead/TelegramBot-OpenAI-API
|
6 | 6 | #
|
7 | 7 | # version of this program
|
8 |
| -version_number = "0.38" |
| 8 | +version_number = "0.39" |
9 | 9 |
|
10 |
| -# test modules |
11 |
| -# import aiohttp # For asynchronous HTTP requests |
| 10 | +# experimental modules |
12 | 11 | import requests
|
13 | 12 |
|
14 | 13 | # main modules
|
|
41 | 40 | from api_key import get_api_key
|
42 | 41 | import bot_commands
|
43 | 42 | import utils
|
| 43 | +from modules import count_tokens, read_total_token_usage, write_total_token_usage, markdown_to_html |
44 | 44 |
|
45 | 45 | # Call the startup message function
|
46 | 46 | utils.print_startup_message(version_number)
|
@@ -155,34 +155,16 @@ def check_global_rate_limit(self):
|
155 | 155 |
|
156 | 156 | # count token usage
|
157 | 157 | def count_tokens(self, text):
|
158 |
| - return len(tokenizer.encode(text)) |
159 |
| - |
| 158 | + return count_tokens(text, tokenizer) |
| 159 | + |
160 | 160 | # read and write token usage
|
161 | 161 | # detect date changes and reset token counter accordingly
|
162 | 162 | def read_total_token_usage(self):
|
163 |
| - try: |
164 |
| - with open(self.token_usage_file, 'r') as file: |
165 |
| - data = json.load(file) |
166 |
| - current_date = datetime.datetime.utcnow().strftime('%Y-%m-%d') |
167 |
| - # Return the usage for the current date, or 0 if not present |
168 |
| - return data.get(current_date, 0) |
169 |
| - except (FileNotFoundError, json.JSONDecodeError): |
170 |
| - # If the file doesn't exist or is invalid, return 0 |
171 |
| - return 0 |
| 163 | + return read_total_token_usage(self.token_usage_file) |
172 | 164 |
|
173 | 165 | # write latest token count data
|
174 | 166 | def write_total_token_usage(self, usage):
|
175 |
| - try: |
176 |
| - with open(self.token_usage_file, 'r') as file: |
177 |
| - data = json.load(file) |
178 |
| - except (FileNotFoundError, json.JSONDecodeError): |
179 |
| - data = {} # Initialize a new dictionary if the file doesn't exist or is invalid |
180 |
| - |
181 |
| - current_date = datetime.datetime.utcnow().strftime('%Y-%m-%d') |
182 |
| - data[current_date] = usage # Update the current date's usage |
183 |
| - |
184 |
| - with open(self.token_usage_file, 'w') as file: |
185 |
| - json.dump(data, file) |
| 167 | + write_total_token_usage(self.token_usage_file, usage) |
186 | 168 |
|
187 | 169 | # logging functionality
|
188 | 170 | def log_message(self, message_type, user_id, message):
|
@@ -246,34 +228,6 @@ def retrieve_chat_history(self):
|
246 | 228 | def split_large_messages(self, message, max_length=4096):
|
247 | 229 | return [message[i:i+max_length] for i in range(0, len(message), max_length)]
|
248 | 230 |
|
249 |
| - # convert markdowns to html |
250 |
| - def markdown_to_html(self, text): |
251 |
| - # Escape HTML special characters |
252 |
| - text = (text.replace('&', '&') |
253 |
| - .replace('<', '<') |
254 |
| - .replace('>', '>') |
255 |
| - .replace('"', '"')) |
256 |
| - |
257 |
| - # Convert markdown code blocks to HTML <pre> tags |
258 |
| - text = re.sub(r'```(.*?)```', r'<pre>\1</pre>', text, flags=re.DOTALL) |
259 |
| - |
260 |
| - # Convert markdown inline code to HTML <code> tags |
261 |
| - text = re.sub(r'`(.*?)`', r'<code>\1</code>', text) |
262 |
| - |
263 |
| - # Convert bold text using markdown syntax to HTML <b> tags |
264 |
| - text = re.sub(r'\*\*(.*?)\*\*', r'<b>\1</b>', text) |
265 |
| - |
266 |
| - # Convert italic text using markdown syntax to HTML <i> tags |
267 |
| - # The regex here is looking for a standalone asterisk or underscore that could denote italics |
268 |
| - # It's also making sure that it doesn't capture bold syntax by checking that an asterisk or underscore is not followed or preceded by another asterisk or underscore |
269 |
| - text = re.sub(r'(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)', r'<i>\1</i>', text) |
270 |
| - text = re.sub(r'(?<!_)_(?!_)(.+?)(?<!_)_(?!_)', r'<i>\1</i>', text) |
271 |
| - |
272 |
| - # Convert [text](url) to clickable links |
273 |
| - text = re.sub(r'\[(.*?)\]\((https?://\S+)\)', r'<a href="\2">\1</a>', text) |
274 |
| - |
275 |
| - return text |
276 |
| - |
277 | 231 | # ~~~~~~~~~~~~~~~~~~~~~
|
278 | 232 | # voice message handler
|
279 | 233 | # ~~~~~~~~~~~~~~~~~~~~~
|
@@ -524,9 +478,8 @@ async def handle_message(self, update: Update, context: CallbackContext) -> None
|
524 | 478 | context.chat_data['chat_history'] = chat_history
|
525 | 479 |
|
526 | 480 | print("Reply message before escaping:", bot_reply, flush=True)
|
527 |
| - # escaped_reply = escape_markdown(bot_reply, version=2) |
528 |
| - # escaped_reply = escape_markdown_v2(bot_reply) |
529 |
| - escaped_reply = self.markdown_to_html(bot_reply) |
| 481 | + |
| 482 | + escaped_reply = markdown_to_html(bot_reply) |
530 | 483 | print("Reply message after escaping:", escaped_reply, flush=True)
|
531 | 484 |
|
532 | 485 | # Log the bot's response
|
|
0 commit comments