kubectl-ai
kubectl-ai copied to clipboard
Enhance OpenAI Client Initialization and Model Handling with Environment Variable Support and Default Model Logic
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
newOpenAIClientFactoryfunction now callsNewOpenAIClientwithout additional parameters, relying on the preloaded env vars. - The
getOpenAIModelfunction determines the model through explicit arguments, configuration, and defaults. - Error messages during model listing are made more descriptive with proper error wrapping.
- The
StartChatmethod now confidently retrieves the model viagetOpenAIModel, 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-goclient stability remain unchanged.