-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
omitempty
option of request struct will generate incorrect request when parameter is 0.
#9
Comments
omitempty
field of struct will generate incorrect request when parameter is 0.omitempty
option of request struct will generate incorrect request when parameter is 0.
Thank you for the issue! So, with
Without
I would check whether OpenAPI defaults match Go's zero-values. In case they do I think it is best to remove |
Looking at type CompletionRequest struct {
Prompt string `json:"prompt,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"` // OpenAPI defaults to 16
Temperature float32 `json:"temperature,omitempty"` // OpenAPI defaults to 1
TopP float32 `json:"top_p,omitempty"` // OpenAPI defaults to 1
N int `json:"n,omitempty"` // OpenAPI defaults to 1
LogProbs int `json:"logprobs,omitempty"`
Model *string `json:"model,omitempty"`
Echo bool `json:"echo,omitempty"`
Stop []string `json:"stop,omitempty"`
PresencePenalty float32 `json:"presence_penalty,omitempty"` // OpenAPI defaults to 0
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // OpenAPI defaults to 0
BestOf int `json:"best_of,omitempty"` // OpenAPI defaults to 1
} |
Perhaps it would be best to make a constructor function for the struct then? Or at least documenting the quirk. As of right now, calling the API with parameters generated from the playground will have (sometimes significantly) different results as calling the exact same parameters from a similar API in another language. |
It seems that playground's default parameters match API ones, except for |
Hello, If we don't want to get rid of the omitempty (because of different defaults), could we make the types for fields like Temperature a pointer (e.g. From reading about other people's usage of Temperature and TopP, it seems like the most common values for them are 1, 0.5, and 0. So 0 isn't an edge case. For now, I'm setting Temperature to a tiny amount to mimic 0, but that seems a little hacky. |
Seeks to re-factor the original repo w/ many breaking changes. Major goals: - Use more idiomatic go style. (Rename the package, accept and return pointers, shorter variable names, etc.) - Much better documentation. Add comments to all exported types, fields, methods, packages, etc. (from OpenAI's documentation). - Make option fields who's go default value is a valid parameter value (and also does not match the API's default) pointers, so omitempty does not strip explicitly set values. (See sashabaranov/go-openai#9 for more). - Have a consistent style throughout. Most endpoints in the original library were implemented independently (and thus their usages feel inconsistent). - Implement all endpoints. Reviewer: neilmadsen
Hi, just another idea to resolve this: when marshalling and unmarshalling, we can set default values other than 0 by building a custom 'UnmarshalJSON' for an Options struct type. reference: https://eli.thegreenplace.net/2020/optional-json-fields-in-go/ |
How do you like an option similar to how it is done in the SQL package from the GO standard library for nullable fields in the database? They just added a separate structure for such properties - NullString, NullFloat64 and etc. (example: https://cs.opensource.google/go/go/+/master:src/database/sql/sql.go;l=319). If valid is false, then send the default value for the API - that is, 1, otherwise explicitly send the value from the corresponding property. |
@fussraider that can also be done, we just need to make sure the marshalling and unmarshalling handles it cleanly. |
Setting the temperature at In my experience using the OpenAI Playground at zero temperature, it's always seemed to be deterministic for me (I don't see responses change when I submit them repeatedly like I am seeing with |
In case it's helpful, OpenAI has explicitly told me in the past that (3) is the case. In real-world applications, I've seen that 0 temp becomes more non-deterministic the more tokens you have in input/output (for example, using ~32k tokens at the max means you have the highest likelihood of non-deterministic behavior even at 0 temp). |
@neilmadsen Ohhhh. You’re right. I went to OpenAI Playground with one of my longer prompts, and am seeing the same behavior I reported above in my comment — it changes even at zero temperature. Thank you for the insight. |
Seems like |
We design our prompt systems in Python and then re-implement them in our golang apps. This Golang SDK not matching the official Python SDK is a problem for us. Could we either:
|
This PR adds a workaround for [this GitHub discussion][gh], describing how chat completion requests which explicitly set temperature = 0 are marshalled to JSON incorrectly (due to the 0 being indistinguishable from the zero value of the field). To do so we add a custom unmarshal method which checks whether the field was defined and set to zero, and explicitly set the temperature to `math.SmallestNonzeroFloat32` in such cases. This isn't perfect but is likely to get very similar results in practice. [gh]: sashabaranov/go-openai#9 (comment)
Been following this thread for some time and I honestly do not understand the disagreement on removing Yes, the difference might be hard to measure, because the multi-agent architecture makes determinism difficult, but that's not an excuse. Thanks for your work and sorry for the criticism, but every time I see this issue I just ask myself "why!?" 🙈 |
The 'omitempty' option of the request structs should be removed. This is because it generates an incorrect request when a parameter is 0. For example, a request with the temperate field set to '0' will actually return a response as if the field had been set to one. This is because the go JSON parser does not differentiate between a 0 value and no value for float32, so Openai will receive a request without a temperate field which then defaults to a temperature value of 1.
The text was updated successfully, but these errors were encountered: