Skip to content

Commit

Permalink
simplify codes using callback
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Mar 10, 2024
1 parent 15f62fa commit 5964ef7
Show file tree
Hide file tree
Showing 3 changed files with 338 additions and 206 deletions.
179 changes: 116 additions & 63 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,103 +5,156 @@
[![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) | [Simplified Chinese](README.md)
[English](README-en.md) | [简体中文](README.md)

Invoke ChatGPT from the command line.

## Installation
## Installation and Configuration

```bash
pip install askchat --upgrade
```

## How to Use

Run simply with the default environment variables:
Configure environment variables:

```bash
ask hello
export OPENAI_API_KEY="your-api-key"
export OPENAI_API_BASE_URL="https://api.openai.com"
export OPENAI_API_BASE="https://api.openai.com/v1"
export OPENAI_MODEL="gpt-3.5-turbo"
```

Specify other options via `askchat`:
Note: The `OPENAI_API_BASE` variable takes precedence over `OPENAI_API_BASE_URL`. Use one of them as needed.

## Usage

To run with the default environment variables:

```bash
# Ask using a specific model
askchat hello -m "baichuan2" --base-url "localhost:8000"
ask hello
```

Generate a default configuration file via environment variables, edit the configuration in `~/.askchat/.env`:
## AskChat

Use `askchat` for more flexibility with parameters and to manage conversations, with related files saved under `~/.askchat/`. Supported options include:

```bash
askchat --generate-config
❯ askchat --help
Usage: askchat [OPTIONS] [MESSAGE]...

Interact with ChatGPT in terminal via chattool

Options:
-m, --model TEXT Model name
-b, --base-url TEXT Base URL of the API (without suffix `/v1`)
--api-base TEXT Base URL of the API (with suffix `/v1`)
-a, --api-key TEXT OpenAI API key
-u, --use-env TEXT Use environment variables from the ENV_PATH
-c Continue the last conversation
-r, --regenerate Regenerate the last conversation
-l, --load TEXT Load the conversation from a file
-p, --print Print the last conversation or a specific conversation
-s, --save TEXT Save the conversation to a file
-d, --delete TEXT Delete the conversation from a file
--list List all the conversation files
--generate-config Generate a configuration file by environment table
--debug Print debug log
--valid-models Print valid models that contain "gpt" in their names
--all-valid-models Print all valid models
-v, --version Print the version
--help Show this message and exit.
```

## Chat Options
### Default Parameters

```bash
# Show the current version
askchat -v
Default parameters for the `askchat` command-line tool, used for direct interaction with ChatGPT or configuring API connection information.

# Print debug logs
askchat --debug
| Parameter | Example | Description |
|--------------------|---------------------------|------------------------------------|
| `<message>` | `askchat hello` | The simplest form of dialogue |
| `-m / --model` | `-m gpt-3.5-turbo` | Specify the model name |
| `-b / --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 (including `/v1`) |
| `-a / --api-key` | `-a sk-xxxxxxx` | Provide the OpenAI API key |
| `-u / --use-env` | `-u prod` | Load environment variables from the specified config file, see `chatenv` |

# Get valid models that include "gpt"
askchat --valid-models
Note: For some model APIs, like ChatGPT, `/v4` is used as the base path of the API. In such cases, use the `--api-base` parameter.

# Get all valid models
askchat --all-valid-models
```
### Conversation Management

Conversation management parameters allow users to save, load, delete, and list conversation histories, as well as continue a previous conversation.

## Managing Conversation History
| Parameter | Example | Description |
|------------------------|---------------------------------|------------------------------------------------|
| `-c` | `askchat -c` | Continue the last conversation |
| `-r / --regenerate` | `askchat -r` | Regenerate the last response of the conversation |
| `-l / --load` | `askchat -l conversation1` | Load conversation history from a file and continue |
| `-p / --print` | `askchat -p [name]` | Print

Manage conversations using `askchat`:
the last or a specified conversation history |
| `-s / --save` | `askchat -s conversation1` | Save the current conversation history to a file |
| `-d / --delete` | `askchat -d conversation1` | Delete a specified conversation history file |
| `--list` | `askchat --list` | List all saved conversation history files |

```bash
askchat hello
# Continue the last conversation: -c
askchat -c please tell me a joke
# Regenerate the last conversation: -r
askchat -r
# Modify and regenerate the last conversation: -r
askchat -r give me some jokes please
# Save the conversation: -s/--save
askchat -s joke
# Load a conversation: -l/--load
askchat -l joke
# Delete a conversation: -d/--delete
askchat -d joke
# List all saved conversations: --list
askchat --list
# Print the last conversation: -p/--print
askchat -p
# Print a specific conversation: -p/--print
askchat -p joke
```
All conversations are saved in `~/.askchat/`, with the most recent conversation saved in `~/.askchat/_last_chat.json`.

## Managing Environment Configuration
### Other Options

Manage different environment configurations with `chatenv`:
These options provide auxiliary functions, such as generating configuration files, printing debug logs, listing models, and showing version information.

```bash
# Create a new environment
chatenv create <name> [--api-key "<api_key>"] [--base-url "<base_url>"] [--api-base "<api_base>"] [--model "<model_name>"]
| Parameter | Example | Description |
|--------------------------|-------------------------------|--------------------------------------------|
| `--generate-config` | `askchat --generate-config` | Generate a config file, saved in `~/.askchat/.env` |
| `--debug` | `askchat --debug` | Print debug logs |
| `--valid-models` | `askchat --valid-models` | Print valid models containing "gpt" in their names |
| `--all-valid-models` | `askchat --all-valid-models` | Print all valid models |
| `-v / --version` | `askchat -v` | Print the version of `askchat` |

Note: `--all-valid-models` prints all available models, including Embedding, dalle-3, tts, etc. Use `--valid-models` to filter these out.

## ChatEnv

`chatenv` is a command-line tool designed for managing different environment configurations for `askchat`, supporting operations such as create, activate, delete, etc. It facilitates switching between different environments, managing API keys, model names, and API base URLs.

1. Create a new environment configuration using the `create` command.

```bash
chatenv create <name> [-a API_KEY] [-b BASE_URL] [--api-base API_BASE] [-m MODEL]
```

2. Activate an environment, setting it as the current configuration.

```bash
chatenv use <name>
```

3. Delete a specified environment configuration file.

```bash
chatenv delete <name>
chatenv delete --default
```

4. List all available environments.

```bash
chatenv list
```

# Activate a specified environment
chatenv use <name>
5. Display the configuration information of a specified environment, or the default environment if no name is provided.

# 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>"]
```bash
chatenv show [name]
```

# List all environments
chatenv list
6. Save the currently active environment configuration to a file with a specified name.

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

# Save the current environment as a new environment file
chatenv save <name>
7. Update one or more settings of a specified or default environment configuration.

# Delete a specified or the default environment configuration
chatenv delete [<name>] [--default]
```
```bash
chatenv config [name] [-a API_KEY] [-b BASE_URL] [--api-base API_BASE] [-m MODEL]
```
Loading

0 comments on commit 5964ef7

Please sign in to comment.