Gemini Supports OpenAI compatibility for both Chat models and Embeddings.

For the Chat model it works perfectly fine with SpringAI, but not for the embeddings.

So, using Gemini embeddings and OpenAI embedding (Spring Ai) will cause a NullPointerException.

Here is the reason why it causes a NullPointerException:

So in OpenAiEmbeddingModel.class we have this piece of code:

private DefaultUsage getDefaultUsage(OpenAiApi.Usage usage) {
    return new DefaultUsage(usage.promptTokens(), usage.completionTokens(), usage.totalTokens(), usage);
}

As far as I have checked, Gemini embedding responses doesn't provide usage stats, so calling usage.promptTokens(), usage.completionTokens(), usage.totalTokens() each will create an NPE exception.

So it would be nice if we could have such an implementation to fix this issue, to be honest, it's a feature, not an issue.

Currently I mock the values, since I don't use the stats, here is an example code:

https://github.com/Lunatix01/ragscan/pull/5/files#diff-2e6f569f1db170073e027f80b1ee4c8623be4b953cc54553ae2fe53dfc1e5ec2R117-R124

Comment From: dev-jonghoonpark

related doc : https://ai.google.dev/gemini-api/docs/openai