Skip to content
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

Log probabilities for chat completion output tokens #625

Merged

Conversation

danai-antoniou
Copy link
Contributor

@danai-antoniou danai-antoniou commented Dec 24, 2023

Describe the change
OpenAI finally released log probabilities for chat completions. This PR implements the required changes to be able to request log probabilities in chat completion requests and receive them in the chat completion response.

Provide OpenAI documentation link
https://platform.openai.com/docs/api-reference/chat/object

Describe your solution
Added logprobs and top_logprobs in the chat completion request and response.

Tests
Manually tested by making chat completions

Issues
Closes #626

Copy link

codecov bot commented Dec 24, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (a09cb0c) 98.44% compared to head (216ffc3) 98.44%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #625   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files          24       24           
  Lines        1354     1354           
=======================================
  Hits         1333     1333           
  Misses         15       15           
  Partials        6        6           

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

@danai-antoniou danai-antoniou marked this pull request as ready for review December 24, 2023 16:35
@danai-antoniou
Copy link
Contributor Author

@sashabaranov would it be possible to get a review? This would unblock us without having to maintain a fork of go-openai 🙂

@sashabaranov
Copy link
Owner

@danai-antoniou, thank you for the PR, and sorry for the sluggish review — I was away for the holidays! :D

chat.go Outdated
// LogProbs indicates whether to return log probabilities of the output tokens or not.
// If true, returns the log probabilities of each output token returned in the content of message.
// This option is currently not available on the gpt-4-vision-preview model.
LogProbs *bool `json:"logprobs,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use *bool and *int here, we'll be unable to set those inline like

req := &openai.ChatCompletionRequest{
	LogProbs: true,
	TopLogProbs: 3,
}

See example: https://go.dev/play/p/YZzhc0nEOzf

Using just bool and int would be more ergonomic for developers and we'll still be able to omitempty as top_logprobs=0 is the same as null.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes good shout ✅

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty!

@sashabaranov sashabaranov merged commit f10955c into sashabaranov:master Jan 9, 2024
3 checks passed
@danai-antoniou
Copy link
Contributor Author

Thank you! ❤️

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.

Add log probabilities for chat completion output tokens
2 participants