Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
madoka committed Apr 2, 2024
0 parents commit 87b6f39
Show file tree
Hide file tree
Showing 25 changed files with 2,184 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Go Build

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- goos: windows
goarch: 386
- goos: windows
goarch: amd64
- goos: windows
goarch: arm64
- goos: linux
goarch: 386
- goos: linux
goarch: amd64
- goos: linux
goarch: arm64
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: Build
run: |
echo "Building for ${{ matrix.goos }} ${{ matrix.goarch }}"
suffix=""
if [ "${{ matrix.goos }}" == "windows" ]; then
suffix=".exe"
fi
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o ./build/freechatgpt-${{ matrix.goos }}-${{ matrix.goarch }}$suffix
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: freechatgpt-${{ matrix.goos }}-${{ matrix.goarch }}
path: ./build/freechatgpt-${{ matrix.goos }}-${{ matrix.goarch }}*
39 changes: 39 additions & 0 deletions .github/workflows/build_docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build_docker

on:
release:
types: [created] # 表示在创建新的 Release 时触发

jobs:
build_docker:
name: Build docker
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- run: |
echo "本次构建的版本为:${GITHUB_REF_NAME} (但是这个变量目前上下文中无法获取到)"
echo 本次构建的版本为:${{ github.ref_name }}
env
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v4
with:
context: .
push: true
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
tags: |
${{ secrets.DOCKERHUB_USERNAME }}/chatgpt-to-api:${{ github.ref_name }}
${{ secrets.DOCKERHUB_USERNAME }}/chatgpt-to-api:latest
43 changes: 43 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Release Workflow
on:
release:
types:
- created
permissions:
contents: write

jobs:

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19

- name: Build
run: go build -o bin/ .

- name: recursively list files
run: ls -R

- name: Get existing release body
id: get_release_body
run: |
echo "::set-output name=body::$(curl -s -H 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' https://api.github.com/repos/${{ github.repository }}/releases/tags/${{ github.ref_path }} | jq -r '.body')"
- name: Upload release artifact
uses: svenstaro/upload-release-action@v2
with:
file: bin/*
file_glob: true
tag: ${{ github.ref }}
body: |
${{ steps.get_release_body.outputs.body }}
repo_token: ${{ secrets.GITHUB_TOKEN }}
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tools/authenticator/100-ACCOUNTS_COMPILED.txt
tools/authenticator/accounts.txt
tools/authenticator/proxies.txt
tools/authenticator/authenticated_accounts.txt
tools/authenticator/access_tokens.txt
*.txt
*.json
freechatgpt
chatgpttoapi
tools/authenticator/.proxies.txt.swp
.env
*.har
.idea/
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Use the official Golang image as the builder
FROM golang:1.20.3-alpine as builder

# Enable CGO to use C libraries (set to 0 to disable it)
# We set it to 0 to build a fully static binary for our final image
ENV CGO_ENABLED=0

# Set the working directory
WORKDIR /app

# Copy the Go Modules manifests (go.mod and go.sum files)
COPY go.mod go.sum ./

# Download the dependencies
RUN go mod download

# Copy the source code
COPY . .

# Build the Go application and output the binary to /app/ChatGPT-Proxy-V4
RUN go build -o /app/ChatGPT-To-API .

# Use a scratch image as the final distroless image
FROM scratch

# Set the working directory
WORKDIR /app

# Copy the built Go binary from the builder stage
COPY --from=builder /app/ChatGPT-To-API /app/ChatGPT-To-API

# Expose the port where the application is running
EXPOSE 8080

# Start the application
CMD [ "./ChatGPT-To-API" ]
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# ChatGPT-to-API
Create a fake API using ChatGPT's website

> ## IMPORTANT
> You will not get free support for this repository. This was made for my own personal use and documentation will continue to be limited as I don't really need documentation. You will find more detailed documentation in the Chinese docs by a contributor.
**API endpoint: http://127.0.0.1:8080/v1/chat/completions.**

[中文文档(Chinese Docs)](https://github.com/xqdoo00o/ChatGPT-to-API/blob/master/README_ZH.md)
## Setup

### Authentication

Access token and PUID(only for PLUS account) retrieval has been automated by [OpenAIAuth](https://github.com/xqdoo00o/OpenAIAuth/) with account email & password.

`accounts.txt` - A list of accounts separated by new line

Format:
```
email:password
...
```

All authenticated access tokens and PUID will store in `access_tokens.json`

Auto renew access tokens and PUID after 7 days

Caution! please use unblocked ip for authentication, first login to `https://chat.openai.com/` to check ip availability if you can.

### HAR file pool

Currently logged in account, using the GPT-4 model and most GPT-3.5 models, you need to configure a HAR file (file with .har suffix) to complete captcha verification.

1. Use a chromium-based browser (Chrome, Edge) to open the browser developer tools (F12), switch to the Network tab, and check the **preserve log** option.

2. Log in to `https://chat.openai.com/`, create a new chat and select the GPT-4 model, enter any text, switch to the GPT-3.5 model, and enter any text.

3. Click the Export HAR button under the Network tab to export the file `chat.openai.com.har` and place it in the `harPool` folder of the same level as this program.

### API Authentication (Optional)

Custom API keys for this fake API, just like OpenAI api

`api_keys.txt` - A list of API keys separated by new line

Format:
```
sk-123456
88888888
...
```

## Getting set up
```
git clone https://github.com/xqdoo00o/ChatGPT-to-API
cd ChatGPT-to-API
go build
./freechatgpt
```

### Environment variables
- `SERVER_HOST` - Set to 127.0.0.1 by default
- `SERVER_PORT` - Set to 8080 by default
- `ENABLE_HISTORY` - Set to false by default

### Files (Optional)
- `proxies.txt` - A list of proxies separated by new line

```
http://127.0.0.1:8888
...
```
- `access_tokens.json` - A JSON array of access tokens for cycling (Alternatively, send a PATCH request to the [correct endpoint](https://github.com/xqdoo00o/ChatGPT-to-API/blob/master/docs/admin.md))
```
{"account1":{token:"access_token1", puid:"puid1"}, "account2":{token:"access_token2", puid:"puid2"}...}
```
- `cookies.json` - A JSON that stores login cookies. If the OpenAI account is logged in with a third party such as Google, you can add a third-party account (also suitable for first-party account) and any password in `accounts.txt`. Modify this file as follows to login account.
```
{
"third party username": [
{
"Name": "__Secure-next-auth.session-token",
"Value": "After logging into a third-party account on browser,the value of __Secure-next-auth.session-token in cookies",
"Path": "/",
"Domain": "",
"Expires": "0001-01-01T00:00:00Z",
"MaxAge": 0,
"Secure": true,
"HttpOnly": true,
"SameSite": 2,
"Unparsed": null
}
]
}
```
## Admin API docs
https://github.com/xqdoo00o/ChatGPT-to-API/blob/master/docs/admin.md
## API usage docs
https://platform.openai.com/docs/api-reference/chat
Loading

0 comments on commit 87b6f39

Please sign in to comment.