Skip to content
This repository was archived by the owner on Oct 6, 2025. It is now read-only.

Commit ebb4723

Browse files
author
Piotr Stankiewicz
committed
Print reasoning content in chat
Signed-off-by: Piotr Stankiewicz <[email protected]>
1 parent 9cb94c8 commit ebb4723

File tree

20 files changed

+2285
-13
lines changed

20 files changed

+2285
-13
lines changed

desktop/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ type OpenAIChatResponse struct {
3333
Model string `json:"model"`
3434
Choices []struct {
3535
Delta struct {
36-
Content string `json:"content"`
37-
Role string `json:"role,omitempty"`
36+
Content string `json:"content"`
37+
Role string `json:"role,omitempty"`
38+
ReasoningContent string `json:"reasoning_content,omitempty"`
3839
} `json:"delta"`
3940
Index int `json:"index"`
4041
FinishReason string `json:"finish_reason"`

desktop/desktop.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/docker/model-runner/pkg/inference"
1919
dmrm "github.com/docker/model-runner/pkg/inference/models"
2020
"github.com/docker/model-runner/pkg/inference/scheduling"
21+
"github.com/fatih/color"
2122
"github.com/pkg/errors"
2223
"go.opentelemetry.io/otel"
2324
)
@@ -362,6 +363,14 @@ func (c *Client) fullModelID(id string) (string, error) {
362363
return "", fmt.Errorf("model with ID %s not found", id)
363364
}
364365

366+
type chatPrinterState int
367+
368+
const (
369+
chatPrinterNone chatPrinterState = iota
370+
chatPrinterContent
371+
chatPrinterReasoning
372+
)
373+
365374
func (c *Client) Chat(backend, model, prompt, apiKey string) error {
366375
model = normalizeHuggingFaceModelName(model)
367376
if !strings.Contains(strings.Trim(model, "/"), "/") {
@@ -411,6 +420,8 @@ func (c *Client) Chat(backend, model, prompt, apiKey string) error {
411420
return fmt.Errorf("error response: status=%d body=%s", resp.StatusCode, body)
412421
}
413422

423+
printerState := chatPrinterNone
424+
reasoningFmt := color.New(color.FgWhite).Add(color.Italic)
414425
scanner := bufio.NewScanner(resp.Body)
415426
for scanner.Scan() {
416427
line := scanner.Text()
@@ -433,9 +444,26 @@ func (c *Client) Chat(backend, model, prompt, apiKey string) error {
433444
return fmt.Errorf("error parsing stream response: %w", err)
434445
}
435446

436-
if len(streamResp.Choices) > 0 && streamResp.Choices[0].Delta.Content != "" {
437-
chunk := streamResp.Choices[0].Delta.Content
438-
fmt.Print(chunk)
447+
if len(streamResp.Choices) > 0 {
448+
if streamResp.Choices[0].Delta.ReasoningContent != "" {
449+
chunk := streamResp.Choices[0].Delta.ReasoningContent
450+
if printerState == chatPrinterContent {
451+
fmt.Print("\n\n")
452+
}
453+
if printerState != chatPrinterReasoning {
454+
reasoningFmt.Println("Thinking:")
455+
}
456+
printerState = chatPrinterReasoning
457+
reasoningFmt.Print(chunk)
458+
}
459+
if streamResp.Choices[0].Delta.Content != "" {
460+
chunk := streamResp.Choices[0].Delta.Content
461+
if printerState == chatPrinterReasoning {
462+
fmt.Print("\n\n")
463+
}
464+
printerState = chatPrinterContent
465+
fmt.Print(chunk)
466+
}
439467
}
440468
}
441469

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ require (
1313
github.com/docker/go-units v0.5.0
1414
github.com/docker/model-distribution v0.0.0-20250724114133-a11d745e582c
1515
github.com/docker/model-runner v0.0.0-20250724122432-ecfa5e7e6807
16+
github.com/fatih/color v1.15.0
1617
github.com/google/go-containerregistry v0.20.6
17-
github.com/mattn/go-isatty v0.0.17
18+
github.com/mattn/go-isatty v0.0.20
1819
github.com/nxadm/tail v1.4.8
1920
github.com/olekukonko/tablewriter v0.0.5
2021
github.com/pkg/errors v0.9.1
@@ -62,6 +63,7 @@ require (
6263
github.com/jaypipes/pcidb v1.0.1 // indirect
6364
github.com/json-iterator/go v1.1.12 // indirect
6465
github.com/klauspost/compress v1.18.0 // indirect
66+
github.com/mattn/go-colorable v0.1.13 // indirect
6567
github.com/mattn/go-runewidth v0.0.16 // indirect
6668
github.com/mattn/go-shellwords v1.0.12 // indirect
6769
github.com/mitchellh/go-homedir v1.1.0 // indirect

go.sum

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ github.com/elastic/go-sysinfo v1.15.3/go.mod h1:K/cNrqYTDrSoMh2oDkYEMS2+a72GRxMv
8888
github.com/elastic/go-windows v1.0.2 h1:yoLLsAsV5cfg9FLhZ9EXZ2n2sQFKeDYrHenkcivY4vI=
8989
github.com/elastic/go-windows v1.0.2/go.mod h1:bGcDpBzXgYSqM0Gx3DM4+UxFj300SZLixie9u9ixLM8=
9090
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0=
91+
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
92+
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
9193
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
9294
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
9395
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
@@ -161,8 +163,11 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
161163
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
162164
github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
163165
github.com/magiconair/properties v1.5.3/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
164-
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
165-
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
166+
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
167+
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
168+
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
169+
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
170+
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
166171
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
167172
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
168173
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@@ -339,6 +344,7 @@ golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBc
339344
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
340345
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
341346
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
347+
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
342348
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
343349
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
344350
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=

vendor/github.com/fatih/color/LICENSE.md

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/fatih/color/README.md

Lines changed: 176 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)