spring-projects / spring-ai

An Application Framework for AI Engineering
https://docs.spring.io/spring-ai/reference/index.html
Apache License 2.0
3.34k stars 861 forks source link

Allow specifying credentials to vertexai embeddings model #1387

Open johanhaleby opened 2 months ago

johanhaleby commented 2 months ago

In 1.0.0-M2, the documentation for vertexai says to do this as part of the prerequisites:

gcloud config set project <PROJECT_ID> &&
gcloud auth application-default login <ACCOUNT>

However, it would be really useful to be able to specify credentials by other means, for example using a GCP service account. The vertex AI google cloud SDK supports this by passing an instance of com.google.auth.Credentials to com.google.api.gax.rpc.ClientContext used in PredictionServiceSettings. Builder. But in org.springframework.ai.vertexai.embedding.VertexAiEmbeddigConnectionDetails the predictionServiceSettings is hardcoded to:

PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build()

making it very difficult to use a service account with spring ai.

account123456789 commented 1 month ago

Dear All,

I am not able to invoke the vertex ai embedding model from spring ai , I am getting permission denied issue ... knowing that i am having the proper privileges into my service account , how can i invoke the embedding model ... Thanks in advance

ddobrin commented 3 weeks ago

Just to confirm, @account123456789, @johanhaleby, have you created the service account key file, downloaded it and set the GOOGLE_APPLICATION_CREDENTIALS account file to the location of the file, as ADC is looking first for that before other locations link

johanhaleby commented 3 weeks ago

@ddobrin Even if that works I don't want to do this. I'd like my application to be able to load the service account key file from classpath or something like Google Cloud Secret Manager.

ddobrin commented 3 weeks ago

@johanhaleby - The prev comment "... GOOGLE_APPLICATION_CREDENTIALS account file to the location of the file..." should have been "... GOOGLE_APPLICATION_CREDENTIALS environment variable to the location of the file..."

If I understand correctly, you are looking for usage in a local solution

garethjevans commented 1 week ago

With #1739 merged, you can configure the builder as follows:

          VertexAiEmbeddingConnectionDetails.builder()
              .withProjectId(projectId)
              .withLocation(region)
              .withApiEndpoint(endpoint)
              .withPredictionServiceSettings(
                  PredictionServiceSettings.newBuilder()
                      .setEndpoint(endpoint)
                      .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
                      .build());

Where credentials is an instance of a GoogleCredentials object (this can be loaded from a service account json)