{"id":49325534,"url":"https://github.com/luye77/llm4j","last_synced_at":"2026-05-29T21:00:54.362Z","repository":{"id":336006350,"uuid":"1145878511","full_name":"luye77/llm4j","owner":"luye77","description":"AI for JDK1.8","archived":false,"fork":false,"pushed_at":"2026-03-21T06:16:19.000Z","size":282,"stargazers_count":34,"open_issues_count":1,"forks_count":12,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-03-21T21:59:38.083Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luye77.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-01-30T10:28:42.000Z","updated_at":"2026-03-21T06:16:22.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/luye77/llm4j","commit_stats":null,"previous_names":["luye77/llm4j"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/luye77/llm4j","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luye77%2Fllm4j","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luye77%2Fllm4j/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luye77%2Fllm4j/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luye77%2Fllm4j/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luye77","download_url":"https://codeload.github.com/luye77/llm4j/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luye77%2Fllm4j/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33670211,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-05-29T02:00:06.066Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-04-26T20:00:31.588Z","updated_at":"2026-05-29T21:00:54.349Z","avatar_url":"https://github.com/luye77.png","language":"Java","funding_links":[],"categories":["人工智能"],"sub_categories":["LLM框架"],"readme":"# llm4j - Java AI SDK\n\n一个参考 Spring AI 设计的轻量级 Java AI SDK，支持多种 AI 模型提供商。\n\n## 特性\n\n- 🚀 **统一接口**: 一致的 API 设计，轻松切换不同 AI 模型\n- 🎯 **Spring AI 兼容**: 参考 Spring AI 的设计理念\n- 🔄 **响应式支持**: 支持流式处理和响应式编程\n- 🛠️ **工具调用**: 自动处理 Function Calling\n- 💬 **对话记忆**: 内置对话历史管理\n- 📚 **RAG 支持**: 检索增强生成功能\n- 🌐 **多模型支持**: OpenAI、通义千问等\n\n## 在 Spring Boot 项目中使用\n\n### 1. 添加依赖\n\n发布仓库为 **GitHub Packages（Maven）**，仓库地址：`https://maven.pkg.github.com/luye77/llm4j`。\n\n**坐标**（版本以仓库 `pom.xml` 为准，示例为 `1.0.2`）：\n\n| 说明 | 值 |\n|------|-----|\n| `groupId` | `io.github.luye77` |\n| 核心库 `artifactId` | `llm4j` |\n| Spring Starter `artifactId` | `llm4j-spring-boot-starter` |\n\nSpring Boot 项目一般只需引入 **`llm4j-spring-boot-starter`**（已传递依赖核心 `llm4j`）。\n\n#### Maven（`pom.xml`）\n\n在 `\u003crepositories\u003e` 中声明 GitHub Packages（`\u003cid\u003e` 需与 `settings.xml` 中 `\u003cserver\u003e` 的 `id` 一致，例如 `github`）：\n\n```xml\n\u003crepositories\u003e\n    \u003crepository\u003e\n        \u003cid\u003egithub\u003c/id\u003e\n        \u003curl\u003ehttps://maven.pkg.github.com/luye77/llm4j\u003c/url\u003e\n        \u003creleases\u003e\u003cenabled\u003etrue\u003c/enabled\u003e\u003c/releases\u003e\n        \u003csnapshots\u003e\u003cenabled\u003etrue\u003c/enabled\u003e\u003c/snapshots\u003e\n    \u003c/repository\u003e\n\u003c/repositories\u003e\n\n\u003cdependencies\u003e\n    \u003cdependency\u003e\n        \u003cgroupId\u003eio.github.luye77\u003c/groupId\u003e\n        \u003cartifactId\u003ellm4j-spring-boot-starter\u003c/artifactId\u003e\n        \u003cversion\u003e1.0.2\u003c/version\u003e\n    \u003c/dependency\u003e\n\u003c/dependencies\u003e\n```\n\n在 **`~/.m2/settings.xml`**（或 CI 中注入的同名配置）中配置拉取凭证，**不要**把 Token 写进业务 `pom.xml`：\n\n```xml\n\u003csettings\u003e\n    \u003cservers\u003e\n        \u003cserver\u003e\n            \u003cid\u003egithub\u003c/id\u003e\n            \u003cusername\u003e你的 GitHub 用户名\u003c/username\u003e\n            \u003cpassword\u003e具有 read:packages 权限的 Personal Access Token\u003c/password\u003e\n        \u003c/server\u003e\n    \u003c/servers\u003e\n\u003c/settings\u003e\n```\n\n#### Gradle（`build.gradle` / `build.gradle.kts`）\n\n```kotlin\nrepositories {\n    maven {\n        url = uri(\"https://maven.pkg.github.com/luye77/llm4j\")\n        credentials {\n            username = project.findProperty(\"gpr.user\") as String? ?: System.getenv(\"GITHUB_ACTOR\")\n            password = project.findProperty(\"gpr.key\") as String? ?: System.getenv(\"GITHUB_TOKEN\")\n        }\n    }\n}\n\ndependencies {\n    implementation(\"io.github.luye77:llm4j-spring-boot-starter:1.0.2\")\n}\n```\n\n可在 `gradle.properties` 或环境变量中配置 `gpr.user` / `gpr.key`，**勿将 Token 提交到仓库**。\n\n若依赖已发布到 **Maven Central**，通常只需在依赖中写坐标，**无需**再配置上述 GitHub 仓库（以实际发布渠道为准）。\n\n---\n\n### 2. 配置文件（`application.yml` / `application.properties`）\n\nStarter 通过配置前缀绑定三类属性：\n\n| 前缀 | 作用 |\n|------|------|\n| `ai.okhttp` | OkHttp：超时、日志级别、代理、是否忽略 SSL 等 |\n| `ai.openai` | OpenAI 兼容：`apiHost`、`apiKey`、接口路径等 |\n| `ai.qwen` | 通义千问：`apiHost`、`apiKey`、兼容模式及相关 URL 等 |\n\n**`application.yml` 示例**：\n\n```yaml\nai:\n  okhttp:\n    log: BASIC\n    connect-timeout: 300\n    read-timeout: 300\n    write-timeout: 300\n    time-unit: SECONDS\n    proxy-url: \"\"\n    proxy-port: 0\n    ignore-ssl: true\n\n  openai:\n    api-host: \"https://api.openai.com/\"\n    api-key: \"${OPENAI_API_KEY:}\"   # 建议用环境变量注入，勿将密钥提交到仓库\n    chat-completion-url: \"v1/chat/completions\"\n    embedding-url: \"v1/embeddings\"\n\n  qwen:\n    api-host: \"https://dashscope.aliyuncs.com/\"\n    api-key: \"${DASHSCOPE_API_KEY:}\"\n    chat-completion-url: \"compatible-mode/v1/chat/completions\"\n    embedding-url: \"compatible-mode/v1/embeddings\"\n    compatible-mode: true\n```\n\n---\n\n### 3. 自动配置的 Bean\n\nStarter 会注册：\n\n| Bean 名称 | 类型 | 说明 |\n|-----------|------|------|\n| `llm4jConfiguration` | `com.bobo.llm4j.config.Configuration` | 全局配置与 OkHttp 等 |\n| `openAiChatModel` | `com.bobo.llm4j.chat.model.ChatModel` | OpenAI 对话模型 |\n| `qwenChatModel` | `com.bobo.llm4j.chat.model.ChatModel` | 通义千问对话模型 |\n\n存在 **两个** `ChatModel` Bean，注入时必须使用 **`@Qualifier`**，不能单独使用 `@Autowired ChatModel`。\n\n---\n\n### 4. 业务代码示例\n\n推荐使用 **`ChatClient`** 流式 API（`ChatClient` 不是 Starter 自动注册的 Bean，通过 `ChatClient.builder(chatModel)` 创建）：\n\n```java\nimport com.bobo.llm4j.chat.client.ChatClient;\nimport com.bobo.llm4j.chat.model.ChatModel;\nimport org.springframework.beans.factory.annotation.Qualifier;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class AiService {\n\n    private final ChatClient chatClient;\n\n    public AiService(@Qualifier(\"qwenChatModel\") ChatModel chatModel) {\n        this.chatClient = ChatClient.builder(chatModel)\n                .defaultSystem(\"你是一个有帮助的助手。\")\n                .build();\n    }\n\n    public String ask(String userMessage) throws Exception {\n        return chatClient.prompt()\n                .user(userMessage)\n                .call()\n                .content();\n    }\n}\n```\n\n使用 OpenAI 时，将 `@Qualifier(\"qwenChatModel\")` 改为 `@Qualifier(\"openAiChatModel\")`。\n\n也可直接调用 `ChatModel#call(Prompt)`，或按需注入 `llm4jConfiguration` 使用底层 API。\n\n---\n\n### 5. 版本兼容说明\n\n当前 `llm4j-spring-boot-starter` 依赖的 **Spring Boot 版本为 2.3.x**。若你的应用为 **Spring Boot 3.x**，请自行验证兼容性（如 Jakarta 命名空间等）。\n\n## 快速开始\n\n### 1. 创建 ChatModel\n\n```java\nimport com.bobo.llm4j.chat.model.ChatModel;\nimport com.bobo.llm4j.platform.openai.chat.OpenAiChatModel;\nimport com.bobo.llm4j.config.Configuration;\nimport com.bobo.llm4j.config.OpenAiConfig;\n\n// 配置\nOpenAiConfig config = new OpenAiConfig();\nconfig.setApiHost(\"https://api.openai.com/\");\nconfig.setApiKey(\"your-api-key\");\n\nConfiguration configuration = new Configuration();\nconfiguration.setOpenAiConfig(config);\n\n// 创建 ChatModel\nChatModel chatModel = new OpenAiChatModel(configuration);\n```\n\n### 2. 同步调用\n\n```java\nimport com.bobo.llm4j.chat.entity.Prompt;\nimport com.bobo.llm4j.chat.entity.Message;\n\nPrompt prompt = Prompt.builder()\n    .model(\"gpt-4o-mini\")\n    .message(Message.withUser(\"你好！\"))\n    .build();\n\nChatResponse response = chatModel.call(prompt);\nString content = response.getGenerations().get(0)\n    .getMessage().getContent().getText();\n```\n\n### 3. 流式调用\n\n```java\nFlux\u003cChatResponse\u003e flux = chatModel.stream(prompt);\nflux.subscribe(response -\u003e {\n    // 处理每个响应片段\n});\n```\n\n## 支持的模型\n\n- ✅ OpenAI (GPT-4, GPT-3.5等)\n- 🔜 更多模型即将支持\n\n## 项目结构\n\n```\nllm4j/\n├── src/main/java/com/bobo/llm4j/\n│   ├── chat/\n│   │   ├── model/          # ChatModel 接口\n│   │   ├── client/         # ChatClient 实现\n│   │   ├── entity/         # 实体类\n│   │   └── prompt/         # Prompt 相关\n│   ├── platform/\n│   │   ├── openai/         # OpenAI 实现\n│   │   └── qwen/           # 通义千问实现\n│   ├── rag/                # RAG 功能\n│   ├── memory/             # 对话记忆\n│   └── config/             # 配置类\n└── src/test/               # 测试和示例\n```\n\n\n## 示例\n\n查看 `src/test` 目录下的示例：\n\n- `ChatClientTest.java` - ChatClient 使用示例\n- `RagServiceTest.java` - RAG 功能示例\n- `ChatMemoryUsageExample.java` - 对话记忆示例\n\n## 贡献\n\n欢迎提交 Issue 和 Pull Request！\n\n## 联系方式\n\n- **Email**: 1554612491@qq.com\n\n## 许可证\n\n本项目基于 Apache License 2.0 开源。\n\n---\n\n**注意**: 如果您正在使用旧版本的 `com.bobo.llm4j.service.ChatModel` 接口，请参考[迁移指南](docs/CHATMODEL_MIGRATION_GUIDE.md)进行升级。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluye77%2Fllm4j","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluye77%2Fllm4j","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluye77%2Fllm4j/lists"}