Skip to content

Commit 714a235

Browse files
committed
Add log probability code
1 parent f8298c9 commit 714a235

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/model/OpenAiChatCompletionChoice.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage.OpenAiChatAssistantMessage;
55
import javax.annotation.Nonnull;
6+
import javax.annotation.Nullable;
67
import lombok.AccessLevel;
78
import lombok.EqualsAndHashCode;
89
import lombok.Getter;
@@ -21,6 +22,12 @@ public class OpenAiChatCompletionChoice extends OpenAiCompletionChoice {
2122
@Setter(onMethod_ = @Nonnull, value = AccessLevel.PACKAGE)
2223
private OpenAiChatAssistantMessage message;
2324

25+
/** Completion chat log probability information for the choice. */
26+
@JsonProperty("logprobs")
27+
@Getter(onMethod_ = @Nullable)
28+
@Setter(onMethod_ = @Nullable, value = AccessLevel.PACKAGE)
29+
private OpenAiChatCompletionLogProbability logProbs;
30+
2431
void addDelta(@Nonnull final OpenAiDeltaChatCompletionChoice delta) {
2532
super.addDelta(delta);
2633

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.sap.ai.sdk.foundationmodels.openai.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import java.util.List;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.ToString;
7+
import lombok.experimental.Accessors;
8+
9+
/** Log probability information for the choice. */
10+
@Accessors(chain = true)
11+
@EqualsAndHashCode
12+
@ToString
13+
public class OpenAiChatCompletionLogProbability {
14+
/** A list of message content tokens with log probability information. */
15+
@JsonProperty("content")
16+
private List<OpenAiChatCompletionLogProbabilityTop> content;
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.sap.ai.sdk.foundationmodels.openai.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import javax.annotation.Nullable;
5+
import lombok.EqualsAndHashCode;
6+
import lombok.ToString;
7+
import lombok.experimental.Accessors;
8+
9+
/** Log probability information for the choice. */
10+
@Accessors(chain = true)
11+
@EqualsAndHashCode
12+
@ToString
13+
public class OpenAiChatCompletionLogProbabilityToken {
14+
/** The token. */
15+
@JsonProperty("token")
16+
private String token;
17+
18+
/** The log probability of this token. */
19+
@JsonProperty("logprob")
20+
private Number logprob;
21+
22+
/** A list of integers representing the UTF-8 bytes representation of the token. */
23+
@JsonProperty("bytes")
24+
@Nullable
25+
private int[] bytes;
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.sap.ai.sdk.foundationmodels.openai.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.EqualsAndHashCode;
5+
import lombok.ToString;
6+
import lombok.experimental.Accessors;
7+
8+
/** Log probability information for the choice. */
9+
@Accessors(chain = true)
10+
@EqualsAndHashCode(callSuper = true)
11+
@ToString
12+
public class OpenAiChatCompletionLogProbabilityTop extends OpenAiChatCompletionLogProbabilityToken {
13+
/**
14+
* List of the most likely tokens and their log probability, at this token position. In rare
15+
* cases, there may be fewer than the number of requested `top_logprobs` returned.
16+
*/
17+
@JsonProperty("top_logprobs")
18+
private OpenAiChatCompletionLogProbabilityToken top_logprobs;
19+
}

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/model/OpenAiCompletionParameters.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,24 @@ public class OpenAiCompletionParameters {
109109
@JsonProperty("stream")
110110
private Boolean stream;
111111

112+
/**
113+
* Whether to return log probabilities of the output tokens or not. If true, returns the log
114+
* probabilities of each output token returned in the `content` of `message`. This option is
115+
* currently not available on the `gpt-4-vision-preview` model. Default: false.
116+
*/
117+
@JsonProperty("logprobs")
118+
@Setter(onParam_ = @Nullable)
119+
private Boolean logprobs;
120+
121+
/**
122+
* An integer between 0 and 5 specifying the number of most likely tokens to return at each token
123+
* position, each with an associated log probability. `logprobs` must be set to `true` if this
124+
* parameter is used.
125+
*/
126+
@JsonProperty("top_logprobs")
127+
@Setter(onParam_ = @Nullable)
128+
private Integer topLogprobs;
129+
112130
/**
113131
* If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only
114132
* <a

0 commit comments

Comments
 (0)