|
| 1 | +# Secure Vertex AI Gemini Poem Demo |
| 2 | + |
| 3 | +This demo showcases the implementation of a secure Vertex AI Gemini Poem Demo which is available only to users authenticated with Google. |
| 4 | + |
| 5 | +## The Demo |
| 6 | + |
| 7 | +### Setup |
| 8 | + |
| 9 | +The demo asks Vertex AI Gemini LLM to write a short 1 paragraph poem, using the access token acquired during the OIDC authorization code flow. |
| 10 | + |
| 11 | +### AI Service |
| 12 | + |
| 13 | +This demo leverages the AI service abstraction, with the interaction between the LLM and the application handled through the AIService interface. |
| 14 | + |
| 15 | +The `io.quarkiverse.langchain4j.sample.PoemAiService` interface uses specific annotations to define the LLM: |
| 16 | + |
| 17 | +```java |
| 18 | +package io.quarkiverse.langchain4j.sample; |
| 19 | + |
| 20 | +import dev.langchain4j.service.SystemMessage; |
| 21 | +import dev.langchain4j.service.UserMessage; |
| 22 | +import io.quarkiverse.langchain4j.RegisterAiService; |
| 23 | + |
| 24 | +@RegisterAiService |
| 25 | +public interface PoemAiService { |
| 26 | + |
| 27 | + /** |
| 28 | + * Ask the LLM to create a poem about Enterprise Java. |
| 29 | + * |
| 30 | + * @return the poem |
| 31 | + */ |
| 32 | + @SystemMessage("You are a professional poet") |
| 33 | + @UserMessage(""" |
| 34 | + Write a short 1 paragraph poem about Java. Set an author name to the model name which created the poem. |
| 35 | + """) |
| 36 | + String writeAPoem(); |
| 37 | + |
| 38 | +} |
| 39 | + |
| 40 | +### Using the AI service |
| 41 | + |
| 42 | +Once defined, you can inject the AI service as a regular bean, and use it: |
| 43 | + |
| 44 | +```java |
| 45 | +package io.quarkiverse.langchain4j.sample; |
| 46 | + |
| 47 | +import java.net.URISyntaxException; |
| 48 | + |
| 49 | +import io.quarkus.security.Authenticated; |
| 50 | +import jakarta.ws.rs.GET; |
| 51 | +import jakarta.ws.rs.Path; |
| 52 | + |
| 53 | +@Path("/poem") |
| 54 | +@Authenticated |
| 55 | +public class PoemResource { |
| 56 | + |
| 57 | + private final PoemAiService aiService; |
| 58 | + |
| 59 | + public PoemResource(PoemAiService aiService) throws URISyntaxException { |
| 60 | + this.aiService = aiService; |
| 61 | + } |
| 62 | + |
| 63 | + @GET |
| 64 | + public String getPoem() { |
| 65 | + return aiService.writeAPoem(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +``` |
| 70 | + |
| 71 | +`PoemResource` can only be accessed by authenticated users. |
| 72 | + |
| 73 | +## Google Authentication |
| 74 | + |
| 75 | +This demo requires users to authenticate with Google. |
| 76 | +All you need to do is to register an application with Google, follow steps listed in the [Quarkus Google](https://quarkus.io/guides/security-openid-connect-providers#google) section. |
| 77 | +Name your Google application as `Quarkus LangChain4j AI`, and make sure an allowed callback URL is set to `http://localhost:8080/login`. |
| 78 | +Google will generate a client id and secret, use them to set `quarkus.oidc.client-id` and `quarkus.oidc.credentials.secret` properties: |
| 79 | + |
| 80 | +```properties |
| 81 | +quarkus.oidc.provider=google |
| 82 | +quarkus.oidc.client-id=${GOOGLE_CLIENT_ID} |
| 83 | +quarkus.oidc.credentials.secret=${GOOGLE_CLIENT_SECRET} |
| 84 | +quarkus.oidc.authentication.extra-params.scope=https://www.googleapis.com/auth/generative-language.retriever,https://www.googleapis.com/auth/cloud-platform |
| 85 | +quarkus.oidc.authentication.redirect-path=/login |
| 86 | + |
| 87 | +# See https://cloud.google.com/vertex-ai/docs/general/locations |
| 88 | +vertex-ai-region=europe-west2 |
| 89 | + |
| 90 | +quarkus.langchain4j.vertexai.gemini.location=https://${vertex-ai-region}-aiplatform.googleapis.com |
| 91 | +quarkus.langchain4j.vertexai.gemini.project-id=${GOOGLE_PROJECT_ID} |
| 92 | +``` |
| 93 | + |
| 94 | +You must enable Vertex AI API in your Google Cloud project. |
| 95 | + |
| 96 | +## Security Considerations |
| 97 | + |
| 98 | +This demo makes it possible to access Google Vertex AI API enabled in the Google Cloud project only to users who: |
| 99 | + |
| 100 | +* Authenticated to Quarkus REST PoemService with Google using OIDC authorization code flow. |
| 101 | +* Authorized `Quarkus LangChain4j AI` application registered in the Google Cloud project to use the access token to access Google Generative API on behalf of the currently authentiicated user. This authorization is requested from users during the authentication process and is configured by adding `quarkus.oidc.authentication.extra-params.scope=https://www.googleapis.com/auth/generative-language.retriever,https://www.googleapis.com/auth/cloud-platform` in the application properties. |
| 102 | +* Quarkus LangChain4j vertex-ai-gemini model provider uses this authorized token on behalf of the current user to access Google Vertex AI endpoint. |
| 103 | + |
| 104 | +## Running the Demo |
| 105 | + |
| 106 | +To run the demo, use the following commands: |
| 107 | + |
| 108 | +```shell |
| 109 | +mvn quarkus:dev |
| 110 | +``` |
| 111 | + |
| 112 | +Then, access `http://localhost:8080`, login to Google, and follow a provided application link to read the poem. |
| 113 | + |
0 commit comments