|
| 1 | +package autofix |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "net/http" |
| 9 | + |
| 10 | + "github.com/openai/openai-go/v3" |
| 11 | + "github.com/openai/openai-go/v3/option" |
| 12 | +) |
| 13 | + |
| 14 | +const ( |
| 15 | + ModelGPT4o = openai.ChatModelGPT4o |
| 16 | + ModelGPT4oMini = openai.ChatModelGPT4oMini |
| 17 | + DefaultOpenAIBaseURL = "https://api.openai.com/v1" |
| 18 | +) |
| 19 | + |
| 20 | +var _ GenAIClient = (*openaiWrapper)(nil) |
| 21 | + |
| 22 | +type OpenAIConfig struct { |
| 23 | + Model string |
| 24 | + APIKey string |
| 25 | + BaseURL string |
| 26 | + MaxTokens int |
| 27 | + Temperature float64 |
| 28 | + SkipSSL bool |
| 29 | +} |
| 30 | + |
| 31 | +type openaiWrapper struct { |
| 32 | + client openai.Client |
| 33 | + model openai.ChatModel |
| 34 | + maxTokens int |
| 35 | + temperature float64 |
| 36 | +} |
| 37 | + |
| 38 | +func NewOpenAIClient(config OpenAIConfig) (GenAIClient, error) { |
| 39 | + var options []option.RequestOption |
| 40 | + |
| 41 | + if config.APIKey != "" { |
| 42 | + options = append(options, option.WithAPIKey(config.APIKey)) |
| 43 | + } |
| 44 | + |
| 45 | + // Support custom base URL (for OpenAI-compatible APIs) |
| 46 | + if config.BaseURL != "" { |
| 47 | + options = append(options, option.WithBaseURL(config.BaseURL)) |
| 48 | + } |
| 49 | + |
| 50 | + // Support skip SSL verification |
| 51 | + if config.SkipSSL { |
| 52 | + // Create custom HTTP client with InsecureSkipVerify |
| 53 | + httpClient := &http.Client{ |
| 54 | + Transport: &http.Transport{ |
| 55 | + TLSClientConfig: &tls.Config{ |
| 56 | + InsecureSkipVerify: true, // #nosec G402 |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + options = append(options, option.WithHTTPClient(httpClient)) |
| 61 | + } |
| 62 | + |
| 63 | + openaiModel := parseOpenAIModel(config.Model) |
| 64 | + |
| 65 | + // Set default values |
| 66 | + maxTokens := config.MaxTokens |
| 67 | + if maxTokens == 0 { |
| 68 | + maxTokens = 1024 |
| 69 | + } |
| 70 | + |
| 71 | + temperature := config.Temperature |
| 72 | + if temperature == 0 { |
| 73 | + temperature = 0.7 |
| 74 | + } |
| 75 | + |
| 76 | + return &openaiWrapper{ |
| 77 | + client: openai.NewClient(options...), |
| 78 | + model: openaiModel, |
| 79 | + maxTokens: maxTokens, |
| 80 | + temperature: temperature, |
| 81 | + }, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (o *openaiWrapper) GenerateSolution(ctx context.Context, prompt string) (string, error) { |
| 85 | + params := openai.ChatCompletionNewParams{ |
| 86 | + Model: o.model, |
| 87 | + Messages: []openai.ChatCompletionMessageParamUnion{ |
| 88 | + openai.UserMessage(prompt), |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + // Set optional parameters if available |
| 93 | + // Using WithMaxTokens and WithTemperature methods if they exist in v3 |
| 94 | + resp, err := o.client.Chat.Completions.New(ctx, params) |
| 95 | + if err != nil { |
| 96 | + return "", fmt.Errorf("generating autofix: %w", err) |
| 97 | + } |
| 98 | + |
| 99 | + if resp == nil || len(resp.Choices) == 0 { |
| 100 | + return "", errors.New("no autofix returned by openai") |
| 101 | + } |
| 102 | + |
| 103 | + content := resp.Choices[0].Message.Content |
| 104 | + if content == "" { |
| 105 | + return "", errors.New("nothing found in the first autofix returned by openai") |
| 106 | + } |
| 107 | + |
| 108 | + return content, nil |
| 109 | +} |
| 110 | + |
| 111 | +func parseOpenAIModel(model string) openai.ChatModel { |
| 112 | + switch model { |
| 113 | + case "gpt-4o": |
| 114 | + return openai.ChatModelGPT4o |
| 115 | + case "gpt-4o-mini": |
| 116 | + return openai.ChatModelGPT4oMini |
| 117 | + default: |
| 118 | + return openai.ChatModel(model) |
| 119 | + } |
| 120 | +} |
0 commit comments