Skip to content

Commit

Permalink
add askchat
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Oct 21, 2023
1 parent ba31bd9 commit 5d4b96a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
45 changes: 37 additions & 8 deletions askchat/askchat.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
"""Main module."""
from chattool import Chat
from chattool import Chat, debug_log
import asyncio
from argparse import ArgumentParser
import askchat
VERSION = askchat.__version__

# print the response in a typewriter way
async def show_resp(chat):
async for resp in chat.async_stream_responses():
for char in resp.delta_content:
print(char, end='', flush=True)
await asyncio.sleep(0.015)

def ask():
"""Interact with ChatGPT in terminal via chattool"""
Expand All @@ -10,14 +19,34 @@ def ask():
parser.add_argument('message', type=str, help='User message')
args = parser.parse_args()
msg = args.message

# print the response in a typewriter way
async def show_resp(chat):
async for resp in chat.async_stream_responses():
for char in resp.delta_content:
print(char, end='', flush=True)
await asyncio.sleep(0.015)

# call
chat = Chat(msg)
asyncio.run(show_resp(chat))

def main():
"""Interact with ChatGPT in terminal via chattool"""
# parse arguments
parser = ArgumentParser()
## use nargs='?' to make message optional
parser.add_argument('message', help='User message', default='', nargs='?')
parser.add_argument('-v', '--version', action='version', version=VERSION)
parser.add_argument('--debug', action='store_true', help='Print debug log')
parser.add_argument('--valid-models', action='store_true', help='Print valid models')
parser.add_argument('-m', '--model', default=None, help='Model name')
args = parser.parse_args()
# show debug log
if args.debug:
debug_log()
return
# show valid models
if args.valid_models:
print('Valid models: ')
print(Chat().get_valid_models())
return
# get message and model
model, msg = args.model, args.message
assert len(msg.strip()), 'Please specify message'
# call
chat = Chat(msg, model=model)
asyncio.run(show_resp(chat))
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ addopts = --ignore=setup.py
[options.entry_points]
console_scripts =
ask = askchat:ask
askchat = askchat.askchat:main

0 comments on commit 5d4b96a

Please sign in to comment.