Skip to content

Commit f10955c

Browse files
Log probabilities for chat completion output tokens (#625)
* Add logprobs * Logprobs pointer * Move toplogporbs * Create toplogprobs struct * Remove pointers
1 parent c9615e0 commit f10955c

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

chat.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,15 @@ type ChatCompletionRequest struct {
200200
// incorrect: `"logit_bias":{"You": 6}`, correct: `"logit_bias":{"1639": 6}`
201201
// refs: https://platform.openai.com/docs/api-reference/chat/create#chat/create-logit_bias
202202
LogitBias map[string]int `json:"logit_bias,omitempty"`
203-
User string `json:"user,omitempty"`
203+
// LogProbs indicates whether to return log probabilities of the output tokens or not.
204+
// If true, returns the log probabilities of each output token returned in the content of message.
205+
// This option is currently not available on the gpt-4-vision-preview model.
206+
LogProbs bool `json:"logprobs,omitempty"`
207+
// TopLogProbs is an integer between 0 and 5 specifying the number of most likely tokens to return at each
208+
// token position, each with an associated log probability.
209+
// logprobs must be set to true if this parameter is used.
210+
TopLogProbs int `json:"top_logprobs,omitempty"`
211+
User string `json:"user,omitempty"`
204212
// Deprecated: use Tools instead.
205213
Functions []FunctionDefinition `json:"functions,omitempty"`
206214
// Deprecated: use ToolChoice instead.
@@ -244,6 +252,28 @@ type FunctionDefinition struct {
244252
// Deprecated: use FunctionDefinition instead.
245253
type FunctionDefine = FunctionDefinition
246254

255+
type TopLogProbs struct {
256+
Token string `json:"token"`
257+
LogProb float64 `json:"logprob"`
258+
Bytes []byte `json:"bytes,omitempty"`
259+
}
260+
261+
// LogProb represents the probability information for a token.
262+
type LogProb struct {
263+
Token string `json:"token"`
264+
LogProb float64 `json:"logprob"`
265+
Bytes []byte `json:"bytes,omitempty"` // Omitting the field if it is null
266+
// TopLogProbs is a list of the most likely tokens and their log probability, at this token position.
267+
// In rare cases, there may be fewer than the number of requested top_logprobs returned.
268+
TopLogProbs []TopLogProbs `json:"top_logprobs"`
269+
}
270+
271+
// LogProbs is the top-level structure containing the log probability information.
272+
type LogProbs struct {
273+
// Content is a list of message content tokens with log probability information.
274+
Content []LogProb `json:"content"`
275+
}
276+
247277
type FinishReason string
248278

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

278309
// ChatCompletionResponse represents a response structure for chat completion API.

0 commit comments

Comments
 (0)