generated from songquanpeng/gin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor AwsClaude to Aws to support both llama3 and claude (#1601
- Loading branch information
Showing
18 changed files
with
595 additions
and
88 deletions.
There are no files selected for viewing
78 changes: 40 additions & 38 deletions
78
relay/adaptor/aws/adapter.go → relay/adaptor/aws/adaptor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,84 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
"github.com/songquanpeng/one-api/common/ctxkey" | ||
"errors" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
"github.com/songquanpeng/one-api/relay/adaptor" | ||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic" | ||
"github.com/songquanpeng/one-api/relay/adaptor/aws/utils" | ||
"github.com/songquanpeng/one-api/relay/meta" | ||
"github.com/songquanpeng/one-api/relay/model" | ||
) | ||
|
||
var _ adaptor.Adaptor = new(Adaptor) | ||
|
||
type Adaptor struct { | ||
meta *meta.Meta | ||
awsClient *bedrockruntime.Client | ||
awsAdapter utils.AwsAdapter | ||
|
||
Meta *meta.Meta | ||
AwsClient *bedrockruntime.Client | ||
} | ||
|
||
func (a *Adaptor) Init(meta *meta.Meta) { | ||
a.meta = meta | ||
a.awsClient = bedrockruntime.New(bedrockruntime.Options{ | ||
a.Meta = meta | ||
a.AwsClient = bedrockruntime.New(bedrockruntime.Options{ | ||
Region: meta.Config.Region, | ||
Credentials: aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(meta.Config.AK, meta.Config.SK, "")), | ||
}) | ||
} | ||
|
||
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(ctxkey.RequestModel, request.Model) | ||
c.Set(ctxkey.ConvertedRequest, claudeReq) | ||
return claudeReq, nil | ||
} | ||
|
||
func (a *Adaptor) ConvertImageRequest(request *model.ImageRequest) (any, error) { | ||
if request == nil { | ||
return nil, errors.New("request is nil") | ||
adaptor := GetAdaptor(request.Model) | ||
if adaptor == nil { | ||
return nil, errors.New("adaptor not found") | ||
} | ||
return request, nil | ||
} | ||
|
||
func (a *Adaptor) DoRequest(c *gin.Context, meta *meta.Meta, requestBody io.Reader) (*http.Response, error) { | ||
return nil, nil | ||
a.awsAdapter = adaptor | ||
return adaptor.ConvertRequest(c, relayMode, request) | ||
} | ||
|
||
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, a.awsClient) | ||
} else { | ||
err, usage = Handler(c, a.awsClient, meta.ActualModelName) | ||
if a.awsAdapter == nil { | ||
return nil, utils.WrapErr(errors.New("awsAdapter is nil")) | ||
} | ||
return | ||
return a.awsAdapter.DoResponse(c, a.AwsClient, meta) | ||
} | ||
|
||
func (a *Adaptor) GetModelList() (models []string) { | ||
for n := range awsModelIDMap { | ||
models = append(models, n) | ||
for model := range adaptors { | ||
models = append(models, model) | ||
} | ||
return | ||
} | ||
|
||
func (a *Adaptor) GetChannelName() string { | ||
return "aws" | ||
} | ||
|
||
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) 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
"github.com/songquanpeng/one-api/common/ctxkey" | ||
"github.com/songquanpeng/one-api/relay/adaptor/anthropic" | ||
"github.com/songquanpeng/one-api/relay/adaptor/aws/utils" | ||
"github.com/songquanpeng/one-api/relay/meta" | ||
"github.com/songquanpeng/one-api/relay/model" | ||
) | ||
|
||
var _ utils.AwsAdapter = new(Adaptor) | ||
|
||
type Adaptor struct { | ||
} | ||
|
||
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(ctxkey.RequestModel, request.Model) | ||
c.Set(ctxkey.ConvertedRequest, claudeReq) | ||
return claudeReq, nil | ||
} | ||
|
||
func (a *Adaptor) DoResponse(c *gin.Context, awsCli *bedrockruntime.Client, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) { | ||
if meta.IsStream { | ||
err, usage = StreamHandler(c, awsCli) | ||
} else { | ||
err, usage = Handler(c, awsCli, meta.ActualModelName) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package aws | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go-v2/service/bedrockruntime" | ||
"github.com/songquanpeng/one-api/common/ctxkey" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/pkg/errors" | ||
"github.com/songquanpeng/one-api/relay/adaptor/aws/utils" | ||
"github.com/songquanpeng/one-api/relay/meta" | ||
"github.com/songquanpeng/one-api/relay/model" | ||
) | ||
|
||
var _ utils.AwsAdapter = new(Adaptor) | ||
|
||
type Adaptor struct { | ||
} | ||
|
||
func (a *Adaptor) ConvertRequest(c *gin.Context, relayMode int, request *model.GeneralOpenAIRequest) (any, error) { | ||
if request == nil { | ||
return nil, errors.New("request is nil") | ||
} | ||
|
||
llamaReq := ConvertRequest(*request) | ||
c.Set(ctxkey.RequestModel, request.Model) | ||
c.Set(ctxkey.ConvertedRequest, llamaReq) | ||
return llamaReq, nil | ||
} | ||
|
||
func (a *Adaptor) DoResponse(c *gin.Context, awsCli *bedrockruntime.Client, meta *meta.Meta) (usage *model.Usage, err *model.ErrorWithStatusCode) { | ||
if meta.IsStream { | ||
err, usage = StreamHandler(c, awsCli) | ||
} else { | ||
err, usage = Handler(c, awsCli, meta.ActualModelName) | ||
} | ||
return | ||
} |
Oops, something went wrong.