Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
feat: async response for Claude and Bard; concurrent update
Browse files Browse the repository at this point in the history
  • Loading branch information
ciuzaak committed Jun 2, 2023
1 parent 2774ab4 commit 8650609
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
12 changes: 9 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ async def recv_msg(update: Update, context: ContextTypes.DEFAULT_TYPE):

if mode == "Claude":
prev_response = ""
for response in session.send_message_stream(input_text):
async for response in session.send_message_stream(input_text):
response = response[:4096]
if abs(len(response) - len(prev_response)) < session.cutoff:
continue
Expand All @@ -161,7 +161,7 @@ async def recv_msg(update: Update, context: ContextTypes.DEFAULT_TYPE):
await message.edit_text(f"❌ Error orrurred: {e}. /reset")

else: # Bard
response = session.send_message(input_text)
response = await session.send_message(input_text)
# get source links
sources = ""
if response["factualityQueries"]:
Expand Down Expand Up @@ -361,7 +361,13 @@ async def post_init(application: Application):

def run_bot():
print(f"[+] bot started, calling loop!")
application = ApplicationBuilder().token(bot_token).post_init(post_init).build()
application = (
ApplicationBuilder()
.token(bot_token)
.post_init(post_init)
.concurrent_updates(True)
.build()
)

user_filter = filters.Chat(chat_id=user_ids)
msg_filter = filters.TEXT
Expand Down
11 changes: 7 additions & 4 deletions utils/bard_utils.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from Bard import Chatbot
from Bard import AsyncChatbot

from config import bard_api


class Bard:
def __init__(self):
self.client = Chatbot(bard_api)
self.client = AsyncChatbot(bard_api)
self.client.SNlM0e = None
self.prev_conversation_id = ""
self.prev_response_id = ""
self.prev_choice_id = ""
Expand All @@ -23,9 +24,11 @@ def revert(self):
self.client.response_id = self.prev_response_id
self.client.choice_id = self.prev_choice_id

def send_message(self, message):
async def send_message(self, message):
if self.client.SNlM0e is None:
self.client.SNlM0e = await self.client._AsyncChatbot__get_snlm0e()
self.prev_conversation_id = self.client.conversation_id
self.prev_response_id = self.client.response_id
self.prev_choice_id = self.client.choice_id
response = self.client.ask(message)
response = await self.client.ask(message)
return response
6 changes: 3 additions & 3 deletions utils/claude_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def change_cutoff(self, cutoff):
return True
return False

def send_message_stream(self, message):
async def send_message_stream(self, message):
self.prompt = f"{self.prompt}{HUMAN_PROMPT} {message}{AI_PROMPT}"
response = self.client.completion_stream(
response = await self.client.acompletion_stream(
prompt=self.prompt,
stop_sequences=[HUMAN_PROMPT],
max_tokens_to_sample=9216,
Expand All @@ -67,6 +67,6 @@ def send_message_stream(self, message):
stream=True,
disable_checks=True,
)
for data in response:
async for data in response:
yield data["completion"]
self.prompt = f"{self.prompt}{data['completion']}"

0 comments on commit 8650609

Please sign in to comment.