# java-ai **Repository Path**: gin9/java-ai ## Basic Information - **Project Name**: java-ai - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-09-04 - **Last Updated**: 2025-09-04 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring AI Demo 这是一个使用Spring AI与OpenAI集成的示例项目。 ## 技术栈 - JDK 17 - Spring Boot 3.2.3 - Spring AI 0.8.1 - Maven - Project Reactor ## 功能特性 - 与OpenAI API集成 - RESTful API接口 - 支持普通对话和流式输出 - 简单的聊天功能 ## 快速开始 1. 克隆项目到本地 2. 在`application.properties`中配置你的OpenAI API密钥 3. 运行项目: ```bash mvn spring-boot:run ``` ## API使用 ### 普通对话接口 发送POST请求到 `/api/ai/chat` 端点: ```bash curl -X POST http://localhost:8080/api/ai/chat \ -H "Content-Type: text/plain" \ -d "你好,请介绍一下你自己" ``` ### 流式输出接口 发送POST请求到 `/api/ai/chat/stream` 端点: ```bash curl -X POST http://localhost:8080/api/ai/chat/stream \ -H "Content-Type: text/plain" \ -H "Accept: application/x-ndjson" \ -d "你好,请介绍一下你自己" ``` 流式响应格式: ```json {"content":"你好","done":false} {"content":"!我是","done":false} {"content":"一个AI助手","done":false} {"content":"","done":true} ``` ## 配置说明 在 `application.properties` 中可以配置以下参数: - `spring.ai.openai.api-key`: OpenAI API密钥 - `spring.ai.openai.model`: 使用的模型(默认:gpt-3.5-turbo) - `spring.ai.openai.temperature`: 响应的随机性(0-1之间,默认:0.7) ## 注意事项 - 使用前请确保已经有有效的OpenAI API密钥 - API密钥不要提交到版本控制系统中 - 流式输出接口使用了Server-Sent Events (SSE),确保客户端支持此功能