AI Java Labs
Exercises for a course on integrating AI services with Java.
README
AI Java Labs
A comprehensive educational repository demonstrating how to integrate various AI tools and services with Java applications.
Overview
This repository contains practical exercises and example implementations for working with AI APIs in Java, including:
- OpenAI Services: Text-to-speech, chat completions, image generation (GPT Image)
- Ollama Integration: Local AI model interactions with text and vision capabilities
- LangChain4j: Framework integration for AI-powered applications
- Tool Calling: Java methods exposed safely as model-callable tools
- Retrieval-Augmented Generation (RAG): Document-based question answering
Getting Started
Prerequisites
- Java 21 or higher
- Gradle 8.4+ (included via wrapper)
- OpenAI API key (for OpenAI services)
- Ollama installation (for local AI models)
Environment Setup
-
OpenAI API Key: Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_openai_api_key_hereOptional provider keys used by some demos/tests:
export GOOGLEAI_API_KEY=your_google_ai_key_here export PERPLEXITY_API_KEY=your_perplexity_key_here -
Ollama Setup: Install Ollama and download the required models:
# Install Ollama from https://ollama.com ollama pull gemma4 ollama pull moondream # For vision tasks
Building the Project
./gradlew build
Running Tests
# Run all tests
./gradlew test
# Run only local tests (no external services required)
./gradlew testLocal
# Run cheap API tests (minimal cost)
./gradlew testCheap
# Run demo tests (for presentations)
./gradlew testDemo
Note: Some tests require external services (OpenAI API, Ollama) to be available and properly configured.
Training Materials
Presentation Slides
Comprehensive training slides are available in multiple formats:
- slides.md - Slidev source presentation
- slides.pdf - PDF export for handouts and reference
- slides.pptx - PowerPoint format for compatibility
To run or export the Slidev presentation:
npm install
npx slidev slides.md
# Export to PDF (requires Playwright)
npx slidev export slides.md
Project Structure
src/
โโโ main/java/com/kousenit/
โ โโโ demos/ # Standalone demo classes for training
โ โ โโโ QuickChatDemo.java
โ โ โโโ TextToSpeechDemo.java
โ โ โโโ MultiModelDemo.java
โ โ โโโ ToolCallingDemo.java
โ โ โโโ StructuredOutputDemo.java
โ โ โโโ StreamingDemo.java
โ โ โโโ ResponsesApiDemo.java # New Responses API with Gson
โ โ โโโ ResponsesApiJacksonDemo.java # Responses API with Jackson
โ โโโ GptImageService.java # GPT Image generation
โ โโโ EasyRAGDemo.java # RAG implementation example
โ โโโ OllamaService.java # Ollama AI service integration
โ โโโ OpenAiService.java # OpenAI API interactions
โ โโโ TextToSpeechService.java # Audio generation from text
โ โโโ *Records.java # Data models for API interactions
โโโ test/java/com/kousenit/ # Comprehensive test suite
Key Features
๐ต Text-to-Speech
Generate MP3 audio files from text using OpenAI's TTS models:
- Models:
tts-1,tts-1-hd,gpt-4o-mini-tts - Multiple voice options: alloy, ash, ballad, coral, echo, fable, nova, onyx, sage, shimmer
๐ค Chat Interactions
Support for both cloud and local AI models:
- OpenAI: gpt-5-nano for advanced conversations
- Ollama: Local gemma4 model (Google's open-source multimodal model)
๐ผ๏ธ Vision Capabilities
Image analysis and description generation:
- Multimodal support with Ollama's moondream model
- Base64 image encoding for API transmission
๐จ Image Generation
Create images from text descriptions using GPT Image:
- High-quality image generation
- Customizable size, quality, and style parameters
๐ ๏ธ Tool Calling
Expose Java methods as model-callable tools through LangChain4j:
- Annotate methods with
@Tool - Document parameters with
@P - Register tools through
AiServices - Keep validation and side effects in Java application code
๐งพ Structured Output
Extract Java records from unstructured text:
- Enable JSON Schema support in LangChain4j
- Return records from AI Service methods
- Validate parsed objects before using them
๐ Document Processing
RAG implementation for document-based question answering:
- LangChain4j integration
- Vector store support for efficient document retrieval
Dependencies
- LangChain4j: 1.15.0 (AI framework)
- Google Gen AI SDK: 1.55.0
- Apache POI: 5.5.1 (Document processing)
- Gson: 2.14.0 (JSON parsing)
- Jackson: 2.21.3 (JSON parsing with JsonNode)
- JUnit: 5.13.4 (Testing)
- Spotless: 7.2.1 (Code formatting)
- Playwright: For Slidev export functionality
Lab Exercises
Detailed step-by-step exercises are available in labs.md, covering:
- Generate Audio from Text - OpenAI TTS integration
- List OpenAI Models - API exploration and model discovery
- Install and Use Ollama - Local AI model setup
- Streaming Responses - Real-time AI interactions
- Vision Requests - Image analysis capabilities
- Conversation Management - Multi-turn chat implementations
- Tool Calling - Let the model call Java methods
- Structured Output - Extract validated Java records
- Image Generation - GPT Image integration
JSON Parsing Approaches
The project demonstrates two JSON parsing strategies for working with AI APIs:
Gson (Google's JSON library)
- Tree-based navigation with
JsonElement,JsonObject, andJsonArray - Used in most service classes and
ResponsesApiDemo - Already integrated throughout the project
Jackson (FasterXML)
JsonNodeAPI with JSON Pointer support (RFC 6901)- Elegant path navigation:
root.at("/path/to/element") - Demonstrated in
ResponsesApiJacksonDemo - Spring Boot's default JSON processor
Both approaches are shown side-by-side to help understand how to work directly with APIs, debug framework behavior, and adopt features that may not map cleanly to a high-level abstraction yet.
Educational Goals
This repository serves as a practical guide for:
- Understanding AI API integration patterns
- Learning modern Java features (records, sealed interfaces, pattern matching)
- Implementing error handling for external services
- Working with different AI model types (text, vision, audio)
- Working with JSON parsing for new API integration
- Building production-ready AI applications
Contributing
This is an educational repository. The code demonstrates various AI integration patterns and serves as reference implementations for learning purposes.
License
MIT License - see LICENSE file for details.
