├── .github
├── build
│ ├── linux.yml
│ ├── mac.yml
│ ├── mac_arm64.yml
│ └── windows.yml
└── workflows
│ └── build.yml
├── .idea
├── .gitignore
├── ChatGPT-API.iml
├── modules.xml
└── vcs.xml
├── README.md
├── cmd
└── main.go
├── go.mod
├── gtp.go
└── gtp_test.go
/.github/build/linux.yml:
--------------------------------------------------------------------------------
1 | env:
2 | - GO111MODULE=on
3 | #before:
4 | # hooks:
5 | # - go mod tidy
6 | project_name: ChatGPT-API
7 | builds:
8 | - id: ChatGPT-linux
9 | ldflags:
10 | - -s -w
11 | binary: ChatGPT
12 | env:
13 | - CGO_ENABLED=1
14 | # 0 你再重新编译链接,那么 Go 链接器会使用 Go 版本的实现,这样你将得到一个没有动态链接的纯静态二进制程序
15 | main: cmd/main.go
16 | goos:
17 | - linux
18 | goarch:
19 | - amd64
20 | archives:
21 | - format: zip
22 |
23 | checksum:
24 | name_template: "{{ .ProjectName }}-linux-checksums.txt"
25 |
--------------------------------------------------------------------------------
/.github/build/mac.yml:
--------------------------------------------------------------------------------
1 | env:
2 | - GO111MODULE=on
3 | #before:
4 | # hooks:
5 | # - go mod tidy
6 | project_name: ChatGPT
7 | builds:
8 | - id: ChatGPT-darwin
9 | ldflags:
10 | - -s -w
11 | binary: ChatGPT
12 | env:
13 | - CGO_ENABLED=1
14 | main: cmd/main.go
15 | goos:
16 | - darwin
17 | goarch:
18 | - amd64
19 |
20 | archives:
21 | - format: zip
22 | replacements:
23 | darwin: macOS
24 |
25 | checksum:
26 | name_template: "{{ .ProjectName }}-mac-checksums.txt"
27 |
--------------------------------------------------------------------------------
/.github/build/mac_arm64.yml:
--------------------------------------------------------------------------------
1 | env:
2 | - GO111MODULE=on
3 | #before:
4 | # hooks:
5 | # - go mod tidy
6 | project_name: ChatGPT
7 | builds:
8 | - id: ChatGPT-darwin
9 | ldflags:
10 | - -s -w
11 | binary: ChatGPT
12 | env:
13 | - CGO_ENABLED=1
14 | main: cmd/main.go
15 | goos:
16 | - darwin
17 | goarch:
18 | - arm64
19 |
20 | archives:
21 | - format: zip
22 | replacements:
23 | darwin: macOS
24 |
25 | checksum:
26 | name_template: "{{ .ProjectName }}-mac-arm64-checksums.txt"
27 |
--------------------------------------------------------------------------------
/.github/build/windows.yml:
--------------------------------------------------------------------------------
1 | env:
2 | - GO111MODULE=on
3 | #before:
4 | # hooks:
5 | # - go mod tidy
6 | project_name: ChatGPT
7 | builds:
8 | - id: ChatGPT-windows
9 | ldflags:
10 | - -s -w
11 | binary: ChatGPT
12 | env:
13 | - CGO_ENABLED=1
14 | - CC=x86_64-w64-mingw32-gcc
15 | - CXX=x86_64-w64-mingw32-g++
16 | main: cmd/main.go
17 | goos:
18 | - windows
19 | goarch:
20 | - amd64
21 |
22 | archives:
23 | - format: zip
24 |
25 | checksum:
26 | name_template: "{{ .ProjectName }}-windows-checksums.txt"
27 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: 🎉 Release Binary
2 | on:
3 | create:
4 | tags:
5 | - v*
6 | workflow_dispatch:
7 |
8 | jobs:
9 | build-mac:
10 | runs-on: macos-latest
11 | steps:
12 | - name: Code checkout
13 | uses: actions/checkout@v2
14 | with:
15 | submodules: recursive
16 | fetch-depth: 0
17 | - name: Checkout submodules
18 | run: git submodule update --init --recursive
19 | - name: Set up Go
20 | uses: actions/setup-go@v2
21 | with:
22 | go-version: 1.18
23 | - name: Install Dependences
24 | run: |
25 | brew install libpcap upx
26 | # git submodule update --init --recursive --remote
27 | - name: Run GoReleaser
28 | uses: goreleaser/goreleaser-action@v2
29 | with:
30 | version: latest
31 | args: release -f .github/build/mac.yml --rm-dist
32 | env:
33 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34 | # https://github.com/marketplace/actions/run-on-architecture#supported-platforms
35 | build-linux:
36 | runs-on: ubuntu-latest
37 | steps:
38 | - name: Code checkout
39 | uses: actions/checkout@v2
40 | with:
41 | submodules: recursive
42 | fetch-depth: 0
43 | - name: Checkout submodules
44 | run: git submodule update --init --recursive
45 | - name: Set up Go
46 | uses: actions/setup-go@v2
47 | with:
48 | go-version: 1.18
49 | - name: Install Dependences
50 | run: |
51 | sudo apt install -yy --fix-missing libpcap-dev upx
52 | # git submodule update --init --recursive --remote
53 | - name: Run GoReleaser
54 | uses: goreleaser/goreleaser-action@v2
55 | with:
56 | version: latest
57 | args: release -f .github/build/linux.yml --rm-dist
58 | env:
59 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60 |
61 | build-windows:
62 | runs-on: windows-latest
63 | steps:
64 | - name: Code checkout
65 | uses: actions/checkout@v2
66 | with:
67 | submodules: recursive
68 | fetch-depth: 0
69 | - name: Checkout submodules
70 | run: git submodule update --init --recursive
71 | - name: Set up Go
72 | uses: actions/setup-go@v2
73 | with:
74 | go-version: 1.18
75 | - name: Run GoReleaser
76 | uses: goreleaser/goreleaser-action@v2
77 | with:
78 | version: latest
79 | args: release -f .github/build/windows.yml --rm-dist
80 | env:
81 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 | # Editor-based HTTP Client requests
5 | /httpRequests/
6 | # Datasource local storage ignored files
7 | /dataSources/
8 | /dataSources.local.xml
9 |
--------------------------------------------------------------------------------
/.idea/ChatGPT-API.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ChatGPT-API
2 |
3 | # How use
4 |
5 | ```
6 | curl -XPOST -v -H "user-agent: Mozilla/5.0 (Windows NT 6.1; rv:45.0) Gecko/20100101 Firefox/45.0" 'https://51pwn.com/chatGPT?q=介绍支持matrix协议、开源客户端聊天软件'
7 | ```
8 |
9 | #### download from release
10 | https://github.com/hktalent/ChatGPT-API/releases
11 |
12 | ```
13 | wget -c https://github.com/hktalent/ChatGPT-API/releases/download/0.0.3/ChatGPT_0.0.3_macOS_amd64.zip
14 | unzip -x ChatGPT_0.0.3_macOS_amd64.zip
15 | ./ChatGPT -i -k [your key]
16 |
17 | ```
18 |
19 |
20 |
21 |
22 | # Example
23 | ```go
24 | package main
25 |
26 | import (
27 | "bufio"
28 | "flag"
29 | "fmt"
30 | gtp "github.com/hktalent/ChatGPT-API"
31 | "os"
32 | "strings"
33 | )
34 |
35 | func doOne(q, k string) {
36 | if got, err := gtp.Completions(q, k); nil == err {
37 | fmt.Println(got)
38 | } else if nil != err {
39 | fmt.Println("gtp.Completions is err:", err)
40 | }
41 | }
42 |
43 | func main() {
44 | key := flag.String("k", "", "your ChatGPT-API token key")
45 | interact := flag.Bool("i", false, "interact")
46 | q := flag.String("q", "", "your question")
47 | flag.Parse()
48 | if *key != "" {
49 | if *interact {
50 | buf := bufio.NewScanner(os.Stdin)
51 | for buf.Scan() {
52 | s := buf.Text()
53 | x1 := strings.TrimSpace(strings.ToLower(s))
54 | if x1 == "exit" || x1 == "quit" || x1 == "q" || x1 == "x" {
55 | break
56 | }
57 | if "" != s {
58 | doOne(s, *key)
59 | }
60 | if nil != buf.Err() {
61 | break
62 | }
63 | }
64 | } else if "" != *q {
65 | doOne(*q, *key)
66 | }
67 | }
68 | }
69 |
70 |
71 | ```
72 |
--------------------------------------------------------------------------------
/cmd/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bufio"
5 | "flag"
6 | "fmt"
7 | gtp "github.com/hktalent/ChatGPT-API"
8 | "os"
9 | "strings"
10 | )
11 |
12 | func doOne(q, k string) {
13 | if got, err := gtp.Completions(q, k); nil == err {
14 | fmt.Println(got)
15 | } else if nil != err {
16 | fmt.Println("gtp.Completions is err:", err)
17 | }
18 | }
19 |
20 | func main() {
21 | key := flag.String("k", "", "your ChatGPT-API token key")
22 | interact := flag.Bool("i", false, "interact")
23 | q := flag.String("q", "", "your question")
24 | flag.Parse()
25 | if *key != "" {
26 | if *interact {
27 | buf := bufio.NewScanner(os.Stdin)
28 | for buf.Scan() {
29 | s := buf.Text()
30 | x1 := strings.TrimSpace(strings.ToLower(s))
31 | if x1 == "exit" || x1 == "quit" || x1 == "q" || x1 == "x" {
32 | break
33 | }
34 | if "" != s {
35 | doOne(s, *key)
36 | }
37 | if nil != buf.Err() {
38 | break
39 | }
40 | }
41 | } else if "" != *q {
42 | doOne(*q, *key)
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/hktalent/ChatGPT-API
2 |
3 | go 1.18
4 |
--------------------------------------------------------------------------------
/gtp.go:
--------------------------------------------------------------------------------
1 | package gtp
2 |
3 | import (
4 | "bytes"
5 | "encoding/json"
6 | "errors"
7 | "fmt"
8 | "io/ioutil"
9 | "log"
10 | "net/http"
11 | "strings"
12 | )
13 |
14 | // 这里 github上收集的一些key,经过测试都已经无法使用了
15 | var Keys = strings.Split(`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
16 | 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
17 | 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
18 | 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
19 | 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
20 | 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
21 | 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
22 | sk-uaVxaKNMvobxyRkramoIjtT3BlbkFJjEOjcT1gj3cG9C2CcQ5
23 | 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
24 | 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
25 | sk-kpHABG8aOsxSch5pA7pSLosxBImxjbb5SC1dnTU0ntNl17Nz
26 | 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
27 | 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
28 | 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
29 | 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
30 | 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
31 | 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
32 | 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
33 | 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
34 | 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
35 | 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
36 | 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
37 | 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
38 | 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
39 | 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
40 | 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`, "\n")
41 |
42 | const BASEURL = "https://api.openai.com/v1/"
43 |
44 | // ChatGPTResponseBody 请求体
45 | type ChatGPTResponseBody struct {
46 | ID string `json:"id"`
47 | Object string `json:"object"`
48 | Created int `json:"created"`
49 | Model string `json:"model"`
50 | Choices []ChoiceItem `json:"choices"`
51 | Usage map[string]interface{} `json:"usage"`
52 | }
53 |
54 | type ChoiceItem struct {
55 | Text string `json:"text"`
56 | Index int `json:"index"`
57 | Logprobs int `json:"logprobs"`
58 | FinishReason string `json:"finish_reason"`
59 | }
60 |
61 | // ChatGPTRequestBody 响应体
62 | type ChatGPTRequestBody struct {
63 | Model string `json:"model"`
64 | Prompt string `json:"prompt"`
65 | MaxTokens int `json:"max_tokens"`
66 | Temperature float32 `json:"temperature"`
67 | TopP int `json:"top_p"`
68 | FrequencyPenalty int `json:"frequency_penalty"`
69 | PresencePenalty int `json:"presence_penalty"`
70 | }
71 |
72 | // Completions gtp文本模型回复
73 | //curl https://api.openai.com/v1/completions
74 | //-H "Content-Type: application/json"
75 | //-H "Authorization: Bearer your chatGPT key"
76 | //-d '{"model": "text-davinci-003", "prompt": "give me good song", "temperature": 0, "max_tokens": 7}'
77 | func Completions(msg string, key string) (string, error) {
78 | requestBody := ChatGPTRequestBody{
79 | Model: "text-davinci-003",
80 | Prompt: msg,
81 | MaxTokens: 1024,
82 | Temperature: 0.7,
83 | TopP: 1,
84 | FrequencyPenalty: 0,
85 | PresencePenalty: 0,
86 | }
87 | requestData, err := json.Marshal(requestBody)
88 |
89 | if err != nil {
90 | return "", err
91 | }
92 | //log.Printf("request gtp json string : %v", string(requestData))
93 | req, err := http.NewRequest("POST", BASEURL+"completions", bytes.NewBuffer(requestData))
94 | if err != nil {
95 | return "", err
96 | }
97 |
98 | apiKey := key
99 | req.Header.Set("Content-Type", "application/json")
100 | req.Header.Set("Authorization", "Bearer "+apiKey)
101 | client := &http.Client{}
102 | response, err := client.Do(req)
103 | if err != nil {
104 | return "", err
105 | }
106 | defer response.Body.Close()
107 | if response.StatusCode != 200 {
108 | return "", errors.New(fmt.Sprintf("gtp api status code not equals 200,code is %d", response.StatusCode))
109 | }
110 | body, err := ioutil.ReadAll(response.Body)
111 | if err != nil {
112 | return "", err
113 | }
114 |
115 | gptResponseBody := &ChatGPTResponseBody{}
116 | //log.Println(string(body))
117 | err = json.Unmarshal(body, gptResponseBody)
118 | if err != nil {
119 | return "", err
120 | }
121 |
122 | var reply string
123 | if len(gptResponseBody.Choices) > 0 {
124 | reply = gptResponseBody.Choices[0].Text
125 | }
126 | log.Printf("gpt response text: %s \n", reply)
127 | return reply, nil
128 | }
129 |
--------------------------------------------------------------------------------
/gtp_test.go:
--------------------------------------------------------------------------------
1 | package gtp
2 |
3 | import (
4 | "log"
5 | "testing"
6 | )
7 |
8 | func TestCompletions(t *testing.T) {
9 | for _, k := range Keys {
10 | if got, err := Completions("从百度查询xx并保存为csv的python代码", k); nil == err {
11 |
12 | if "" != got {
13 | println(k)
14 | log.Println(got)
15 | break
16 | }
17 | }
18 |
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------