-
Notifications
You must be signed in to change notification settings - Fork 54
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
Builders are unnecessarily complex in some cases #53
Comments
Since ChatCompletionUserMessageParam used, why role is not set automatically, it's very annoying.
` |
Yes, I agree! We are actually planning to update the SDK to automatically set this value for you.
In Java, builders are the common approach to make construction of complex deeply nested objects readable and flexible. Otherwise you'd have to pass the parameters to a constructor in the right order, which is easy to get wrong. Curious if you encountered any difficulties other than the |
still good approach |
Are you referring to the builders or something else? |
ya i am talking regarding builder |
Well, using builders make sense in SOME situations where you have 6-7 or more constructor arguments
I am capable to pass 3-4-5 parameters to constructor in right order, this is not a problem (and having Javadoc helps) In my code I have to do this (unfortunately):
This code above seriously over-engineered (because it use over-engineered API):
The question is WHY?
|
I hear you, but there are other benefits:
I agree this code is not ideal! As I mentioned above, we're working on improving this part. Here are some of the upcoming improvements that we're planning to make before the beta release (SDK is currently alpha):
To answer "why" it's currently like this: we are generating this code from the OpenAI OpenAPI spec using our Stainless code generator and we're still iterating! I can assure you we'll work out all the issues before the GA release of the SDK. |
I didn't see this and opened up a redundant one. I think we should probably distance from polarizing thoughts (builder good vs bad) and more into making a simpler api, like exist in other languages in openai and like exist in other SDKs in java this was the issue I raised about this vs python #81 Here's an example of spring-ai which is also a lot easier to read due to a lot less symbols and methods in the foreground, despite it using a builder pattern ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
OpenAiChatOptions.builder()
.model("gpt-4-o")
.temperature(0.4)
.build()
)); If we decide to use an approach that looks like auto-generation, what's likely to happen is folks will release their own libraries that wrap this one to avoid complicated code. That's fine, but there are drawbacks especially in a new project notably version lock-ups, etc. I really hope we can lean into the UX here and solve it here. |
Did you read through the future improvements I mentioned above that should simplify the API? Any thoughts about that? |
Enums(or constants), shorter factory names, and overloads sound like progress. A 'diff' block quote (triple backtick) of the before after using the README example might help get buy in from others as it will show the end game in a way that doesn't require a mental refactoring tool ;) |
I also want to clarify that I strongly believe we can generate a great SDK that doesn't require wrapping. The Python SDK is fully generated by Stainless too :) We care a great deal about good UX. It's just that this SDK is still in alpha, so we're iterating.
Good point :) My initial goal is something like this: import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ChatCompletionUserMessageParam;
import com.openai.models.ChatModel;
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.addMessage(
ChatCompletionUserMessageParam.builder()
.content("Say this is a test")
.build())
.model(ChatModel.O1)
.build();
ChatCompletion chatCompletion = client.chat().completions().create(params); You could call And after that, I'm thinking of maybe also adding overloads for user and assistant messages (e.g. import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ChatModel;
ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
.addUserMessage("Say this is a test")
.model(ChatModel.O1)
.build();
ChatCompletion chatCompletion = client.chat().completions().create(params); |
lovely. I might suggest that in the optimized case you could consider addMessage to default to user, or have a param of the role. regardless, looks like a fantastic outcome. |
ps motivation is important. I work at Elastic and we are working on opentelemetry instrumentation for this SDK. I want to put an "easy example" together and then have it traced (technically also metrics and logs) by default. So, I ran into this as I wondered if it was better to do a showcase with something as simple as we can make it vs the now state. |
As of v0.13.0 this entirely implemented with the exception of |
curious.. was this change intentional? - val.asResponseFormatText().type().toString());
+ val.asResponseFormatText()._type().toString()); |
Yup, the convention we have is that raw For example, in this case you don't really need to ever access the Let me know if you have any thoughts or feedback about that! |
@TomerAberbach thanks. that part was in internal instrumentation, so ok if stable. For user-side, what is the replacement constant for this, assuming you want to say set it to assistant? .role(ChatCompletionUserMessageParam.Role.USER) |
also these. Not sure the new constants to use here.
|
The replacement is you don't have to specify it at all! The fact that the role is It is also no longer necessary to set So for any constant that you've noticed has disappeared in v0.13.0, you can assume that we now automatically set it for you. |
Thanks for the help, I suspect this will build just need to wait on it to finish ;) elastic/elastic-otel-java#514 |
Nice! Do let me know if you have any feedback or thoughts on the changes. e.g. if you find it intuitive, if you find something confusing, etc. |
So far, I really like the changes and would close out this issue. The drift isn't a big deal and worth it. |
@TomerAberbach we have a test for explicit response format, basically that the json ends up correct even if default. Is there a constant form of this, or is builder().build() the way? ChatCompletionCreateParams.builder()
.responseFormat(ResponseFormatText.builder().build()) |
ps blog went out which has the old syntax until our agent is rebuilt to support latest. Thanks for all the help and also releasing this SDK. https://www.elastic.co/observability-labs/blog/elastic-opentelemetry-openai |
Right now We can't make However, maybe there's an opportunity here to optimize for these cases where you'd have to write
Nice! I saw your PR was merged. Anything left before you can update the blog post? |
With the release of v0.18.0, builders should be lovely and simple to use. See the readme and the examples for usage patterns. If you encounter further issues, then please file a new issue for that specific problem |
Are those all kinds of builder necessary?
The text was updated successfully, but these errors were encountered: