-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathtext_message_handler.py
More file actions
1982 lines (1614 loc) · 96.3 KB
/
Copy pathtext_message_handler.py
File metadata and controls
1982 lines (1614 loc) · 96.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# text_message_handler.py
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# github.com/FlyingFathead/TelegramBot-OpenAI-API/
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import re
import html
import configparser
import os
import sys
import httpx
import requests
import logging
import datetime
import time
import json
import asyncio
import openai
import utils
from utils import holiday_replacements
import holidays
import pytz
from bs4 import BeautifulSoup
from telegram import Update
from telegram.ext import CallbackContext
from telegram.constants import ParseMode
from telegram import constants
from telegram.constants import ChatAction
from telegram.error import TimedOut
# time & date handling
from timedate_handler import (
get_ordinal_suffix,
get_english_timestamp_str,
get_finnish_timestamp_str
)
# reminder handling
from reminder_handler import handle_add_reminder, handle_delete_reminder, handle_edit_reminder, handle_view_reminders
# tg-bot specific stuff
from modules import markdown_to_html
# the tg-bot's API function calls
from config_paths import CONFIG_PATH
from config_paths import (
ELASTICSEARCH_ENABLED, ELASTICSEARCH_HOST, ELASTICSEARCH_PORT,
ELASTICSEARCH_SCHEME, ELASTICSEARCH_USERNAME, ELASTICSEARCH_PASSWORD
)
from custom_functions import custom_functions, observe_chat
from api_get_duckduckgo_search import get_duckduckgo_search
from api_get_openrouteservice import get_route, get_directions_from_addresses, format_and_translate_directions
from api_get_openweathermap import get_weather, format_and_translate_weather, format_weather_response
from api_get_maptiler import get_coordinates_from_address, get_static_map_image
from api_get_global_time import get_global_time
from api_get_stock_prices_yfinance import get_stock_price, search_stock_symbol
from api_get_website_dump import get_website_dump
from calc_module import calculate_expression
# handlers for the custom function calls
from api_perplexity_search import query_perplexity
# from perplexity_handler import handle_query_perplexity
# from api_perplexity_search import query_perplexity, translate_response, translate_response_chunked, smart_chunk, split_message
# from api_perplexity_search import query_perplexity, smart_chunk, split_message
# url processing
from url_handler import process_url_message
# Get the 'ChatLogger' defined in main.py
logger = logging.getLogger('ChatLogger')
# Load the configuration file
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
# automatic model picker
config_auto = configparser.ConfigParser()
config_auto.read(CONFIG_PATH)
# Read the holiday notification flag
enable_holiday_notification = config.getboolean('HolidaySettings', 'EnableHolidayNotification', fallback=False)
# RAG via elasticsearch
# from elasticsearch_handler import search_es_for_context
# from elasticsearch_functions import action_token_functions
# Access the Elasticsearch enabled flag
elasticsearch_enabled = config.getboolean('Elasticsearch', 'ElasticsearchEnabled', fallback=False)
ELASTICSEARCH_ENABLED = elasticsearch_enabled
if elasticsearch_enabled:
try:
from elasticsearch_handler import search_es_for_context
from elasticsearch_functions import action_token_functions
logging.info("Elasticsearch modules imported successfully.")
except ImportError:
logging.error("Elasticsearch is enabled in config.ini but the 'elasticsearch' module is not installed.")
elasticsearch_enabled = False
else:
search_es_for_context = None
action_token_functions = {}
logging.info("Elasticsearch is disabled in config.ini.")
# additional check for message length
MAX_TELEGRAM_MESSAGE_LENGTH = 4000
# Add extra wait time if we're in the middle of i.e. a translation process
extra_wait_time = 30 # Additional seconds to wait when in translation mode
# --- NEW: Import SQLite utilities ---
try:
# Assuming db_utils.py is in the same src/ directory
from db_utils import _get_daily_usage_sync, _update_daily_usage_sync, DB_PATH, DB_INITIALIZED_SUCCESSFULLY
except ImportError:
logging.critical("Failed to import from db_utils.py! SQLite usage tracking will be disabled.")
_get_daily_usage_sync = None
_update_daily_usage_sync = None
DB_PATH = None
DB_INITIALIZED_SUCCESSFULLY = False
# --- End Import ---
# get today's usage regarding OpenAI API's responses (for auto-switching)
def get_today_usage():
"""
Return (premium_used, mini_used) for today if DB is ready,
or None if DB not ready or usage could not be retrieved.
"""
if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH:
return None # Not ready
usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d')
daily_usage = _get_daily_usage_sync(DB_PATH, usage_date)
return daily_usage # daily_usage is (premium_tokens, mini_tokens) or None
# get model family and build payload
def model_supports_temperature(model: str) -> bool:
"""
Harry rule:
Only gpt-4* models get temperature.
GPT-5.x / other newer reasoning-ish models do NOT get temperature.
"""
model = (model or "").strip().lower()
return model.startswith("gpt-4")
def build_openai_tools(function_specs):
"""
Convert legacy ChatKeke custom_functions format:
{"name": "...", "description": "...", "parameters": {...}}
into modern Chat Completions tools format:
{"type": "function", "function": {...}}
If already in tools format, keep it.
"""
tools = []
for item in function_specs or []:
if isinstance(item, dict) and item.get("type") == "function" and "function" in item:
tools.append(item)
else:
tools.append({
"type": "function",
"function": item
})
return tools
def build_chat_payload(bot, messages, *, include_functions=True, max_tokens=None):
"""
Centralized Chat Completions payload builder.
Important:
- GPT-4* gets temperature.
- Non-GPT-4 models do NOT get temperature.
- Function calling uses modern tools/tool_choice for all models.
"""
payload = {
"model": bot.model,
"messages": messages,
}
if max_tokens is not None:
if model_supports_temperature(bot.model):
payload["max_tokens"] = max_tokens
else:
payload["max_completion_tokens"] = max_tokens
if model_supports_temperature(bot.model):
payload["temperature"] = bot.temperature
else:
bot.logger.info(
"Model %s is not gpt-4*; omitting temperature.",
bot.model
)
if include_functions:
tools = build_openai_tools(custom_functions)
if tools:
payload["tools"] = tools
payload["tool_choice"] = "auto"
if include_functions:
tools = build_openai_tools(custom_functions)
if tools:
payload["tools"] = tools
payload["tool_choice"] = "auto"
payload["parallel_tool_calls"] = False
return payload
def extract_chat_reply_or_raise(response_json):
"""
Prevent KeyError: 'choices' from hiding the real OpenAI error.
"""
if not isinstance(response_json, dict):
raise RuntimeError(f"OpenAI response was not a JSON object: {response_json!r}")
if "error" in response_json:
raise RuntimeError(f"OpenAI API error: {response_json['error']}")
if "choices" not in response_json:
raise RuntimeError(f"OpenAI response missing 'choices': {response_json}")
choices = response_json.get("choices") or []
if not choices:
raise RuntimeError(f"OpenAI response had empty choices: {response_json}")
message = choices[0].get("message") or {}
content = message.get("content") or ""
return content.strip()
def extract_function_call_or_none(response_json):
"""
Supports both:
- modern Chat Completions tool_calls
- old legacy function_call
Returns:
{"name": str, "arguments": str, "tool_call_id": str|None}
or None.
"""
if not isinstance(response_json, dict):
raise RuntimeError(f"OpenAI response was not a JSON object: {response_json!r}")
if "error" in response_json:
raise RuntimeError(f"OpenAI API error: {response_json['error']}")
choices = response_json.get("choices") or []
if not choices:
raise RuntimeError(f"OpenAI response had empty choices: {response_json}")
message_obj = choices[0].get("message") or {}
# Modern tools/tool_calls path.
tool_calls = message_obj.get("tool_calls") or []
if tool_calls:
first_tool_call = tool_calls[0]
if first_tool_call.get("type") == "function":
fn = first_tool_call.get("function") or {}
return {
"name": fn.get("name"),
"arguments": fn.get("arguments") or "{}",
"tool_call_id": first_tool_call.get("id"),
}
# Legacy functions/function_call path.
legacy_function_call = message_obj.get("function_call")
if legacy_function_call:
return {
"name": legacy_function_call.get("name"),
"arguments": legacy_function_call.get("arguments") or "{}",
"tool_call_id": None,
}
return None
# model picker auto-switch
def pick_model_auto_switch(bot):
if not config_auto.has_section('ModelAutoSwitch'):
logging.info("Auto-switch is not configured. Using default model: %s", bot.model)
return True
if not config_auto['ModelAutoSwitch'].getboolean('Enabled', fallback=False):
logging.info("ModelAutoSwitch.Enabled = False => skipping auto-switch, using %s", bot.model)
return True
premium_model = config_auto['ModelAutoSwitch'].get('PremiumModel', 'gpt-4')
fallback_model = config_auto['ModelAutoSwitch'].get('FallbackModel', 'gpt-3.5-turbo')
premium_limit = config_auto['ModelAutoSwitch'].getint('PremiumTokenLimit', 500000)
fallback_limit = config_auto['ModelAutoSwitch'].getint('MiniTokenLimit', 10000000)
fallback_action = config_auto['ModelAutoSwitch'].get('FallbackLimitAction', 'Deny')
if not DB_INITIALIZED_SUCCESSFULLY or not DB_PATH:
logging.warning("DB not initialized or path missing — can't auto-switch, fallback to default model.")
return True
usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d')
daily_usage = _get_daily_usage_sync(DB_PATH, usage_date)
if not daily_usage:
daily_premium_tokens, daily_fallback_tokens = (0, 0)
else:
daily_premium_tokens, daily_fallback_tokens = daily_usage
# --> NOW WE CAN SAFELY LOG THE USAGE & LIMITS <--
logging.info(f"Daily premium tokens = {daily_premium_tokens}, daily fallback tokens = {daily_fallback_tokens}")
logging.info(f"Premium limit = {premium_limit}, Fallback limit = {fallback_limit}, fallback_action = {fallback_action}")
# Decide if we can still use the premium model
if daily_premium_tokens < premium_limit:
bot.model = premium_model
logging.info("Using premium model: %s, daily usage = %d", bot.model, daily_premium_tokens)
return True
else:
# Premium limit exceeded => check fallback usage
if daily_fallback_tokens < fallback_limit:
bot.model = fallback_model
logging.info("Premium limit reached; using fallback model: %s, fallback usage = %d",
bot.model, daily_fallback_tokens)
return True
else:
# Fallback usage also exceeded => check action
if fallback_action.lower() == 'deny':
logging.warning("Fallback limit also reached => Deny further usage.")
return False
elif fallback_action.lower() == 'warn':
logging.warning("Fallback limit reached but ignoring => 'Warn' => proceed with fallback anyway.")
bot.model = fallback_model
return True
else:
logging.info("Fallback limit reached => 'Proceed' => continuing anyway with fallback.")
bot.model = fallback_model
return True
# text message handling logic
async def handle_message(bot, update: Update, context: CallbackContext, logger) -> None:
# 1) Auto-switch first if applicable
can_proceed = pick_model_auto_switch(bot)
if not can_proceed:
bot.logger.warning("Denied request because daily usage limits are exceeded for both premium & fallback.")
await context.bot.send_message(
chat_id=update.effective_chat.id,
text="Sorry, we've hit today's usage limit — cannot proceed. ☹️ Please try again tomorrow!"
)
return
else:
bot.logger.info(f"Proceeding with the request using model '{bot.model}'.")
# Extract chat_id as soon as possible from the update object
chat_id = update.effective_chat.id
# Initialize a flag to indicate whether a response has been sent
response_sent = False
# send a "holiday message" if the bot is on a break
if bot.is_bot_disabled:
await context.bot.send_message(chat_id=update.message.chat_id, text=bot.bot_disabled_msg)
return
# Before anything else, check the global rate limit
if bot.check_global_rate_limit():
await context.bot.send_message(chat_id=update.message.chat_id, text="The bot is currently busy. Please try again in a minute.")
return
# process a text message
try:
# Create an Event to control the typing animation
stop_typing_event = asyncio.Event()
# Start the typing animation in a background task
typing_task = asyncio.create_task(send_typing_animation(context.bot, chat_id, stop_typing_event))
# Check if there is a transcribed text available
if 'transcribed_text' in context.user_data:
user_message = context.user_data['transcribed_text']
# Clear the transcribed text after using it
del context.user_data['transcribed_text']
else:
user_message = update.message.text
chat_id = update.message.chat_id
user_token_count = bot.count_tokens(user_message)
# Debug print to check types
bot.logger.info(f"[Token counting/debug] user_token_count type: {type(user_token_count)}, value: {user_token_count}")
bot.logger.info(f"[Token counting/debug] bot.total_token_usage type: {type(bot.total_token_usage)}, value: {bot.total_token_usage}")
# Convert max_tokens_config to an integer
# Attempt to read max_tokens_config as an integer
try:
# max_tokens_config = int(bot.config.get('GlobalMaxTokenUsagePerDay', '100000'))
max_tokens_config = bot.config.getint('GlobalMaxTokenUsagePerDay', 100000)
is_no_limit = max_tokens_config == 0
bot.logger.info(f"[Token counting/debug] max_tokens_config type: {type(max_tokens_config)}, value: {max_tokens_config}")
# Debug: Print the value read from token_usage.json
bot.logger.info(f"[Debug] Total token usage from file: {bot.total_token_usage}")
except ValueError:
bot.logger.error("Invalid value for GlobalMaxTokenUsagePerDay in the configuration file.")
await update.message.reply_text("An error occurred while processing your request: couldn't get proper token count. Please try again later.")
return
# Safely compare user_token_count and max_tokens_config
if not is_no_limit and (bot.total_token_usage + user_token_count) > max_tokens_config:
await update.message.reply_text("The bot has reached its daily token limit. Please try again tomorrow.")
return
# Debug: Print before token limit checks
bot.logger.info(f"[Debug] is_no_limit: {is_no_limit}, user_token_count: {user_token_count}, max_tokens_config: {max_tokens_config}")
# ~~~~~~~~~~~~~~~
# Make timestamp
# ~~~~~~~~~~~~~~~
now_utc = datetime.datetime.utcnow()
# We'll keep this for session timeout comparisons
current_time = now_utc
day_of_week = now_utc.strftime("%A")
system_timestamp = now_utc.strftime("%Y-%m-%d %H:%M:%S UTC")
english_line = get_english_timestamp_str(now_utc)
finnish_line = get_finnish_timestamp_str(now_utc)
# Combine them however you like, e.g.:
#
# Monday, April 9th, 2025 | Time (UTC): 12:34:56
# maanantai, 9. huhtikuuta 2025, klo 15:34:56 Suomen aikaa
#
current_timestamp_str = f"{english_line}\n{finnish_line}"
# We'll put that into a system message
timestamp_system_msg = {
"role": "system",
"content": current_timestamp_str
}
# Add the user's tokens to the total usage (JSON style)
bot.total_token_usage += user_token_count
# Log the incoming user message
bot.logger.info(f"Received message from {update.message.from_user.username} ({chat_id}): {user_message}")
# Check if session timeout is enabled and if session is timed out
if bot.session_timeout_minutes > 0:
timeout_seconds = bot.session_timeout_minutes * 60 # Convert minutes to seconds
if 'last_message_time' in context.chat_data:
last_message_time = context.chat_data['last_message_time']
elapsed_time = (current_time - last_message_time).total_seconds()
if elapsed_time > timeout_seconds:
# Log the length of chat history before trimming
chat_history_length_before = len(context.chat_data.get('chat_history', []))
bot.logger.info(f"Chat history length before trimming: {chat_history_length_before}")
# Session timeout logic
if bot.max_retained_messages == 0:
# Clear entire history
context.chat_data['chat_history'] = []
bot.logger.info(f"'MaxRetainedMessages' set to 0, cleared the entire chat history due to session timeout.")
else:
# Keep the last N messages
context.chat_data['chat_history'] = context.chat_data['chat_history'][-bot.max_retained_messages:]
bot.logger.info(f"Retained the last {bot.max_retained_messages} messages due to session timeout.")
# Log the length of chat history after trimming
chat_history_length_after = len(context.chat_data.get('chat_history', []))
bot.logger.info(f"Chat history length after trimming: {chat_history_length_after}")
bot.logger.info(f"[DebugInfo] Session timed out. Chat history updated.")
else:
# Log the skipping of session timeout check
bot.logger.info(f"[DebugInfo] Session timeout check skipped as 'SessionTimeoutMinutes' is set to 0.")
# Update the time of the last message
context.chat_data['last_message_time'] = current_time
# Log the current chat history
bot.logger.debug(f"Current chat history: {context.chat_data.get('chat_history')}")
# Initialize chat_history as an empty list if it doesn't exist
chat_history = context.chat_data.get('chat_history', [])
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Insert the new system msg
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~
# logger info on the appended system message
logger.info(f"Inserting timestamp system message: {current_timestamp_str}")
chat_history.append(timestamp_system_msg)
# Append the new user message to the chat history
chat_history.append({"role": "user", "content": user_message})
# Prepare the conversation history to send to the OpenAI API
# # // old method that included the timestamp in the original system message
# system_timestamp = datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
# system_message = {"role": "system", "content": f"System time+date: {system_timestamp}, {day_of_week}): {bot.system_instructions}"}
system_message = {"role": "system", "content": f"Instructions: {bot.system_instructions}"}
chat_history_with_system_message = [system_message] + chat_history
# Trim chat history if it exceeds a specified length or token limit
bot.trim_chat_history(chat_history, bot.max_tokens)
# Log the incoming user message
bot.log_message('User', update.message.from_user.id, update.message.text)
# Check if holiday notification is enabled
if enable_holiday_notification:
# Get the current date and time in Finland's timezone
now = datetime.datetime.now(pytz.timezone('Europe/Helsinki'))
# Create a holidays object for Finland
fi_holidays = holidays.Finland()
# Add non-official but widely celebrated holidays
additional_holidays = {
datetime.date(now.year, 4, 30): "[en] May Day Eve [fi] vappuaatto",
datetime.date(now.year, 7, 26): "[fi] ChatKeken syntymäpäivät! Ole iloinen koko päivän ajan! [en] ChatKeke's Birthday! Be cheerful the entire day!",
datetime.date(now.year, 12, 31): "[en] New Year's Eve [fi] uudenvuodenaatto"
}
# Update the holidays object with the additional holidays
fi_holidays.update(additional_holidays)
# Check if the current date is a holiday
if now.date() in fi_holidays:
holiday_name = fi_holidays.get(now.date())
finnish_name = holiday_replacements.get(holiday_name, holiday_name)
holiday_message = f"HUOMIO: Suomessa on tänään juhlapäivä: {finnish_name}. Muista mainita juhlapyhästä käyttäjälle tervehtiessäsi (käytä suomeksi tervehtiessäsi VAIN suomenkielistä juhlapyhän nimeä) ja kysellessä kuulumisia! (esim. hyvää joulua!, hauskaa vappua!, hyvää juhannusta!, iloista uutta vuotta!, jne. \n(In English: Today is a Finnish holiday: {finnish_name}. Include that in your current understanding and mention it, especially if you're talking about anything current.)"
chat_history_with_system_message.insert(1, {"role": "system", "content": holiday_message})
# (old) // Show typing animation
# await context.bot.send_chat_action(chat_id=update.effective_message.chat_id, action=constants.ChatAction.TYPING)
# Process any YouTube URLs before the Elasticsearch RAG
youtube_context_messages = await process_url_message(user_message)
logger.info(f"YouTube context messages: {youtube_context_messages}")
# # Process YouTube URLs and append data.
# for youtube_context in youtube_context_messages:
# chat_history_with_system_message.append({
# "role": "system",
# "content": youtube_context
# })
for youtube_context in youtube_context_messages:
chat_history_with_system_message.append({
"role": "system",
"content": youtube_context
})
logger.info(f"Added YouTube context: {youtube_context}")
# ~~~~~~~~~~~~~~~~~
# Elasticsearch RAG
# ~~~~~~~~~~~~~~~~~
# es_context = await search_es_for_context(user_message)
# Initialize chat_history_with_es_context to default value
chat_history_with_es_context = chat_history_with_system_message
# Assuming ELASTICSEARCH_ENABLED is true and we have fetched es_context
if elasticsearch_enabled and search_es_for_context:
logger.info(f"Elasticsearch is enabled, searching for context for user message: {user_message}")
# es_context = await search_es_for_context(user_message)
es_context = await search_es_for_context(user_message, config)
action_triggered = False # Flag to check if an action was triggered based on tokens
if es_context and es_context.strip():
logger.info(f"Elasticsearch found additional context: {es_context}")
# Iterate through your action tokens and check if any exist in the es_context
for token, function in action_token_functions.items():
if token in es_context:
logger.info(f"Action token found: {token}. Executing corresponding function.")
# Execute the mapped function
chat_history_with_es_context = await function(context, update, chat_history_with_system_message)
action_triggered = True
break # Stop checking after the first match to avoid multiple actions
# If no action token was found, just add the Elasticsearch context to the chat history
if not action_triggered:
chat_history_with_es_context = [{"role": "system", "content": "Elasticsearch RAG data: " + es_context}] + chat_history_with_system_message
else:
logger.info("No relevant or non-empty context found via Elasticsearch. Proceeding.")
chat_history_with_es_context = chat_history_with_system_message
else:
chat_history_with_es_context = chat_history_with_system_message
# ~~~~~~~~~~~
# API request
# ~~~~~~~~~~~
for attempt in range(bot.max_retries):
try:
# Prepare the payload for the API request
# payload = {
# "model": bot.model,
# #"messages": context.chat_data['chat_history'],
# # "messages": chat_history_with_system_message, # Updated to include system message
# "messages": chat_history_with_es_context,
# "temperature": bot.temperature, # Use the TEMPERATURE variable loaded from config.ini
# "functions": custom_functions,
# "function_call": 'auto' # Allows the model to dynamically choose the function
# }
# // new payload build method
payload = build_chat_payload(bot, chat_history_with_es_context)
# Make the API request
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai.api_key}"
}
async with httpx.AsyncClient() as client:
response = await client.post("https://api.openai.com/v1/chat/completions",
data=json.dumps(payload),
headers=headers,
timeout=bot.timeout)
# Check if response status is 401 (Unauthorized)
if response.status_code == 401:
bot.logger.error("Received 401 Unauthorized: Invalid OpenAI API key. Please set up your API key correctly.")
await context.bot.send_message(
chat_id=chat_id,
text="😐", # First message with just the emoji
parse_mode=ParseMode.HTML
)
await context.bot.send_message(
chat_id=chat_id,
text="Error: Invalid OpenAI API key. Please contact the administrator to resolve this issue.",
parse_mode=ParseMode.HTML
)
return # Stop further execution in case of 401 error
raw_response_text = response.text
if response.status_code >= 400:
bot.logger.error(
"OpenAI API call failed, status code = %d, body = %s",
response.status_code,
raw_response_text[:4000],
)
await context.bot.send_message(
chat_id=chat_id,
text=f"OpenAI API error {response.status_code}. Check logs.",
parse_mode=ParseMode.HTML
)
stop_typing_event.set()
return
try:
response_json = response.json()
except Exception as e:
bot.logger.error(
"OpenAI returned non-JSON response: %s; parse error: %s",
raw_response_text[:4000],
e,
)
await context.bot.send_message(
chat_id=chat_id,
text="OpenAI returned a non-JSON response. Check logs.",
parse_mode=ParseMode.HTML
)
stop_typing_event.set()
return
if "choices" not in response_json:
bot.logger.error("OpenAI response missing 'choices': %s", response_json)
await context.bot.send_message(
chat_id=chat_id,
text="OpenAI response had no choices. Check logs.",
parse_mode=ParseMode.HTML
)
stop_typing_event.set()
return
bot.logger.info("OpenAI API call succeeded, status code = %d", response.status_code)
# ~~~~~ read the usage once we have the `response_json` ~~~~~
if "usage" in response_json:
usage_obj = response_json["usage"]
# Log everything we got
bot.logger.info(f"OpenAI usage field => {usage_obj}")
# They typically have 'prompt_tokens', 'completion_tokens', and 'total_tokens'
prompt_used = usage_obj.get("prompt_tokens", 0)
completion_used = usage_obj.get("completion_tokens", 0)
total_used = usage_obj.get("total_tokens", 0)
bot.logger.info(f"Used {prompt_used} prompt tokens + {completion_used} completion tokens = {total_used} total tokens in this request.")
# Figure out if we're “premium” or “mini”
# (If your config has multiple fallback possibilities, do it your own way.
# For simplicity, we just compare the current `bot.model` to the PremiumModel from config.)
premium_model_name = config_auto["ModelAutoSwitch"].get("PremiumModel", "gpt-4")
if bot.model == premium_model_name:
tier = "premium"
bot.logger.info(f"We're using the premium model => usage credited to 'premium_tokens'.")
else:
tier = "mini"
bot.logger.info(f"We're using the fallback model => usage credited to 'mini_tokens'.")
# Now actually log it to SQLite
if DB_INITIALIZED_SUCCESSFULLY and DB_PATH:
usage_date = datetime.datetime.utcnow().strftime('%Y-%m-%d')
bot.logger.info(f"Updating DB with {total_used} tokens on {usage_date} for tier='{tier}'.")
_update_daily_usage_sync(DB_PATH, usage_date, tier, total_used)
else:
bot.logger.warning("DB not initialized => can't store usage info in daily_usage table.")
else:
bot.logger.warning("No 'usage' field found in the API response. Could not update daily usage stats.")
# Log the API request payload
bot.logger.info(f"API Request Payload: {payload}")
# ~~~~~~~~~~~~~~~~~~
# > function calling
# ~~~~~~~~~~~~~~~~~~
# Check for a modern tool_call or legacy function_call in the response
function_call = extract_function_call_or_none(response_json)
if function_call:
function_name = function_call["name"]
# ~~~~~~~~~~~~~~~~~~~~~~
# Calculator Function
# ~~~~~~~~~~~~~~~~~~~~~~
# calculator module for mathematical equations
if function_name == 'calculate_expression':
arguments = json.loads(function_call.get('arguments', '{}'))
expression = arguments.get('expression', '')
if expression:
try:
# Set a timeout of 5 seconds (adjust as needed)
calc_result = await asyncio.wait_for(calculate_expression(expression), timeout=5)
if calc_result is None or calc_result.strip() == "":
# Handle the case where the calculation returned None or an empty result
system_message = "Calculator returned None or an empty result. Please ensure the expression is valid."
bot.logger.warning(f"Calculator returned None or empty result for expression: '{expression}'")
else:
# Proper result was returned, log and format it
# calc_result = f"`{calc_result}`" # Wrap the result in backticks for code formatting in Markdown.
calc_result = f"{calc_result}" # Wrap the result in <code> tags for HTML formatting.
system_message = (
f"[Calculator result, explain to the user in their language if needed. "
"IMPORTANT: Do not translate or simplify the mathematical expressions themselves. "
"NOTE: Telegram doesn't support LaTeX. Use simple HTML formatting, i.e. <code></code> if need be, note that <pre> or <br> are NOT allowed HTML tags. If the user explicitly asks for LaTeX or mentions LaTeX, use LaTeX formatting; "
f"otherwise, use plain text or Unicode with simple HTML.] Result:\n{calc_result}\n\n"
"[NOTE: format your response appropriately, possibly incorporating additional context or user intent, TRANSLATE it to the user's language if needed.]"
).format(calc_result=calc_result) # This ensures the result is inserted correctly
bot.logger.info(f"Calculation result: {calc_result}")
except asyncio.TimeoutError:
# Handle the case where the calculation took too long
system_message = "Calculation timed out after 5 seconds. Please try a simpler expression."
bot.logger.error(f"TimeoutError: Calculation for expression '{expression}' exceeded the time limit.")
except Exception as e:
# Handle other exceptions
system_message = f"An error occurred while evaluating the expression: {str(e)}"
bot.logger.error(f"Error evaluating expression '{expression}': {e}")
else:
system_message = "Please provide a valid expression for calculation."
bot.logger.warning("Received an empty expression for calculation.")
# Append the calculation result or the error message as a system message
chat_history.append({"role": "system", "content": system_message})
context.chat_data['chat_history'] = chat_history
# Debugging: Log the updated chat history
bot.logger.info(f"Updated chat history with calculator result: {chat_history}")
# Make an API request using the updated chat history
response_json = await make_api_request(
bot,
chat_history,
bot.timeout,
include_functions=False
)
# Extract and handle the content from the API response
bot_reply_content = extract_chat_reply_or_raise(response_json)
bot.logger.info(f"Bot's response content: '{bot_reply_content}'")
bot_reply = bot_reply_content.strip() if bot_reply_content else ""
# Update usage metrics and logs
bot_token_count = bot.count_tokens(bot_reply)
bot.total_token_usage += bot_token_count
bot.write_total_token_usage(bot.total_token_usage)
bot.logger.info(f"Bot's response to {update.message.from_user.username} ({chat_id}): '{bot_reply}'")
# Ensure the bot has a substantive response to send
if bot_reply:
# escaped_reply = markdown_to_html(bot_reply)
escaped_reply = bot_reply
# Log the bot's response
bot.log_message(
message_type='Bot',
message=bot_reply,
source='Calculator Module'
)
await context.bot.send_message(chat_id=chat_id, text=escaped_reply, parse_mode=ParseMode.HTML)
else:
# If no content to send, log and add a system message
bot.logger.error("Attempted to send an empty message.")
system_message = "Calculator returned an empty result or encountered an error, resulting in no response to send."
chat_history.append({"role": "system", "content": system_message})
context.chat_data['chat_history'] = chat_history
# Optionally, you can log this as a fallback
bot.logger.info("Added system message indicating an empty result.")
# Finalize the function call
stop_typing_event.set()
context.user_data.pop('active_translation', None)
return
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# weather via openweathermap api
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elif function_name == 'get_weather':
# Fetch the weather data
arguments = json.loads(function_call.get('arguments', '{}'))
city_name = arguments.get('city_name', 'DefaultCity')
country = arguments.get('country', None) # Fetch the country parameter, defaulting to None if not provided
# Now pass the country parameter to your get_weather function
weather_info = await get_weather(city_name, country=country) # Assuming get_weather is updated to accept country
# Add the received weather data as a system message
if weather_info:
system_message = f"[OpenWeatherMap API request returned data, use according to your own discernment as to what include and what format, by default, use emojis (you're in Telegram)]: {weather_info}"
else:
system_message = "[OpenWeatherMap API request failed to retrieve data]"
# Append the system message to the chat history
chat_history.append({"role": "system", "content": system_message})
context.chat_data['chat_history'] = chat_history
# Prepare the payload for the API request with updated chat history
response_json = await make_api_request(
bot,
chat_history,
bot.timeout,
include_functions=False
)
# Log the API request payload
bot.logger.info(f"API Request Payload: {payload}")
# Safely get the content or default to an empty string if not found
bot_reply_content = extract_chat_reply_or_raise(response_json)
# Only call strip if bot_reply_content is not None
bot_reply = bot_reply_content.strip() if bot_reply_content else "🤔"
# Count tokens in the bot's response
bot_token_count = bot.count_tokens(bot_reply)
# Add the bot's tokens to the total usage
bot.total_token_usage += bot_token_count
# Update the total token usage file
bot.write_total_token_usage(bot.total_token_usage)
# Log the bot's response
bot.logger.info(f"Bot's response to {update.message.from_user.username} ({chat_id}): {bot_reply}")
# Append the bot's response to the chat history
chat_history.append({"role": "assistant", "content": bot_reply})
# Update the chat history in context with the new messages
context.chat_data['chat_history'] = chat_history
# View the output (i.e. for markdown etc formatting debugging)
logger.info(f"[Debug] Reply message before escaping: {bot_reply}")
# escaped_reply = markdown_to_html(bot_reply)
try:
escaped_reply = markdown_to_html(bot_reply)
except Exception as e:
bot.logger.error(f"markdown_to_html failed: {e}")
escaped_reply = html.escape(bot_reply) # Safe fallback
# escaped_reply = bot_reply
logger.info(f"[Debug] Reply message after escaping: {escaped_reply}")
# Log the bot's response
bot.log_message(
message_type='Bot',
message=bot_reply,
source='Weather API'
)
await context.bot.send_message(
chat_id=chat_id,
text=escaped_reply,
parse_mode=ParseMode.HTML
)
stop_typing_event.set()
context.user_data.pop('active_translation', None)
return # Exit the loop after handling the custom function
# ~~~~~~~~~~~~~~~~~
# DuckDuckGo Search
# ~~~~~~~~~~~~~~~~~
elif function_name == 'get_duckduckgo_search':
arguments = json.loads(function_call.get('arguments', '{}'))
search_query = arguments.get('search_query', '')
if search_query:
search_results = await get_duckduckgo_search(search_query, user_message)
if search_results:
system_message = f"[DuckDuckGo Search Results]: {search_results}\n\n[NOTE: format your response as Telegram-compatible HTML with links. Translate your response to the user's language if necessary (= if the user talked to you in Finnish, respond in Finnish).][Use SIMPLE, Telegram-compliant HTML: Use these HTML tags if needed: <b> for bold, <i> for italics, <u> for underline, <s> for strikethrough, <code> for inline code, <pre> for preformatted blocks, and <a href=...> for hyperlinks.. Do NOT use <pre>, <br>, <ul>, <li> or Markdown in your response!]"
else:
system_message = "No results found for your query."
else:
system_message = "Please provide a search query."
# Append the search results or the relevant message as a system message
chat_history.append({"role": "system", "content": system_message})
context.chat_data['chat_history'] = chat_history
# Debugging: Log the updated chat history
bot.logger.info(f"Updated chat history: {chat_history}")
# Make an API request using the updated chat history
response_json = await make_api_request(
bot,
chat_history,
bot.timeout,
include_functions=False
)
# Extract and handle the content from the API response
bot_reply_content = extract_chat_reply_or_raise(response_json)
bot.logger.info(f"Bot's response content: '{bot_reply_content}'")
bot_reply = bot_reply_content.strip() if bot_reply_content else ""
# Update usage metrics and logs
bot_token_count = bot.count_tokens(bot_reply)
bot.total_token_usage += bot_token_count
bot.write_total_token_usage(bot.total_token_usage)
bot.logger.info(f"Bot's response to {update.message.from_user.username} ({chat_id}): '{bot_reply}'")
# Ensure the bot has a substantive response to send
if bot_reply:
# Function to clean unsupported tags
# # // old method
# def sanitize_html(content):
# # Remove unsupported HTML tags
# for tag in ['<pre>', '</pre>', '<br>', '<br/>', '</br>', '<div>', '</div>', '<span>', '</span>', '<p>', '</p>']:
# content = content.replace(tag, '')
# # Optionally: Replace line breaks with "\n" to preserve formatting
# content = content.replace('<br>', '\n').replace('<br/>', '\n')
# return content
# Convert markdown to HTML
# escaped_reply = markdown_to_html(bot_reply)
try:
escaped_reply = markdown_to_html(bot_reply)
except Exception as e:
bot.logger.error(f"markdown_to_html failed: {e}")
escaped_reply = html.escape(bot_reply) # Safe fallback
# Sanitize the HTML to remove any unsupported tags
escaped_reply = sanitize_html(escaped_reply)
# Log the bot's response from DuckDuckGo Search
bot.log_message(
message_type='Bot',
message=bot_reply,
source='DuckDuckGo Search'
)
await context.bot.send_message(chat_id=chat_id, text=escaped_reply, parse_mode=ParseMode.HTML)