|
| 1 | +package gogpt |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "encoding/json" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | +) |
| 11 | + |
| 12 | +type ChatCompletionStreamChoiceDelta struct { |
| 13 | + Content string `json:"content"` |
| 14 | +} |
| 15 | + |
| 16 | +type ChatCompletionStreamChoice struct { |
| 17 | + Index int `json:"index"` |
| 18 | + Delta ChatCompletionStreamChoiceDelta `json:"delta"` |
| 19 | + FinishReason string `json:"finish_reason"` |
| 20 | +} |
| 21 | + |
| 22 | +type ChatCompletionStreamResponse struct { |
| 23 | + ID string `json:"id"` |
| 24 | + Object string `json:"object"` |
| 25 | + Created int64 `json:"created"` |
| 26 | + Model string `json:"model"` |
| 27 | + Choices []ChatCompletionStreamChoice `json:"choices"` |
| 28 | +} |
| 29 | + |
| 30 | +// ChatCompletionStream |
| 31 | +// Note: Perhaps it is more elegant to abstract Stream using generics. |
| 32 | +type ChatCompletionStream struct { |
| 33 | + emptyMessagesLimit uint |
| 34 | + isFinished bool |
| 35 | + |
| 36 | + reader *bufio.Reader |
| 37 | + response *http.Response |
| 38 | +} |
| 39 | + |
| 40 | +func (stream *ChatCompletionStream) Recv() (response ChatCompletionStreamResponse, err error) { |
| 41 | + if stream.isFinished { |
| 42 | + err = io.EOF |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + var emptyMessagesCount uint |
| 47 | + |
| 48 | +waitForData: |
| 49 | + line, err := stream.reader.ReadBytes('\n') |
| 50 | + if err != nil { |
| 51 | + return |
| 52 | + } |
| 53 | + |
| 54 | + var headerData = []byte("data: ") |
| 55 | + line = bytes.TrimSpace(line) |
| 56 | + if !bytes.HasPrefix(line, headerData) { |
| 57 | + emptyMessagesCount++ |
| 58 | + if emptyMessagesCount > stream.emptyMessagesLimit { |
| 59 | + err = ErrTooManyEmptyStreamMessages |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + goto waitForData |
| 64 | + } |
| 65 | + |
| 66 | + line = bytes.TrimPrefix(line, headerData) |
| 67 | + if string(line) == "[DONE]" { |
| 68 | + stream.isFinished = true |
| 69 | + err = io.EOF |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + err = json.Unmarshal(line, &response) |
| 74 | + return |
| 75 | +} |
| 76 | + |
| 77 | +func (stream *ChatCompletionStream) Close() { |
| 78 | + stream.response.Body.Close() |
| 79 | +} |
| 80 | + |
| 81 | +func (stream *ChatCompletionStream) GetResponse() *http.Response { |
| 82 | + return stream.response |
| 83 | +} |
| 84 | + |
| 85 | +// CreateChatCompletionStream — API call to create a chat completion w/ streaming |
| 86 | +// support. It sets whether to stream back partial progress. If set, tokens will be |
| 87 | +// sent as data-only server-sent events as they become available, with the |
| 88 | +// stream terminated by a data: [DONE] message. |
| 89 | +func (c *Client) CreateChatCompletionStream( |
| 90 | + ctx context.Context, |
| 91 | + request ChatCompletionRequest, |
| 92 | +) (stream *ChatCompletionStream, err error) { |
| 93 | + request.Stream = true |
| 94 | + req, err := c.newStreamRequest(ctx, "POST", "/chat/completions", request) |
| 95 | + if err != nil { |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + resp, err := c.config.HTTPClient.Do(req) //nolint:bodyclose // body is closed in stream.Close() |
| 100 | + if err != nil { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + stream = &ChatCompletionStream{ |
| 105 | + emptyMessagesLimit: c.config.EmptyMessagesLimit, |
| 106 | + reader: bufio.NewReader(resp.Body), |
| 107 | + response: resp, |
| 108 | + } |
| 109 | + return |
| 110 | +} |
0 commit comments