MCPcopy Index your code
hub / github.com/53AI/53AIHub / testChannel

Function testChannel

api/controller/channel-test.go:151–264  ·  view source on GitHub ↗
(ctx context.Context, channel *model.Channel, request *relaymodel.GeneralOpenAIRequest)

Source from the content-addressed store, hash-verified

149}
150
151func testChannel(ctx context.Context, channel *model.Channel, request *relaymodel.GeneralOpenAIRequest) (responseMessage string, err error, openaiErr *relaymodel.Error, actualModel string) {
152 //startTime := time.Now()
153 w := httptest.NewRecorder()
154 c, _ := gin.CreateTestContext(w)
155 c.Request = &http.Request{
156 Method: "POST",
157 URL: &url.URL{Path: "/v1/chat/completions"},
158 Body: nil,
159 Header: make(http.Header),
160 }
161 c.Request.Header.Set("Authorization", "Bearer "+channel.Key)
162 c.Request.Header.Set("Content-Type", "application/json")
163 c.Set(ctxkey.Channel, channel.Type)
164 c.Set(ctxkey.BaseURL, channel.GetBaseURL())
165 cfg, _ := channel.LoadConfig()
166 c.Set(ctxkey.Config, cfg)
167 middleware.SetupContextForSelectedChannel(c, channel, "")
168 meta := meta.GetByContext(c)
169 apiType := model.GetApiType(channel.Type)
170 meta.APIType = apiType
171 // apiType := channeltype.ToAPIType(channel.Type)
172 adaptor := service.GetAdaptor(meta.APIType)
173 err = service.SetCustomConfig(&adaptor, &custom.CustomConfig{
174 ConversationId: "",
175 UserId: "53AIHub",
176 })
177 if err != nil {
178 return "", err, nil, ""
179 }
180 // adaptor := relay.GetAdaptor(apiType)
181 if adaptor == nil {
182 return "", fmt.Errorf("invalid api type: %d, adaptor is nil", apiType), nil, ""
183 }
184 adaptor.Init(meta)
185 modelName := request.Model
186 modelMap := channel.GetModelMapping()
187 if modelName == "" || !strings.Contains(channel.Models, modelName) {
188 modelNames := strings.Split(channel.Models, ",")
189 if len(modelNames) > 0 {
190 modelName = modelNames[0]
191 }
192 }
193 if modelMap != nil && modelMap[modelName] != "" {
194 modelName = modelMap[modelName]
195 }
196 meta.OriginModelName, meta.ActualModelName = request.Model, modelName
197 request.Model = modelName
198 convertedRequest, err := adaptor.ConvertRequest(c, relaymode.ChatCompletions, request)
199 if err != nil {
200 return "", err, nil, ""
201 }
202 jsonData, err := json.Marshal(convertedRequest)
203 if err != nil {
204 return "", err, nil, ""
205 }
206 defer func() {
207 //logContent := fmt.Sprintf("渠道 %s 测试成功,响应:%s", channel.Name, responseMessage)
208 if err != nil || openaiErr != nil {

Callers 2

TestChannelFunction · 0.85

Calls 13

parseTestResponseFunction · 0.85
SetMethod · 0.80
LoadConfigMethod · 0.80
ErrorfMethod · 0.80
GetModelMappingMethod · 0.80
GetBaseURLMethod · 0.45
InitMethod · 0.45
ConvertRequestMethod · 0.45
DoRequestMethod · 0.45
DoResponseMethod · 0.45
NewMethod · 0.45
StringMethod · 0.45

Tested by 1

TestChannelFunction · 0.68