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

Rex/dev #5

Merged
merged 3 commits into from
Mar 24, 2024
Merged
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
3 changes: 2 additions & 1 deletion README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Manage conversation history, including saving, loading, deleting, and listing co
|-----------------|------------------------|-----------------------------------------------|
| `-c` | `askchat -c <message>` | Continue the last conversation |
| `--regenerate` | `askchat -r` | Regenerate the last response of the conversation |
| `--load` | `askchat -l <file> [message]` | Load conversation history from a file and continue |
| `--load` | `askchat -l <file>` | Load conversation history from a file |
| `--print` | `askchat -p [<file>]` | Print the last or a specified conversation history |
| `--save` | `askchat -s <file>` | Save the current conversation history to a file |
| `--delete` | `askchat -d <file>` | Delete a specified conversation history file |
Expand All @@ -108,6 +108,7 @@ Default parameters for `askchat`, used for direct interaction with ChatGPT or co
| `--base-url` | `-b https://api.example.com` | Set the Base URL (excluding `/v1`) |
| `--api-base` | `--api-base https://api.example.com/v1` | Set the Base URL |
| `--api-key` | `-a sk-xxxxxxx` | Provide the OpenAI API key |
| `--option` | `-o top_p 1 temperature 0.5` | Set request parameters |
| `--use-env` | `-u prod` | Load environment variables from the specified config file, see [AskEnv](#askenv) |

Note: Some model APIs, like ChatGPT, use `/v4` as the base path of the API, so the `--api-base` parameter would be needed.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Options:
|---------------------|------------------|--------------------------------------------|
| `-c` | `askchat -c <message>` | 继续上一次的对话 |
| `--regenerate` | `askchat -r` | 重新生成上一次对话的最后回复 |
| `--load` | `askchat -l <file> [message]` | 从文件加载对话历史,继续对话 |
| `--load` | `askchat -l <file>` | 加载历史对话 |
| `--print` | `askchat -p [<file>]` | 打印上次或指定的对话历史 |
| `--save` | `askchat -s <file>` | 将当前对话历史保存到文件 |
| `--delete` | `askchat -d <file>` | 删除指定的对话历史文件 |
Expand All @@ -106,6 +106,7 @@ Options:
| `--base-url` | `-b https://api.example.com` | 设置 Base URL (不包含 `/v1`) |
| `--api-base` | `--api-base https://api.example.com/v1` | 设置 Base URL |
| `--api-key` | `-a sk-xxxxxxx` | 提供 OpenAI API 密钥 |
| `--option` | `-o top_p 1 temperature 0.5` | 设置请求参数 |
| `--use-env` | `-u prod` | 使用指定配置文件加载环境变量,详见 [AskEnv](#askenv) |

注:一些模型 API,比如智谱,使用 `/v4` 作为 API 的基础路径,这时得用 `--api-base` 参数。
Expand Down
8 changes: 5 additions & 3 deletions askchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '1.1.1'
__version__ = '1.1.2'

import asyncio
from pathlib import Path
Expand Down Expand Up @@ -32,12 +32,14 @@
]

# common functions
async def show_resp(chat):
async def show_resp(chat, **options):
msg = ''
async for char in chat.async_stream_responses(textonly=True):
async for char in chat.async_stream_responses(textonly=True, **options):

Check warning on line 37 in askchat/__init__.py

View check run for this annotation

Codecov / codecov/patch

askchat/__init__.py#L37

Added line #L37 was not covered by tests
print(char, end='', flush=True)
msg += char
await asyncio.sleep(0.01)
if not msg.endswith('\n'):
print() # add a newline if the message doesn't end with one

Check warning on line 42 in askchat/__init__.py

View check run for this annotation

Codecov / codecov/patch

askchat/__init__.py#L41-L42

Added lines #L41 - L42 were not covered by tests
return msg

def write_config(config_file, api_key, model, base_url, api_base):
Expand Down
46 changes: 21 additions & 25 deletions askchat/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@
load_dotenv(CONFIG_FILE, override=True)
chattool.load_envs()

# load environment variables from the configuration file
setup()

# callback functions for general options
def generate_config_callback(ctx, param, value):
"""Generate a configuration file by environment table."""
if not value:
return
if not value: return

Check warning on line 39 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L39

Added line #L39 was not covered by tests
api_key, model = os.getenv("OPENAI_API_KEY"), os.getenv("OPENAI_API_MODEL")
base_url, api_base = os.getenv("OPENAI_API_BASE_URL"), os.getenv("OPENAI_API_BASE")
# save the config file
Expand All @@ -51,21 +47,21 @@
ctx.exit()

def debug_log_callback(ctx, param, value):
if not value:
return
if not value: return
setup()

Check warning on line 51 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L50-L51

Added lines #L50 - L51 were not covered by tests
debug_log()
ctx.exit()

def valid_models_callback(ctx, param, value):
if not value:
return
if not value: return
setup()
click.echo('Valid models that contain "gpt" in their names:')
click.echo(pprint(Chat().get_valid_models()))
ctx.exit()

def all_valid_models_callback(ctx, param, value):
if not value:
return
if not value: return
setup()
click.echo('All valid models:')
click.echo(pprint(Chat().get_valid_models(gpt_only=False)))
ctx.exit()
Expand Down Expand Up @@ -106,6 +102,15 @@
click.echo(f" - {file.stem}")
ctx.exit()

def load_chat_callback(ctx, param, value):
if not value: return
try:
shutil.copyfile(CONFIG_PATH / f"{value}.json", LAST_CHAT_FILE)
click.echo(f"Loaded conversation from {CONFIG_PATH}/{value}.json")
except FileNotFoundError:
click.echo(f"The specified conversation {value} does not exist." +\

Check warning on line 111 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L106-L111

Added lines #L106 - L111 were not covered by tests
"Please check the chat list with `--list` option.")
ctx.exit()

Check warning on line 113 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L113

Added line #L113 was not covered by tests
# callback function for --use-env option
def use_env_callback(ctx, param, value):
if not value:
Expand Down Expand Up @@ -135,7 +140,7 @@
# Chat with history
@click.option('-c', is_flag=True, help='Continue the last conversation')
@click.option('-r', '--regenerate', is_flag=True, help='Regenerate the last conversation')
@click.option('-l', '--load', default=None, help='Load the conversation from a file')
@click.option('-l', '--load', default=None, type=ChatFileCompletionType(), callback=load_chat_callback, expose_value=False, help='Load a conversation from a file')
# Handling chat history
@click.option('-p', '--print', is_flag=True, help='Print the last conversation or a specific conversation')
@click.option('-s', '--save', callback=save_chat_callback, expose_value=False, help='Save the conversation to a file')
Expand All @@ -147,9 +152,11 @@
@click.option('--valid-models', is_flag=True, callback=valid_models_callback, expose_value=False, help='Print valid models that contain "gpt" in their names')
@click.option('--all-valid-models', is_flag=True, callback=all_valid_models_callback, expose_value=False, help='Print all valid models')
@click.option('-v', '--version', is_flag=True, callback=version_callback, expose_value=False, help='Print the version')
@click.option('-o', '--option', multiple=True, type=(str, str), help='Additional options for show_resp in the form of key=value')
def main( message, model, base_url, api_base, api_key, use_env
, c, regenerate, load, print):
, c, regenerate, print, option):
"""Interact with ChatGPT in terminal via chattool"""
setup()

Check warning on line 159 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L159

Added line #L159 was not covered by tests
message_text = ' '.join(message).strip()
if use_env and not message_text: return
# set values for the environment variables
Expand Down Expand Up @@ -183,17 +190,6 @@
click.echo("You should have at least two messages in the conversation")
return
chat.pop()
elif load: # load and continue the conversation
try:
shutil.copyfile(CONFIG_PATH / f"{load}.json", LAST_CHAT_FILE)
click.echo(f"Loaded conversation from {CONFIG_PATH}/{load}.json")
except FileNotFoundError:
click.echo(f"The specified conversation {load} does not exist." +\
"Please check the chat list with `--list` option.")
if not message_text: # if no message is provided, just quit
return
chat = Chat.load(LAST_CHAT_FILE)
chat.user(message_text)
elif c: # continue the last conversation
if not message_text:
click.echo("Please specify message!")
Expand All @@ -210,7 +206,7 @@
return
chat.user(message_text)
# Add chat response
chat.assistant(asyncio.run(show_resp(chat)))
chat.assistant(asyncio.run(show_resp(chat, **dict(option))))

Check warning on line 209 in askchat/cli.py

View check run for this annotation

Codecov / codecov/patch

askchat/cli.py#L209

Added line #L209 was not covered by tests
chat.save(LAST_CHAT_FILE, mode='w')

if __name__ == '__main__':
Expand Down
2 changes: 0 additions & 2 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ tox==3.14.0
coverage==4.5.4
Sphinx==1.8.5
twine==1.14.0

pytest==6.2.4

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

from setuptools import setup, find_packages

VERSION = '1.1.1'
VERSION = '1.1.2'

with open('README.md') as readme_file:
readme = readme_file.read()

requirements = ['chattool>=3.1.2', "python-dotenv>=0.17.0", 'Click>=8.0']
requirements = ['chattool>=3.1.3', "python-dotenv>=0.17.0", 'Click>=8.0']

test_requirements = ['pytest>=3']

Expand Down
Loading