Repository: GoogleCloudPlatform/kubectl-ai
File: gollm/openai.go (Go)

Code before:

	completion, err := c.client.Chat.Completions.New(ctx, chatReq)
	if err != nil {
		return nil, fmt.Errorf("failed to generate OpenAI completion: %w", err)
	}

	// Check if there are choices and a message
	if len(completion.Choices) == 0 || completion.Choices[0].Message.Content == "" {
		return nil, errors.New("received an empty response from OpenAI")
	}

	// Return the content of the first choice
	resp := &simpleCompletionResponse{
		content: completion.Choices[0].Message.Content,
	}

	return resp, nil
}

// SetResponseSchema is not implemented yet.
func (c *OpenAIClient) SetResponseSchema(schema *Schema) error {
	klog.Warning("OpenAIClient.SetResponseSchema is not implemented yet")
	return nil
}

// ListModels returns slice of strings with model IDs, may not work for openAI compatible providers
func (c *OpenAIClient) ListModels(ctx context.Context) ([]string, error) {

	var opt option.RequestOption = nil

	endpoint := os.Getenv("OPENAI_ENDPOINT") // if another endpoint is used
	if endpoint != "" {
		opt = option.WithBaseURL(endpoint)
	}

	res, err := c.client.Models.List(ctx, opt)
	if err != nil {

		if endpoint != "" {
			return nil, fmt.Errorf(`
			There was an error in listing models from %s. 
			Please verify if the endpoint used is fully OpenAI compatible`,
				endpoint)
		}
		return nil, fmt.Errorf("There was an error in listing models from OpenAI")
	}

	modelsId := make([]string, 0, len(res.Data))
	for _, model := range res.Data {
		modelsId = append(modelsId, model.ID)
	}

Review comment (suggestion):
The comment could be more descriptive about why it might not work for OpenAI-compatible providers and should follow Go's documentation standard with the function name as the first word.

```diff
-// ListModels returns slice of strings with model IDs, may not work for openAI compatible providers
+// ListModels returns a slice of strings with model IDs.
+// Note: This may not work with all OpenAI-compatible providers if they don't fully implement
+// the Models.List endpoint or return data in a different format.
```

Code after:

	completion, err := c.client.Chat.Completions.New(ctx, chatReq)
	if err != nil {
		return nil, fmt.Errorf("failed to generate OpenAI completion: %w", err)
	}

	// Check if there are choices and a message
	if len(completion.Choices) == 0 || completion.Choices[0].Message.Content == "" {
		return nil, errors.New("received an empty response from OpenAI")
	}

	// Return the content of the first choice
	resp := &simpleCompletionResponse{
		content: completion.Choices[0].Message.Content,
	}

	return resp, nil
}

// SetResponseSchema is not implemented yet.
func (c *OpenAIClient) SetResponseSchema(schema *Schema) error {
	klog.Warning("OpenAIClient.SetResponseSchema is not implemented yet")
	return nil
}

// ListModels returns a slice of strings with model IDs.
// Note: This may not work with all OpenAI-compatible providers if they don't fully implement
// the Models.List endpoint or return data in a different format.
func (c *OpenAIClient) ListModels(ctx context.Context) ([]string, error) {

	var opts []option.RequestOption

	endpoint := os.Getenv("OPENAI_ENDPOINT") // if another endpoint is used
	if endpoint != "" {
		opts = append(opts, option.WithBaseURL(endpoint))
	}

	res, err := c.client.Models.List(ctx, opts...)
	if err != nil {

		if endpoint != "" {
			return nil, fmt.Errorf(`
			There was an error in listing models from %s. 
			Please verify if the endpoint used is fully OpenAI compatible`,
				endpoint)
		}
		return nil, fmt.Errorf("There was an error in listing models from OpenAI")
	}

	modelsIDs := make([]string, 0, len(res.Data))
	for _, model := range res.Data {