forked from labring/aiproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadaptor.go
More file actions
149 lines (122 loc) · 3.59 KB
/
adaptor.go
File metadata and controls
149 lines (122 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package antling
import (
"fmt"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
"github.com/labring/aiproxy/core/model"
"github.com/labring/aiproxy/core/relay/adaptor"
"github.com/labring/aiproxy/core/relay/adaptor/anthropic"
"github.com/labring/aiproxy/core/relay/adaptor/openai"
"github.com/labring/aiproxy/core/relay/adaptor/registry"
"github.com/labring/aiproxy/core/relay/meta"
"github.com/labring/aiproxy/core/relay/mode"
"github.com/labring/aiproxy/core/relay/utils"
)
var _ adaptor.Adaptor = (*Adaptor)(nil)
type Adaptor struct {
openai.Adaptor
}
func init() {
registry.Register(model.ChannelTypeAntLing, &Adaptor{})
}
const baseURL = "https://api.tbox.cn/api"
const (
chatCompletionsPath = "/llm/v1/chat/completions"
anthropicMessagesPath = "/anthropic/v1/messages"
)
func (a *Adaptor) DefaultBaseURL() string {
return baseURL
}
func (a *Adaptor) SupportMode(mt *meta.Meta) bool {
m := adaptor.ModeFromMeta(mt)
return m == mode.ChatCompletions ||
m == mode.Anthropic ||
m == mode.Gemini
}
func (a *Adaptor) GetRequestURL(
meta *meta.Meta,
_ adaptor.Store,
c *gin.Context,
) (adaptor.RequestURL, error) {
var endpointPath string
switch meta.Mode {
case mode.ChatCompletions, mode.Gemini:
endpointPath = chatCompletionsPath
case mode.Anthropic:
endpointPath = anthropicMessagesPath
default:
return adaptor.RequestURL{}, fmt.Errorf("unsupported mode: %s", meta.Mode)
}
targetURL, err := url.JoinPath(meta.Channel.BaseURL, endpointPath)
if err != nil {
return adaptor.RequestURL{}, err
}
if meta.Mode == mode.Anthropic && c != nil {
if beta := c.Query("beta"); beta != "" {
parsedURL, err := url.Parse(targetURL)
if err != nil {
return adaptor.RequestURL{}, err
}
queryValues := parsedURL.Query()
queryValues.Set("beta", beta)
parsedURL.RawQuery = queryValues.Encode()
targetURL = parsedURL.String()
}
}
return adaptor.RequestURL{
Method: http.MethodPost,
URL: targetURL,
}, nil
}
func (a *Adaptor) SetupRequestHeader(
meta *meta.Meta,
store adaptor.Store,
c *gin.Context,
req *http.Request,
) error {
if meta.Mode != mode.Anthropic {
return a.Adaptor.SetupRequestHeader(meta, store, c, req)
}
req.Header.Set("Authorization", "Bearer "+meta.Channel.Key)
req.Header.Set(anthropic.AnthropicTokenHeader, meta.Channel.Key)
anthropicVersion := c.Request.Header.Get("Anthropic-Version")
if anthropicVersion == "" {
anthropicVersion = anthropic.AnthropicVersion
}
req.Header.Set("Anthropic-Version", anthropicVersion)
if rawBetas := c.Request.Header.Get(anthropic.AnthropicBeta); rawBetas != "" {
req.Header.Set(anthropic.AnthropicBeta, rawBetas)
}
return nil
}
func (a *Adaptor) ConvertRequest(
meta *meta.Meta,
store adaptor.Store,
req *http.Request,
) (adaptor.ConvertResult, error) {
if meta.Mode == mode.Anthropic {
return anthropic.ConvertRequest(meta, req)
}
return a.Adaptor.ConvertRequest(meta, store, req)
}
func (a *Adaptor) DoResponse(
meta *meta.Meta,
store adaptor.Store,
c *gin.Context,
resp *http.Response,
) (adaptor.DoResponseResult, adaptor.Error) {
if meta.Mode == mode.Anthropic {
if utils.IsStreamResponse(resp) {
return anthropic.StreamHandler(meta, c, resp)
}
return anthropic.Handler(meta, c, resp)
}
return a.Adaptor.DoResponse(meta, store, c, resp)
}
func (a *Adaptor) Metadata() adaptor.Metadata {
return adaptor.Metadata{
Readme: "Ant Ling (蚂蚁百灵) endpoint\nOpenAI-compatible chat endpoint: /api/llm/v1/chat/completions\nAnthropic-compatible endpoint: /api/anthropic/v1/messages\nSupports Gemini-compatible request conversion via OpenAI chat endpoint",
Models: ModelList,
}
}