-
Notifications
You must be signed in to change notification settings - Fork 70
feat(openai): implement openai interface #276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 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 |
Summary of ChangesHello @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
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this 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.
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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:
- Add a
globalOpenAIClient *openai.Client
variable to the globalvar
block. - In
InitOpenAIConfig
, setglobalOpenAIClient = nil
to handle potential re-initialization. - Modify this function to check if
globalOpenAIClient
isnil
. If it is, create the client and store it. Otherwise, return the cached client.
Signed-off-by: kasanatte <[email protected]>
caf8ef9
to
4e832b4
Compare
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?: