diff --git a/bot/chat_gpt.py b/bot/chat_gpt.py index a7a5bf8..0ab8ed9 100644 --- a/bot/chat_gpt.py +++ b/bot/chat_gpt.py @@ -53,8 +53,9 @@ def generate_response( function = [] print('Unsupported model. Plugins not used') messages = [ - {"role": "system", "content": plugin_result}, + {"role": "system", "content": instruction}, + {"role": "system", "content": plugin_result}, *history ] try: @@ -62,6 +63,7 @@ def generate_response( model=model, messages=messages, functions=function, + function_call='auto', stream=True ) return response_stream @@ -70,7 +72,7 @@ def generate_response( if "rate limit" in text.lower(): retries += 1 if retries >= 3: - break + return text else: print(f"Rate limit on {model}. Retrying after 5 seconds") time.sleep(5) diff --git a/bot/database.py b/bot/database.py index cc0f5cb..b803441 100644 --- a/bot/database.py +++ b/bot/database.py @@ -58,5 +58,19 @@ def get_history(self, user_id): def delete_user_history(self, user_id): query = """DELETE FROM history WHERE user_id=?""" + self.conn.execute(query, (user_id,)) + self.conn.commit() + + def delete_last_2_user_history(self, user_id): + query = """ + DELETE FROM history + WHERE rowid IN ( + SELECT rowid + FROM history + WHERE user_id = ? + ORDER BY rowid DESC + LIMIT 2 + ) + """ self.conn.execute(query, (user_id,)) self.conn.commit() \ No newline at end of file diff --git a/bot_service.py b/bot_service.py index 05f393a..deda873 100644 --- a/bot_service.py +++ b/bot_service.py @@ -207,6 +207,7 @@ async def chat(self, call, waiting_id, bot, process_prompt = ''): async for response in response_stream: if self.cancel_flag: break + if isinstance(response, str): full_text += response if full_text == '': continue @@ -303,7 +304,7 @@ def generate_keyboard(self,key): builder.button(text=f"{self.lm.available_lang['languages'][lang_code]}({lang_code})") elif key == 'model': for model in self.gpt.models: - if model.startswith('gpt'): + #if model.startswith('gpt'): builder.button(text=model) elif key == 'size': for size in self.valid_sizes: diff --git a/language_files/en.yml b/language_files/en.yml index 73dd273..7b98bfb 100644 --- a/language_files/en.yml +++ b/language_files/en.yml @@ -120,7 +120,7 @@ Generate a response based on the context of an image, even if the image itself i EXTRA_PROMPT: " Don't make any assumptions, use the functions. -Don't say you don't have realtime information, make use of the functions to reply to the user with realtime information. +Never say you don't have realtime information, make use of the functions to reply to the user with realtime information. You have internet access using the functions. Use search functions to get realtime information. Don't make assumptions about what values to plug into functions. Ask for clarification if a user request is ambiguous. Only use the functions you have been provided with. diff --git a/main.py b/main.py index b6fcf7b..fae11d2 100644 --- a/main.py +++ b/main.py @@ -192,6 +192,7 @@ async def regenerate(callback: types.CallbackQuery): if callback.from_user.id not in service.last_call or callback.from_user.id not in service.last_msg_ids: return #delete previous message + service.db.delete_last_2_user_history(callback.from_user.id) await bot.delete_message(chat_id=callback.message.chat.id, message_id=service.last_msg_ids[callback.from_user.id]) # Regenerate response waiting_id = await create_waiting_message(chat_id=callback.message.chat.id)