adaptor.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package coze
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "one-api/dto"
  9. "one-api/relay/channel"
  10. "one-api/relay/common"
  11. "one-api/types"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. )
  15. type Adaptor struct {
  16. }
  17. // ConvertAudioRequest implements channel.Adaptor.
  18. func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *common.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
  19. return nil, errors.New("not implemented")
  20. }
  21. // ConvertClaudeRequest implements channel.Adaptor.
  22. func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *common.RelayInfo, request *dto.ClaudeRequest) (any, error) {
  23. return nil, errors.New("not implemented")
  24. }
  25. // ConvertEmbeddingRequest implements channel.Adaptor.
  26. func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *common.RelayInfo, request dto.EmbeddingRequest) (any, error) {
  27. return nil, errors.New("not implemented")
  28. }
  29. // ConvertImageRequest implements channel.Adaptor.
  30. func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *common.RelayInfo, request dto.ImageRequest) (any, error) {
  31. return nil, errors.New("not implemented")
  32. }
  33. // ConvertOpenAIRequest implements channel.Adaptor.
  34. func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *common.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
  35. if request == nil {
  36. return nil, errors.New("request is nil")
  37. }
  38. return convertCozeChatRequest(c, *request), nil
  39. }
  40. // ConvertOpenAIResponsesRequest implements channel.Adaptor.
  41. func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *common.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
  42. return nil, errors.New("not implemented")
  43. }
  44. // ConvertRerankRequest implements channel.Adaptor.
  45. func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
  46. return nil, errors.New("not implemented")
  47. }
  48. // DoRequest implements channel.Adaptor.
  49. func (a *Adaptor) DoRequest(c *gin.Context, info *common.RelayInfo, requestBody io.Reader) (any, error) {
  50. if info.IsStream {
  51. return channel.DoApiRequest(a, c, info, requestBody)
  52. }
  53. // 首先发送创建消息请求,成功后再发送获取消息请求
  54. // 发送创建消息请求
  55. resp, err := channel.DoApiRequest(a, c, info, requestBody)
  56. if err != nil {
  57. return nil, err
  58. }
  59. // 解析 resp
  60. var cozeResponse CozeChatResponse
  61. respBody, err := io.ReadAll(resp.Body)
  62. if err != nil {
  63. return nil, err
  64. }
  65. err = json.Unmarshal(respBody, &cozeResponse)
  66. if cozeResponse.Code != 0 {
  67. return nil, errors.New(cozeResponse.Msg)
  68. }
  69. c.Set("coze_conversation_id", cozeResponse.Data.ConversationId)
  70. c.Set("coze_chat_id", cozeResponse.Data.Id)
  71. // 轮询检查消息是否完成
  72. for {
  73. err, isComplete := checkIfChatComplete(a, c, info)
  74. if err != nil {
  75. return nil, err
  76. } else {
  77. if isComplete {
  78. break
  79. }
  80. }
  81. time.Sleep(time.Second * 1)
  82. }
  83. // 发送获取消息请求
  84. return getChatDetail(a, c, info)
  85. }
  86. // DoResponse implements channel.Adaptor.
  87. func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *common.RelayInfo) (usage any, err *types.NewAPIError) {
  88. if info.IsStream {
  89. usage, err = cozeChatStreamHandler(c, info, resp)
  90. } else {
  91. usage, err = cozeChatHandler(c, info, resp)
  92. }
  93. return
  94. }
  95. // GetChannelName implements channel.Adaptor.
  96. func (a *Adaptor) GetChannelName() string {
  97. return ChannelName
  98. }
  99. // GetModelList implements channel.Adaptor.
  100. func (a *Adaptor) GetModelList() []string {
  101. return ModelList
  102. }
  103. // GetRequestURL implements channel.Adaptor.
  104. func (a *Adaptor) GetRequestURL(info *common.RelayInfo) (string, error) {
  105. return fmt.Sprintf("%s/v3/chat", info.BaseUrl), nil
  106. }
  107. // Init implements channel.Adaptor.
  108. func (a *Adaptor) Init(info *common.RelayInfo) {
  109. }
  110. // SetupRequestHeader implements channel.Adaptor.
  111. func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *common.RelayInfo) error {
  112. channel.SetupApiRequestHeader(info, c, req)
  113. req.Set("Authorization", "Bearer "+info.ApiKey)
  114. return nil
  115. }