|
| 1 | +package llm |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "io" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "net/http/httputil" |
| 10 | + "net/url" |
| 11 | + "path" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/gptscript-ai/gptscript/pkg/builtin" |
| 15 | + "github.com/gptscript-ai/gptscript/pkg/openai" |
| 16 | +) |
| 17 | + |
| 18 | +func (r *Registry) ProxyInfo() (string, string, error) { |
| 19 | + r.proxyLock.Lock() |
| 20 | + defer r.proxyLock.Unlock() |
| 21 | + |
| 22 | + if r.proxyURL != "" { |
| 23 | + return r.proxyToken, r.proxyURL, nil |
| 24 | + } |
| 25 | + |
| 26 | + l, err := net.Listen("tcp", "127.0.0.1:0") |
| 27 | + if err != nil { |
| 28 | + return "", "", err |
| 29 | + } |
| 30 | + |
| 31 | + go func() { |
| 32 | + _ = http.Serve(l, r) |
| 33 | + r.proxyLock.Lock() |
| 34 | + defer r.proxyLock.Unlock() |
| 35 | + _ = l.Close() |
| 36 | + r.proxyURL = "" |
| 37 | + }() |
| 38 | + |
| 39 | + r.proxyURL = "http://" + l.Addr().String() |
| 40 | + return r.proxyToken, r.proxyURL, nil |
| 41 | +} |
| 42 | + |
| 43 | +func (r *Registry) ServeHTTP(w http.ResponseWriter, req *http.Request) { |
| 44 | + if r.proxyToken != strings.TrimPrefix(req.Header.Get("Authorization"), "Bearer ") { |
| 45 | + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + inBytes, err := io.ReadAll(req.Body) |
| 50 | + if err != nil { |
| 51 | + http.Error(w, err.Error(), http.StatusBadRequest) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + var ( |
| 56 | + model string |
| 57 | + data = map[string]any{} |
| 58 | + ) |
| 59 | + |
| 60 | + if json.Unmarshal(inBytes, &data) == nil { |
| 61 | + model, _ = data["model"].(string) |
| 62 | + } |
| 63 | + |
| 64 | + if model == "" { |
| 65 | + model = builtin.GetDefaultModel() |
| 66 | + } |
| 67 | + |
| 68 | + c, err := r.getClient(req.Context(), model) |
| 69 | + if err != nil { |
| 70 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + oai, ok := c.(*openai.Client) |
| 75 | + if !ok { |
| 76 | + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + auth, targetURL := oai.ProxyInfo() |
| 81 | + if targetURL == "" { |
| 82 | + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + newURL, err := url.Parse(targetURL) |
| 87 | + if err != nil { |
| 88 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + newURL.Path = path.Join(newURL.Path, req.URL.Path) |
| 93 | + |
| 94 | + rp := httputil.ReverseProxy{ |
| 95 | + Director: func(proxyReq *http.Request) { |
| 96 | + proxyReq.Body = io.NopCloser(bytes.NewReader(inBytes)) |
| 97 | + proxyReq.URL = newURL |
| 98 | + proxyReq.Header.Del("Authorization") |
| 99 | + proxyReq.Header.Add("Authorization", "Bearer "+auth) |
| 100 | + proxyReq.Host = newURL.Hostname() |
| 101 | + }, |
| 102 | + } |
| 103 | + rp.ServeHTTP(w, req) |
| 104 | +} |
0 commit comments