This is an educational repository for learning Generative AI development with Java. It provides a comprehensive hands-on course covering Large Language Models (LLMs), prompt engineering, embeddings, RAG (Retrieval-Augmented Generation), and the Model Context Protocol (MCP).
Key Technologies:
- Java 21
- Spring Boot 3.5.x
- Spring AI 1.1.x
- Maven
- LangChain4j
- GitHub Models, Azure OpenAI, and OpenAI SDKs
Architecture:
- Multiple standalone Spring Boot applications organized by chapters
- Sample projects demonstrating different AI patterns
- GitHub Codespaces-ready with pre-configured dev containers
- Java 21 or higher
- Maven 3.x
- GitHub personal access token (for GitHub Models)
- Optional: Azure OpenAI credentials
Option 1: GitHub Codespaces (Recommended)
# Fork the repository and create a codespace from GitHub UI
# The dev container will automatically install all dependencies
# Wait ~2 minutes for environment setupOption 2: Local Dev Container
# Clone repository
git clone https://github.com/microsoft/Generative-AI-for-beginners-java.git
cd Generative-AI-for-beginners-java
# Open in VS Code with Dev Containers extension
# Reopen in Container when promptedOption 3: Local Setup
# Install dependencies
sudo apt-get update
sudo apt-get install -y maven openjdk-21-jdk
# Verify installation
java -version
mvn -versionGitHub Token Setup:
# Create a GitHub Personal Access Token
# Set environment variable
export GITHUB_TOKEN="your-token-here"Azure OpenAI Setup (Optional):
# For examples using Azure OpenAI
cd 02-SetupDevEnvironment/examples/basic-chat-azure
cp .env.example .env
# Edit .env with your Azure OpenAI credentials/
├── 01-IntroToGenAI/ # Chapter 1: Introduction
├── 02-SetupDevEnvironment/ # Chapter 2: Environment setup
│ └── examples/ # Working examples
├── 03-CoreGenerativeAITechniques/ # Chapter 3: Core techniques
├── 04-PracticalSamples/ # Chapter 4: Sample projects
│ ├── calculator/ # MCP service example
│ ├── foundrylocal/ # Local model integration
│ └── petstory/ # Multi-modal app
├── 05-ResponsibleGenAI/ # Chapter 5: Responsible AI
└── translations/ # Multi-language support
Running a Spring Boot application:
cd [project-directory]
mvn spring-boot:runBuilding a project:
cd [project-directory]
mvn clean installStarting the MCP Calculator Server:
cd 04-PracticalSamples/calculator
mvn spring-boot:run
# Server runs on http://localhost:8080Running Client Examples:
# After starting the server in another terminal
cd 04-PracticalSamples/calculator
# Direct MCP client
mvn exec:java -Dexec.mainClass="com.microsoft.mcp.sample.client.SDKClient"
# AI-powered client (requires GITHUB_TOKEN)
mvn exec:java -Dexec.mainClass="com.microsoft.mcp.sample.client.LangChain4jClient"
# Interactive bot
mvn exec:java -Dexec.mainClass="com.microsoft.mcp.sample.client.Bot"Spring Boot DevTools is included in projects that support hot reload:
# Changes to Java files will automatically reload when saved
mvn spring-boot:runRun all tests in a project:
cd [project-directory]
mvn testRun tests with verbose output:
mvn test -XRun specific test class:
mvn test -Dtest=CalculatorServiceTest- Test files use JUnit 5 (Jupiter)
- Test classes are located in
src/test/java/ - Client examples in the calculator project are in
src/test/java/com/microsoft/mcp/sample/client/
Many examples are interactive applications that require manual testing:
- Start the application with
mvn spring-boot:run - Test endpoints or interact with the CLI
- Verify expected behavior matches documentation in each project's README.md
- Free tier limits: 15 requests/minute, 150/day
- 5 concurrent requests maximum
- Test content filtering with responsible AI examples
- Java Version: Java 21 with modern features
- Style: Follow standard Java conventions
- Naming:
- Classes: PascalCase
- Methods/variables: camelCase
- Constants: UPPER_SNAKE_CASE
- Package names: lowercase
- Use
@Servicefor business logic - Use
@RestControllerfor REST endpoints - Configuration via
application.ymlorapplication.properties - Environment variables preferred over hard-coded values
- Use
@Toolannotation for MCP-exposed methods
src/
├── main/
│ ├── java/
│ │ └── com/microsoft/[component]/
│ │ ├── [Component]Application.java
│ │ ├── config/
│ │ ├── controller/
│ │ ├── service/
│ │ └── exception/
│ └── resources/
│ ├── application.yml
│ └── static/
└── test/
└── java/
└── com/microsoft/[component]/
- Managed via Maven
pom.xml - Spring AI BOM for version management
- LangChain4j for AI integrations
- Spring Boot starter parent for Spring dependencies
- Add JavaDoc for public APIs
- Include explanatory comments for complex AI interactions
- Document MCP tool descriptions clearly
Build without tests:
mvn clean install -DskipTestsBuild with all checks:
mvn clean installPackage application:
mvn package
# Creates JAR in target/ directory- Compiled classes:
target/classes/ - Test classes:
target/test-classes/ - JAR files:
target/*.jar - Maven artifacts:
target/
Development:
# application.yml
spring:
ai:
openai:
api-key: ${GITHUB_TOKEN}
base-url: https://models.inference.ai.azure.comProduction:
- Use Azure AI Foundry Models instead of GitHub Models
- Update base-url to Azure OpenAI endpoint
- Manage secrets via Azure Key Vault or environment variables
- This is an educational repository with sample applications
- Not designed for production deployment as-is
- Samples demonstrate patterns to adapt for production use
- See individual project READMEs for specific deployment notes
- GitHub Models: Free tier for learning, no credit card required
- Azure OpenAI: Production-ready, requires Azure subscription
- Code is compatible between both - just change endpoint and API key
Each sample project is standalone:
# Navigate to specific project
cd 04-PracticalSamples/[project-name]
# Each has its own pom.xml and can be built independently
mvn clean installJava Version Mismatch:
# Verify Java 21
java -version
# Update JAVA_HOME if needed
export JAVA_HOME=/usr/lib/jvm/msopenjdk-currentDependency Download Issues:
# Clear Maven cache and retry
rm -rf ~/.m2/repository
mvn clean installGitHub Token Not Found:
# Set in current session
export GITHUB_TOKEN="your-token-here"
# Or use .env file in project directory
echo "GITHUB_TOKEN=your-token-here" > .envPort Already in Use:
# Spring Boot uses port 8080 by default
# Change in application.properties:
server.port=8081- Documentation available in 45+ languages via automated translation
- Translations in
translations/directory - Translation managed by GitHub Actions workflow
- Start with 02-SetupDevEnvironment
- Follow chapters in order (01 → 05)
- Complete hands-on examples in each chapter
- Explore sample projects in Chapter 4
- Learn responsible AI practices in Chapter 5
The .devcontainer/devcontainer.json configures:
- Java 21 development environment
- Maven pre-installed
- VS Code Java extensions
- Spring Boot tools
- GitHub Copilot integration
- Docker-in-Docker support
- Azure CLI
- GitHub Models free tier has rate limits
- Use appropriate batch sizes for embeddings
- Consider caching for repeated API calls
- Monitor token usage for cost optimization
- Never commit
.envfiles (already in.gitignore) - Use environment variables for API keys
- GitHub tokens should have minimal required scopes
- Follow responsible AI guidelines in Chapter 5