Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit667374f

Browse files
committed
proxying to providers
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
1 parent5ca9357 commit667374f

File tree

2 files changed

+72
-137
lines changed

2 files changed

+72
-137
lines changed

‎aibridged/bridge.go

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,105 @@ import (
44
"fmt"
55
"net"
66
"net/http"
7+
"net/http/httputil"
8+
"net/url"
9+
"os"
10+
"strings"
711

812
"golang.org/x/xerrors"
913
)
1014

1115
typeBridgestruct {
12-
srv*http.Server
13-
addrstring
16+
httpSrv*http.Server
17+
addrstring
1418
}
1519

1620
funcNewBridge(addrstring)*Bridge {
1721
mux:=&http.ServeMux{}
18-
mux.HandleFunc("/v1/chat/completions",bridgeOpenAIRequest)
19-
mux.HandleFunc("/v1/messages",bridgeAnthropicRequest)
22+
mux.HandleFunc("/v1/chat/completions",proxyOpenAIRequest)
23+
mux.HandleFunc("/v1/messages",proxyAnthropicRequest)
2024

2125
srv:=&http.Server{
2226
Addr:addr,
2327
Handler:mux,
2428
// TODO: other settings.
2529
}
2630

27-
return&Bridge{srv:srv}
31+
return&Bridge{httpSrv:srv}
2832
}
2933

30-
func(b*Bridge)Serve()error {
31-
list,err:=net.Listen("tcp",b.srv.Addr)
34+
funcproxyOpenAIRequest(w http.ResponseWriter,r*http.Request) {
35+
target,err:=url.Parse("https://api.openai.com")
3236
iferr!=nil {
33-
returnxerrors.Errorf("listen: %w",err)
37+
http.Error(w,"failed to parse OpenAI URL",http.StatusInternalServerError)
38+
return
3439
}
3540

36-
b.addr=list.Addr().String()
41+
proxy:=httputil.NewSingleHostReverseProxy(target)
42+
originalDirector:=proxy.Director
43+
proxy.Director=func(req*http.Request) {
44+
originalDirector(req)
3745

38-
returnb.srv.Serve(list)// TODO: TLS.
39-
}
46+
// Add OpenAI-specific headers
47+
ifstrings.TrimSpace(req.Header.Get("Authorization"))=="" {
48+
req.Header.Set("Authorization",fmt.Sprintf("Bearer %s",os.Getenv("OPENAI_API_KEY")))
49+
}
4050

41-
func (b*Bridge)Addr()string {
42-
returnb.addr
43-
}
51+
ifreq.Header.Get("Content-Type")=="" {
52+
req.Header.Set("Content-Type","application/json")
53+
}
4454

45-
funcbridgeOpenAIRequest(w http.ResponseWriter,r*http.Request) {
46-
fmt.Println("OpenAI")
55+
req.Host=target.Host
56+
req.URL.Scheme=target.Scheme
57+
req.URL.Host=target.Host
58+
59+
fmt.Printf("Proxying %s request to: %s\n",req.Method,req.URL.String())
60+
}
61+
proxy.ServeHTTP(w,r)
4762
}
4863

49-
funcbridgeAnthropicRequest(w http.ResponseWriter,r*http.Request) {
50-
fmt.Println("Anthropic")
64+
funcproxyAnthropicRequest(w http.ResponseWriter,r*http.Request) {
65+
target,err:=url.Parse("https://api.anthropic.com")
66+
iferr!=nil {
67+
http.Error(w,"failed to parse Anthropic URL",http.StatusInternalServerError)
68+
return
69+
}
70+
71+
proxy:=httputil.NewSingleHostReverseProxy(target)
72+
originalDirector:=proxy.Director
73+
proxy.Director=func(req*http.Request) {
74+
originalDirector(req)
75+
76+
// Add Anthropic-specific headers
77+
ifstrings.TrimSpace(req.Header.Get("x-api-key"))=="" {
78+
req.Header.Set("x-api-key",os.Getenv("ANTHROPIC_API_KEY"))
79+
}
80+
req.Header.Set("anthropic-version","2023-06-01")
81+
82+
ifreq.Header.Get("Content-Type")=="" {
83+
req.Header.Set("Content-Type","application/json")
84+
}
85+
86+
req.Host=target.Host
87+
req.URL.Scheme=target.Scheme
88+
req.URL.Host=target.Host
89+
90+
fmt.Printf("Proxying %s request to: %s\n",req.Method,req.URL.String())
91+
}
92+
proxy.ServeHTTP(w,r)
5193
}
5294

53-
funcbridgeRequest(w http.ResponseWriter,r*http.Request) {
95+
func (b*Bridge)Serve()error {
96+
list,err:=net.Listen("tcp",b.httpSrv.Addr)
97+
iferr!=nil {
98+
returnxerrors.Errorf("listen: %w",err)
99+
}
54100

101+
b.addr=list.Addr().String()
102+
103+
returnb.httpSrv.Serve(list)// TODO: TLS.
104+
}
105+
106+
func (b*Bridge)Addr()string {
107+
returnb.addr
55108
}

‎aibridged/http_proxy.go

Lines changed: 0 additions & 118 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp