cozeChatHelper
This commit is contained in:
@@ -49,3 +49,30 @@ type CozeChatUsage struct {
|
|||||||
OutputCount int `json:"output_count"`
|
OutputCount int `json:"output_count"`
|
||||||
InputCount int `json:"input_count"`
|
InputCount int `json:"input_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CozeChatDetailResponse struct {
|
||||||
|
Data []CozeChatV3MessageDetail `json:"data"`
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Detail CozeResponseDetail `json:"detail"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CozeChatV3MessageDetail struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Role string `json:"role"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
BotId string `json:"bot_id"`
|
||||||
|
ChatId string `json:"chat_id"`
|
||||||
|
Content json.RawMessage `json:"content"`
|
||||||
|
MetaData json.RawMessage `json:"meta_data"`
|
||||||
|
CreatedAt int64 `json:"created_at"`
|
||||||
|
SectionId string `json:"section_id"`
|
||||||
|
UpdatedAt int64 `json:"updated_at"`
|
||||||
|
ContentType string `json:"content_type"`
|
||||||
|
ConversationId string `json:"conversation_id"`
|
||||||
|
ReasoningContent string `json:"reasoning_content"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type CozeResponseDetail struct {
|
||||||
|
Logid string `json:"logid"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,12 +2,14 @@ package coze
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"one-api/dto"
|
"one-api/dto"
|
||||||
"one-api/relay/common"
|
"one-api/relay/common"
|
||||||
relaycommon "one-api/relay/common"
|
relaycommon "one-api/relay/common"
|
||||||
|
"one-api/relay/helper"
|
||||||
"one-api/service"
|
"one-api/service"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -47,14 +49,47 @@ func cozeChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rela
|
|||||||
}
|
}
|
||||||
// convert coze response to openai response
|
// convert coze response to openai response
|
||||||
var response dto.TextResponse
|
var response dto.TextResponse
|
||||||
var cozeResponse CozeChatResponse
|
var cozeResponse CozeChatDetailResponse
|
||||||
|
response.Model = info.UpstreamModelName
|
||||||
err = json.Unmarshal(responseBody, &cozeResponse)
|
err = json.Unmarshal(responseBody, &cozeResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
return service.OpenAIErrorWrapper(err, "unmarshal_response_body_failed", http.StatusInternalServerError), nil
|
||||||
}
|
}
|
||||||
response.Model = info.UpstreamModelName
|
if cozeResponse.Code != 0 {
|
||||||
// TODO: 处理 cozeResponse
|
return service.OpenAIErrorWrapper(errors.New(cozeResponse.Msg), fmt.Sprintf("%d", cozeResponse.Code), http.StatusInternalServerError), nil
|
||||||
return nil, nil
|
}
|
||||||
|
// 从上下文获取 usage
|
||||||
|
var usage dto.Usage
|
||||||
|
usage.PromptTokens = c.GetInt("coze_input_count")
|
||||||
|
usage.CompletionTokens = c.GetInt("coze_output_count")
|
||||||
|
usage.TotalTokens = c.GetInt("coze_token_count")
|
||||||
|
response.Usage = usage
|
||||||
|
response.Id = helper.GetResponseID(c)
|
||||||
|
|
||||||
|
var responseContent json.RawMessage
|
||||||
|
for _, data := range cozeResponse.Data {
|
||||||
|
if data.Type == "answer" {
|
||||||
|
responseContent = data.Content
|
||||||
|
response.Created = data.CreatedAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 添加 response.Choices
|
||||||
|
response.Choices = []dto.OpenAITextResponseChoice{
|
||||||
|
{
|
||||||
|
Index: 0,
|
||||||
|
Message: dto.Message{Role: "assistant", Content: responseContent},
|
||||||
|
FinishReason: "stop",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
jsonResponse, err := json.Marshal(response)
|
||||||
|
if err != nil {
|
||||||
|
return service.OpenAIErrorWrapper(err, "marshal_response_body_failed", http.StatusInternalServerError), nil
|
||||||
|
}
|
||||||
|
c.Writer.Header().Set("Content-Type", "application/json")
|
||||||
|
c.Writer.WriteHeader(resp.StatusCode)
|
||||||
|
_, _ = c.Writer.Write(jsonResponse)
|
||||||
|
|
||||||
|
return nil, &usage
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfChatComplete(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (error, bool) {
|
func checkIfChatComplete(a *Adaptor, c *gin.Context, info *relaycommon.RelayInfo) (error, bool) {
|
||||||
|
|||||||
Reference in New Issue
Block a user