From b8ebbed5d69f1da975761fa7018d43b57a549fcb Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 7 Dec 2023 00:51:21 +0800 Subject: [PATCH] fix: recognize '/v1' prefix --- process.go | 4 +++- structure.go | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/process.go b/process.go index f19f540..1f8377f 100644 --- a/process.go +++ b/process.go @@ -91,7 +91,9 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s out.Host = remote.Host out.URL.Scheme = remote.Scheme out.URL.Host = remote.Host - out.URL.Path = in.URL.Path + + out.URL.Path = upstream.URL.Path + strings.TrimPrefix(in.URL.Path, "/v1") + out.Header = http.Header{} out.Header.Set("Host", remote.Host) if upstream.SK == "asis" { diff --git a/structure.go b/structure.go index 6f48bae..3d5b649 100644 --- a/structure.go +++ b/structure.go @@ -2,6 +2,7 @@ package main import ( "log" + "net/url" "os" "gopkg.in/yaml.v3" @@ -19,6 +20,7 @@ type OPENAI_UPSTREAM struct { SK string `yaml:"sk"` Endpoint string `yaml:"endpoint"` Timeout int64 `yaml:"timeout"` + URL *url.URL } func readConfig(filepath string) Config { @@ -50,5 +52,14 @@ func readConfig(filepath string) Config { config.DBAddr = "./db.sqlite" } + for i, upstream := range config.Upstreams { + // parse upstream endpoint URL + endpoint, err := url.Parse(upstream.Endpoint) + if err != nil { + log.Fatalf("Can't parse upstream endpoint URL '%s': %s", upstream.Endpoint, err) + } + config.Upstreams[i].URL = endpoint + } + return config }