Skip to content

Commit

Permalink
Delete from db also when Regenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
noes14155 committed Sep 22, 2023
1 parent 6a1f424 commit a74df4d
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
6 changes: 4 additions & 2 deletions bot/chat_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ 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:
response_stream = openai.ChatCompletion.create(
model=model,
messages=messages,
functions=function,
function_call='auto',
stream=True
)
return response_stream
Expand All @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions bot/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
3 changes: 2 additions & 1 deletion bot_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion language_files/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a74df4d

Please sign in to comment.