Skip to content

Commit

Permalink
Log probabilities for chat completion output tokens (#625)
Browse files Browse the repository at this point in the history
* Add logprobs

* Logprobs pointer

* Move toplogporbs

* Create toplogprobs struct

* Remove pointers
  • Loading branch information
danai-antoniou committed Jan 9, 2024
1 parent c9615e0 commit f10955c
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,15 @@ type ChatCompletionRequest struct {
// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
LogitBias map[string]int `json:"logit_bias,omitempty"`
User string `json:"user,omitempty"`
// 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"`
// TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each
// token position, each with an associated log probability.
// logprobs must be set to true if this parameter is used.
TopLogProbs int `json:"top_logprobs,omitempty"`
User string `json:"user,omitempty"`
// Deprecated: use Tools instead.
Functions []FunctionDefinition `json:"functions,omitempty"`
// Deprecated: use ToolChoice instead.
Expand Down Expand Up @@ -244,6 +252,28 @@ type FunctionDefinition struct {
// Deprecated: use FunctionDefinition instead.
type FunctionDefine = FunctionDefinition

type TopLogProbs struct {
Token string `json:"token"`
LogProb float64 `json:"logprob"`
Bytes []byte `json:"bytes,omitempty"`
}

// LogProb represents the probability information for a token.
type LogProb struct {
Token string `json:"token"`
LogProb float64 `json:"logprob"`
Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null
// TopLogProbs is a list of the most likely tokens and their log probability, at this token position.
// In rare cases, there may be fewer than the number of requested top_logprobs returned.
TopLogProbs []TopLogProbs `json:"top_logprobs"`
}

// LogProbs is the top-level structure containing the log probability information.
type LogProbs struct {
// Content is a list of message content tokens with log probability information.
Content []LogProb `json:"content"`
}

type FinishReason string

const (
Expand Down Expand Up @@ -273,6 +303,7 @@ type ChatCompletionChoice struct {
// content_filter: Omitted content due to a flag from our content filters
// null: API response still in progress or incomplete
FinishReason FinishReason `json:"finish_reason"`
LogProbs *LogProbs `json:"logprobs,omitempty"`
}

// ChatCompletionResponse represents a response structure for chat completion API.
Expand Down

0 comments on commit f10955c

Please sign in to comment.