Skip to content

Commit 0164b5d

Browse files
authored
optimize: OpenAI chat model code cleanup (#2483)
* optimize: use already defined variables Signed-off-by: yuluo-yx <[email protected]>
1 parent 3f344a3 commit 0164b5d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: models/spring-ai-openai/src/main/java/org/springframework/ai/openai/OpenAiChatModel.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -196,26 +196,26 @@ public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatRespons
196196
return new ChatResponse(List.of());
197197
}
198198

199-
List<Generation> generations = choices.stream().map(choice -> {
200199
// @formatter:off
200+
List<Generation> generations = choices.stream().map(choice -> {
201201
Map<String, Object> metadata = Map.of(
202202
"id", chatCompletion.id() != null ? chatCompletion.id() : "",
203203
"role", choice.message().role() != null ? choice.message().role().name() : "",
204204
"index", choice.index(),
205205
"finishReason", choice.finishReason() != null ? choice.finishReason().name() : "",
206206
"refusal", StringUtils.hasText(choice.message().refusal()) ? choice.message().refusal() : "");
207-
// @formatter:on
208207
return buildGeneration(choice, metadata, request);
209208
}).toList();
209+
// @formatter:on
210210

211211
RateLimit rateLimit = OpenAiResponseHeaderExtractor.extractAiResponseHeaders(completionEntity);
212212

213213
// Current usage
214-
OpenAiApi.Usage usage = completionEntity.getBody().usage();
214+
OpenAiApi.Usage usage = chatCompletion.usage();
215215
Usage currentChatResponseUsage = usage != null ? getDefaultUsage(usage) : new EmptyUsage();
216216
Usage accumulatedUsage = UsageUtils.getCumulativeUsage(currentChatResponseUsage, previousChatResponse);
217217
ChatResponse chatResponse = new ChatResponse(generations,
218-
from(completionEntity.getBody(), rateLimit, accumulatedUsage));
218+
from(chatCompletion, rateLimit, accumulatedUsage));
219219

220220
observationContext.setResponse(chatResponse);
221221

0 commit comments

Comments
 (0)