| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- package coze
- import (
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "net/http"
- "one-api/dto"
- "one-api/relay/channel"
- "one-api/relay/common"
- "one-api/types"
- "time"
- "github.com/gin-gonic/gin"
- )
- type Adaptor struct {
- }
- // ConvertAudioRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertAudioRequest(c *gin.Context, info *common.RelayInfo, request dto.AudioRequest) (io.Reader, error) {
- return nil, errors.New("not implemented")
- }
- // ConvertClaudeRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertClaudeRequest(c *gin.Context, info *common.RelayInfo, request *dto.ClaudeRequest) (any, error) {
- return nil, errors.New("not implemented")
- }
- // ConvertEmbeddingRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertEmbeddingRequest(c *gin.Context, info *common.RelayInfo, request dto.EmbeddingRequest) (any, error) {
- return nil, errors.New("not implemented")
- }
- // ConvertImageRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertImageRequest(c *gin.Context, info *common.RelayInfo, request dto.ImageRequest) (any, error) {
- return nil, errors.New("not implemented")
- }
- // ConvertOpenAIRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *common.RelayInfo, request *dto.GeneralOpenAIRequest) (any, error) {
- if request == nil {
- return nil, errors.New("request is nil")
- }
- return convertCozeChatRequest(c, *request), nil
- }
- // ConvertOpenAIResponsesRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertOpenAIResponsesRequest(c *gin.Context, info *common.RelayInfo, request dto.OpenAIResponsesRequest) (any, error) {
- return nil, errors.New("not implemented")
- }
- // ConvertRerankRequest implements channel.Adaptor.
- func (a *Adaptor) ConvertRerankRequest(c *gin.Context, relayMode int, request dto.RerankRequest) (any, error) {
- return nil, errors.New("not implemented")
- }
- // DoRequest implements channel.Adaptor.
- func (a *Adaptor) DoRequest(c *gin.Context, info *common.RelayInfo, requestBody io.Reader) (any, error) {
- if info.IsStream {
- return channel.DoApiRequest(a, c, info, requestBody)
- }
- // 首先发送创建消息请求,成功后再发送获取消息请求
- // 发送创建消息请求
- resp, err := channel.DoApiRequest(a, c, info, requestBody)
- if err != nil {
- return nil, err
- }
- // 解析 resp
- var cozeResponse CozeChatResponse
- respBody, err := io.ReadAll(resp.Body)
- if err != nil {
- return nil, err
- }
- err = json.Unmarshal(respBody, &cozeResponse)
- if cozeResponse.Code != 0 {
- return nil, errors.New(cozeResponse.Msg)
- }
- c.Set("coze_conversation_id", cozeResponse.Data.ConversationId)
- c.Set("coze_chat_id", cozeResponse.Data.Id)
- // 轮询检查消息是否完成
- for {
- err, isComplete := checkIfChatComplete(a, c, info)
- if err != nil {
- return nil, err
- } else {
- if isComplete {
- break
- }
- }
- time.Sleep(time.Second * 1)
- }
- // 发送获取消息请求
- return getChatDetail(a, c, info)
- }
- // DoResponse implements channel.Adaptor.
- func (a *Adaptor) DoResponse(c *gin.Context, resp *http.Response, info *common.RelayInfo) (usage any, err *types.NewAPIError) {
- if info.IsStream {
- usage, err = cozeChatStreamHandler(c, info, resp)
- } else {
- usage, err = cozeChatHandler(c, info, resp)
- }
- return
- }
- // GetChannelName implements channel.Adaptor.
- func (a *Adaptor) GetChannelName() string {
- return ChannelName
- }
- // GetModelList implements channel.Adaptor.
- func (a *Adaptor) GetModelList() []string {
- return ModelList
- }
- // GetRequestURL implements channel.Adaptor.
- func (a *Adaptor) GetRequestURL(info *common.RelayInfo) (string, error) {
- return fmt.Sprintf("%s/v3/chat", info.BaseUrl), nil
- }
- // Init implements channel.Adaptor.
- func (a *Adaptor) Init(info *common.RelayInfo) {
- }
- // SetupRequestHeader implements channel.Adaptor.
- func (a *Adaptor) SetupRequestHeader(c *gin.Context, req *http.Header, info *common.RelayInfo) error {
- channel.SetupApiRequestHeader(info, c, req)
- req.Set("Authorization", "Bearer "+info.ApiKey)
- return nil
- }
|