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

Code before:
	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)
	}

	return modelsId, nil
}

// --- Chat Session Implementation ---

type openAIChatSession struct {
	client              openai.Client
	history             []openai.ChatCompletionMessageParamUnion
	model               string
	functionDefinitions []*FunctionDefinition            // Stored in gollm format
	tools               []openai.ChatCompletionToolParam // Stored in OpenAI format
}

// Ensure openAIChatSession implements the Chat interface.
var _ Chat = (*openAIChatSession)(nil)

// SetFunctionDefinitions stores the function definitions and converts them to OpenAI format.
func (cs *openAIChatSession) SetFunctionDefinitions(defs []*FunctionDefinition) error {
	cs.functionDefinitions = defs
	cs.tools = nil // Clear previous tools
	if len(defs) > 0 {

Review comment (suggestion):
The variable name modelsId doesn't follow Go naming conventions. It should be modelIDs or modelIds for better readability.

```diff
-	modelsId := make([]string, 0, len(res.Data))
-	for _, model := range res.Data {
-		modelsId = append(modelsId, model.ID)
-	}
+	modelIDs := make([]string, 0, len(res.Data))
+	for _, model := range res.Data {
+		modelIDs = append(modelIDs, model.ID)
+	}
```

Code after:
	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 {
		modelsIDs = append(modelsIDs, model.ID)
	}

	return modelsIDs, nil
}

// --- Chat Session Implementation ---

type openAIChatSession struct {
	client              openai.Client
	history             []openai.ChatCompletionMessageParamUnion
	model               string
	functionDefinitions []*FunctionDefinition            // Stored in gollm format
	tools               []openai.ChatCompletionToolParam // Stored in OpenAI format
}

// Ensure openAIChatSession implements the Chat interface.
var _ Chat = (*openAIChatSession)(nil)

// SetFunctionDefinitions stores the function definitions and converts them to OpenAI format.
func (cs *openAIChatSession) SetFunctionDefinitions(defs []*FunctionDefinition) error {
	cs.functionDefinitions = defs