Skip to content

Conversation

kasanatte
Copy link
Contributor

What type of PR is this?
/kind feature

What this PR does / why we need it:
This PR implements the interface for connecting to the OpenAI API. It provides helper functions to create a configured OpenAI client based on the application's settings. This is a core component for providing the LLM capabilities to the AI Assistant.

Which issue(s) this PR fixes:
Fixes #273

Special notes for your reviewer:
It depends on the changes from PR #274

Does this PR introduce a user-facing change?:

NONE

@karmada-bot karmada-bot added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 26, 2025
@karmada-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign kevin-wangzefeng for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Copy link

Summary of Changes

Hello @kasanatte, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request lays the groundwork for integrating OpenAI's Large Language Model (LLM) capabilities into the AI Assistant. It establishes a robust and thread-safe mechanism for configuring and accessing the OpenAI API, which is a critical prerequisite for all future LLM-powered features. The changes focus on setting up the core client and configuration management without introducing any user-facing functionality yet.

Highlights

  • New OpenAI Interface: Introduces a new pkg/openai package dedicated to managing connectivity and configuration for the OpenAI API.
  • Client Configuration and Retrieval: Provides helper functions, InitOpenAIConfig and GetOpenAIClient, to initialize and retrieve a properly configured OpenAI client based on application settings, including API key and endpoint.
  • Thread-Safe Access: Implements sync.RWMutex to ensure thread-safe access and modification of the global OpenAI configuration, preventing race conditions.
  • Model and Configuration Status: Includes functions GetOpenAIModel to retrieve the configured OpenAI model (with a default fallback) and IsOpenAIConfigured to check if the OpenAI integration is properly set up.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@karmada-bot karmada-bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 26, 2025
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a new package to interface with the OpenAI API. The implementation is straightforward, using global variables and a mutex for configuration management. My review includes two main suggestions to improve the design. First, I recommend decoupling the new openai package from the application-specific options package by passing configuration primitives instead of the whole options struct. Second, for efficiency, I suggest caching the OpenAI client instance instead of creating a new one on every request. These changes will enhance the code's modularity, testability, and performance.

Comment on lines +36 to +47
func InitOpenAIConfig(opts *options.Options) {
openAIMutex.Lock()
defer openAIMutex.Unlock()

globalOpenAIOptions = opts
openAIInitialized = true

klog.InfoS("OpenAI configuration initialized",
"hasAPIKey", opts.OpenAIAPIKey != "",
"model", opts.OpenAIModel,
"endpoint", opts.OpenAIEndpoint)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function takes the entire *options.Options struct, creating a tight coupling between the pkg/openai package and the application-specific cmd/api/app/options package. To improve modularity and make this package more reusable and testable, it's better to decouple pkg code from cmd code.

Consider changing the function signature to accept only the necessary configuration values as arguments, for example: func InitOpenAIConfig(apiKey, model, endpoint string). The calling code in the cmd layer would then be responsible for extracting these values from the options and passing them in. You would also need to adjust the global variables in this package to store these values directly instead of the whole options object.

Comment on lines +50 to +68
func GetOpenAIClient() (*openai.Client, error) {
openAIMutex.RLock()
defer openAIMutex.RUnlock()

if !openAIInitialized || globalOpenAIOptions == nil {
return nil, errors.New("OpenAI not initialized, call InitOpenAIConfig first")
}

if globalOpenAIOptions.OpenAIAPIKey == "" {
return nil, errors.New("OpenAI API key not configured")
}

config := openai.DefaultConfig(globalOpenAIOptions.OpenAIAPIKey)
if globalOpenAIOptions.OpenAIEndpoint != "" {
config.BaseURL = globalOpenAIOptions.OpenAIEndpoint
}

return openai.NewClientWithConfig(config), nil
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This function creates a new openai.Client instance on every call. This is inefficient because it results in unnecessary object allocations. It's a better practice to create the client once and reuse the instance for subsequent calls, as go-openai clients are safe for concurrent use.

I recommend caching the client after its first creation. You can implement lazy initialization using the existing openAIMutex to ensure thread safety. For this, you would:

  1. Add a globalOpenAIClient *openai.Client variable to the global var block.
  2. In InitOpenAIConfig, set globalOpenAIClient = nil to handle potential re-initialization.
  3. Modify this function to check if globalOpenAIClient is nil. If it is, create the client and store it. Otherwise, return the cached client.

@kasanatte kasanatte force-pushed the feature/openai-interface branch from caf8ef9 to 4e832b4 Compare September 27, 2025 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/feature Categorizes issue or PR as related to a new feature. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Umbrella][Summer OSPP 2025] Integrate Karmada-MCP-Server into the Karmada Dashboard

2 participants