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
0d62db7c
authored
Oct 18, 2025
by
Seefs
Committed by
GitHub
Oct 18, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #2065 from somnifex/main
fix: handle JSON parsing for thinking content in ollama stream
parents
69598b84
e4a2904c
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
3 deletions
+24
-3
relay/channel/ollama/stream.go
+24
-3
No files found.
relay/channel/ollama/stream.go
View file @
0d62db7c
...
@@ -121,7 +121,14 @@ func ollamaStreamHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http
...
@@ -121,7 +121,14 @@ 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"
{
delta
.
Choices
[
0
]
.
Delta
.
SetReasoningContent
(
raw
)
// 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
)
}
}
}
}
}
// tool calls
// tool calls
...
@@ -209,7 +216,14 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
...
@@ -209,7 +216,14 @@ 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"
{
reasoningBuilder
.
WriteString
(
raw
)
// 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
)
}
}
}
}
}
if
ck
.
Message
!=
nil
&&
ck
.
Message
.
Content
!=
""
{
if
ck
.
Message
!=
nil
&&
ck
.
Message
.
Content
!=
""
{
...
@@ -229,7 +243,14 @@ func ollamaChatHandler(c *gin.Context, info *relaycommon.RelayInfo, resp *http.R
...
@@ -229,7 +243,14 @@ 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"
{
reasoningBuilder
.
WriteString
(
raw
)
// 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
)
}
}
}
}
}
aggContent
.
WriteString
(
single
.
Message
.
Content
)
aggContent
.
WriteString
(
single
.
Message
.
Content
)
...
...
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