kubectl-ai icon indicating copy to clipboard operation
kubectl-ai copied to clipboard

Enhance OpenAI Client Initialization and Model Handling with Environment Variable Support and Default Model Logic

Open tuannvm opened this issue 7 months ago • 0 comments

PR Summary

This update refactors the OpenAI client initialization and configuration to improve maintainability and clarity. It involves loading environment variables at package-level during initialization, adjusting model selection logic to prioritize explicit parameters and configuration, and simplifying model management by relying solely on the preconfigured client instance. Additionally, error handling is improved to propagate context more clearly. These changes streamline the code flow, reduce redundancy, and ensure environment-based configuration is loaded consistently.

Affected Modules

  • gollm/openai.go
  • Initialization routines and environment variable handling
  • Client creation and model listing functions
  • Chat session management and model selection logic

Key Details

  • Environment variables (OPENAI_API_KEY, OPENAI_ENDPOINT, OPENAI_API_BASE, OPENAI_MODEL) are loaded once during package initialization and stored in package scope.
  • The newOpenAIClientFactory function now calls NewOpenAIClient without additional parameters, relying on the preloaded env vars.
  • The getOpenAIModel function determines the model through explicit arguments, configuration, and defaults.
  • Error messages during model listing are made more descriptive with proper error wrapping.
  • The StartChat method now confidently retrieves the model via getOpenAIModel, avoiding redundant defaults.

Potential Impacts

  • Environment variables must be correctly configured before application startup.
  • Changes assume no immediate need for per-instance overrides of environment variables.
  • Error handling now provides clearer diagnostics, aiding troubleshooting.
  • Dependency externalities such as openai-go client stability remain unchanged.

tuannvm avatar May 10 '25 05:05 tuannvm