drainStream reads the entire content of a chat completion stream and returns the concatenated delta content. The stream is always closed before returning.
(stream chat.MessageStream)
| 168 | // returns the concatenated delta content. The stream is always closed before |
| 169 | // returning. |
| 170 | func drainStream(stream chat.MessageStream) (string, error) { |
| 171 | defer stream.Close() |
| 172 | |
| 173 | var content strings.Builder |
| 174 | for { |
| 175 | response, err := stream.Recv() |
| 176 | if errors.Is(err, io.EOF) { |
| 177 | return content.String(), nil |
| 178 | } |
| 179 | if err != nil { |
| 180 | return "", err |
| 181 | } |
| 182 | if len(response.Choices) > 0 { |
| 183 | content.WriteString(response.Choices[0].Delta.Content) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // buildPrompt formats the user messages into the system+user message pair |
| 189 | // sent to the model. |
no test coverage detected