Skip to content

Commit 17f8603

Browse files
committed
Fixed streaming responses
1 parent 5844acf commit 17f8603

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

cmd/llm/chat.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,16 @@ func (cmd *ChatCmd) Run(globals *Globals) error {
3838
return err
3939
}
4040

41+
// Current buffer
42+
var buf string
43+
4144
// Set the options
4245
opts := []llm.Opt{}
4346
if !cmd.NoStream {
4447
opts = append(opts, llm.WithStream(func(cc llm.Completion) {
45-
if text := cc.Text(0); text != "" {
46-
count := strings.Count(text, "\n")
47-
fmt.Print(strings.Repeat("\033[F", count) + strings.Repeat(" ", count) + "\r")
48-
fmt.Print(text)
49-
}
48+
text := cc.Text(0)
49+
fmt.Print(strings.TrimPrefix(text, buf))
50+
buf = text
5051
}))
5152
}
5253
if cmd.System != "" {
@@ -91,6 +92,7 @@ func (cmd *ChatCmd) Run(globals *Globals) error {
9192
if len(calls) == 0 {
9293
break
9394
}
95+
9496
if session.Text(0) != "" {
9597
globals.term.Println(session.Text(0))
9698
} else {
@@ -100,15 +102,20 @@ func (cmd *ChatCmd) Run(globals *Globals) error {
100102
}
101103
globals.term.Println("Calling ", strings.Join(names, ", "))
102104
}
105+
103106
if results, err := globals.toolkit.Run(ctx, calls...); err != nil {
104107
return err
105108
} else if err := session.FromTool(ctx, results...); err != nil {
106109
return err
107110
}
108111
}
109112

110-
// Print the response
111-
globals.term.Println("\n" + session.Text(0) + "\n")
113+
// Print the response, if not streaming
114+
if cmd.NoStream {
115+
globals.term.Println("\n" + session.Text(0) + "\n")
116+
} else {
117+
globals.term.Println()
118+
}
112119
}
113120
})
114121
}

cmd/llm/main.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,17 @@ func main() {
109109

110110
// Create an agent
111111
opts := []llm.Opt{}
112-
if cli.OllamaEndpoint != "" {
113-
opts = append(opts, agent.WithOllama(cli.OllamaEndpoint, clientopts...))
114-
}
115-
if cli.AnthropicKey != "" {
116-
opts = append(opts, agent.WithAnthropic(cli.AnthropicKey, clientopts...))
117-
}
118-
if cli.MistralKey != "" {
119-
opts = append(opts, agent.WithMistral(cli.MistralKey, clientopts...))
120-
}
112+
/*
113+
if cli.OllamaEndpoint != "" {
114+
opts = append(opts, agent.WithOllama(cli.OllamaEndpoint, clientopts...))
115+
}
116+
if cli.AnthropicKey != "" {
117+
opts = append(opts, agent.WithAnthropic(cli.AnthropicKey, clientopts...))
118+
}
119+
if cli.MistralKey != "" {
120+
opts = append(opts, agent.WithMistral(cli.MistralKey, clientopts...))
121+
}
122+
*/
121123
if cli.OpenAIKey != "" {
122124
opts = append(opts, agent.WithOpenAI(cli.OpenAIKey, clientopts...))
123125
}

cmd/llm/models.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
// Packages
99
llm "github.com/mutablelogic/go-llm"
1010
agent "github.com/mutablelogic/go-llm/pkg/agent"
11-
ollama "github.com/mutablelogic/go-llm/pkg/ollama"
1211
)
1312

1413
////////////////////////////////////////////////////////////////////////////////
@@ -83,12 +82,14 @@ func (cmd *DownloadModelCmd) Run(globals *Globals) error {
8382
}
8483
// Download the model
8584
switch agent.Name() {
86-
case "ollama":
87-
model, err := agent.(*ollama.Client).PullModel(ctx, cmd.Model)
88-
if err != nil {
89-
return err
90-
}
91-
fmt.Println(model)
85+
/*
86+
case "ollama":
87+
model, err := agent.(*ollama.Client).PullModel(ctx, cmd.Model)
88+
if err != nil {
89+
return err
90+
}
91+
fmt.Println(model)
92+
*/
9293
default:
9394
return fmt.Errorf("Agent %q does not support model download", agent.Name())
9495
}

pkg/agent/opt.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import (
44
// Packages
55
client "github.com/mutablelogic/go-client"
66
llm "github.com/mutablelogic/go-llm"
7-
anthropic "github.com/mutablelogic/go-llm/pkg/anthropic"
8-
mistral "github.com/mutablelogic/go-llm/pkg/mistral"
9-
ollama "github.com/mutablelogic/go-llm/pkg/ollama"
107
openai "github.com/mutablelogic/go-llm/pkg/openai"
118
)
129

1310
////////////////////////////////////////////////////////////////////////////////
1411
// PUBLIC METHODS
15-
12+
/*
1613
func WithOllama(endpoint string, opts ...client.ClientOpt) llm.Opt {
1714
return func(o *llm.Opts) error {
1815
client, err := ollama.New(endpoint, opts...)
@@ -45,6 +42,7 @@ func WithMistral(key string, opts ...client.ClientOpt) llm.Opt {
4542
}
4643
}
4744
}
45+
*/
4846

4947
func WithOpenAI(key string, opts ...client.ClientOpt) llm.Opt {
5048
return func(o *llm.Opts) error {

pkg/openai/completion.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package openai
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"strings"
87

98
// Packages
@@ -249,7 +248,6 @@ func streamEvent(response *Response, evt client.TextStreamEvent) error {
249248
}
250249

251250
func appendCompletion(response *Response, c *Completion) error {
252-
fmt.Println(c)
253251
// Append a new completion
254252
for {
255253
if c.Index < uint64(len(response.Completions)) {
@@ -303,6 +301,31 @@ func appendCompletion(response *Response, c *Completion) error {
303301
message.Media.Append(c.Delta.Media)
304302
}
305303

304+
// Append tool calls
305+
for i := range c.Delta.Calls {
306+
if i >= len(message.Calls) {
307+
message.Calls = append(message.Calls, toolcall{})
308+
}
309+
}
310+
311+
for i, call := range c.Delta.Calls {
312+
if call.meta.Id != "" {
313+
message.Calls[i].meta.Id = call.meta.Id
314+
}
315+
if call.meta.Index != 0 {
316+
message.Calls[i].meta.Index = call.meta.Index
317+
}
318+
if call.meta.Type != "" {
319+
message.Calls[i].meta.Type = call.meta.Type
320+
}
321+
if call.meta.Function.Name != "" {
322+
message.Calls[i].meta.Function.Name = call.meta.Function.Name
323+
}
324+
if call.meta.Function.Arguments != "" {
325+
message.Calls[i].meta.Function.Arguments += call.meta.Function.Arguments
326+
}
327+
}
328+
306329
// Return success
307330
return nil
308331
}

0 commit comments

Comments
 (0)