Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Added try except when a cached chat exist in local copy, but does not…
Browse files Browse the repository at this point in the history
… in server chat history
  • Loading branch information
scmanjarrez committed Jun 11, 2023
1 parent 0537365 commit 0ac5641
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,14 @@ there are some commands that are hidden:
> /unlock <passwd> - Unlock bot functionalities with a password
> /get <config/cookies> - Retrieve config.json or cookies.json, respectively
> /update <config/cookies> - Update config.json or cookies.json, respectively
> /history_update - Force chat history update
> /reset - Reload bot files
> /cancel - Cancel current update action
> ```
> Be careful with /history_update. History will be recreated, removing ownership
> and moving the ownsership to admin1 (if multiple admins are present, to the first one)
In order to use inline queries, you need to enable them in [@BotFather](https://t.me/BotFather).
For ease of use, use the placeholder
```
Expand Down
65 changes: 40 additions & 25 deletions src/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,46 @@ async def run(self) -> None:
)
ut.CONV["all"][self.cid][cur_conv][1] = self.text
start = time.time()
async for final, resp in ut.CONV["all"][self.cid][cur_conv][
0
].ask_stream(
prompt=self.text,
conversation_style=getattr(ConversationStyle, db.style(self.cid)),
):
current = time.time()
if current - start > 0.4 and not final:
text = SRCH_RESP.sub("", resp)
resp = GEN_RESP.sub("\\1", text)
resp = REF_INLINE_ST.sub("", resp)
resp = REF_ST.sub("", resp)
resp = resp.strip()
if resp:
text = (
f"<b>You</b>: {html.escape(self.text)}\n\n"
f"<b>Bing</b>: {html.escape(resp)}"
)
if not self.inline:
await ut.edit(self.edit, text)
else:
await ut.edit_inline(self.update, self.context, text)
start = current
if final:
self._response = resp
try:
async for final, resp in ut.CONV["all"][self.cid][cur_conv][
0
].ask_stream(
prompt=self.text,
conversation_style=getattr(
ConversationStyle, db.style(self.cid)
),
):
current = time.time()
if current - start > 0.4 and not final:
text = SRCH_RESP.sub("", resp)
resp = GEN_RESP.sub("\\1", text)
resp = REF_INLINE_ST.sub("", resp)
resp = REF_ST.sub("", resp)
resp = resp.strip()
if resp:
text = (
f"<b>You</b>: {html.escape(self.text)}\n\n"
f"<b>Bing</b>: {html.escape(resp)}"
)
if not self.inline:
await ut.edit(self.edit, text)
else:
await ut.edit_inline(
self.update, self.context, text
)
start = current
if final:
self._response = resp
except KeyError:
# Conversation was removed from Bing but we still have a reference
await ut.CONV["all"][self.cid][cur_conv][0].close()
del ut.CONV["all"][self.cid][cur_conv]
ut.CONV["current"][self.cid] = ""
await self.edit.delete()
await ut.is_active_conversation(self.update, new=True)
query = BingAI(self.update, self.context)
await query.run()
return
if not self.inline:
ut.RUN[self.cid][cur_conv].remove(turn)
ut.delete_job(self.context, job_name)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ async def retrieve_history() -> None:
data = json.loads(await resp.text())
if data["chats"]:
client_id = data["clientId"]
CONV["all"].clear()
CONV["current"].clear()
RUN.clear()
for chat in data["chats"]:
name = chat["chatName"]
conversation_id = chat["conversationId"]
Expand Down

0 comments on commit 0ac5641

Please sign in to comment.