@@ -23,6 +23,7 @@ import (
23
23
type AssistantChatCmdOptions struct {
24
24
name string
25
25
message string
26
+ stream bool
26
27
json bool
27
28
}
28
29
@@ -43,19 +44,20 @@ func NewAssistantChatCmd() *cobra.Command {
43
44
return
44
45
}
45
46
46
- // If no message is provided drop them into chat
47
+ // If no message is provided drop them into interactive chat
47
48
if options .message == "" {
48
49
startChat (options .name )
49
50
} else {
50
51
// If message is provided, send it to the assistant
51
- sendMessage (options .name , options .message )
52
+ sendMessage (options .name , options .message , options . stream )
52
53
}
53
54
},
54
55
}
55
56
56
57
cmd .Flags ().StringVarP (& options .name , "name" , "n" , "" , "name of the assistant to chat with" )
57
- cmd .Flags ().BoolVar (& options .json , "json" , false , "output as JSON" )
58
58
cmd .Flags ().StringVarP (& options .message , "message" , "m" , "" , "your message to the assistant" )
59
+ cmd .Flags ().BoolVarP (& options .stream , "stream" , "s" , false , "stream chat message responses" )
60
+ cmd .Flags ().BoolVar (& options .json , "json" , false , "output as JSON" )
59
61
cmd .MarkFlagRequired ("content" )
60
62
61
63
cmd .AddCommand (NewAssistantChatClearCmd ())
@@ -94,7 +96,8 @@ func startChat(asstName string) {
94
96
checkForChatCommands (text )
95
97
96
98
if text != "" {
97
- _ , err := sendMessage (asstName , text )
99
+ // Stream here since we're in interactive chat mode
100
+ _ , err := sendMessage (asstName , text , true )
98
101
if err != nil {
99
102
pcio .Printf ("Error sending message: %s\n " , err )
100
103
continue
@@ -103,27 +106,54 @@ func startChat(asstName string) {
103
106
}
104
107
}
105
108
106
- func sendMessage (asstName string , message string ) (* models.ChatCompletionModel , error ) {
109
+ func sendMessage (asstName string , message string , stream bool ) (* models.ChatCompletionModel , error ) {
107
110
response := & models.ChatCompletionModel {}
108
111
109
- err := style .Spinner ("" , func () error {
110
- chatResponse , err := assistants .GetAssistantChatCompletions (asstName , message )
112
+ var chatGetter func () error
113
+ if stream {
114
+ chatGetter = streamChatResponse (response , asstName , message , stream )
115
+ } else {
116
+ chatGetter = getChatResponse (response , asstName , message , stream )
117
+ }
118
+
119
+ err := chatGetter ()
120
+ if err != nil {
121
+ return nil , err
122
+ }
123
+
124
+ return response , nil
125
+ }
126
+
127
+ func getChatResponse (resp * models.ChatCompletionModel , asstName string , message string , stream bool ) func () error {
128
+ return func () error {
129
+ chatResponse , err := assistants .GetAssistantChatCompletions (asstName , message , stream )
111
130
if err != nil {
112
131
exit .Error (err )
113
132
}
114
133
115
- response = chatResponse
134
+ resp = chatResponse
116
135
117
136
for _ , choice := range chatResponse .Choices {
118
137
presenters .PrintAssistantChatResponse (choice .Message .Content )
119
138
}
120
139
return nil
121
- })
122
- if err != nil {
123
- return nil , err
124
140
}
141
+ }
125
142
126
- return response , nil
143
+ func streamChatResponse (resp * models.ChatCompletionModel , asstName string , message string , stream bool ) func () error {
144
+ return func () error {
145
+ chatResponse , err := assistants .GetAssistantChatCompletions (asstName , message , stream )
146
+ if err != nil {
147
+ exit .Error (err )
148
+ }
149
+
150
+ resp = chatResponse
151
+
152
+ for _ , choice := range chatResponse .Choices {
153
+ presenters .PrintAssistantChatResponse (choice .Message .Content )
154
+ }
155
+ return nil
156
+ }
127
157
}
128
158
129
159
func displayChatHistory (asstName string , maxNoMsgs int ) {
@@ -137,7 +167,7 @@ func displayChatHistory(asstName string, maxNoMsgs int) {
137
167
presenters .PrintChatHistory (chat , maxNoMsgs )
138
168
}
139
169
140
- // This function checks the input for accepted chat commands
170
+ // Checks the input for accepted chat commands
141
171
func checkForChatCommands (text string ) {
142
172
switch text {
143
173
case "exit()" :
0 commit comments