Skip to content

Commit

Permalink
make new release: add chatenv, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Mar 9, 2024
1 parent 341081a commit dc13569
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 76 deletions.
96 changes: 65 additions & 31 deletions README-en.md
Original file line number Diff line number Diff line change
@@ -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 <name> [--api-key "<api_key>"] [--base-url "<base_url>"] [--api-base "<api_base>"] [--model "<model_name>"]

# Activate a specified environment
chatenv use <name>

# Update environment configuration
chatenv config [<name>] [--api-key "<new_api_key>"] [--base-url "<new_base_url>"] [--api-base "<new_api_base>"] [--model "<new_model_name>"]

# List all environments
chatenv list

# Show variables of a specified or default environment
chatenv show [<name>]

# Save the current environment as a new environment file
chatenv save <name>

# Delete a specified or the default environment configuration
chatenv delete [<name>] [--default]
```
64 changes: 47 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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` 管理对话:
Expand All @@ -75,4 +77,32 @@ askchat --list
askchat -p
# 打印指定的对话:-p/--print
askchat -p joke
```
```

## 管理环境配置

通过 `chatenv` 管理不同的环境配置:

```bash
# 创建新环境
chatenv create <name> [--api-key "<api_key>"] [--base-url "<base_url>"] [--api-base "<api_base>"] [--model "<model_name>"]

# 激活指定环境
chatenv use <name>

# 更新环境配置
chatenv config [<name>] [--api-key "<new_api_key>"] [--base-url "<new_base_url>"] [--api-base "<new_api_base>"] [--model "<new_model_name>"]

# 列出所有环境
chatenv list

# 显示指定环境或默认环境的变量
chatenv show [<name>]

# 保存当前环境为一个新的环境文件
chatenv save <name>

# 删除指定的环境或默认环境配置
chatenv delete [<name>] [--default]
```

4 changes: 2 additions & 2 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__ = '0.3.4'
__version__ = '0.4.0'

import asyncio

Expand Down Expand Up @@ -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")
"# You can use `askchat --all-valid-models` to see supported models")
51 changes: 27 additions & 24 deletions askchat/chatenvs.py → askchat/chatenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.")
Expand Down Expand Up @@ -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')
Expand All @@ -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():
Expand All @@ -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()
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit dc13569

Please sign in to comment.