diff --git a/examples/go-chat/main.go b/examples/go-chat/main.go index 07430305..426ba137 100644 --- a/examples/go-chat/main.go +++ b/examples/go-chat/main.go @@ -1,9 +1,11 @@ package main import ( + "bufio" "context" "fmt" "log" + "os" "github.com/ollama/ollama/api" ) @@ -19,18 +21,6 @@ func main() { Role: "system", Content: "Provide very brief, concise responses", }, - api.Message{ - Role: "user", - Content: "Name some unusual animals", - }, - api.Message{ - Role: "assistant", - Content: "Monotreme, platypus, echidna", - }, - api.Message{ - Role: "user", - Content: "which of these is the most dangerous?", - }, } ctx := context.Background() @@ -39,13 +29,29 @@ func main() { Messages: messages, } + respString := "" respFunc := func(resp api.ChatResponse) error { - fmt.Print(resp.Message.Content) + respString += resp.Message.Content + if resp.Done { + fmt.Println("\t", respString) + messages = append(messages, api.Message{ + Role: "assistant", + Content: respString, + }) + respString = "" + } return nil } - err = client.Chat(ctx, req, respFunc) - if err != nil { - log.Fatal(err) + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + req.Messages = append(req.Messages, api.Message{ + Role: "user", + Content: scanner.Text(), + }) + err = client.Chat(ctx, req, respFunc) + if err != nil { + log.Fatal(err) + } } }