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
e4a2904c
authored
Oct 17, 2025
by
somnifex
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: handle JSON parsing for thinking content in ollama stream and chat handlers
parent
2df75d14
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
21 additions
and
0 deletions
+21
-0
relay/channel/ollama/stream.go
+21
-0
No files found.
relay/channel/ollama/stream.go
View file @
e4a2904c
...
@@ -121,9 +121,16 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http
...
@@ -121,9 +121,16 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http
if
chunk
.
Message
!=
nil
&&
len
(
chunk
.
Message
.
Thinking
)
>
0
{
if
chunk
.
Message
!=
nil
&&
len
(
chunk
.
Message
.
Thinking
)
>
0
{
raw
:=
strings
.
TrimSpace
(
string
(
chunk
.
Message
.
Thinking
))
raw
:=
strings
.
TrimSpace
(
string
(
chunk
.
Message
.
Thinking
))
if
raw
!=
""
&&
raw
!=
"null"
{
if
raw
!=
""
&&
raw
!=
"null"
{
// Unmarshal the JSON string to get the actual content without quotes
var
thinkingContent
string
if
err
:=
json
.
Unmarshal
(
chunk
.
Message
.
Thinking
,
&
thinkingContent
);
err
==
nil
{
delta
.
Choices
[
0
]
.
Delta
.
SetReasoningContent
(
thinkingContent
)
}
else
{
// Fallback to raw string if it's not a JSON string
delta
.
Choices
[
0
]
.
Delta
.
SetReasoningContent
(
raw
)
delta
.
Choices
[
0
]
.
Delta
.
SetReasoningContent
(
raw
)
}
}
}
}
}
// tool calls
// tool calls
if
chunk
.
Message
!=
nil
&&
len
(
chunk
.
Message
.
ToolCalls
)
>
0
{
if
chunk
.
Message
!=
nil
&&
len
(
chunk
.
Message
.
ToolCalls
)
>
0
{
delta
.
Choices
[
0
]
.
Delta
.
ToolCalls
=
make
([]
dto
.
ToolCallResponse
,
0
,
len
(
chunk
.
Message
.
ToolCalls
))
delta
.
Choices
[
0
]
.
Delta
.
ToolCalls
=
make
([]
dto
.
ToolCallResponse
,
0
,
len
(
chunk
.
Message
.
ToolCalls
))
...
@@ -209,9 +216,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
...
@@ -209,9 +216,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
if
ck
.
Message
!=
nil
&&
len
(
ck
.
Message
.
Thinking
)
>
0
{
if
ck
.
Message
!=
nil
&&
len
(
ck
.
Message
.
Thinking
)
>
0
{
raw
:=
strings
.
TrimSpace
(
string
(
ck
.
Message
.
Thinking
))
raw
:=
strings
.
TrimSpace
(
string
(
ck
.
Message
.
Thinking
))
if
raw
!=
""
&&
raw
!=
"null"
{
if
raw
!=
""
&&
raw
!=
"null"
{
// Unmarshal the JSON string to get the actual content without quotes
var
thinkingContent
string
if
err
:=
json
.
Unmarshal
(
ck
.
Message
.
Thinking
,
&
thinkingContent
);
err
==
nil
{
reasoningBuilder
.
WriteString
(
thinkingContent
)
}
else
{
// Fallback to raw string if it's not a JSON string
reasoningBuilder
.
WriteString
(
raw
)
reasoningBuilder
.
WriteString
(
raw
)
}
}
}
}
}
if
ck
.
Message
!=
nil
&&
ck
.
Message
.
Content
!=
""
{
if
ck
.
Message
!=
nil
&&
ck
.
Message
.
Content
!=
""
{
aggContent
.
WriteString
(
ck
.
Message
.
Content
)
aggContent
.
WriteString
(
ck
.
Message
.
Content
)
}
else
if
ck
.
Response
!=
""
{
}
else
if
ck
.
Response
!=
""
{
...
@@ -229,9 +243,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
...
@@ -229,9 +243,16 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
if
len
(
single
.
Message
.
Thinking
)
>
0
{
if
len
(
single
.
Message
.
Thinking
)
>
0
{
raw
:=
strings
.
TrimSpace
(
string
(
single
.
Message
.
Thinking
))
raw
:=
strings
.
TrimSpace
(
string
(
single
.
Message
.
Thinking
))
if
raw
!=
""
&&
raw
!=
"null"
{
if
raw
!=
""
&&
raw
!=
"null"
{
// Unmarshal the JSON string to get the actual content without quotes
var
thinkingContent
string
if
err
:=
json
.
Unmarshal
(
single
.
Message
.
Thinking
,
&
thinkingContent
);
err
==
nil
{
reasoningBuilder
.
WriteString
(
thinkingContent
)
}
else
{
// Fallback to raw string if it's not a JSON string
reasoningBuilder
.
WriteString
(
raw
)
reasoningBuilder
.
WriteString
(
raw
)
}
}
}
}
}
aggContent
.
WriteString
(
single
.
Message
.
Content
)
aggContent
.
WriteString
(
single
.
Message
.
Content
)
}
else
{
}
else
{
aggContent
.
WriteString
(
single
.
Response
)
aggContent
.
WriteString
(
single
.
Response
)
...
...
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