Limour

Limour

临床医学在读。

【記錄】Go 實現 OpenAI API HTTP 代理

代碼參考自 GO-OPENAI-PROXY,由 GPT-3.5 輔助修改。

優點#

  • 支持多 key 輪詢,且 key 對前端透明
  • 可以對 messages 進行自定義修改
  • 不受網絡環境影響

編譯反代程序#

package main

import (
    "io"
    "io/ioutil"
    "log"
    "net/http"
    "net/url"
    "strings"
    "bytes"
    "encoding/json"
    "time"
    "sync"
)

var (
    target    = "https://api.openai.com" // 目標域名
    mu sync.Mutex
    count int
)

func main() {
    http.HandleFunc("/", handleRequest)
    http.ListenAndServe(":9000", nil)
}

func get1Key(key string) string {
    mu.Lock()
    defer mu.Unlock()

    arr := strings.Split(key, "|")
    randomIndex := count % len(arr)
    count++
    if count > 999999 {
        count = 0
    }
    randomSubstr := arr[randomIndex]
    log.Println("Authorization", randomSubstr)
    return randomSubstr
}

// 獲取給定請求主體的json解碼器
func requestBodyDecoder(request *http.Request) *json.Decoder {
    // 讀取主體到緩衝區
    body, err := ioutil.ReadAll(request.Body)
    if err != nil {
        log.Printf("Error reading body: %v", err)
        panic(err)
    }

    // 因為go lang如果讀取了主體,則任何後續調用都無法再次讀取主體....
    // request.Body = ioutil.NopCloser(bytes.NewBuffer(body))

    return json.NewDecoder(ioutil.NopCloser(bytes.NewBuffer(body)))
}

// 在需要設置響應頭的地方調用setResponseHeader函數即可
func setResponseHeader(w http.ResponseWriter) {
    w.Header().Set("Access-Control-Allow-Methods", "GET,HEAD,POST,OPTIONS")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Max-Age", "86400")
    w.Header().Set("Access-Control-Allow-Headers", "authorization,content-type")
}

func handleBody(r *http.Request) io.ReadCloser {
    // 讀取請求體
    decoder := requestBodyDecoder(r)
    // 解析JSON數據
    var requestData map[string]interface{}
    err := decoder.Decode(&requestData)
    if err != nil {
        // 處理解析JSON數據異常
        log.Printf("Error decoding body: %v", err)
        panic(err)
    }
    // 獲取"messages"列表
    messages, ok := requestData["messages"].([]interface{})
    if !ok {
        // "messages"字段不存在或類型不正確
        log.Printf("Error reading messages: %v", ok)
        panic(ok)
    }
    // 按需修改"messages"列表
    // log.Println("debug 5", len(messages), len(messages) > 4)
    if len(messages) > 4 {
        firstMessage, ok := messages[0].(map[string]interface{})
        if !ok {
            // 第一个消息類型不正確
            log.Printf("Error reading firstMessage: %v", ok)
            panic(ok)
        }

        role, roleOk := firstMessage["role"].(string)
        // log.Println("debug 6", roleOk, role, role == "system", strings.EqualFold(role, "system"))
        if roleOk && strings.EqualFold(role, "system") {
            // 將第一個消息移動到倒數第三個位置
            thirdToLastIndex := len(messages) - 3
            messages_copy := make([]interface{}, 0)
            messages_copy = append(messages_copy, messages[0])
            messages_copy = append(messages_copy, messages[thirdToLastIndex:]...)
            messages = append(messages[1:thirdToLastIndex], messages_copy...)
            
            // 更新"messages"列表
            requestData["messages"] = messages
            log.Println("move system role to ", thirdToLastIndex)
        }
    }
    // 將更新後的數據編碼為JSON
    var buf bytes.Buffer
    encoder := json.NewEncoder(&buf)
    err = encoder.Encode(requestData)
    if err != nil {
        // 處理編碼JSON數據異常
        log.Printf("Error encoding body: %v", err)
        panic(err)
    }

    return ioutil.NopCloser(&buf)
}

func handleRequest(w http.ResponseWriter, r *http.Request) {
    // 過濾無效URL
    _, err := url.Parse(r.URL.String())
    if err != nil {
        log.Println("Error parsing URL: ", err.Error())
        http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
        return
    }

    // 去掉環境前綴(針對騰訊雲,如果包含的話,目前我只用到了test和release)
    newPath := strings.Replace(r.URL.Path, "/release", "", 1)
    newPath = strings.Replace(newPath, "/test", "", 1)

    // 拼接目標URL
    targetURL := target + newPath

    // 創建代理HTTP請求
    var proxyReq *http.Request
    // log.Println("debug 1", targetURL, r.Method)
    // log.Println("debug 2", strings.Contains(targetURL, "chat/completions"))
    // log.Println("debug 3", strings.EqualFold(r.Method, "POST"))
    if strings.Contains(targetURL, "chat/completions") && strings.EqualFold(r.Method, "POST")  {
        // log.Println("debug 4-0")
        proxyReq, err = http.NewRequest(r.Method, targetURL, handleBody(r))
    } else {
        // log.Println("debug 4-1")
        proxyReq, err = http.NewRequest(r.Method, targetURL, r.Body)
    }

    if err != nil {
        log.Println("Error creating proxy request: ", err.Error())
        http.Error(w, "Error creating proxy request", http.StatusInternalServerError)
        return
    }

    // 將原始請求頭複製到新請求中
    keys := strings.Split(r.Header.Get("Authorization"), " ")
    if len(keys) == 2 {
           r.Header.Set("Authorization", "Bearer " + get1Key(keys[1]))
    }
    if _, ok := r.Header["User-Agent"]; !ok {
        // 明確禁用User-Agent,以便不設置為默認值
        r.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36")
    }
    proxyReq.Header = http.Header{
        "Content-Type":  []string{"application/json"},
        "Authorization": []string{r.Header.Get("Authorization")},
        "User-Agent":    []string{r.Header.Get("User-Agent")},
    }

    
    // 默認超時時間設置為60s
    client := &http.Client{
        Timeout: 60 * time.Second,
    }

    // 向 OpenAI 發起代理請求
    resp, err := client.Do(proxyReq)
    if err != nil {
        log.Println("Error sending proxy request: ", err.Error())
        http.Error(w, err.Error(), http.StatusInternalServerError)
        return
    }
    defer resp.Body.Close()

    // 設置響應頭
    setResponseHeader(w)
    
    // 將響應狀態碼設置為原始響應狀態碼
    w.WriteHeader(resp.StatusCode)

    // 將響應實體寫入到響應流中(支持流式響應)
    buf := make([]byte, 1024)
    for {
        if n, err := resp.Body.Read(buf); err == io.EOF || n == 0 {
            return
        } else if err != nil {
            log.Println("error while reading respbody: ", err.Error())
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        } else {
            if _, err = w.Write(buf[:n]); err != nil {
                log.Println("error while writing resp: ", err.Error())
                http.Error(w, err.Error(), http.StatusInternalServerError)
                return
            }
            w.(http.Flusher).Flush()
        }
    }
}
CC=musl-gcc /home/jovyan/go/bin/go1.20.1 build -tags musl -o openai -trimpath -ldflags '-linkmode "external" -extldflags "-static" -s -w -buildid=' ./openai-proxy.go

docker 部署#

mkdir -p ~/app/apio && cd ~/app/apio && nano docker-compose.yml
chmod 777 openai
sudo docker-compose up -d && sudo docker-compose logs
version: '3.3'
services:
    apio:
        restart: always
        image: alpine:latest
        volumes:
          - ./openai:/bin/openai
        entrypoint: ["/bin/openai"]
 
networks:
  default:
    external: true
    name: ngpm

Nginx Proxy Manager 反代#

image

前端測試#

  • BetterChatGPT
  • API 端點 填入https://yourdomain/token/v1/chat/completions
  • API 密鑰 隨便填
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。