From 5c712bf223dc0a860a251df7a65afd6f844072e5 Mon Sep 17 00:00:00 2001 From: freesrz93 Date: Wed, 7 Aug 2024 19:01:28 +0800 Subject: [PATCH] feat: support pipeline --- README.md | 33 ++++++++++++------ README_ZH.md | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 8 +++++ 3 files changed, 129 insertions(+), 11 deletions(-) create mode 100644 README_ZH.md diff --git a/README.md b/README.md index 1ade8b4..9a97225 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,15 @@ # Ask GPT -Ask GPT is a CLI tool to chat with OpenAI LLMs. +English | [简体中文](./README_ZH.md) + +Ask GPT is a CLI tool to chat with OpenAI LLMs via API. ## Features -- **Custom Roles**: Customise multi roles, each with its own system prompt to tailor interactions. +- **Custom Roles**: Customise multi roles, each with its own system prompt. - **Save/Retrieve Conversations**: Save conversations locally and continue it seamlessly in the future. - **Streaming Output**: See the output instantly after an input, no need to wait for completion. +- **Support Pipeline**: Send any output of other CLI tools to AI via pipeline. - **Interactive mode**: Chat with AI interactively. Offer a user experience similar to the web-based ChatGPT. - **Support Models**: Compatibility with all models accessible through the OpenAI API. - **Edit Config Easily**: One-click access to configuration file for easy setup and customization. @@ -17,7 +20,11 @@ Download the pre-built binary from [release](https://github.com/freesrz93/ask-gp ## Usage -1. After the first run, the app will create the config file at +1. Run ask-gpt for the first time: + ```shell + ag -v + ``` + and the app will create the config file at ``` %USERPROFILE%/.config/ask-gpt/config.yaml ``` @@ -48,29 +55,33 @@ Download the pre-built binary from [release](https://github.com/freesrz93/ask-gp ag -s "translation" -i "Translate the given sentences to Chinese" ``` -6. To create a new role, use `-n`: +6. Use pipeline: + ```shell + cat raw.txt | ag -i "Translate above contents to Chinese" + ``` + +7. To create a new role, use `-n`: ```shell ag -n ``` then set its name, description and prompt interactively. -7. To use a role, use `-r` (the role should already exist): +8. To use a role, use `-r` (the role should already exist): ```shell ag -r "translator" -i "Translate the given sentences to Chinese" ``` `-r` can be used with `-s` but only valid when creating a conversation. -8. To show history of the conversation, use `-h`: +9. To show history of the conversation, use `-h`: ```shell ag -s "translation" -h -i ``` this will print previous messages and continue with interactive mode. -9. For more usage, see: - - ```shell - chatgpt --help - ``` +10. For more usage, see: + ```shell + chatgpt --help + ``` ## Configuration diff --git a/README_ZH.md b/README_ZH.md new file mode 100644 index 0000000..582a4bc --- /dev/null +++ b/README_ZH.md @@ -0,0 +1,99 @@ +# Ask GPT + +[English](./README.md) | 简体中文 + +Ask GPT 是一个命令行工具, 用于通过 API 与 OpenAI 的 GPT 模型聊天. + +## 功能特点 + +- **自定义角色**: 可自定义多个角色, 每个角色都有自己的 prompt. +- **保存/检索对话**: 将对话保存到本地, 并在稍后无缝继续. +- **流式输出**: 输入后即时看到输出, 无需等待完成. +- **管道支持**: 通过管道将任何其他命令行工具的输出发送给 AI. +- **交互模式**: 与 AI 交互聊天, 提供类似于基于网页的 ChatGPT 的用户体验. +- **多种模型**: 兼容通过 OpenAI API 访问的所有模型. +- **编辑文件配置**: 一键编辑配置文件, 便于设置和自定义. + +## 安装 + +从 [release](https://github.com/freesrz93/ask-gpt/releases/latest) 下载对应平台的预构建二进制文件并添加到 path 中. + +## 使用 + +1. 首次运行 ask-gpt: + ```shell + ag -v + ``` + 将在以下位置创建配置文件 + Windows: + ``` + %USERPROFILE%/.config/ask-gpt/config.yaml + ``` + Linux/macOS: + ``` + $HOME/.config/ask-gpt/config.yaml + ``` + +2. 编辑配置文件. 有关详细信息, 参见 [配置](#配置). + +3. 快速提问: + ```shell + ag "世界上最好的编程语言是什么?" + ``` + +4. 使用 `-s` 创建新的/继续已有对话: + ```shell + ag -s "chat 1" "讲个故事" + ``` + +5. 使用 `-i` 互动聊天: + ```shell + ag -i "将以下句子翻译为中文" + ``` + `-s` 可以与 `-i` 一起使用: + ```shell + ag -s "translation" -i "将以下句子翻译为中文" + ``` + +6. 使用管道: + ```shell + cat raw.txt | ag -i "将上述内容翻译为中文" + ``` + +7. 使用 `-n` 创建新角色: + ```shell + ag -n + ``` + 然后可以交互式的设置其名称、描述和提示. + +8. 使用 `-r` 使用角色(角色需存在): + ```shell + ag -r "translator" -i "将以下句子翻译为中文" + ``` + `-r` 可以与 `-s` 一起使用, 但仅在创建对话时有效. + +9. 使用 `-h` 显示对话历史: + ```shell + ag -s "translation" -h -i + ``` + 这将打印之前的消息并进入交互模式. + +10. 更多用法参见: + ```shell + chatgpt --help + ``` + +## 配置 + +| 名称 | 意义 | 默认值 | +|-------------------|---------------------------------------------------------------------------------------------------------|-----------------------------| +| url | OpenAI API 的 URL. | "https://api.openai.com/v1" | +| api_key | OpenAI API 密钥. | "" | +| model | 要使用的模型. | "gpt-4o-mini" | +| max_tokens | 单次 API 调用消耗的令牌数上限. | 4096 | +| temperature | 见 [OpenAI 文档](https://platform.openai.com/docs/api-reference/chat/create#chat-create-temperature) | 0.5 | +| top_p | 见 [OpenAI 文档](https://platform.openai.com/docs/api-reference/chat/create#chat-create-top_p) | 1.0 | +| frequency_penalty | 见 [OpenAI 文档](https://platform.openai.com/docs/api-reference/chat/create#chat-create-frequency_penalty) | 0 | +| presence_penalty | 见 [OpenAI 文档](https://platform.openai.com/docs/api-reference/chat/create#chat-create-presence_penalty) | 0 | +| editor | 用于编辑配置文件的编辑器. 必须在 path 中. | "code" | +| editor_arg | 传递给编辑器的参数. `%path` 将被替换为实际配置文件路径. | "%path" | diff --git a/main.go b/main.go index 4a89b22..9c325e3 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "io" "os" "strings" @@ -77,6 +78,13 @@ func handleSession(cmd *cobra.Command, args []string) error { } input := strings.Join(args, " ") + stat, _ := os.Stdin.Stat() + if (stat.Mode() & os.ModeCharDevice) == 0 { + pipe, err := io.ReadAll(os.Stdin) + if err == nil { + input = string(pipe) + "\n" + input + } + } if input != "" { P(AIPrefix) err = Client.Stream(s, input)