Skip to content

Commit

Permalink
fix dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
RexWzh committed Mar 8, 2024
1 parent 0109281 commit 6fe6894
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 141 deletions.
71 changes: 0 additions & 71 deletions README-ZH.md

This file was deleted.

73 changes: 73 additions & 0 deletions README-en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# 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)

[English](README-en.md) | [简体中文](README.md)

Interact with ChatGPT in terminal via chattool.

## Installation

```bash
pip install askchat
```

## Usage

A simple way:
```bash
ask hello
```

Ask with more options via `askchat`:
```bash
# ask with a specific model
askchat hello -m "baichuan2" --base_url "localhost:8000"
```

Generate config file for default options:
```bash
askchat --generate-config
```

You might edit the config at `~/.askchat/.env`.

Other options:
```bash
# current version
askchat -v
# print the debug log
askchat --debug
# get valid models that contains "gpt"
askchat --valid-models
# get all valid models
askchat --all-valid-models
```

## Advance usage

You can manage your chats with `askchat`:

```bash
askchat hello
# continue the last chat: -c
askchat -c tell me a joke please
# regenerate the last conversation: -r
askchat -r
# regenerate the last conversation with new message: -r
askchat -r give me some jokes please
# save the chat: -s/--save
askchat -s joke
# load the chat: -l/--load
askchat -l joke
# delete the chat: -d/--delete
askchat -d joke
# list all saved chats: --list
askchat --list
# print the last chat: -p/--print
askchat -p
# print the given chat: -p/--print
askchat -p joke
```
68 changes: 37 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,72 +1,78 @@
# 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)

[![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)

Interact with ChatGPT in terminal via chattool.
[English](README-en.md) | [简体中文](README.md)

## Installation
在命令行中调用 ChatGPT。

## 安装

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

## Usage
## 使用方法

A simple way:
简单运行方式,使用默认的环境变量:
```bash
ask hello
```

Ask with more options via `askchat`:
通过 `askchat` 指定其他选项:
```bash
# ask with a specific model
# 使用特定模型提问
askchat hello -m "baichuan2" --base_url "localhost:8000"
```

Generate config file for default options:
通过环境变量生成默认的配置文件,可以在 `~/.askchat/.env` 中编辑配置
```bash
askchat --generate-config
```

You might edit the config at `~/.askchat/.env`.
修改配置参数,比如
```bash
askchat --config --api-key "your_api_key"
```

Other options:

其他选项:
```bash
# current version
# 当前版本
askchat -v
# print the debug log
# 打印调试日志
askchat --debug
# get valid models that contains "gpt"
# 获取包含"gpt"的有效模型
askchat --valid-models
# get all valid models
# 获取所有有效模型
askchat --all-valid-models
```

## Advance usage
## 管理对话记录

You can manage your chats with `askchat`:
使用 `askchat` 管理对话:

```bash
askchat hello
# continue the last chat: -c
askchat -c tell me a joke please
# regenerate the last conversation: -r
# 继续上一次对话:-c
askchat -c 请给我讲个笑话
# 重新生成最后一次对话:-r
askchat -r
# regenerate the last conversation with new message: -r
# 修改并重新生成最后一次对话:-r
askchat -r give me some jokes please
# save the chat: -s/--save
# 保存对话:-s/--save
askchat -s joke
# load the chat: -l/--load
# 加载对话:-l/--load
askchat -l joke
# delete the chat: -d/--delete
# 删除对话:-d/--delete
askchat -d joke
# list all saved chats: --list
# 列出所有保存的对话:--list
askchat --list
# print the last chat: -p/--print
# 打印最后一次对话:-p/--print
askchat -p
# print the given chat: -p/--print
# 打印指定的对话:-p/--print
askchat -p joke
```
```
2 changes: 1 addition & 1 deletion askchat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

__author__ = """Rex Wang"""
__email__ = '[email protected]'
__version__ = '0.3.3'
__version__ = '0.3.4'

from .askchat import ask
75 changes: 39 additions & 36 deletions askchat/askchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def main():
## arguments for chat message
parser.add_argument('message', help='User message', default='', nargs='*')
parser.add_argument('-m', '--model', default=None, help='Model name')
parser.add_argument('--base-url', default=None, help='base url of the api(without suffix `/v1`)')
parser.add_argument("--api-key", default=None, help="API key")
parser.add_argument('-b', '--base-url', default=None, help='base url of the api(without suffix `/v1`)')
parser.add_argument('-a', "--api-key", default=None, help="OpenAI API key")
## Chat with history
parser.add_argument('-c', action='store_true', help='Continue the last conversation')
parser.add_argument('-r', action='store_true', help='Regenerate the last conversation')
Expand All @@ -60,47 +60,17 @@ def main():
parser.add_argument('--generate-config', action="store_true", help="Generate a configuration file by environment table")
parser.add_argument('-v', '--version', action='version', version=VERSION)
args = parser.parse_args()

# set values
os.makedirs(CONFIG_PATH, exist_ok=True)
## read para from config file
if os.path.exists(CONFIG_FILE):
load_dotenv(CONFIG_FILE, override=True)
## set para from command line
if args.api_key:
os.environ['OPENAI_API_KEY'] = args.api_key
if args.base_url:
os.environ['OPENAI_API_BASE_URL'] = args.base_url
if args.model:
os.environ['OPENAI_API_MODEL'] = args.model
load_envs()

# show debug log
if args.debug:
debug_log()
return

# show valid models
if args.valid_models:
print('Valid models that contain "gpt" in their names:')
pprint(Chat().get_valid_models())
return
if args.all_valid_models:
print('All valid models:')
pprint(Chat().get_valid_models(gpt_only=False))
return


# generate config file
if args.generate_config:
api_key = os.environ.get("OPENAI_API_KEY")
base_url = os.environ.get("OPENAI_API_BASE_URL")
model = os.environ.get("OPENAI_API_MODEL")
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")
# move the old config file to a temporary file
if os.path.exists(CONFIG_FILE):
# create a temporary file
# move the old config file to a temporary file
os.makedirs("/tmp", exist_ok=True)
tmp_file = os.path.join("/tmp", str(uuid.uuid4())[:8] + ".askchat.env")
# move the old config file to a temporary file
shutil.move(CONFIG_FILE, tmp_file)
print(f"Moved old config file to {tmp_file}")
# save the config file
Expand All @@ -111,6 +81,8 @@ def main():
"# Current version: " + VERSION + "\n\n" +\
"# The base url of the API (without suffix /v1)\n" +\
"OPENAI_API_BASE_URL=\n\n" +\
"# The base url of the API (with suffix /v1)\n" +\
"OPENAI_API_BASE=\n\n" +\
"# Your API key\n" +\
"OPENAI_API_KEY=\n\n" +\
"# The model name\n" +\
Expand All @@ -119,10 +91,40 @@ def main():
# write the environment table
if api_key: set_key(CONFIG_FILE, "OPENAI_API_KEY", api_key)
if base_url: set_key(CONFIG_FILE, "OPENAI_API_BASE_URL", base_url)
if api_base: set_key(CONFIG_FILE, "OPENAI_API_BASE", api_base)
if model: set_key(CONFIG_FILE, "OPENAI_API_MODEL", model)
print("Created config file at", CONFIG_FILE)
return

# set values
## 1. read from config file
if os.path.exists(CONFIG_FILE):
load_dotenv(CONFIG_FILE, override=True)
## 2. read from command line
if args.api_key:
os.environ['OPENAI_API_KEY'] = args.api_key
if args.base_url:
os.environ['OPENAI_API_BASE_URL'] = args.base_url
if args.model:
os.environ['OPENAI_API_MODEL'] = args.model
## 3. read from environment variables
load_envs()

# show debug log
if args.debug:
debug_log()
return

# show valid models
if args.valid_models:
print('Valid models that contain "gpt" in their names:')
pprint(Chat().get_valid_models())
return
if args.all_valid_models:
print('All valid models:')
pprint(Chat().get_valid_models(gpt_only=False))
return

# deal with chat history
call_history = False
## load chat
Expand Down Expand Up @@ -158,6 +160,7 @@ def main():
names = args.print
assert len(names) <= 1, "Only one file can be specified"
new_file = os.path.join(CONFIG_PATH, names[0]) + ".json" if len(names) else LAST_CHAT_FILE
assert os.path.exists(new_file), "No conversation file found, check the chat list with `--list` option"
chat = Chat.load(new_file)
chat.print_log()
call_history = True
Expand Down
Loading

0 comments on commit 6fe6894

Please sign in to comment.