fix: recognize '/v1' prefix

This commit is contained in:
2023-12-07 00:51:21 +08:00
parent 412aefdacc
commit b8ebbed5d6
2 changed files with 14 additions and 1 deletions

View File

@@ -91,7 +91,9 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
out.Host = remote.Host out.Host = remote.Host
out.URL.Scheme = remote.Scheme out.URL.Scheme = remote.Scheme
out.URL.Host = remote.Host 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 = http.Header{}
out.Header.Set("Host", remote.Host) out.Header.Set("Host", remote.Host)
if upstream.SK == "asis" { if upstream.SK == "asis" {

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"net/url"
"os" "os"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -19,6 +20,7 @@ type OPENAI_UPSTREAM struct {
SK string `yaml:"sk"` SK string `yaml:"sk"`
Endpoint string `yaml:"endpoint"` Endpoint string `yaml:"endpoint"`
Timeout int64 `yaml:"timeout"` Timeout int64 `yaml:"timeout"`
URL *url.URL
} }
func readConfig(filepath string) Config { func readConfig(filepath string) Config {
@@ -50,5 +52,14 @@ func readConfig(filepath string) Config {
config.DBAddr = "./db.sqlite" 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 return config
} }