diff --git a/README-en.md b/README-en.md index 415dc8a..3934aa1 100644 --- a/README-en.md +++ b/README-en.md @@ -1,73 +1,107 @@ # askchat + [![PyPI version](https://img.shields.io/pypi/v/askchat.svg)](https://pypi.python.org/pypi/askchat) -[![Tests](https://github.com/rexwzh/askchat/actions/workflows/test.yml/badge.svg)](https://github.com/rexwzh/askchat/actions/workflows/test.yml/) -[![Documentation Status](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://rexwzh.github.io/askchat/) -[![Coverage](https://codecov.io/gh/rexwzh/askchat/branch/main/graph/badge.svg)](https://codecov.io/gh/rexwzh/askchat) +[![Tests](https://github.com/cubenlp/askchat/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/askchat/actions/workflows/test.yml/) +[![Documentation Status](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://cubenlp.github.io/askchat/) +[![Coverage](https://codecov.io/gh/cubenlp/askchat/branch/main/graph/badge.svg)](https://codecov.io/gh/cubenlp/askchat) -[English](README-en.md) | [简体中文](README.md) +[English](README-en.md) | [Simplified Chinese](README.md) -Interact with ChatGPT in terminal via chattool. +Invoke ChatGPT from the command line. ## Installation ```bash -pip install askchat +pip install askchat --upgrade ``` -## Usage +## How to Use + +Run simply with the default environment variables: -A simple way: ```bash ask hello ``` -Ask with more options via `askchat`: +Specify other options via `askchat`: + ```bash -# ask with a specific model -askchat hello -m "baichuan2" --base_url "localhost:8000" +# Ask using a specific model +askchat hello -m "baichuan2" --base-url "localhost:8000" ``` -Generate config file for default options: +Generate a default configuration file via environment variables, edit the configuration in `~/.askchat/.env`: + ```bash askchat --generate-config ``` -You might edit the config at `~/.askchat/.env`. +## Chat Options -Other options: ```bash -# current version -askchat -v -# print the debug log +# Show the current version +askchat -v + +# Print debug logs askchat --debug -# get valid models that contains "gpt" + +# Get valid models that include "gpt" askchat --valid-models -# get all valid models + +# Get all valid models askchat --all-valid-models ``` -## Advance usage -You can manage your chats with `askchat`: +## Managing Conversation History + +Manage conversations using `askchat`: ```bash askchat hello -# continue the last chat: -c -askchat -c tell me a joke please -# regenerate the last conversation: -r +# Continue the last conversation: -c +askchat -c please tell me a joke +# Regenerate the last conversation: -r askchat -r -# regenerate the last conversation with new message: -r +# Modify and regenerate the last conversation: -r askchat -r give me some jokes please -# save the chat: -s/--save +# Save the conversation: -s/--save askchat -s joke -# load the chat: -l/--load +# Load a conversation: -l/--load askchat -l joke -# delete the chat: -d/--delete +# Delete a conversation: -d/--delete askchat -d joke -# list all saved chats: --list +# List all saved conversations: --list askchat --list -# print the last chat: -p/--print +# Print the last conversation: -p/--print askchat -p -# print the given chat: -p/--print +# Print a specific conversation: -p/--print askchat -p joke ``` + +## Managing Environment Configuration + +Manage different environment configurations with `chatenv`: + +```bash +# Create a new environment +chatenv create [--api-key ""] [--base-url ""] [--api-base ""] [--model ""] + +# Activate a specified environment +chatenv use + +# Update environment configuration +chatenv config [] [--api-key ""] [--base-url ""] [--api-base ""] [--model ""] + +# List all environments +chatenv list + +# Show variables of a specified or default environment +chatenv show [] + +# Save the current environment as a new environment file +chatenv save + +# Delete a specified or the default environment configuration +chatenv delete [] [--default] +``` \ No newline at end of file diff --git a/README.md b/README.md index 5d4e5f1..b454970 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # askchat -[![PyPI版本](https://img.shields.io/pypi/v/askchat.svg)](https://pypi.python.org/pypi/askchat) -[![测试](https://github.com/rexwzh/askchat/actions/workflows/test.yml/badge.svg)](https://github.com/rexwzh/askchat/actions/workflows/test.yml/) -[![文档状态](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://rexwzh.github.io/askchat/) -[![覆盖率](https://codecov.io/gh/rexwzh/askchat/branch/main/graph/badge.svg)](https://codecov.io/gh/rexwzh/askchat) +[![PyPI version](https://img.shields.io/pypi/v/askchat.svg)](https://pypi.python.org/pypi/askchat) +[![Tests](https://github.com/cubenlp/askchat/actions/workflows/test.yml/badge.svg)](https://github.com/cubenlp/askchat/actions/workflows/test.yml/) +[![Documentation Status](https://img.shields.io/badge/docs-github_pages-blue.svg)](https://cubenlp.github.io/askchat/) +[![Coverage](https://codecov.io/gh/cubenlp/askchat/branch/main/graph/badge.svg)](https://codecov.io/gh/cubenlp/askchat) [English](README-en.md) | [简体中文](README.md) @@ -17,40 +17,42 @@ pip install askchat --upgrade ## 使用方法 -简单运行方式,使用默认的环境变量: +使用默认的环境变量进行简单运行: + ```bash ask hello ``` 通过 `askchat` 指定其他选项: + ```bash # 使用特定模型提问 -askchat hello -m "baichuan2" --base_url "localhost:8000" +askchat hello -m "baichuan2" --base-url "localhost:8000" ``` -通过环境变量生成默认的配置文件,可以在 `~/.askchat/.env` 中编辑配置 -```bash -askchat --generate-config -``` +通过环境变量生成默认的配置文件,在 `~/.askchat/.env` 中编辑配置: -修改配置参数,比如 ```bash -askchat --config --api-key "your_api_key" +askchat --generate-config ``` +## 聊天选项 -其他选项: ```bash -# 当前版本 -askchat -v +# 显示当前版本 +askchat -v + # 打印调试日志 askchat --debug -# 获取包含"gpt"的有效模型 + +# 获取包含 "gpt" 的有效模型 askchat --valid-models + # 获取所有有效模型 askchat --all-valid-models ``` + ## 管理对话记录 使用 `askchat` 管理对话: @@ -75,4 +77,32 @@ askchat --list askchat -p # 打印指定的对话:-p/--print askchat -p joke -``` \ No newline at end of file +``` + +## 管理环境配置 + +通过 `chatenv` 管理不同的环境配置: + +```bash +# 创建新环境 +chatenv create [--api-key ""] [--base-url ""] [--api-base ""] [--model ""] + +# 激活指定环境 +chatenv use + +# 更新环境配置 +chatenv config [] [--api-key ""] [--base-url ""] [--api-base ""] [--model ""] + +# 列出所有环境 +chatenv list + +# 显示指定环境或默认环境的变量 +chatenv show [] + +# 保存当前环境为一个新的环境文件 +chatenv save + +# 删除指定的环境或默认环境配置 +chatenv delete [] [--default] +``` + diff --git a/askchat/__init__.py b/askchat/__init__.py index 5c172e9..af71df3 100644 --- a/askchat/__init__.py +++ b/askchat/__init__.py @@ -2,7 +2,7 @@ __author__ = """Rex Wang""" __email__ = '1073853456@qq.com' -__version__ = '0.3.4' +__version__ = '0.4.0' import asyncio @@ -30,4 +30,4 @@ def write_var(f, var, value, desc): write_var(f, "OPENAI_API_KEY", api_key, "Your API key") write_var(f, "OPENAI_API_MODEL", model, "The model name\n" +\ - "# You can use `askchat --all-valid-models` to see the valid models") \ No newline at end of file + "# You can use `askchat --all-valid-models` to see supported models") \ No newline at end of file diff --git a/askchat/chatenvs.py b/askchat/chatenv.py similarity index 75% rename from askchat/chatenvs.py rename to askchat/chatenv.py index 40aaf54..e5af1e3 100644 --- a/askchat/chatenvs.py +++ b/askchat/chatenv.py @@ -10,7 +10,7 @@ @click.group() def cli(): - """askenvs CLI for managing askchat environments.""" + """chatenv CLI for managing askchat environments.""" if not ENV_PATH.exists(): ENV_PATH.mkdir(parents=True) @@ -36,7 +36,7 @@ def create(name, api_key, base_url, api_base, model): config_path = ENV_PATH / f'{name}.env' if config_path.exists(): click.echo(f"Environment '{name}' already exists." +\ - "Use 'askenvs delete' to delete it first.") + "Use 'chatenv delete' to delete it first.") else: write_config(config_path, api_key, model, base_url, api_base) click.echo(f"Environment '{name}' created.") @@ -65,14 +65,18 @@ def delete(name, default): click.echo(f"Environment '{name}' not found.") @cli.command() -def current(): - """Print current environment variables.""" - if MAIN_ENV_PATH.exists(): - with MAIN_ENV_PATH.open() as f: +@click.argument('name', required=False) +def show(name): + """Print environment variables. Show default if no name is provided.""" + config_path = ENV_PATH / f'{name}.env' if name else MAIN_ENV_PATH + if config_path.exists(): + with config_path.open() as f: click.echo(f.read()) else: - click.echo("No active environment." +\ - "You can use `askchat --generate-config` to create one.") + if name: + click.echo(f"Environment '{name}' not found.") + else: + click.echo("No active environment. You can use `chatenv create` to create one.") @cli.command() @click.argument('name') @@ -88,7 +92,7 @@ def save(name): @cli.command() @click.argument('name') -def activate(name): +def use(name): """Activate an environment by replacing the .env file.""" config_path = ENV_PATH / f'{name}.env' if config_path.exists(): @@ -103,26 +107,25 @@ def activate(name): @click.option('-b', '--base-url', help='Base URL of the API (without suffix `/v1`)') @click.option('--api-base', help='Base URL of the API (with suffix `/v1`)') @click.option('-m', '--model', help='Model name') -def env(api_key, base_url, api_base, model): +@click.argument('name', required=False) +def config(name, api_key, base_url, api_base, model): """Update default .env values.""" - updated = False - MAIN_ENV_PATH.touch(exist_ok=True) + if not any([api_key, base_url, api_base, model]): + click.echo("No updates made. Provide at least one option to update.") + return + config_path = ENV_PATH / f'{name}.env' if name else MAIN_ENV_PATH + if not config_path.exists(): + click.echo(f"Environment '{config_path}' not found.") + return if api_key: - set_key(MAIN_ENV_PATH, "API_KEY", api_key) - updated = True + set_key(config_path, "OPENAI_API_KEY", api_key) if base_url: - set_key(MAIN_ENV_PATH, "BASE_URL", base_url) - updated = True + set_key(config_path, "OPENAI_API_BASE_URL", base_url) if api_base: - set_key(MAIN_ENV_PATH, "API_BASE", api_base) - updated = True + set_key(config_path, "OPENAI_API_BASE", api_base) if model: - set_key(MAIN_ENV_PATH, "MODEL", model) - updated = True - if updated: - click.echo("Default environment updated.") - else: - click.echo("No updates made. Provide at least one option to update.") + set_key(config_path, "OPENAI_API_MODEL", model) + click.echo(f"Environment {config_path} updated.") if __name__ == '__main__': cli() diff --git a/setup.cfg b/setup.cfg index 2e21ef8..90feb51 100644 --- a/setup.cfg +++ b/setup.cfg @@ -10,5 +10,5 @@ addopts = --ignore=setup.py console_scripts = ask = askchat.ask:main askchat = askchat.askchat:main - chatenvs = askchat.chatenvs:cli + chatenv = askchat.chatenv:cli diff --git a/setup.py b/setup.py index 615099a..4a1f5de 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup, find_packages -VERSION = '0.3.4' +VERSION = '0.4.0' with open('README.md') as readme_file: readme = readme_file.read()