Skip to content

Add non-breaking TemperatureOpt field to ChatCompletionRequest that can be set to explicit zero. #983

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

misberner
Copy link

This PR adds a TemperatureOpt *float32 field to ChatCompletionRequest, that allows distinguishing between an unset (default) temperature and explicit zero. This has been reported in several issues (e.g., #9), but this solution explicitly proposes a non-breaking change.

OpenAI doc: https://platform.openai.com/docs/api-reference/chat/create#chat-create-temperature

The new field is taken into account via custom JSON marshaling/unmarshaling functions, resorting to the legacy Temperature field (now marked as deprecated) whenever TemperatureOpt is not set. The unmarshaling logic prioritizes the legacy Temperature field whenever unambiguous to avoid incompatibilities; TemperatureOpt is only used when necessary to represent an explicit zero. A newly added GetTemperature() method allows programmatic users to get the temperature value that would be used for marshaling.

Note: There is one breaking change, which I think however can be regarded as sufficiently minor to justify ignoring it: when unmarshaling a ChatCompletionRequest with an explicit temperature setting of zero, then setting the legacy Temperature field to zero, and then re-using/re-marshaling the request, the result will be a request with an explicit zero temperature, rather than (as would previously be the case) a default temperature setting. But this seems pretty fabricated to me.

Testing is done via unit test, ensuring correct marshaling and unmarshaling behavior in various scenarious (no field set - default temperature, legacy field set, new field set, both field sets, explicit zero temperature).

Issues:

Copy link

codecov bot commented May 2, 2025

Codecov Report

Attention: Patch coverage is 92.68293% with 3 lines in your changes missing coverage. Please review.

Project coverage is 85.57%. Comparing base (774fc9d) to head (1fd2c83).
Report is 97 commits behind head on master.

Files with missing lines Patch % Lines
chat.go 92.50% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #983       +/-   ##
===========================================
- Coverage   98.46%   85.57%   -12.89%     
===========================================
  Files          24       43       +19     
  Lines        1364     2308      +944     
===========================================
+ Hits         1343     1975      +632     
- Misses         15      310      +295     
- Partials        6       23       +17     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant