Skip to content

Commit

Permalink
Modified: error handling and improved API usage query (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
signebedi committed Apr 6, 2023
1 parent 57a2b51 commit 62258bc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gptty/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def chat_async_wrapper(config_path:str, verbose:bool):
"""

# Print the text in cyan
click.echo(f"{CYAN}{title}\nWelcome to gptty (v.{__version__}), a ChatGPT wrapper in your TTY.\nType :help in the chat interface if you need help getting started.{' Verbose / Debug mode is on.' if verbose else ''}{RESET}\n")
click.echo(f"{CYAN}{title}\nWelcome to gptty (v.{__version__}), a ChatGPT wrapper in your TTY. Type :help in the chat interface if you need help getting started.{' Verbose / Debug mode is on. Query prompts will be preceded by your daily API usage in the format (query count, query tokens, response tokens).' if verbose else ''}{RESET}\n")

if not os.path.exists(config_path):
click.echo(f"{RED}FAILED to access app config file at {config_path}. Are you sure this is a valid config file? Run `gptty chat --help` for more information. You can get a sample config at <https://github.com/signebedi/gptty/blob/master/assets/gptty.ini.example>.")
Expand Down
23 changes: 14 additions & 9 deletions gptty/gptty.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,19 @@

def usage_stats_today():

r = openai.api_requestor.APIRequestor()
resp = r.request("GET", f'/usage?date={datetime.now().strftime("%Y-%m-%d")}')
resp_object = resp[0].data
try:
r = openai.api_requestor.APIRequestor()
resp = r.request("GET", f'/usage?date={datetime.now().strftime("%Y-%m-%d")}')
resp_object = resp[0].data
except:
return None
# requests_today = resp_object['data'][0]['n_requests'] # num requests
# query_tokens_today = resp_object['data'][0]['n_context_tokens_total'] # query tokens
# response_tokens_today = resp_object['data'][0]['n_generated_tokens_total'] # response tokens

requests_today = resp_object['data'][0]['n_requests'] # num requests
query_tokens_today = resp_object['data'][0]['n_context_tokens_total'] # query tokens
response_tokens_today = resp_object['data'][0]['n_generated_tokens_total'] # response tokens
requests_today = sum(item["n_requests"] for item in resp_object['data'])
query_tokens_today = sum(item["n_context_tokens_total"] for item in resp_object['data'])
response_tokens_today = sum(item["n_generated_tokens_total"] for item in resp_object['data'])

return requests_today, query_tokens_today, response_tokens_today

Expand Down Expand Up @@ -235,11 +241,10 @@ async def create_chat_room(configs=get_config_data(), log_responses:bool=True, c
# Get user input
try:

usage = usage_stats_today() if verbose else ""

with patch_stdout():
i = await session.prompt_async(ANSI(f"{CYAN}{usage}> "), style=Style.from_dict({'': 'ansicyan'}))
print(f"{ERASE_LINE}{MOVE_CURSOR_UP}{GREY}{usage}> {i}\n", end="")
i = await session.prompt_async(ANSI(f"{CYAN}{usage_stats_today() if verbose else ''}> "), style=Style.from_dict({'': 'ansicyan'}))
print(f"{ERASE_LINE}{MOVE_CURSOR_UP}{GREY}{usage_stats_today() if verbose else ''}> {i}\n", end="")

# i = await ainput(f"{CYAN}> ")
tag,question = get_tag_from_text(i)
Expand Down

0 comments on commit 62258bc

Please sign in to comment.