adaptor.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package submodel
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "one-api/dto"
  7. "one-api/relay/channel"
  8. "one-api/relay/channel/openai"
  9. relaycommon "one-api/relay/common"
  10. "one-api/types"
  11. "github.com/gin-gonic/gin"
  12. )
  13. type Adaptor struct {
  14. }
  15. func (a *Adaptor) ConvertClaudeRequest(*gin.Context, *relaycommon.RelayInfo, *dto.ClaudeRequest) (any, error) {
  16. return nil, errors.New("not implemented")
  17. }
  18. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  19. return nil, errors.New("not implemented")
  20. }
  21. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  22. return nil, errors.New("not implemented")
  23. }
  24. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  25. }
  26. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  27. return relaycommon.GetFullRequestURL(info.BaseUrl, info.RequestURLPath, info.ChannelType), nil
  28. }
  29. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  30. channel.SetupApiRequestHeader(info, c, req)
  31. req.Set("Authorization", "Bearer "+info.ApiKey)
  32. return nil
  33. }
  34. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  35. if request == nil {
  36. return nil, errors.New("request is nil")
  37. }
  38. return request, nil
  39. }
  40. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  41. return nil, errors.New("not implemented")
  42. }
  43. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  44. return nil, errors.New("not implemented")
  45. }
  46. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  47. return nil, errors.New("not implemented")
  48. }
  49. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  50. return channel.DoApiRequest(a, c, info, requestBody)
  51. }
  52. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *types.NewAPIError) {
  53. if info.IsStream {
  54. usage, err = openai.OaiStreamHandler(c, info, resp)
  55. } else {
  56. usage, err = openai.OpenaiHandler(c, info, resp)
  57. }
  58. return
  59. }
  60. func (a *Adaptor) GetModelList() []string {
  61. return ModelList
  62. }
  63. func (a *Adaptor) GetChannelName() string {
  64. return ChannelName
  65. }