1
+ """GPT generate clients."""
1
2
import time
2
3
from threading import Thread
3
4
8
9
from logger import logger
9
10
from models .configs import GlobalConfigs
10
11
11
-
12
- STOP_TYPING = False
12
+ stop_typing = False
13
13
cfg = GlobalConfigs ()
14
14
bot = telebot .TeleBot (cfg .telegram_token_bot .get_secret_value ())
15
15
16
- IGNORE_ERRORS = [
16
+ IGNORE_ERRORS = [ # Hardcode, need refactor
17
17
"A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message" # noqa E501
18
18
]
19
19
20
20
21
21
def set_is_typing (chat_id ):
22
+ """Set bot status typing."""
22
23
while True :
23
- if STOP_TYPING :
24
+ if stop_typing :
24
25
return
25
- logger .debug (f"STOP_TYPING is False. Seting status typing. Chat id: { chat_id } " )
26
+ logger .debug (f"stop_typing is False. Seting status typing. Chat id: { chat_id } " )
26
27
bot .send_chat_action (chat_id , action = "typing" )
27
28
time .sleep (4 )
28
29
29
30
30
31
def get_full_len_list (my_list : list ):
32
+ """Get full len list."""
31
33
cnt = 0
32
34
for i in my_list :
33
35
cnt += len (i )
34
36
return cnt
35
37
36
38
37
39
def get_str_from_list (my_list : list ):
38
- return '' .join (str (x ) for x in my_list )
40
+ """Get string from list."""
41
+ return "" .join (str (x ) for x in my_list )
39
42
40
43
41
44
class GPT4TextGenerate (BaseSettings ):
45
+ """GPT4TextGenerate class."""
42
46
43
47
def message_responser (self , prompt , chat_id , msg_id ):
48
+ """Set typing bot status and generate text."""
44
49
if chat_id != 0 :
45
50
Thread (target = set_is_typing , args = (chat_id ,)).start ()
46
- # try:
47
51
result_generate = self .gpt4_generate (
48
- prompt = prompt ,
49
- chat_id = chat_id ,
50
- msg_id = msg_id
52
+ prompt = prompt , chat_id = chat_id , msg_id = msg_id
51
53
)
52
54
if result_generate :
53
- STOP_TYPING = True
55
+ stop_typing = True
54
56
logger .debug (f"Result sent successful for chat id: { chat_id } " )
55
57
return True
56
- # except Exception as e:
57
- # logger.error(f"Fatal error: {e}")
58
- STOP_TYPING = True # noqa F841
58
+ stop_typing = True # noqa F841
59
59
self .send_message ("Smth error" , chat_id )
60
60
raise RuntimeError
61
61
62
- def gpt4_generate (self , chat_id , prompt , msg_id ):
62
+ def gpt4_generate (self , chat_id , prompt , msg_id ): # noqa: CFQ004,CFQ001
63
+ """Generate text with prompt and send."""
63
64
prompt = cfg .pre_prompt + prompt
64
65
response = g4f .ChatCompletion .create (
65
- # model='gpt-3.5-turbo',
66
- model = 'gpt-4' ,
66
+ model = "gpt-4" ,
67
67
messages = [{"role" : "user" , "content" : prompt }],
68
- # stream=True,
69
- provider = g4f .Provider .ChatgptAi
68
+ provider = g4f .Provider .ChatgptAi ,
70
69
)
71
70
full_text = []
72
71
max_length = 150
@@ -84,7 +83,7 @@ def gpt4_generate(self, chat_id, prompt, msg_id):
84
83
return True
85
84
msg_id = self .send_message (
86
85
text = get_str_from_list (full_text [last_len_list :]),
87
- chat_id = chat_id
86
+ chat_id = chat_id ,
88
87
)
89
88
len_list = len (full_text )
90
89
cnt_divisions += 1
@@ -94,13 +93,13 @@ def gpt4_generate(self, chat_id, prompt, msg_id):
94
93
bot .edit_message_text (
95
94
chat_id = chat_id ,
96
95
text = get_str_from_list (full_text [len_list :]),
97
- message_id = msg_id
96
+ message_id = msg_id ,
98
97
)
99
98
last_len_list = len (full_text )
100
99
if chat_id == 0 :
101
100
bot .edit_message_text (
102
101
inline_message_id = msg_id ,
103
- text = get_str_from_list (full_text [len_list :])
102
+ text = get_str_from_list (full_text [len_list :]),
104
103
)
105
104
except telebot .apihelper .ApiTelegramException as e :
106
105
if str (e ) in IGNORE_ERRORS :
@@ -113,27 +112,23 @@ def gpt4_generate(self, chat_id, prompt, msg_id):
113
112
bot .edit_message_text (
114
113
inline_message_id = msg_id ,
115
114
text = get_str_from_list (full_text [len_list :]),
116
- # parse_mode='Markdown'
117
- # Added markdown validator
118
115
)
119
116
return True
120
117
bot .edit_message_text (
121
118
chat_id = chat_id ,
122
119
text = get_str_from_list (full_text [len_list :]),
123
120
message_id = msg_id ,
124
- # parse_mode='Markdown'
125
- # Added markdown validator
126
121
)
127
122
except telebot .apihelper .ApiTelegramException as e :
128
123
if str (e ) in IGNORE_ERRORS :
129
124
return True
130
- else :
131
- raise RuntimeError (e )
125
+ raise RuntimeError (e )
132
126
return True
133
127
134
128
def send_message (self , text , chat_id ):
129
+ """Send message with chunk per 4096 sumbol."""
135
130
max_length = 4096
136
- chunks = [text [i :i + max_length ] for i in range (0 , len (text ), max_length )]
131
+ chunks = [text [i : i + max_length ] for i in range (0 , len (text ), max_length )]
137
132
msg_id = 0
138
133
try :
139
134
for chunk in chunks :
@@ -144,5 +139,4 @@ def send_message(self, text, chat_id):
144
139
except telebot .apihelper .ApiTelegramException as e :
145
140
if str (e ) in IGNORE_ERRORS :
146
141
return msg_id
147
- else :
148
- raise RuntimeError (e )
142
+ raise RuntimeError (e )
0 commit comments