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
fccda187
authored
Jul 17, 2025
by
IcedTangerine
Committed by
GitHub
Jul 17, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1369 from RedwindA/fix/xai-non-stream
fix xai-non-stream
parents
31adc393
3368b268
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
19 deletions
+23
-19
relay/channel/xai/dto.go
+12
-13
relay/channel/xai/text.go
+11
-6
No files found.
relay/channel/xai/dto.go
View file @
fccda187
...
@@ -4,24 +4,24 @@ import "one-api/dto"
...
@@ -4,24 +4,24 @@ import "one-api/dto"
// ChatCompletionResponse represents the response from XAI chat completion API
// ChatCompletionResponse represents the response from XAI chat completion API
type
ChatCompletionResponse
struct
{
type
ChatCompletionResponse
struct
{
Id
string
`json:"id"`
Id
string
`json:"id"`
Object
string
`json:"object"`
Object
string
`json:"object"`
Created
int64
`json:"created"`
Created
int64
`json:"created"`
Model
string
`json:"model"`
Model
string
`json:"model"`
Choices
[]
dto
.
ChatCompletionsStreamResponseChoice
Choices
[]
dto
.
OpenAITextResponseChoice
`json:"choices"`
Usage
*
dto
.
Usage
`json:"usage"`
Usage
*
dto
.
Usage
`json:"usage"`
SystemFingerprint
string
`json:"system_fingerprint"`
SystemFingerprint
string
`json:"system_fingerprint"`
}
}
// quality, size or style are not supported by xAI API at the moment.
// quality, size or style are not supported by xAI API at the moment.
type
ImageRequest
struct
{
type
ImageRequest
struct
{
Model
string
`json:"model"`
Model
string
`json:"model"`
Prompt
string
`json:"prompt" binding:"required"`
Prompt
string
`json:"prompt" binding:"required"`
N
int
`json:"n,omitempty"`
N
int
`json:"n,omitempty"`
// Size string `json:"size,omitempty"`
// Size string `json:"size,omitempty"`
// Quality string `json:"quality,omitempty"`
// Quality string `json:"quality,omitempty"`
ResponseFormat
string
`json:"response_format,omitempty"`
ResponseFormat
string
`json:"response_format,omitempty"`
// Style string `json:"style,omitempty"`
// Style string `json:"style,omitempty"`
// User string `json:"user,omitempty"`
// User string `json:"user,omitempty"`
// ExtraFields json.RawMessage `json:"extra_fields,omitempty"`
// ExtraFields json.RawMessage `json:"extra_fields,omitempty"`
}
}
\ No newline at end of file
relay/channel/xai/text.go
View file @
fccda187
...
@@ -82,21 +82,26 @@ func xAIHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response
...
@@ -82,21 +82,26 @@ func xAIHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.Response
defer
common
.
CloseResponseBodyGracefully
(
resp
)
defer
common
.
CloseResponseBodyGracefully
(
resp
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
responseBody
,
err
:=
io
.
ReadAll
(
resp
.
Body
)
var
response
*
dto
.
SimpleResponse
err
=
common
.
Unmarshal
(
responseBody
,
&
response
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
types
.
NewError
(
err
,
types
.
ErrorCodeBadResponseBody
)
return
nil
,
types
.
NewError
(
err
,
types
.
ErrorCodeBadResponseBody
)
}
}
response
.
Usage
.
CompletionTokens
=
response
.
Usage
.
TotalTokens
-
response
.
Usage
.
PromptTokens
var
xaiResponse
ChatCompletionResponse
response
.
Usage
.
CompletionTokenDetails
.
TextTokens
=
response
.
Usage
.
CompletionTokens
-
response
.
Usage
.
CompletionTokenDetails
.
ReasoningTokens
err
=
common
.
Unmarshal
(
responseBody
,
&
xaiResponse
)
if
err
!=
nil
{
return
nil
,
types
.
NewError
(
err
,
types
.
ErrorCodeBadResponseBody
)
}
if
xaiResponse
.
Usage
!=
nil
{
xaiResponse
.
Usage
.
CompletionTokens
=
xaiResponse
.
Usage
.
TotalTokens
-
xaiResponse
.
Usage
.
PromptTokens
xaiResponse
.
Usage
.
CompletionTokenDetails
.
TextTokens
=
xaiResponse
.
Usage
.
CompletionTokens
-
xaiResponse
.
Usage
.
CompletionTokenDetails
.
ReasoningTokens
}
// new body
// new body
encodeJson
,
err
:=
common
.
Marshal
(
r
esponse
)
encodeJson
,
err
:=
common
.
Marshal
(
xaiR
esponse
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
types
.
NewError
(
err
,
types
.
ErrorCodeBadResponseBody
)
return
nil
,
types
.
NewError
(
err
,
types
.
ErrorCodeBadResponseBody
)
}
}
common
.
IOCopyBytesGracefully
(
c
,
resp
,
encodeJson
)
common
.
IOCopyBytesGracefully
(
c
,
resp
,
encodeJson
)
return
&
r
esponse
.
Usage
,
nil
return
xaiR
esponse
.
Usage
,
nil
}
}
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