AGENTS.md 项目概述 这是一个用于学习使用 Java 开发生成式 AI的教育性代码库。它提供了一个全面的实践课程,涵盖了大型语言模型(LLMs)、提示工程、嵌入技术、RAG(检索增强生成)以及模型上下文协议(MCP)。 关键技术: Java 21 Spring Boot 3.5.x Spring AI 1.1.x Maven LangChain4j GitHub Models、Azure OpenAI 和 OpenAI SDKs 架构: 按章节组织的多个独立 Spring Boot 应用程序 展示不同 AI 模式的示例项目 支持 GitHub Codespaces,预配置开发容器 设置命令 前置条件 Java 21 或更高版本 Maven 3.
这是一个用于学习使用 Java 开发生成式 AI的教育性代码库。它提供了一个全面的实践课程,涵盖了大型语言模型(LLMs)、提示工程、嵌入技术、RAG(检索增强生成)以及模型上下文协议(MCP)。
关键技术:
架构:
选项 1:GitHub Codespaces(推荐)
# Fork the repository and create a codespace from GitHub UI # The dev container will automatically install all dependencies # Wait ~2 minutes for environment setup
选项 2:本地开发容器
# 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 prompted
选项 3:本地设置
# Install dependencies sudo apt-get update sudo apt-get install -y maven openjdk-21-jdk # Verify installation java -version mvn -version
GitHub 令牌设置:
# Create a GitHub Personal Access Token # Set environment variable export GITHUB_TOKEN="your-token-here"
Azure OpenAI 设置(可选):
# 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
运行 Spring Boot 应用程序:
cd [project-directory] mvn spring-boot:run
构建项目:
cd [project-directory] mvn clean install
启动 MCP 计算器服务器:
cd 04-PracticalSamples/calculator mvn spring-boot:run # Server runs on http://localhost:8080
运行客户端示例:
# 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:
# Changes to Java files will automatically reload when saved mvn spring-boot:run
运行项目中的所有测试:
cd [project-directory] mvn test
运行带详细输出的测试:
mvn test -X
运行特定测试类:
mvn test -Dtest=CalculatorServiceTest
src/test/java/src/test/java/com/microsoft/mcp/sample/client/许多示例是交互式应用程序,需要手动测试:
mvn spring-boot:run 启动应用程序@Service 处理业务逻辑@RestController 处理 REST 端点application.yml 或 application.properties@Tool 注解标记 MCP 暴露的方法src/ ├── main/ │ ├── java/ │ │ └── com/microsoft/[component]/ │ │ ├── [Component]Application.java │ │ ├── config/ │ │ ├── controller/ │ │ ├── service/ │ │ └── exception/ │ └── resources/ │ ├── application.yml │ └── static/ └── test/ └── java/ └── com/microsoft/[component]/
pom.xml 管理跳过测试构建:
mvn clean install -DskipTests
完整检查构建:
mvn clean install
打包应用程序:
mvn package # Creates JAR in target/ directory
target/classes/target/test-classes/target/*.jartarget/开发环境:
# application.yml spring: ai: openai: api-key: ${GITHUB_TOKEN} base-url: https://models.inference.ai.azure.com
生产环境:
每个示例项目都是独立的:
# Navigate to specific project cd 04-PracticalSamples/[project-name] # Each has its own pom.xml and can be built independently mvn clean install
Java 版本不匹配:
# Verify Java 21 java -version # Update JAVA_HOME if needed export JAVA_HOME=/usr/lib/jvm/msopenjdk-current
依赖下载问题:
# Clear Maven cache and retry rm -rf ~/.m2/repository mvn clean install
未找到 GitHub 令牌:
# Set in current session export GITHUB_TOKEN="your-token-here" # Or use .env file in project directory echo "GITHUB_TOKEN=your-token-here" > .env
端口已被占用:
# Spring Boot uses port 8080 by default # Change in application.properties: server.port=8081
translations/ 目录.devcontainer/devcontainer.json 配置了:
.env 文件(已在 .gitignore 中)免责声明:
本文档使用AI翻译服务 Co-op Translator 进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用此翻译而产生的任何误解或误读不承担责任。