|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + |
| 8 | + "github.com/openai/openai-go" |
| 9 | + "maragu.dev/gai" |
| 10 | +) |
| 11 | + |
| 12 | +type ChatCompleteModel string |
| 13 | + |
| 14 | +const ( |
| 15 | + ChatCompleteModelGPT4o = ChatCompleteModel(openai.ChatModelGPT4o) |
| 16 | + ChatCompleteModelGPT4oMini = ChatCompleteModel(openai.ChatModelGPT4oMini) |
| 17 | +) |
| 18 | + |
| 19 | +type ChatCompleter struct { |
| 20 | + Client *openai.Client |
| 21 | + log *slog.Logger |
| 22 | + model ChatCompleteModel |
| 23 | +} |
| 24 | + |
| 25 | +type NewChatCompleterOptions struct { |
| 26 | + Model ChatCompleteModel |
| 27 | +} |
| 28 | + |
| 29 | +func (c *Client) NewChatCompleter(opts NewChatCompleterOptions) *ChatCompleter { |
| 30 | + return &ChatCompleter{ |
| 31 | + Client: c.Client, |
| 32 | + log: c.log, |
| 33 | + model: opts.Model, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// ChatComplete satisfies [gai.ChatCompleter]. |
| 38 | +func (c *ChatCompleter) ChatComplete(ctx context.Context, req gai.ChatCompleteRequest) (gai.ChatCompleteResponse, error) { |
| 39 | + var messages []openai.ChatCompletionMessageParamUnion |
| 40 | + |
| 41 | + for _, m := range req.Messages { |
| 42 | + switch m.Role { |
| 43 | + case gai.MessageRoleUser: |
| 44 | + var parts []openai.ChatCompletionContentPartUnionParam |
| 45 | + for _, part := range m.Parts { |
| 46 | + switch part.Type { |
| 47 | + case gai.MessagePartTypeText: |
| 48 | + parts = append(parts, openai.TextPart(part.Text())) |
| 49 | + default: |
| 50 | + panic("not implemented") |
| 51 | + } |
| 52 | + } |
| 53 | + messages = append(messages, openai.UserMessageParts(parts...)) |
| 54 | + |
| 55 | + default: |
| 56 | + panic("not implemented") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + params := openai.ChatCompletionNewParams{ |
| 61 | + Messages: openai.F(messages), |
| 62 | + Model: openai.F(openai.ChatModel(c.model)), |
| 63 | + } |
| 64 | + |
| 65 | + if req.Temperature != nil { |
| 66 | + params.Temperature = openai.F(req.Temperature.Float64()) |
| 67 | + } |
| 68 | + |
| 69 | + stream := c.Client.Chat.Completions.NewStreaming(ctx, params) |
| 70 | + |
| 71 | + return gai.NewChatCompleteResponse(func(yield func(gai.MessagePart, error) bool) { |
| 72 | + defer func() { |
| 73 | + if err := stream.Close(); err != nil { |
| 74 | + c.log.Info("Error closing stream", "error", err) |
| 75 | + } |
| 76 | + }() |
| 77 | + |
| 78 | + var acc openai.ChatCompletionAccumulator |
| 79 | + for stream.Next() { |
| 80 | + chunk := stream.Current() |
| 81 | + acc.AddChunk(chunk) |
| 82 | + |
| 83 | + if _, ok := acc.JustFinishedContent(); ok { |
| 84 | + break |
| 85 | + } |
| 86 | + |
| 87 | + if _, ok := acc.JustFinishedToolCall(); ok { |
| 88 | + continue |
| 89 | + // TODO handle tool call |
| 90 | + // println("Tool call stream finished:", tool.Index, tool.Name, tool.Arguments) |
| 91 | + } |
| 92 | + |
| 93 | + if refusal, ok := acc.JustFinishedRefusal(); ok { |
| 94 | + yield(gai.MessagePart{}, fmt.Errorf("refusal: %v", refusal)) |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + if len(chunk.Choices) > 0 { |
| 99 | + if !yield(gai.TextMessagePart(chunk.Choices[0].Delta.Content), nil) { |
| 100 | + return |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if err := stream.Err(); err != nil { |
| 106 | + yield(gai.MessagePart{}, err) |
| 107 | + } |
| 108 | + }), nil |
| 109 | +} |
| 110 | + |
| 111 | +var _ gai.ChatCompleter = (*ChatCompleter)(nil) |
0 commit comments