From 80409811dc90ea63af0f7a6b64ce4274ae4810bc Mon Sep 17 00:00:00 2001 From: hibobmaster <32976627+hibobmaster@users.noreply.github.com> Date: Fri, 5 Jan 2024 21:41:58 +0800 Subject: [PATCH] Make gptbot more compatible by using non-streaming method --- CHANGELOG.md | 3 +++ src/bot.py | 2 +- src/gptbot.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d9d4726..7670904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 1.5.3 +- Make gptbot more compatible by using non-streaming method + ## 1.5.2 - Expose more stable diffusion webui api parameters diff --git a/src/bot.py b/src/bot.py index 118d475..70d48cd 100644 --- a/src/bot.py +++ b/src/bot.py @@ -1245,7 +1245,7 @@ class Bot: async def chat(self, room_id, reply_to_event_id, prompt, sender_id, user_message): try: await self.client.room_typing(room_id, timeout=int(self.timeout) * 1000) - content = await self.chatbot.ask_async( + content = await self.chatbot.ask_async_v2( prompt=prompt, convo_id=sender_id, ) diff --git a/src/gptbot.py b/src/gptbot.py index 31e9c72..c9cfed4 100644 --- a/src/gptbot.py +++ b/src/gptbot.py @@ -250,6 +250,54 @@ class Chatbot: full_response: str = "".join([r async for r in response]) return full_response + async def ask_async_v2( + self, + prompt: str, + role: str = "user", + convo_id: str = "default", + model: str = None, + pass_history: bool = True, + **kwargs, + ) -> str: + # Make conversation if it doesn't exist + if convo_id not in self.conversation: + self.reset(convo_id=convo_id, system_prompt=self.system_prompt) + self.add_to_conversation(prompt, "user", convo_id=convo_id) + self.__truncate_conversation(convo_id=convo_id) + # Get response + response = await self.aclient.post( + url=self.api_url, + headers={"Authorization": f"Bearer {kwargs.get('api_key', self.api_key)}"}, + json={ + "model": model or self.engine, + "messages": self.conversation[convo_id] if pass_history else [prompt], + # kwargs + "temperature": kwargs.get("temperature", self.temperature), + "top_p": kwargs.get("top_p", self.top_p), + "presence_penalty": kwargs.get( + "presence_penalty", + self.presence_penalty, + ), + "frequency_penalty": kwargs.get( + "frequency_penalty", + self.frequency_penalty, + ), + "n": kwargs.get("n", self.reply_count), + "user": role, + "max_tokens": min( + self.get_max_tokens(convo_id=convo_id), + kwargs.get("max_tokens", self.max_tokens), + ), + }, + timeout=kwargs.get("timeout", self.timeout), + ) + resp = response.json() + full_response = resp["choices"][0]["message"]["content"] + self.add_to_conversation( + full_response, resp["choices"][0]["message"]["role"], convo_id=convo_id + ) + return full_response + def reset(self, convo_id: str = "default", system_prompt: str = None) -> None: """ Reset the conversation