Skip to content

[GH-2609] Fix resource leak issue in VertexAiTextEmbeddingModel #2611

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

Closed
wants to merge 1 commit into from
Closed
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 @@ -128,37 +128,38 @@ public EmbeddingResponse call(EmbeddingRequest request) {
.observation(this.observationConvention, DEFAULT_OBSERVATION_CONVENTION, () -> observationContext,
this.observationRegistry)
.observe(() -> {
PredictionServiceClient client = createPredictionServiceClient();
try (PredictionServiceClient client = createPredictionServiceClient()) {
Copy link
Contributor Author

@rodrigomalara rodrigomalara Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the proposed fix


EndpointName endpointName = this.connectionDetails.getEndpointName(finalOptions.getModel());
EndpointName endpointName = this.connectionDetails.getEndpointName(finalOptions.getModel());

PredictRequest.Builder predictRequestBuilder = getPredictRequestBuilder(request, endpointName,
finalOptions);
PredictRequest.Builder predictRequestBuilder = getPredictRequestBuilder(request, endpointName,
finalOptions);

PredictResponse embeddingResponse = this.retryTemplate
.execute(context -> getPredictResponse(client, predictRequestBuilder));
PredictResponse embeddingResponse = this.retryTemplate
.execute(context -> getPredictResponse(client, predictRequestBuilder));

int index = 0;
int totalTokenCount = 0;
List<Embedding> embeddingList = new ArrayList<>();
for (Value prediction : embeddingResponse.getPredictionsList()) {
Value embeddings = prediction.getStructValue().getFieldsOrThrow("embeddings");
Value statistics = embeddings.getStructValue().getFieldsOrThrow("statistics");
Value tokenCount = statistics.getStructValue().getFieldsOrThrow("token_count");
totalTokenCount = totalTokenCount + (int) tokenCount.getNumberValue();
int index = 0;
int totalTokenCount = 0;
List<Embedding> embeddingList = new ArrayList<>();
for (Value prediction : embeddingResponse.getPredictionsList()) {
Value embeddings = prediction.getStructValue().getFieldsOrThrow("embeddings");
Value statistics = embeddings.getStructValue().getFieldsOrThrow("statistics");
Value tokenCount = statistics.getStructValue().getFieldsOrThrow("token_count");
totalTokenCount = totalTokenCount + (int) tokenCount.getNumberValue();

Value values = embeddings.getStructValue().getFieldsOrThrow("values");
Value values = embeddings.getStructValue().getFieldsOrThrow("values");

float[] vectorValues = VertexAiEmbeddingUtils.toVector(values);
float[] vectorValues = VertexAiEmbeddingUtils.toVector(values);

embeddingList.add(new Embedding(vectorValues, index++));
}
EmbeddingResponse response = new EmbeddingResponse(embeddingList,
generateResponseMetadata(finalOptions.getModel(), totalTokenCount));
embeddingList.add(new Embedding(vectorValues, index++));
}
EmbeddingResponse response = new EmbeddingResponse(embeddingList,
generateResponseMetadata(finalOptions.getModel(), totalTokenCount));

observationContext.setResponse(response);
observationContext.setResponse(response);

return response;
return response;
}
});
}

Expand Down