├── imgs └── shot.png ├── go.mod ├── conf.json ├── go.sum ├── .gitignore ├── LICENSE ├── .github └── workflows │ └── release.yml ├── cmd └── main.go └── README.md /imgs/shot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fleeto/pipe2gpt/HEAD/imgs/shot.png -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module gptdiag 2 | 3 | go 1.20 4 | 5 | require github.com/sashabaranov/go-openai v1.5.7 6 | -------------------------------------------------------------------------------- /conf.json: -------------------------------------------------------------------------------- 1 | { 2 | "data":{"k8s": "简明扼要地用 Kubernetes 专家的身份判断一下这段输出有什么问题,要整齐列出问题对象和可能原因以及操作建议:"} 3 | } 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/sashabaranov/go-openai v1.5.7 h1:8DGgRG+P7yWixte5j720y6yiXgY3Hlgcd0gcpHdltfo= 2 | github.com/sashabaranov/go-openai v1.5.7/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg= 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore for GoLand and macOS 2 | 3 | # GoLand 编辑器相关 4 | .idea/ 5 | 6 | # macOS 系统文件 7 | .DS_Store 8 | 9 | # Go 语言相关 10 | *.exe 11 | *.exe~ 12 | *.dll 13 | *.so 14 | *.dylib 15 | *.test 16 | *.out 17 | *.o 18 | *.a 19 | *.log 20 | /vendor/ 21 | /bin/ 22 | !vendor/plugin/*/bin/ 23 | 24 | # GoLand 运行调试配置文件 25 | *.iml 26 | 27 | # 临时文件 28 | *.swp 29 | *.swo 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Vincent 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | release: 5 | types: 6 | - published 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | permissions: 12 | contents: write 13 | strategy: 14 | matrix: 15 | goos: [linux, darwin] 16 | goarch: [amd64, arm64] 17 | steps: 18 | - name: Set up Go 19 | uses: actions/setup-go@v2 20 | with: 21 | go-version: '^1.16' 22 | 23 | - name: Check out code 24 | uses: actions/checkout@v2 25 | 26 | - name: Build 27 | run: | 28 | export GOOS=${{ matrix.goos }} 29 | export GOARCH=${{ matrix.goarch }} 30 | export GO111MODULE=on 31 | go build -o bin/pipe2gpt-$GOOS-$GOARCH cmd/main.go 32 | chmod +x bin/pipe2gpt-$GOOS-$GOARCH 33 | 34 | - name: Package 35 | run: | 36 | mkdir -p bin/package/pipe2gpt 37 | cp bin/pipe2gpt-${{ matrix.goos }}-${{ matrix.goarch }} bin/package/pipe2gpt/pipe2gpt 38 | cp conf.json bin/package/pipe2gpt/conf.json 39 | tar czf bin/pipe2gpt-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz -C bin/package pipe2gpt 40 | 41 | - name: Upload binaries to release 42 | uses: svenstaro/upload-release-action@v2 43 | with: 44 | repo_token: ${{ secrets.GITHUB_TOKEN }} 45 | file: bin/pipe2gpt-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz 46 | asset_name: pipe2gpt-${{ matrix.goos }}-${{ matrix.goarch }}.tar.gz 47 | tag: ${{ github.ref }} 48 | overwrite: true 49 | prerelease: false 50 | -------------------------------------------------------------------------------- /cmd/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "bufio" 5 | "context" 6 | "encoding/json" 7 | "flag" 8 | "fmt" 9 | "io/ioutil" 10 | "log" 11 | "os" 12 | "strings" 13 | 14 | openai "github.com/sashabaranov/go-openai" 15 | ) 16 | 17 | type Config struct { 18 | Data map[string]string `json:"data"` 19 | } 20 | 21 | func readConfig(filePath string) (*Config, error) { 22 | fileBytes, err := ioutil.ReadFile(filePath) 23 | if err != nil { 24 | return nil, err 25 | } 26 | var config Config 27 | err = json.Unmarshal(fileBytes, &config) 28 | if err != nil { 29 | return nil, err 30 | } 31 | 32 | return &config, nil 33 | } 34 | 35 | func readStdin() (string, error) { 36 | scanner := bufio.NewScanner(os.Stdin) 37 | var lines []string 38 | for scanner.Scan() { 39 | lines = append(lines, scanner.Text()) 40 | } 41 | if err := scanner.Err(); err != nil { 42 | return "", err 43 | } 44 | return strings.Join(lines, "\n"), nil 45 | } 46 | 47 | func main() { 48 | openaiToken, ok := os.LookupEnv("OPENAI_TOKEN") 49 | if !ok { 50 | log.Fatal("Failed to read OPENAI_TOKEN environment variable.") 51 | } 52 | 53 | typeFlag := flag.String("type", "", "Type to be retrieved from conf.json") 54 | flag.Parse() 55 | 56 | if *typeFlag == "" { 57 | log.Fatal("Error: '--type' flag must be provided.") 58 | } 59 | 60 | config, err := readConfig("conf.json") 61 | if err != nil { 62 | log.Fatalf("Error reading config file: %v", err) 63 | } 64 | prompt, ok := config.Data[*typeFlag] 65 | if !ok { 66 | log.Fatalf("Error: Type '%s' not found in conf.json", *typeFlag) 67 | } 68 | 69 | kubeOutput, err := readStdin() 70 | if err != nil { 71 | log.Fatalf("Error reading from stdin: %v", err) 72 | } 73 | 74 | client := openai.NewClient(openaiToken) 75 | resp, err := client.CreateChatCompletion( 76 | context.Background(), 77 | openai.ChatCompletionRequest{ 78 | Model: openai.GPT3Dot5Turbo, 79 | Messages: []openai.ChatCompletionMessage{ 80 | { 81 | Role: openai.ChatMessageRoleUser, 82 | Content: prompt + "\n" + kubeOutput, 83 | }, 84 | }, 85 | }, 86 | ) 87 | 88 | if err != nil { 89 | fmt.Printf("ChatCompletion error: %v\n", err) 90 | return 91 | } 92 | 93 | fmt.Println(resp.Choices[0].Message.Content) 94 | } 95 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pipe2GPT 2 | 3 | ![example](imgs/shot.png) 4 | 5 | 这个项目最有趣的一个点是:本项目包含的所有文件的绝大部分内容,都是由 ChatGPT 生成的。 6 | 7 | Pipe2GPT 是一个使用 Go 编写的命令行工具,用于与 OpenAI GPT-3.5-turbo 模型进行交互。用户可以从 `conf.json` 文件中获取指定类型的 prompt,并将从标准输入获取的文本一起作为 GPT-3.5-turbo 模型的输入。程序通过 OpenAI API 发送请求并获取模型生成的回复,然后将回复内容输出到控制台。 8 | 9 | ~~~command 10 | $ kubectl describe po test-deployment-7fcc4f4fd-p8j27 | bin/pipe2gpt --type=k8s 11 | ~~~ 12 | 13 | ## 开始 14 | 15 | ### 安装 16 | 17 | 1. 确保您已安装 [Go](https://golang.org/doc/install) 。 18 | 2. 克隆此仓库到您的本地计算机。 19 | 3. 进入项目根目录,运行 `./build.sh` 以构建项目。 20 | 4. 配置环境变量 `OPENAI_TOKEN`。 21 | 22 | ### 使用 23 | 24 | 修改 `conf.json` 文件,添加您需要的 Prompt 类型,用管道将命令行输出内容给 Pipe2GPT。 25 | 26 | 例如:`kubectl get pods | pipe2gpt --type=k8s` 27 | 28 | ## 注意事项 29 | 30 | 请确保您已在系统中设置了 OpenAI API 密钥(环境变量 `OPENAI_TOKEN`)。 31 | 32 | ## 许可 33 | 34 | 用 MIT 许可。有关详细信息,请参阅 LICENSE 文件。 35 | 36 | ## 变更历史 37 | 38 | v0.0.5 2023 年 03 月 25 日,使用 ChatGPT 编写了 `release.yml`,进行二进制的构建和发布。 39 | 40 | --- 41 | 42 | The most interesting aspect of this project is that the vast majority of the contents in all of the files are generated by ChatGPT. 43 | 44 | Pipe2GPT is a command-line tool written in Go that interacts with the OpenAI GPT-3.5-turbo model. Users can obtain a specific type of prompt from the `conf.json` file and use it along with the text obtained from standard input as input to the GPT-3.5-turbo model. The program sends requests through the OpenAI API and receives the model-generated responses, which are then output to the console. 45 | 46 | ~~~command 47 | $ kubectl describe po test-deployment-7fcc4f4fd-p8j27 | bin/pipe2gpt --type=k8s 48 | ~~~ 49 | 50 | ## Getting Started 51 | 52 | ### Installation 53 | 54 | 1. Ensure you have [Go](https://golang.org/doc/install) installed. 55 | 2. Clone this repository to your local machine. 56 | 3. In the project root directory, run `./build.sh` to build the project. 57 | 4. Set up the environment 58 | 59 | ### Usage 60 | 61 | To modify the conf.json file, add the prompt type(s) you need and pipe the command line output to pipe2gpt. 62 | 63 | For example: `kubectl get pods | pipe2gpt --type=k8s` 64 | 65 | ## Notes 66 | 67 | Make sure you have set the OpenAI API key (environment variable OPENAI_TOKEN) in your system. 68 | 69 | ## License 70 | 71 | This project is licensed under the MIT License. For more information, see the LICENSE file. 72 | 73 | ## Change Log 74 | 75 | v0.0.5, March 25th, 2023, used ChatGPT to write release.yml for binary build and deployment. --------------------------------------------------------------------------------