Skip to content

Commit c29c592

Browse files
committed
Finished mistral streaming
1 parent fa2019f commit c29c592

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

cmd/api/mistral.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,19 @@ func mistralChat(ctx context.Context, w *tablewriter.Writer, args []string) erro
138138
}
139139
if mistralStream != nil {
140140
opts = append(opts, mistral.OptStream(func(choice schema.MessageChoice) {
141-
fmt.Printf("%v\n", choice)
141+
w := w.Output()
142+
if choice.Delta == nil {
143+
return
144+
}
145+
if choice.Delta.Role != "" {
146+
fmt.Fprintf(w, "\n%v: ", choice.Delta.Role)
147+
}
148+
if choice.Delta.Content != "" {
149+
fmt.Fprintf(w, "%v", choice.Delta.Content)
150+
}
151+
if choice.FinishReason != "" {
152+
fmt.Printf("\nfinish_reason: %q\n", choice.FinishReason)
153+
}
142154
}))
143155
}
144156
if mistralSafePrompt {
@@ -164,6 +176,10 @@ func mistralChat(ctx context.Context, w *tablewriter.Writer, args []string) erro
164176
return err
165177
}
166178

167-
// Write table
168-
return w.Write(responses)
179+
// Write table (if not streaming)
180+
if mistralStream == nil || !*mistralStream {
181+
return w.Write(responses)
182+
} else {
183+
return nil
184+
}
169185
}

pkg/openai/schema/message.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ func (m MessageChoice) String() string {
149149
return string(data)
150150
}
151151

152+
func (m MessageDelta) String() string {
153+
data, _ := json.MarshalIndent(m, "", " ")
154+
return string(data)
155+
}
152156
func (c Content) String() string {
153157
data, _ := json.MarshalIndent(c, "", " ")
154158
return string(data)

0 commit comments

Comments
 (0)