Skip to main content

building-ai-apps-with-genkit-java

Guide for building AI-powered Java applications using the Genkit Java framework. Use this skill when the user wants to create a new AI app, add AI features to an existing Java project, define flows, call models, use tools, build RAG pipelines, manage prompts, handle structured output, set up multi-turn chat, create agents, run evaluations, or deploy with Genkit Java. Covers all supported providers (OpenAI, Google Gemini, Anthropic, Ollama, AWS Bedrock, Azure, and more).

Jump to install

Source facts

Repository
genkit-ai/genkit-java
Last source activity
July 6, 2026 at 17:55
Detected SKILL.md language
English
Stars
24
Forks
0

Install options

The review-first prompt is selected by default. You can switch to a direct command or download a local copy.

Review the source files

Read SKILL.md and any companion files shown by SkillsMP before deciding whether to install.

Showing SKILL.md

SKILL.md
Source instructions · Read-only preview
name
building-ai-apps-with-genkit-java
description
Guide for building AI-powered Java applications using the Genkit Java framework. Use this skill when the user wants to create a new AI app, add AI features to an existing Java project, define flows, call models, use tools, build RAG pipelines, manage prompts, handle structured output, set up multi-turn chat, create agents, run evaluations, or deploy with Genkit Java. Covers all supported providers (OpenAI, Google Gemini, Anthropic, Ollama, AWS Bedrock, Azure, and more).
argument-hint
Describe what you want to build (e.g., "a chatbot with RAG", "a Spring Boot app with Gemini", "structured output with OpenAI")
# Building AI Applications with Genkit Java You are helping a developer build AI-powered applications using **Genkit Java**, the open-source Java AI framework by Google. This skill covers everything an end user needs: setup, configuration, all APIs, providers, patterns, and deployment. --- ## Quick Start ### Prerequisites - **Java 21+** - **Maven** - **API key** for your chosen provider (OpenAI, Google, Anthropic, etc.) - **Genkit CLI** (optional, for Dev UI): `npm install -g genkit` ### Minimal pom.xml ```xml <project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-ai-app</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>21</maven.compiler.source> <maven.compiler.target>21</maven.compiler.target> <genkit.version>1.0.0-SNAPSHOT</genkit.version> </properties> <dependencies> <!-- Genkit core --> <dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit</artifactId> <version>${genkit.version}</version> </dependency> <!-- Pick a model provider (see Provider Setup below) --> <dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit-plugin-openai</artifactId> <version>${genkit.version}</version> </dependency> <!-- HTTP server (pick one) --> <dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit-plugin-jetty</artifactId> <version>${genkit.version}</version> </dependency> <!-- Logging --> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.5.32</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.5.0</version> <configuration> <mainClass>com.example.MyApp</mainClass> </configuration> </plugin> </plugins> </build> </project> ``` ### Minimal Application ```java package com.example; import com.google.genkit.Genkit; import com.google.genkit.core.GenkitOptions; import com.google.genkit.ai.GenerateOptions; import com.google.genkit.ai.GenerationConfig; import com.google.genkit.ai.model.ModelResponse; import com.google.genkit.core.flow.Flow; import com.google.genkit.plugins.openai.OpenAIPlugin; import com.google.genkit.plugins.jetty.JettyPlugin; import com.google.genkit.plugins.jetty.JettyPluginOptions; public class MyApp { public static void main(String[] args) throws Exception { JettyPlugin jetty = new JettyPlugin( JettyPluginOptions.builder().port(8080).build()); Genkit genkit = Genkit.builder() .options(GenkitOptions.builder() .devMode(true) .reflectionPort(3100) .build()) .plugin(OpenAIPlugin.create()) .plugin(jetty) .build(); genkit.defineFlow("ask", String.class, String.class, (ctx, question) -> genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o-mini") .prompt(question) .build()).getText()); jetty.start(); } } ``` ### Run It ```bash export OPENAI_API_KEY=sk-... mvn compile exec:java # Or with Dev UI (recommended) genkit start -- mvn compile exec:java ``` ### Test It ```bash curl -X POST http://localhost:8080/ask \ -H 'Content-Type: application/json' \ -d '"What is the capital of France?"' ``` --- ## Provider Setup ### Maven Artifacts (all `com.google.genkit`, version `1.0.0-SNAPSHOT`) | Provider | Artifact | Env Var | Plugin Init | |----------|----------|---------|-------------| | OpenAI | `genkit-plugin-openai` | `OPENAI_API_KEY` | `OpenAIPlugin.create()` | | Google Gemini | `genkit-plugin-google-genai` | `GOOGLE_GENAI_API_KEY` | `GoogleGenAIPlugin.create()` | | Anthropic | `genkit-plugin-anthropic` | `ANTHROPIC_API_KEY` | `AnthropicPlugin.create()` | | Ollama | `genkit-plugin-ollama` | none (local) | `OllamaPlugin.create("gemma3n:e4b")` | | AWS Bedrock | `genkit-plugin-aws-bedrock` | AWS credentials | `AwsBedrockPlugin.create("us-east-1")` | | Azure Foundry | `genkit-plugin-azure-foundry` | Azure credentials | `AzureFoundryPlugin.create()` | | DeepSeek | `genkit-plugin-deepseek` | `DEEPSEEK_API_KEY` | `DeepSeekPlugin.create()` | | Mistral | `genkit-plugin-mistral` | `MISTRAL_API_KEY` | `MistralPlugin.create()` | | Groq | `genkit-plugin-groq` | `GROQ_API_KEY` | `GroqPlugin.create()` | | Cohere | `genkit-plugin-cohere` | `COHERE_API_KEY` | `CoherePlugin.create()` | | xAI | `genkit-plugin-xai` | `XAI_API_KEY` | `XAIPlugin.create()` | | Any OpenAI-compatible | `genkit-plugin-compat-oai` | varies | `CompatOAIPlugin.create(options)` | ### Model Names by Provider ``` # OpenAI openai/gpt-4o, openai/gpt-4o-mini, openai/gpt-4-turbo, openai/gpt-3.5-turbo openai/o1-preview, openai/o1-mini openai/text-embedding-3-small, openai/text-embedding-3-large openai/dall-e-3, openai/dall-e-2, openai/gpt-image-1 # Google Gemini googleai/gemini-2.5-flash, googleai/gemini-1.5-pro, googleai/gemini-1.5-flash googleai/gemini-embedding-001, googleai/imagen-3.0-generate-002 # Anthropic anthropic/claude-sonnet-4-5-20250929, anthropic/claude-opus-4-5-20251101 anthropic/claude-haiku-4-5-20251001 anthropic/claude-opus-4-1, anthropic/claude-sonnet-4 # Ollama (any model you have pulled) ollama/gemma3n:e4b, ollama/llama3, ollama/mistral # AWS Bedrock aws-bedrock/amazon.nova-lite-v1:0, aws-bedrock/amazon.nova-pro-v1:0 aws-bedrock/anthropic.claude-sonnet-4-5-20250929-v1:0 aws-bedrock/meta.llama3-2-90b-instruct-v1:0 ``` ### Passing API Keys Programmatically ```java // Instead of environment variables OpenAIPlugin.create("sk-your-key-here") AnthropicPlugin.create("sk-ant-your-key-here") GoogleGenAIPlugin.create("AIza...") ``` --- ## Server Options ### Jetty (Lightweight) ```xml <dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit-plugin-jetty</artifactId> <version>${genkit.version}</version> </dependency> ``` ```java JettyPlugin jetty = new JettyPlugin( JettyPluginOptions.builder().port(8080).build()); // Flows are exposed at: POST http://localhost:8080/{flowName} ``` ### Spring Boot ```xml <dependency> <groupId>com.google.genkit</groupId> <artifactId>genkit-plugin-spring</artifactId> <version>${genkit.version}</version> </dependency> ``` ```java // Flows exposed at: POST http://localhost:8080/api/flows/{flowName} // Health check: GET http://localhost:8080/health // List flows: GET http://localhost:8080/api/flows ``` --- ## Defining Flows Flows are observable, HTTP-callable functions that form the backbone of your app. ### Simple Flow (no AI context needed) ```java genkit.defineFlow("greet", String.class, String.class, (name) -> "Hello, " + name + "!"); ``` ### Flow with AI Generation ```java genkit.defineFlow("summarize", String.class, String.class, (ctx, text) -> genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o-mini") .prompt("Summarize this: " + text) .build()).getText()); ``` ### Flow with Custom Input/Output Types ```java public record TranslateInput(String text, String targetLanguage) {} public record TranslateOutput(String translation, String detectedLanguage) {} genkit.defineFlow("translate", TranslateInput.class, TranslateOutput.class, (ctx, input) -> { ModelResponse response = genkit.generate( GenerateOptions.<TranslateOutput>builder() .model("openai/gpt-4o") .prompt("Translate to " + input.targetLanguage() + ": " + input.text()) .outputClass(TranslateOutput.class) .build()); return response.getOutput(); }); ``` ### Flow with Middleware ```java genkit.defineFlow("secured", String.class, String.class, (ctx, input) -> processInput(input), List.of(loggingMiddleware, authMiddleware)); ``` --- ## Generation API ### Basic Text Generation ```java ModelResponse response = genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o") .prompt("Explain quantum computing in simple terms") .build()); String text = response.getText(); ``` ### With Configuration ```java ModelResponse response = genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o") .prompt("Write a creative poem") .config(GenerationConfig.builder() .temperature(0.9) .maxOutputTokens(2048) .topP(0.95) .topK(40) .stopSequences(List.of("\n\n\n")) .build()) .build()); ``` ### System Prompt ```java ModelResponse response = genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o") .system("You are a pirate. Respond in pirate speak.") .prompt("How do I cook pasta?") .build()); ``` ### Multi-Turn Conversation ```java ModelResponse response = genkit.generate( GenerateOptions.builder() .model("openai/gpt-4o") .messages(List.of( Message.system("You are a helpful math tutor."), Message.user("What is 2+2?"), Message.model("2+2 equals 4."), Message.user("What about 2+2+2?"))) .build()); ``` ### Streaming ```java ModelResponse response = genkit.generateStream( GenerateOptions.builder() .model("openai/gpt-4o") .prompt("Write a long story about a dragon") .build(), chunk -> System.out.print(chunk.getText())); // Print as it arrives ``` ### Structured Output (JSON) Use `@JsonProperty` and `@JsonPropertyDescription` for schema generation: ```java public class Recipe { @JsonProperty(required = true) @JsonPropertyDescription("Name of the recipe") private String title; @JsonProperty(required = true) @JsonPropertyDescription("List of ingredients with quantities")
View on GitHub
This SKILL.md is very large, so SkillsMP previews the first section here. View on GitHub