Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ee7cdae0
authored
May 13, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cozeChatHelper
parent
7e0b2521
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
66 additions
and
4 deletions
+66
-4
relay/channel/coze/dto.go
+27
-0
relay/channel/coze/relay-coze.go
+39
-4
No files found.
relay/channel/coze/dto.go
View file @
ee7cdae0
...
@@ -49,3 +49,30 @@ type CozeChatUsage struct {
...
@@ -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"`
}
relay/channel/coze/relay-coze.go
View file @
ee7cdae0
...
@@ -2,12 +2,14 @@ package coze
...
@@ -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
...
@@ -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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment