mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-01 09:07:27 +08:00
Merge pull request #606 from chaitin/fix-llm-proxy
fix: 修复 llm 代理的路径兼容问题
This commit is contained in:
3 files changed
+41
-16
No files matched your search
+4
-1
@@ -23,4 +23,7 @@ frontend/release/
|
||||
*.tgz
|
||||
|
||||
# Builder
|
||||
dist
|
||||
dist
|
||||
|
||||
# ai
|
||||
.superpowers/
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"net/url"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -147,26 +147,55 @@ func (p *Proxy) resolveModel(ctx context.Context, token string) (*modelContext,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var LLMAllowPaths []string = []string{
|
||||
"/v1/messages",
|
||||
"/chat/completions",
|
||||
"/responses",
|
||||
}
|
||||
|
||||
func fetchAllowPath(path string) string {
|
||||
for _, v := range LLMAllowPaths {
|
||||
if strings.HasSuffix(path, v) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (p *Proxy) rewrite(r *httputil.ProxyRequest) {
|
||||
path := r.In.URL.Path
|
||||
p.logger.With("path", path).DebugContext(r.In.Context(), "new rewrite request")
|
||||
|
||||
ctx, ok := r.In.Context().Value(contextKey{}).(*proxyContext)
|
||||
if !ok || ctx == nil || ctx.model == nil {
|
||||
p.logger.WarnContext(r.In.Context(), "missing model context")
|
||||
return
|
||||
}
|
||||
|
||||
uppath := fetchAllowPath(path)
|
||||
if uppath == "" {
|
||||
p.logger.With("path", path).WarnContext(r.In.Context(), "unsupport api type")
|
||||
return
|
||||
}
|
||||
|
||||
m := ctx.model
|
||||
baseURL, err := url.Parse(m.baseURL)
|
||||
ul, err := url.Parse(m.baseURL)
|
||||
if err != nil {
|
||||
p.logger.ErrorContext(r.In.Context(), "parse model base url failed", "base_url", m.baseURL, "error", err)
|
||||
return
|
||||
}
|
||||
r.Out.URL.Scheme = baseURL.Scheme
|
||||
r.Out.URL.Host = baseURL.Host
|
||||
r.Out.URL.Path = joinURLPath(baseURL.Path, ctx.upstreamPath)
|
||||
r.Out.URL.RawQuery = r.In.URL.RawQuery
|
||||
r.Out.Host = baseURL.Host
|
||||
r.Out.URL.Scheme = ul.Scheme
|
||||
r.Out.URL.Host = ul.Host
|
||||
r.Out.URL.Path = filepath.Join(ul.Path, uppath)
|
||||
r.Out.Header.Set("Authorization", "Bearer "+m.apiKey)
|
||||
r.Out.Header.Set("X-Api-Key", m.apiKey)
|
||||
r.SetXForwarded()
|
||||
r.Out.Host = ul.Host
|
||||
p.logger.With(
|
||||
"model", m.modelName,
|
||||
"in", r.In.URL.String(),
|
||||
"out", r.Out.URL.String(),
|
||||
).DebugContext(r.In.Context(), "rewrite request success")
|
||||
}
|
||||
|
||||
func (p *Proxy) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
|
||||
@@ -198,10 +227,3 @@ func readRequestModel(body []byte) (string, error) {
|
||||
}
|
||||
return payload.Model, nil
|
||||
}
|
||||
|
||||
func joinURLPath(basePath, requestPath string) string {
|
||||
if basePath == "" || basePath == "/" {
|
||||
return requestPath
|
||||
}
|
||||
return path.Join(basePath, requestPath)
|
||||
}
|
||||
@@ -26,7 +26,7 @@ POSTGRES_IMAGE="${POSTGRES_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/b
|
||||
REDIS_IMAGE="${REDIS_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/redis:8.0-alpine3.21}"
|
||||
CLICKHOUSE_IMAGE="${CLICKHOUSE_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/clickhouse-server:26.3.9}"
|
||||
RUSTFS_IMAGE="${RUSTFS_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/basic/rustfs:1.0.0-beta.2}"
|
||||
ORCHESTRATOR_IMAGE="${ORCHESTRATOR_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode/codingmatrix-orchestrator:alpha-latest}"
|
||||
ORCHESTRATOR_IMAGE="${ORCHESTRATOR_IMAGE:-chaitin-registry.cn-hangzhou.cr.aliyuncs.com/monkeycode/codingmatrix-orchestrator:main-1f7201de}"
|
||||
DEVBOX_IMAGE="${DEVBOX_IMAGE:-ghcr.io/chaitin/monkeycode-runner/devbox:latest}"
|
||||
HTTP_PROXY="${HTTP_PROXY:-${http_proxy:-}}"
|
||||
HTTPS_PROXY="${HTTPS_PROXY:-${https_proxy:-}}"
|
||||
|
||||
Reference in new issue
Block a user