mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-18 17:46:37 +08:00
* Refactor AWS adapter to improve model management - Replace hardcoded model list in `adapter.go` with a function to get models from `awsModelIDMap` - Update `GetModelList` function to return model list directly - Add `GetChannelName` function to get channel name from `Adaptor` object * Improve error handling and code organization in main.go - Replace switch statement with a map to map AWS model IDs to OpenAI model IDs - Return an error if the model is not found in the map - Use a single return statement instead of wrapping multiple return statements in the `awsModelID` function - Add a new error message for when the model is not found in the map in the `Handler` function
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
package aws
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
|
|
"github.com/Laisky/errors/v2"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/songquanpeng/one-api/common"
|
|
"github.com/songquanpeng/one-api/relay/adaptor"
|
|
"github.com/songquanpeng/one-api/relay/adaptor/anthropic"
|
|
"github.com/songquanpeng/one-api/relay/meta"
|
|
"github.com/songquanpeng/one-api/relay/model"
|
|
)
|
|
|
|
var _ adaptor.Adaptor = new(Adaptor)
|
|
|
|
type Adaptor struct {
|
|
}
|
|
|
|
func (a *Adaptor) Init(meta *meta.Meta) {
|
|
|
|
}
|
|
|
|
func (a *Adaptor) GetRequestURL(meta *meta.Meta) (string, error) {
|
|
return "", nil
|
|
}
|
|
|
|
func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Request, meta *meta.Meta) error {
|
|
return nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
|
|
claudeReq := anthropic.ConvertRequest(*request)
|
|
c.Set(common.CtxKeyRequestModel, request.Model)
|
|
c.Set(common.CtxKeyRawRequest, request)
|
|
c.Set(common.CtxKeyConvertedRequest, claudeReq)
|
|
return claudeReq, nil
|
|
}
|
|
|
|
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) {
|
|
if request == nil {
|
|
return nil, errors.New("request is nil")
|
|
}
|
|
return request, nil
|
|
}
|
|
|
|
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) {
|
|
if meta.IsStream {
|
|
err, usage = StreamHandler(c, resp)
|
|
} else {
|
|
err, usage = Handler(c, resp, meta.PromptTokens, meta.ActualModelName)
|
|
}
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetModelList() (models []string) {
|
|
for n := range awsModelIDMap {
|
|
models = append(models, n)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func (a *Adaptor) GetChannelName() string {
|
|
return "aws"
|
|
}
|