跳转到主要内容

未提供项目描述

项目描述

ai.py

一个与ChatGPT API在命令行交互的单文件Python脚本。

特性

  • 使用快捷键访问预定义提示
  • 在输出中突出显示代码
  • 支持一次性查询和对话
  • 使用如!set之类的特殊命令来控制聊天时的行为

安装

只需将脚本复制到$PATH中的某个文件夹,例如/usr/local/bin。您也可以将其重命名为ai以去除.py扩展名。

以下是一个可以直接将脚本安装到您系统中的命令

curl https://raw.githubusercontent.com/reorx/ai.py/master/ai.py -o /usr/local/bin/ai && chmod +x /usr/local/bin/ai

您也可以使用pip或pipx安装它

pip install aidotpy

用法

将您的OpenAI API密钥粘贴到~/.config/ai-py/config.json中,或将其设置为AI_PY_API_KEY环境变量。

mkdir -p ~/.config/ai-py
echo '{"api_key":"<Your API key>"}' > ~/.config/ai-py/config.json

有关脚本的详细用法,请阅读./ai.py -h的描述

usage: ai [-h] [-s SYSTEM] [-c] [--history HISTORY] [-w] [-v] [-t] [-d] [--version] [PROMPT]

A simple CLI for ChatGPT API

positional arguments:
  PROMPT                your prompt, leave it empty to run REPL. you can use @ to load prompt
                        from the prompts file.

options:
  -h, --help            show this help message and exit
  -s SYSTEM, --system SYSTEM
                        system message to use at the beginning of the conversation. if starts
                        with @, the message will be located through the prompts file
  -c, --conversation    enable conversation, which means all the messages will be sent to the
                        API, not just the last one. This is only useful to REPL
  --history HISTORY     load the history from a JSON file.
  -w, --write-history   write new messages to --history file after each chat.
  -v, --verbose         verbose mode, show execution info and role in the message
  -t, --show-tokens     show a breakdown of the tokens used in the prompt and in the response
  -d, --debug           debug mode, enable logging
  --version             show program's version number and exit

一次性查询

将提示作为第一个参数传递

./ai.py 'hello world'

您也可以通过管道(|)传递提示

head README.md | ./ai.py 'Proofreading the following text:'

交互式编程环境(REPL)

无参数运行,用于读取-评估-打印循环

./ai.py

默认情况下,只有最后一条消息和系统消息会发送到API,如果您想让它记住所有上下文(即发送每次聊天中的所有消息),请添加-c参数以启用对话

./ai.py -c

系统消息

您可以传递一条系统消息来定义助手的行为

./ai.py -s 'You are a proofreader' 'its nice know you'

您还可以将预定义的系统消息保存到~/.config/ai-py/prompts.json中,并通过在开头使用@来引用它们,这将在下一节中介绍。

提示快捷键

您可以在~/.config/ai-py/prompts.json中预定义提示,并通过使用@作为前缀来引用它们。这适用于系统消息和用户消息。

假设您的~/.config/ai-py/prompts.json看起来像这样

{
  "system": {
    "cli": "As a technology assistant with expertise in command line, answer questions in simple and short words for users who have a high-level background. Provide only one example, and explain as less as possible."
  },
  "user": {
    "native": "Paraphrase the following sentences to make it more native:\n",
    "revise": "Revise the following sentences to make them more clear concise and coherent:\n",
    "": ""
  }
}

然后您可以通过在系统消息中使用cli提示快捷键来使用

./ai.py -s @cli

并在用户消息中使用nativerevise提示快捷键来使用

./ai.py '@native its nice know you'

It's great to get to know you.

详细模式

添加-v以打印API调用中使用的角色名称和参数。

截图

特殊命令

您可以使用特殊命令来控制脚本在交互式编程环境(REPL)中运行时的行为。

以下是可用命令的列表

  • !set <key> <value>:在配置中设置键值对,可用的键有
    • verbose:设置为TrueFalse,例如!set verbose True
    • conversation:设置为TrueFalse,例如!set conversation True
    • system:设置系统消息。例如!set system you are a poet!set system @cli
    • params:设置ChatGPT API的参数。例如!set params temperature 0.5
    • model:设置要使用的模型。例如!set model gpt-4
  • !info:打印执行信息
  • !write-history:将当前消息写入历史文件。例如!write-history history.json

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

此版本没有可用的源分发文件。请参阅生成分发存档的教程

构建分发

aidotpy-0.3.1-py3-none-any.whl (9.8 kB 查看散列值)

上传时间 Python 3

由以下支持