Skip to content

Track the latency of individual requests separately #268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -171,60 +171,9 @@ public String translate(
prompt, systemPrompt, userPrompt, prompt.getContextMessages(), prompt.isJsonResponse());

return Mono.fromCallable(
() -> {
OpenAIClient.ChatCompletionsResponse chatCompletionsResponse =
openAIClient.getChatCompletions(chatCompletionsRequest).join();
if (chatCompletionsResponse.choices().size() > 1) {
logger.error(
"Multiple choices returned for text unit {}, expected only one",
tmTextUnit.getId());
meterRegistry
.counter("OpenAILLMService.translate.error.multiChoiceResponse")
.increment();
throw new AIException(
"Multiple response choices returned for text unit "
+ tmTextUnit.getId()
+ ", expected only one");
}
if (chatCompletionsResponse
.choices()
.getFirst()
.finishReason()
.equals(
OpenAIClient.ChatCompletionsStreamResponse.Choice.FinishReasons.STOP
.getValue())) {
String response = chatCompletionsResponse.choices().getFirst().message().content();
logger.debug(
"TmTextUnit id: {}, {} translation response: {}",
tmTextUnit.getId(),
targetBcp47Tag,
response);
if (prompt.isJsonResponse()) {
try {
logger.debug("Parsing JSON response for key: {}", prompt.getJsonResponseKey());
response =
objectMapper.readTree(response).get(prompt.getJsonResponseKey()).asText();
logger.debug("Parsed translation: {}", response);
} catch (JsonProcessingException e) {
logger.error("Error parsing JSON response: {}", response, e);
throw new AIException("Error parsing JSON response: " + response);
}
}
meterRegistry
.counter("OpenAILLMService.translate.result", "success", "true")
.increment();
return response;
}
String message =
String.format(
"Error translating text unit %d from %s to %s, response finish_reason: %s",
tmTextUnit.getId(),
sourceBcp47Tag,
targetBcp47Tag,
chatCompletionsResponse.choices().getFirst().finishReason());
logger.error(message);
throw new AIException(message);
})
() ->
sendTranslationRequest(
tmTextUnit, sourceBcp47Tag, targetBcp47Tag, prompt, chatCompletionsRequest))
.doOnError(
e -> {
logger.error(
Expand Down Expand Up @@ -253,6 +202,59 @@ public String translate(
"Error translating text unit " + tmTextUnit.getId() + " to " + targetBcp47Tag));
}

@Timed("OpenAILLMService.sendTranslationRequest")
private String sendTranslationRequest(
TMTextUnit tmTextUnit,
String sourceBcp47Tag,
String targetBcp47Tag,
AIPrompt prompt,
OpenAIClient.ChatCompletionsRequest chatCompletionsRequest) {
OpenAIClient.ChatCompletionsResponse chatCompletionsResponse =
openAIClient.getChatCompletions(chatCompletionsRequest).join();
if (chatCompletionsResponse.choices().size() > 1) {
logger.error(
"Multiple choices returned for text unit {}, expected only one", tmTextUnit.getId());
meterRegistry.counter("OpenAILLMService.translate.error.multiChoiceResponse").increment();
throw new AIException(
"Multiple response choices returned for text unit "
+ tmTextUnit.getId()
+ ", expected only one");
}
if (chatCompletionsResponse
.choices()
.getFirst()
.finishReason()
.equals(OpenAIClient.ChatCompletionsStreamResponse.Choice.FinishReasons.STOP.getValue())) {
String response = chatCompletionsResponse.choices().getFirst().message().content();
logger.debug(
"TmTextUnit id: {}, {} translation response: {}",
tmTextUnit.getId(),
targetBcp47Tag,
response);
if (prompt.isJsonResponse()) {
try {
logger.debug("Parsing JSON response for key: {}", prompt.getJsonResponseKey());
response = objectMapper.readTree(response).get(prompt.getJsonResponseKey()).asText();
logger.debug("Parsed translation: {}", response);
} catch (JsonProcessingException e) {
logger.error("Error parsing JSON response: {}", response, e);
throw new AIException("Error parsing JSON response: " + response);
}
}
meterRegistry.counter("OpenAILLMService.translate.result", "success", "true").increment();
return response;
}
String message =
String.format(
"Error translating text unit %d from %s to %s, response finish_reason: %s",
tmTextUnit.getId(),
sourceBcp47Tag,
targetBcp47Tag,
chatCompletionsResponse.choices().getFirst().finishReason());
logger.error(message);
throw new AIException(message);
}

@Timed("OpenAILLMService.checkString")
private List<AICheckResult> checkString(
AssetExtractorTextUnit textUnit, List<AIPrompt> prompts, Repository repository) {
Expand Down