acheong08 / ChatGPT

Reverse engineered ChatGPT API
GNU General Public License v2.0
28.03k stars 4.48k forks source link

Use async in CLI use #232

Closed Harry-Jing closed 1 year ago

Harry-Jing commented 1 year ago

This can get quicker response, when receive first data, it can show it immediately

acheong08 commented 1 year ago

I'm unsure how to use async. Do you mind adding it to https://github.com/acheong08/ChatGPT/blob/main/src/revChatGPT/__main__.py under a --async argument?

alexandreteles commented 1 year ago

I think that using async by default is probably the best idea. I was about to propose some changes to the async version of the library as well that I think could be bundled up with this:

  1. Consider using uvloop. It shouldn't change anything in your codebase besides the import and the loop setup while providing a basically free speedup to the library (check how to implement it here)
  2. Consider using aiofiles for file operations
acheong08 commented 1 year ago

I think that using async by default is probably the best idea

As long as it's stable, that's fine. --stream can probably be used to handle the current method of using generators

Consider using uvloop. It shouldn't change anything in your codebase besides the import and the loop setup while providing a basically free speedup to the library (check how to implement it here) Consider using aiofiles for file operations

Those also look good. However, I would rather not add too many dependencies as this will only be used for CLI whereas the intent for this project is for use as a library.

Harry-Jing commented 1 year ago

seem like it is really easy to use uvloop you only need add

import asyncio
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

without chaning anything

Harry-Jing commented 1 year ago

But there is some problem when using uvloop "It not support Windows", whitch this is the information I got from the Internet.

acheong08 commented 1 year ago

The main problem here isn't whether or not it's easy to use them. It's more of that it bloats the dependencies which won't be used by most users (of the library as a dependency)

alexandreteles commented 1 year ago

seem like it is really easy to use uvloop

Yup. Aiofiles would require changing your file operarions, but it's really simple to do. Operating files asynchronously isn't necessarily faster but it'll free your thread to do something else in the meantime which can be useful for developers cycling through multiple accounts.

this will only be used for CLI whereas the intent for this project is for use as a library.

The changes I proposed are general changes to the async library, they wouldn't be restricted to CLI usage.

acheong08 commented 1 year ago

The changes I proposed are general changes to the async library, they wouldn't be restricted to CLI usage.

There read/write is not the concern of the Chatbot classes. It should be handled by the users. The __main__.py is a demo.

UVloop does seem useful but

"It not support Windows", whitch this is the information I got from the Internet.

If that is the case, I would rather not risk it

alexandreteles commented 1 year ago

If that is the case, I would rather not risk it

Fair point. Devs can always setup the async loop themselves and override the library behavior.

acheong08 commented 1 year ago

It uses streaming generator by default which gets data as soon as it arrives. Same speed (maybe faster) than async