adaptor.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package openrouter
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "io"
  7. "net/http"
  8. "one-api/dto"
  9. "one-api/relay/channel"
  10. "one-api/relay/channel/openai"
  11. relaycommon "one-api/relay/common"
  12. )
  13. type Adaptor struct {
  14. }
  15. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  16. //TODO implement me
  17. return nil, errors.New("not implemented")
  18. }
  19. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.ImageRequest) (any, error) {
  20. //TODO implement me
  21. return nil, errors.New("not implemented")
  22. }
  23. func (a *Adaptor) Init(info *relaycommon.RelayInfo) {
  24. }
  25. func (a *Adaptor) GetRequestURL(info *relaycommon.RelayInfo) (string, error) {
  26. return fmt.Sprintf("%s/v1/chat/completions", info.BaseUrl), nil
  27. }
  28. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *relaycommon.RelayInfo) error {
  29. channel.SetupApiRequestHeader(info, c, req)
  30. req.Set("Authorization", fmt.Sprintf("Bearer %s", info.ApiKey))
  31. req.Set("HTTP-Referer", "https://github.com/Calcium-Ion/new-api")
  32. req.Set("X-Title", "New API")
  33. return nil
  34. }
  35. func (a *Adaptor) ConvertRequest(c *gin.Context, info *relaycommon.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  36. return request, nil
  37. }
  38. func (a *Adaptor) DoRequest(c *gin.Context, info *relaycommon.RelayInfo, requestBody io.Reader) (any, error) {
  39. return channel.DoApiRequest(a, c, info, requestBody)
  40. }
  41. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  42. return nil, errors.New("not implemented")
  43. }
  44. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *relaycommon.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  45. return nil, errors.New("not implemented")
  46. }
  47. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *relaycommon.RelayInfo) (usage any, err *dto.OpenAIErrorWithStatusCode) {
  48. if info.IsStream {
  49. err, usage = openai.OaiStreamHandler(c, resp, info)
  50. } else {
  51. err, usage = openai.OpenaiHandler(c, resp, info.PromptTokens, info.UpstreamModelName)
  52. }
  53. return
  54. }
  55. func (a *Adaptor) GetModelList() []string {
  56. return ModelList
  57. }
  58. func (a *Adaptor) GetChannelName() string {
  59. return ChannelName
  60. }