@@ -120,32 +120,41 @@ options:
120120Use Async for the best experience, for example:
121121
122122``` python
123- import asyncio, json
124-
123+ import asyncio
124+ import json
125125from pathlib import Path
126- from re_edge_gpt import Chatbot, ConversationStyle
127126
128- cookies = json.loads(open (str (Path(str (Path.cwd()) + " /bing_cookies.json" )), encoding = " utf-8" ).read())
127+ from re_edge_gpt import Chatbot
128+ from re_edge_gpt import ConversationStyle
129+
130+
131+ # If you are using jupyter pls install this package
132+ # from nest_asyncio import apply
133+
134+
135+ async def test_ask () -> None :
136+ cookies = json.loads(open (
137+ str (Path(str (Path.cwd()) + " /bing_cookies.json" )), encoding = " utf-8" ).read())
138+ bot = await Chatbot.create(cookies = cookies)
139+ response = await bot.ask(
140+ prompt = " find me some information about the new ai released by meta." ,
141+ conversation_style = ConversationStyle.balanced,
142+ simplify_response = True ,
143+ )
144+ await bot.close()
145+ print (json.dumps(response, indent = 2 ))
146+ assert response
129147
130- async def main ():
131- bot = await Chatbot.create(cookies = cookies)
132- response = await bot.ask(prompt = " Hello world" , conversation_style = ConversationStyle.creative, simplify_response = True )
133- print (json.dumps(response, indent = 2 )) # Returns
134- """
135- {
136- "text": str,
137- "author": str,
138- "sources": list[dict],
139- "sources_text": str,
140- "suggestions": list[str],
141- "messages_left": int
142- }
143- """
144- await bot.close()
145148
146149if __name__ == " __main__" :
147- loop = asyncio.get_event_loop()
148- loop.run_until_complete(main())
150+ # If you are using jupyter pls use nest_asyncio apply()
151+ # apply()
152+ try :
153+ loop = asyncio.get_running_loop()
154+ except RuntimeError :
155+ loop = asyncio.get_event_loop()
156+ loop.run_until_complete(test_ask())
157+
149158```
150159
151160</details >
@@ -174,7 +183,6 @@ if __name__ == "__main__":
174183> * copy the value from the _ U
175184
176185``` python
177- import asyncio
178186import os
179187import shutil
180188from pathlib import Path
@@ -203,23 +211,13 @@ def test_generate_image_sync():
203211 image_list = sync_gen.get_images(" tree" )
204212 print (image_list)
205213
206-
207- # Generate image list async
208- async def test_generate_image_async ():
209- image_list = await async_gen.get_images(" tree" )
210- print (image_list)
211-
212-
213214if __name__ == " __main__" :
214215 # Make dir to save image
215216 Path(" test_output" ).mkdir(exist_ok = True )
216217 # Save image
217218 test_save_images_sync()
218219 # Generate image sync
219220 test_generate_image_sync()
220- # Generate image async
221- loop = asyncio.get_event_loop()
222- loop.run_until_complete(test_generate_image_async())
223221 # Remove dir
224222 shutil.rmtree(test_output_dir)
225223```
@@ -230,6 +228,9 @@ if __name__ == "__main__":
230228
231229<details open >
232230
231+ > * Q: RuntimeError: This event loop is already running
232+ > * A: If you are using Jupyter, pls use nest_asyncio.apply()
233+
233234> * Q: Exception: UnauthorizedRequest: Cannot retrieve user status.
234235> * A: Renew your cookie file.
235236
0 commit comments