Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make request async and show that request is being processed #1

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The clients depend on the following Python packages:
To install the dependencies, run the following command:

```bash
pip install openai tkhtmlview
pip install openai tkhtmlview tqdm
```

## Usage
Expand Down
29 changes: 25 additions & 4 deletions cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env python3
import os
import readline

import asyncio
import openai
from tqdm import tqdm

MODEL = "gpt-4"

Expand All @@ -22,7 +22,29 @@
def user_input_dict(prompt):
return {"role": "user", "content": prompt}


async def get_response_async():
loop = asyncio.get_event_loop()
response_future = loop.run_in_executor(None, lambda: openai.ChatCompletion.create(
model=MODEL, messages=messages, temperature=0.1))

# Display dotting
pbar = tqdm(total=1, bar_format='{desc}', position=0, leave=True)
x = 0
while not response_future.done():
pbar.set_description_str("." * x, refresh=True)
x += 1
await asyncio.sleep(0.5)

pbar.close()

return await response_future


price = 0

loop = asyncio.get_event_loop()

while True:
user_input = input("You: ")
if not user_input:
Expand All @@ -32,8 +54,7 @@ def user_input_dict(prompt):

messages.append(user_input_dict(user_input))

response = openai.ChatCompletion.create(
model=MODEL, messages=messages, temperature=0.1)
response = loop.run_until_complete(get_response_async())

message = response.choices[0]["message"]
messages.append(message)
Expand Down
Loading