-
Notifications
You must be signed in to change notification settings - Fork 295
Add support for the OpenAI API #242
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
Summary of ChangesHello @levigross, 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 models into the system. It establishes the necessary components to send requests, process responses, and manage streaming interactions with OpenAI's API, ensuring compatibility with existing LLM interfaces. The current implementation focuses on core generation capabilities, with plans to extend support for advanced features like tool calling in future iterations. 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 introduces basic support for the OpenAI API by creating an adapter that conforms to the project's model.LLM interface. The implementation is well-structured, separating concerns like request building, response parsing, and stream handling into their own files. However, I've identified a few critical issues, including a couple of compilation errors and incorrect pointer semantics in the model constructor. I also found a potential race condition in stream handling and a gap in error handling for function call tracking. Addressing these points will significantly improve the robustness and correctness of this new integration.
| return m.generate(ctx, params) | ||
| } | ||
|
|
||
| func (m *openAIModel) generate(ctx context.Context, params responses.ResponseNewParams) iter.Seq2[*model.LLMResponse, error] { |
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.
Could you please support chat completion? it's better for compatible with most providers.
This PR enables basic support for OpenAI models (and endpoints that expose a OpenAI API compatible API interface). I am stressing on *basic* support because we will leave tool calling to the next PR :)
|
@gemini-code-assist please rereview this code |
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 introduces a comprehensive and well-structured adapter for the OpenAI API, implementing the model.LLM interface. The implementation covers request and response conversion, streaming support, and robust error handling for unsupported features. The code is well-tested with good unit and integration test coverage.
My main feedback is a critical issue in NewModel that will prevent compilation due to an attempt to access an unexported field on the openai.Client struct. I've provided suggestions to refactor this to use a pointer to the client, which is a more idiomatic and robust approach for validation in Go. Once this is addressed, the package should be in great shape.
| func NewModel(_ context.Context, modelName string, client openai.Client) (model.LLM, error) { | ||
| // We drop the context because OpenAI doesn't take a context when creating a new Client | ||
| if modelName == "" { | ||
| return nil, ErrModelNameRequired | ||
| } | ||
| if len(client.Options) == 0 { | ||
| return nil, ErrClientRequired | ||
| } | ||
| return &openAIModel{ | ||
| client: &client, | ||
| name: modelName, | ||
| }, 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.
To fix the compilation issue with client.Options and improve the function's contract, the signature of NewModel should be changed to accept a pointer to an openai.Client. This allows for a nil check to validate the client's presence.
Consequently, the openAIModel struct should store this pointer directly.
| func NewModel(_ context.Context, modelName string, client openai.Client) (model.LLM, error) { | |
| // We drop the context because OpenAI doesn't take a context when creating a new Client | |
| if modelName == "" { | |
| return nil, ErrModelNameRequired | |
| } | |
| if len(client.Options) == 0 { | |
| return nil, ErrClientRequired | |
| } | |
| return &openAIModel{ | |
| client: &client, | |
| name: modelName, | |
| }, nil | |
| func NewModel(_ context.Context, modelName string, client *openai.Client) (model.LLM, error) { | |
| // We drop the context because OpenAI doesn't take a context when creating a new Client | |
| if modelName == "" { | |
| return nil, ErrModelNameRequired | |
| } | |
| if client == nil { | |
| return nil, ErrClientRequired | |
| } | |
| return &openAIModel{ | |
| client: client, | |
| name: modelName, | |
| }, nil | |
| } |
| option.WithBaseURL(server.URL+"/v1"), | ||
| ) | ||
|
|
||
| llm, err := NewModel(t.Context(), openai.ChatModelGPT4oMini, client) |
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.
| tests := []struct { | ||
| name string | ||
| modelName openai.ChatModel | ||
| client openai.Client | ||
| wantErr error | ||
| }{ | ||
| { | ||
| name: "missing model name", | ||
| modelName: "", | ||
| client: client, | ||
| wantErr: ErrModelNameRequired, | ||
| }, | ||
| { | ||
| name: "missing client", | ||
| modelName: openai.ChatModelGPT4o, | ||
| client: openai.Client{}, | ||
| wantErr: ErrClientRequired, | ||
| }, | ||
| } |
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.
Following the suggested change in openai.go to use a pointer for the openai.Client, this test table needs to be updated to reflect the new signature of NewModel. The client field in the test struct should be a pointer, and the test cases should be adjusted accordingly. For the "missing client" case, nil should be passed to trigger ErrClientRequired.
| tests := []struct { | |
| name string | |
| modelName openai.ChatModel | |
| client openai.Client | |
| wantErr error | |
| }{ | |
| { | |
| name: "missing model name", | |
| modelName: "", | |
| client: client, | |
| wantErr: ErrModelNameRequired, | |
| }, | |
| { | |
| name: "missing client", | |
| modelName: openai.ChatModelGPT4o, | |
| client: openai.Client{}, | |
| wantErr: ErrClientRequired, | |
| }, | |
| } | |
| tests := []struct { | |
| name string | |
| modelName openai.ChatModel | |
| client *openai.Client | |
| wantErr error | |
| }{ | |
| { | |
| name: "missing model name", | |
| modelName: "", | |
| client: &client, | |
| wantErr: ErrModelNameRequired, | |
| }, | |
| { | |
| name: "missing client", | |
| modelName: openai.ChatModelGPT4o, | |
| client: nil, | |
| wantErr: ErrClientRequired, | |
| }, | |
| } |
|
@dpasiukevich What do I need to do to get this PR reviewed? 😀 |
This PR enables basic support for OpenAI models (and endpoints that expose a OpenAI API compatible API interface).
I am stressing on basic support because we will leave tool calling to the next PR :)