├── .github └── workflows │ ├── docker-build.yaml │ └── release.yml ├── .gitignore ├── Data └── placeholder ├── Dockerfile ├── LICENSE ├── README.md ├── docker-compose.yml ├── go.mod ├── go.sum ├── handlers ├── admin.go ├── api.go ├── client.go └── init.go ├── main.go ├── types └── types.go └── utils ├── auth.go └── utils.go /.github/workflows/docker-build.yaml: -------------------------------------------------------------------------------- 1 | name: docker build 2 | 3 | on: 4 | push: 5 | branches: 6 | - "**" 7 | tags: 8 | - "**" 9 | pull_request_target: 10 | 11 | permissions: 12 | contents: read 13 | packages: write 14 | 15 | jobs: 16 | docker: 17 | name: Publish Docker image 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@v2 22 | - name: Docker meta 23 | id: docker_meta 24 | uses: docker/metadata-action@v3.2.0 25 | with: 26 | images: ghcr.io/${{ github.repository }} 27 | flavor: | 28 | latest=false 29 | tags: | 30 | type=ref,event=pr 31 | type=ref,event=branch 32 | type=sha,prefix=,format=long,event=branch 33 | type=ref,event=tag 34 | type=sha,prefix=,format=long,event=tag 35 | type=raw,value=latest,enable=${{ endsWith(github.ref, github.event.repository.default_branch) }} 36 | - name: Set up Docker Buildx 37 | uses: docker/setup-buildx-action@v1 38 | - name: Login to GitHub Container Registry 39 | uses: docker/login-action@v1 40 | with: 41 | registry: ghcr.io 42 | username: ${{ github.repository_owner }} 43 | password: ${{ secrets.GITHUB_TOKEN }} 44 | - name: Cache Docker layers 45 | uses: actions/cache@v2.1.5 46 | with: 47 | path: /tmp/.buildx-cache 48 | key: ${{ runner.os }}-buildx-${{ github.sha }} 49 | restore-keys: | 50 | ${{ runner.os }}-buildx- 51 | - name: Build and push 52 | uses: docker/build-push-action@v2 53 | with: 54 | context: . 55 | push: true 56 | tags: ${{ steps.docker_meta.outputs.tags }} 57 | labels: ${{ steps.docker_meta.outputs.labels }} 58 | cache-from: type=local,src=/tmp/.buildx-cache 59 | cache-to: type=local,dest=/tmp/.buildx-cache,mode=max 60 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release Go Binaries 2 | 3 | on: 4 | release: 5 | types: [created] 6 | workflow_dispatch: 7 | env: 8 | CMD_PATH: ./ 9 | 10 | 11 | jobs: 12 | releases-matrix: 13 | name: Release Matrix 14 | runs-on: ubuntu-latest 15 | strategy: 16 | matrix: 17 | goos: [linux, windows, darwin] 18 | goarch: ["386", amd64,arm64] 19 | exclude: 20 | - goarch: "386" 21 | goos: darwin 22 | steps: 23 | - uses: actions/checkout@v2 24 | 25 | - name: Set APP_VERSION env 26 | run: echo APP_VERSION=$(echo ${GITHUB_REF} | rev | cut -d'/' -f 1 | rev ) >> ${GITHUB_ENV} 27 | - name: Set BUILD_TIME env 28 | run: echo BUILD_TIME=$(date) >> ${GITHUB_ENV} 29 | - name: Environment Printer 30 | uses: managedkaos/print-env@v1.0 31 | 32 | - uses: wangyoucao577/go-release-action@v1.34 33 | with: 34 | github_token: ${{ secrets.ACTIONS_TOKEN }} 35 | goos: ${{ matrix.goos }} 36 | goarch: ${{ matrix.goarch }} 37 | project_path: "${{ env.CMD_PATH }}" 38 | build_flags: -v 39 | ldflags: -X "main.appVersion=${{ env.APP_VERSION }}" -X "main.buildTime=${{ env.BUILD_TIME }}" -X main.gitCommit=${{ github.sha }} -X main.gitRef=${{ github.ref }} 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | Data/auth.db 17 | -------------------------------------------------------------------------------- /Data/placeholder: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/acheong08/ChatGPT-API-server/499e268e33e1f59f6e155ab4732a1f2a61dfca07/Data/placeholder -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.19-alpine AS build 2 | 3 | RUN apk add --no-cache git 4 | RUN apk add gcc build-base 5 | 6 | RUN mkdir -p /go/src/ChatGPT-API-server 7 | WORKDIR /go/src/ChatGPT-API-server 8 | 9 | RUN git clone https://github.com/ChatGPT-Hackers/ChatGPT-API-server/ . 10 | RUN go install . 11 | 12 | FROM alpine:latest 13 | COPY --from=build /go/bin/ChatGPT-API-server /usr/local/bin/ 14 | 15 | RUN apk add --no-cache curl 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Antonio Cheong 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > # Official API released by OpenAI. Please use that instead. The model name is `text-chat-davinci-002-20230126` 2 | 3 | # ChatGPT API Server 4 | [![Release Go Binaries](https://github.com/acheong08/ChatGPT-API-server/actions/workflows/release.yml/badge.svg)](https://github.com/acheong08/ChatGPT-API-server/actions/workflows/release.yml) 5 | # Quickstart 6 | 7 | ## Setup 8 | 9 | 1. Install Go 10 | 2. `go install github.com/acheong08/ChatGPT-API-server@latest` 11 | 12 | If the latest commit fails, try using one of the release binaries 13 | 14 | # Build 15 | 16 | 1. `git clone https://github.com/acheong08/ChatGPT-API-server/` 17 | 2. `cd ChatGPT-API-server` 18 | 3. `go install .` 19 | 20 | # Usage 21 | 22 | `ChatGPT-API-server ` 23 | 24 | The admin key can be anything you want. It's just for authenticating yourself. 25 | 26 | # Connect agents 27 | 28 | Take note of your IP address or domain name. This could be `localhost` or a remote IP address. The default port is `8080` 29 | 30 | Check out our [firefox agent](https://github.com/acheong08/ChatGPT-API-agent). More versions in the works. 31 | 32 | There is also a [Python based client](https://github.com/ahmetkca/chatgpt-unofficial-api-docker/tree/ChatGPT-API-agent) by @ahmetkca (WIP) 33 | 34 | # Usage 35 | 36 | ## Quickstart 37 | 38 | (After connecting agents) 39 | 40 | ```bash 41 | $ curl "http://localhost:8080/api/ask" -X POST --header 'Authorization: ' -d '{"content": "Hello world", "conversation_id": "", "parent_id": ""}' 42 | ``` 43 | Note: if you want to use `conversation_id`, you also need to use `parent_id`! 44 | 45 | ## Routes 46 | 47 | ```go 48 | router.GET("/client/register", handlers.Client_register) // Used by agent 49 | router.POST("/api/ask", handlers.API_ask) // For making ChatGPT requests 50 | router.GET("/api/connections", handlers.API_getConnections) // For debugging 51 | router.POST("/admin/users/add", handlers.Admin_userAdd) // Adds an API token 52 | router.POST("/admin/users/delete", handlers.Admin_userDel) // Invalidates a token (based on user_id) 53 | router.GET("/admin/users", handlers.Admin_usersGet) // Get all users 54 | ``` 55 | 56 | ### Parameters for each route 57 | 58 | #### /client/register (GET) 59 | 60 | N/A. Used for websocket 61 | 62 | #### /api/ask (POST) 63 | 64 | Headers: `Authorization: ` 65 | 66 | _The user token can be set by the admin via /admin/users/add. You can also use the api key as the token. Both work by default_ 67 | 68 | Data: 69 | 70 | ```json 71 | { 72 | "content": "Hello world", 73 | "conversation_id": "", 74 | "parent_id": "" 75 | } 76 | ``` 77 | 78 | Do not enter conversation or parent id if not available. 79 | If you want to use either of these, you need to specify both! i.e. `request.parent_id=response.response_id` and `request.conversation_id=response.conversation_id` 80 | 81 | Response: 82 | 83 | ```json 84 | { 85 | "id": "", 86 | "response_id": "", 87 | "conversation_id": "", 88 | "content": "", 89 | "error": "" 90 | } 91 | ``` 92 | 93 | #### /api/connections (GET) 94 | 95 | Headers: None 96 | 97 | Data: None 98 | 99 | Response: 100 | 101 | ```json 102 | { 103 | "connections": [ 104 | { 105 | "Ws": {}, 106 | "Id": "", 107 | "Heartbeat": "