Closed JayGarland closed 8 months ago
Use await or multiple bot instances to process this.
can you give me some ideas how to create multiple bot instances? I use self.bot = await Chatbot.create method to create one, but not sure if it can be created multiple times when call the function which creates the bot.
You can create multiple bot instances if you want. Just call the Chatbot.create function. Additionally, you can reset the bot when an exception is raised using Chatbot.create
import asyncio
import json
from pathlib import Path
from re_edge_gpt import Chatbot
from re_edge_gpt import ConversationStyle
# If you are using jupyter pls install this package
# from nest_asyncio import apply
async def test_ask() -> None:
bot = None
another_bot = None
try:
mode = "Bing"
if mode == "Bing":
cookies: list[dict] = json.loads(open(
str(Path(str(Path.cwd()) + "/bing_cookies.json")), encoding="utf-8").read())
else:
cookies: list[dict] = json.loads(open(
str(Path(str(Path.cwd()) + "/copilot_cookies.json")), encoding="utf-8").read())
bot = await Chatbot.create(cookies=cookies, mode=mode)
another_bot = await Chatbot.create(cookies=cookies, mode=mode)
response = await bot.ask(
prompt="HELLO",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
search_result=True
)
print(json.dumps(response, indent=2, ensure_ascii=False))
response = await another_bot.ask(
prompt="HELLO",
conversation_style=ConversationStyle.balanced,
simplify_response=True,
search_result=True)
# If you are using non ascii char you need set ensure_ascii=False
print(json.dumps(response, indent=2, ensure_ascii=False))
# Raw response
# print(response)
assert response
except Exception as error:
# You can reset the bot here and simply send the user a bot error message instead of raising an error.
raise error
finally:
if bot is not None:
await bot.close()
if another_bot is not None:
await another_bot.close()
if __name__ == "__main__":
# If you are using jupyter pls use nest_asyncio apply()
# apply()
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = asyncio.get_event_loop()
loop.run_until_complete(test_ask())
thanks your review sir! I found out why that error happens cuz I closed an aiosession connect everytime I use the bot. lol. I have one more question, must I close the bot after everychat? would it cause any problem if I don't close it? or didn't close the aiosession connect when the resptext interrupted in generating process?
You don’t need to close the bot every time. Only call the close function when the program exits or when you want to close the bot, or when an exception occurs. However, you need to monitor message_left. If message_left equals 0, you should reset the bot.
would happen this error if I send message while the bot is in the process generating replies of the previous question.