ITADN
kharvd/gpt-cli
kharvd/gpt-cli · 文件 下载 ZIP
文件最后提交记录最后更新时间
README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈

gpt-cli

用于聊天 LLM 的命令行界面。

立即试用

export ANTHROPIC_API_KEY=xcxc
uvx --from gpt-command-line gpt

支持的提供商

  • OpenAI
  • Anthropic
  • Google Gemini
  • Cohere
  • 其他与 OpenAI 兼容的 API(例如 Together、OpenRouter、使用 LM Studio 的本地模型)

screenshot

功能

  • 命令行界面: 直接在终端中与 ChatGPT 或 Claude 交互。
  • 模型自定义: 为每个助手覆盖默认的模型、temperature 和 top_p 值,从而对 AI 的行为进行细粒度控制。
  • 扩展思考模式: 启用 Claude 3.7 的扩展思考能力,以查看其针对复杂问题的推理过程。
  • 用量跟踪: 通过 token 计数和价格信息跟踪您的 API 使用情况。
  • 键盘快捷键: 使用 Ctrl-C、Ctrl-D 和 Ctrl-R 快捷键,以便更轻松地管理对话和控制输入。
  • 多行输入: 进入多行模式,以处理更复杂的查询或对话。
  • Markdown 支持: 启用或禁用聊天会话中的 markdown 格式,以根据您的偏好定制输出。
  • 预定义消息: 为您的自定义助手设置预定义消息,以建立上下文或角色扮演场景。
  • 多个助手: 轻松在不同的助手之间切换,包括在配置文件中定义的通用、开发和自定义助手。
  • 灵活配置: 在 YAML 配置文件中定义您的助手、模型参数和 API 密钥,便于自定义和管理。

安装

此安装假设在 Linux/OSX 机器上可用 Python 和 pip。

pip install gpt-command-line

从源码安装最新版本:

pip install git+https://github.com/kharvd/gpt-cli.git

或者通过手动克隆仓库来安装:

git clone https://github.com/kharvd/gpt-cli.git
cd gpt-cli
pip install .

将 OpenAI API 密钥添加到您的 .bashrc 文件中(位于主目录的根目录下)。 在此示例中我们使用 nano,您可以使用任何文本编辑器。

nano ~/.bashrc
export OPENAI_API_KEY=<your_key_here>

运行该工具

gpt

您也可以使用 gpt.yml 文件进行配置。请参阅下文中的 Configuration 部分。

用法

请确保将 OPENAI_API_KEY 环境变量设置为您的 OpenAI API 密钥(或按照下文所述将其放入 ~/.config/gpt-cli/gpt.yml 文件中)。

usage: gpt [-h] [--no_markdown] [--model MODEL] [--temperature TEMPERATURE] [--top_p TOP_P]
              [--thinking THINKING_BUDGET] [--log_file LOG_FILE] 
              [--log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] [--prompt PROMPT] 
              [--execute EXECUTE] [--no_stream] [{dev,general,bash}]

Run a chat session with ChatGPT. See https://github.com/kharvd/gpt-cli for more information.

positional arguments:
  {dev,general,bash}
                        The name of assistant to use. `general` (default) is a generally helpful
                        assistant, `dev` is a software development assistant with shorter
                        responses. You can specify your own assistants in the config file
                        ~/.config/gpt-cli/gpt.yml. See the README for more information.

optional arguments:
  -h, --help            show this help message and exit
  --no_markdown         Disable markdown formatting in the chat session.
  --model MODEL         The model to use for the chat session. Overrides the default model defined
                        for the assistant.
  --temperature TEMPERATURE
                        The temperature to use for the chat session. Overrides the default
                        temperature defined for the assistant.
  --top_p TOP_P         The top_p to use for the chat session. Overrides the default top_p defined
                        for the assistant.
  --thinking THINKING_BUDGET
                        Enable Claude's extended thinking mode with the specified token budget.
                        Only applies to Claude 3.7 models.
  --log_file LOG_FILE   The file to write logs to. Supports strftime format codes.
  --log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        The log level to use
  --prompt PROMPT, -p PROMPT
                        If specified, will not start an interactive chat session and instead will
                        print the response to standard output and exit. May be specified multiple
                        times. Use `-` to read the prompt from standard input. Implies
                        --no_markdown.
  --execute EXECUTE, -e EXECUTE
                        If specified, passes the prompt to the assistant and allows the user to
                        edit the produced shell command before executing it. Implies --no_stream.
                        Use `-` to read the prompt from standard input.
  --no_stream           If specified, will not stream the response to standard output. This is
                        useful if you want to use the response in a script. Ignored when the
                        --prompt option is not specified.
  --no_price            Disable price logging.

输入 :q 或 Ctrl-D 退出,:c 或 Ctrl-C 清除对话,:r 或 Ctrl-R 重新生成上一条回复。 要进入多行模式,请输入反斜杠 \ 后跟一个新行。按 ESC 然后按 Enter 退出多行模式。

dev 助手被指示为软件开发专家,并提供简短的回复。

$ gpt dev

bash 助手被指示为 bash 脚本专家,仅提供 bash 命令。使用 --execute 选项来执行命令。它与 gpt-4 模型配合效果最佳。

gpt bash -e "How do I list files in a directory?"

这将提示您在执行前编辑您的 $EDITOR 中的命令。

配置

您可以在配置文件 ~/.config/gpt-cli/gpt.yml 中配置助手。该文件是一个 YAML 文件,具有以下结构(另请参阅 config.py)

default_assistant: <assistant_name>
markdown: False
openai_api_key: <openai_api_key>
anthropic_api_key: <anthropic_api_key>
log_file: <path>
log_level: <DEBUG|INFO|WARNING|ERROR|CRITICAL>
assistants:
  <assistant_name>:
    model: <model_name>
    temperature: <temperature>
    top_p: <top_p>
    thinking_budget: <token_budget>  # Claude 3.7 models only
    messages:
      - { role: <role>, content: <message> }
      - ...
  <assistant_name>:
    ...

您也可以覆盖预定义助手的参数。

您可以通过设置 default_assistant 字段来指定要使用的默认助手。如果您未指定,默认助手为 general。您还可以指定用于助手的 modeltemperaturetop_p。如果您未指定它们,将使用默认值。这些参数也可以通过命令行参数进行覆盖。

示例:

default_assistant: dev
markdown: True
openai_api_key: <openai_api_key>
assistants:
  pirate:
    model: gpt-4
    temperature: 1.0
    messages:
      - { role: system, content: "You are a pirate." }
$ gpt pirate

> Arrrr
Ahoy, matey! What be bringing ye to these here waters? Be it treasure or adventure ye seek, we be sailing the high seas together. Ready yer map and compass, for we have a long voyage ahead!

使用 !include 向助手读取其他上下文

你可以使用 !include <file_path> 将文件读取到助手的上下文中。

default_assistant: dev
markdown: True
openai_api_key: <openai_api_key>
assistants:
  pirate:
    model: gpt-4
    temperature: 1.0
    messages:
      - { role: system, content: !include "pirate.txt" }

自定义 OpenAI API URL

如果你使用的是与 OpenAI Python SDK 兼容的其他模型,可以通过修改配置文件中的 openai_base_url 设置或使用 OPENAI_BASE_URL 环境变量进行配置。

示例:

openai_base_url: https://your-custom-api-url.com/v1

使用 oai-compat: 前缀为模型名称,以向 API 传递非 GPT 模型名称。例如,要在 Together 上与 Llama3-70b 进行对话,请使用以下命令:

OPENAI_API_KEY=$TOGETHER_API_KEY OPENAI_BASE_URL=https://api.together.xyz/v1 gpt general --model oai-compat:meta-llama/Llama-3-70b-chat-hf

在将请求发送到 API 之前,前缀会被移除。

类似地,使用 oai-azure: 模型名称前缀来使用通过 Azure Open AI 部署的模型。例如,oai-azure:my-deployment-name

通过助手配置,您可以为特定助手覆盖基础 URL 和 API 密钥。

# ~/.config/gpt-cli/gpt.yml
assistants:
  llama:
    model: oai-compat:meta-llama/llama-3.3-70b-instruct
    openai_base_url_override: https://openrouter.ai/api/v1
    openai_api_key_override: $OPENROUTER_API_KEY

其他聊天机器人

Anthropic Claude

要使用 Claude,你需要拥有来自 Anthropic 的 API 密钥(目前 API 访问需排队等候)。获取 API 密钥后,你可以添加一个环境变量

export ANTHROPIC_API_KEY=<your_key_here>

~/.config/gpt-cli/gpt.yml 中的一行配置:

anthropic_api_key: <your_key_here>

现在你应该能够使用 --model claude-3-(opus|sonnet|haiku)-<date> 运行 gpt

gpt --model claude-3-opus-20240229

Claude 3.7 Sonnet 扩展思考模式

Claude 3.7 Sonnet 支持扩展思考模式,该模式会在给出最终答案之前展示 Claude 的推理过程。这对于复杂分析、高级 STEM 问题以及具有多重约束的任务非常有用。

使用 --thinking 参数启用它,并指定思考过程的 token 预算:

gpt --model claude-3-7-sonnet-20250219 --thinking 32000

您还可以在配置中为特定的助手配置思考模式:

assistants:
  math:
    model: claude-3-7-sonnet-20250219
    thinking_budget: 32000
    messages:
      - { role: system, content: "You are a math expert." }

注意:当启用思考模式时,根据 Claude API 的要求,temperature 会自动设置为 1.0,并且 top_p 会被取消设置。

Google Gemini

export GOOGLE_API_KEY=<your_key_here>

google_api_key: <your_key_here>

Cohere

export COHERE_API_KEY=<your_key_here>

cohere_api_key: <your_key_here>