|
31 | 31 | import org.springframework.ai.chat.messages.UserMessage;
|
32 | 32 | import org.springframework.ai.chat.metadata.ChatGenerationMetadata;
|
33 | 33 | import org.springframework.ai.chat.metadata.ChatResponseMetadata;
|
| 34 | +import org.springframework.ai.chat.metadata.EmptyUsage; |
34 | 35 | import org.springframework.ai.chat.metadata.RateLimit;
|
35 | 36 | import org.springframework.ai.chat.model.AbstractToolCallSupport;
|
36 | 37 | import org.springframework.ai.chat.model.ChatModel;
|
@@ -163,7 +164,7 @@ public ChatResponse call(Prompt prompt) {
|
163 | 164 | List<Generation> generations = choices.stream().map(choice -> {
|
164 | 165 | // @formatter:off
|
165 | 166 | Map<String, Object> metadata = Map.of(
|
166 |
| - "id", chatCompletion.id(), |
| 167 | + "id", chatCompletion.id() != null ? chatCompletion.id() : "", |
167 | 168 | "role", choice.message().role() != null ? choice.message().role().name() : "",
|
168 | 169 | "finishReason", choice.finishReason() != null ? choice.finishReason().name() : "");
|
169 | 170 | // @formatter:on
|
@@ -265,12 +266,12 @@ private Generation buildGeneration(Choice choice, Map<String, Object> metadata)
|
265 | 266 | private ChatResponseMetadata from(OpenAiApi.ChatCompletion result, RateLimit rateLimit) {
|
266 | 267 | Assert.notNull(result, "OpenAI ChatCompletionResult must not be null");
|
267 | 268 | var builder = ChatResponseMetadata.builder()
|
268 |
| - .withId(result.id()) |
269 |
| - .withUsage(OpenAiUsage.from(result.usage())) |
270 |
| - .withModel(result.model()) |
| 269 | + .withId(result.id() != null ? result.id() : "") |
| 270 | + .withUsage(result.usage() != null ? OpenAiUsage.from(result.usage()) : new EmptyUsage()) |
| 271 | + .withModel(result.model() != null ? result.model() : "") |
271 | 272 | .withRateLimit(rateLimit)
|
272 |
| - .withKeyValue("created", result.created()) |
273 |
| - .withKeyValue("system-fingerprint", result.systemFingerprint()); |
| 273 | + .withKeyValue("created", result.created() != null ? result.created() : 0L) |
| 274 | + .withKeyValue("system-fingerprint", result.systemFingerprint() != null ? result.systemFingerprint() : ""); |
274 | 275 | if (rateLimit != null) {
|
275 | 276 | builder.withRateLimit(rateLimit);
|
276 | 277 | }
|
|
0 commit comments