adaptor.go 3.9 KB

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