一款面向 JDK 8+ 的 Java AI Agentic 开发套件:统一的大模型接入、Tool Calling、MCP、A2A、RAG、Agent Runtime,以及内置的 Coding Agent CLI / TUI / ACP。
- Gradle:
implementation 'io.github.lnyo-cly:ai4j:2.4.2' - Maven:
<dependency><groupId>io.github.lnyo-cly</groupId><artifactId>ai4j</artifactId><version>2.4.2</version></dependency>
设置 OPENAI_API_KEY 后,下面代码即可发出第一条请求:
import io.github.lnyocly.ai4j.config.OpenAiConfig;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletion;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatCompletionResponse;
import io.github.lnyocly.ai4j.platform.openai.chat.entity.ChatMessage;
import io.github.lnyocly.ai4j.service.Configuration;
import io.github.lnyocly.ai4j.service.IChatService;
import io.github.lnyocly.ai4j.service.PlatformType;
import io.github.lnyocly.ai4j.service.factory.AiService;
public class Ai4jFirstChat {
public static void main(String[] args) {
OpenAiConfig openAiConfig = new OpenAiConfig();
openAiConfig.setApiKey(System.getenv("OPENAI_API_KEY"));
Configuration configuration = new Configuration();
configuration.setOpenAiConfig(openAiConfig);
AiService aiService = new AiService(configuration);
IChatService chatService = aiService.getChatService(PlatformType.OPENAI);
ChatCompletion request = ChatCompletion.builder()
.model("gpt-4o-mini")
.message(ChatMessage.withUser("用一句话介绍 ai4j"))
.build();
ChatCompletionResponse response = chatService.chatCompletion(request);
System.out.println(response.getChoices().get(0).getMessage().getContent().getText());
}
}输出示例:
ai4j 是一款面向 JDK 8+ 的 Java AI Agentic 开发套件,覆盖统一模型接入、Tool Calling、MCP 与 RAG。
换成 DashScope / DeepSeek / Ollama 等其他平台?只需替换
PlatformType与对应 Config,其余代码不变。
- 文档站:https://lnyo-cly.github.io/ai4j/
- 5 分钟跑通 / 能力地图
- Coding Agent CLI / TUI / ACP / A2A Protocol
- CHANGELOG / CONTRIBUTING
OpenAI / OpenAI-compatible, Anthropic, DashScope(通义/百炼), Doubao(火山方舟/豆包), DeepSeek, Moonshot, Zhipu(智谱), Hunyuan(腾讯混元), Lingyi(零一万物), Ollama, MiniMax, Baichuan, Suno;Rerank(Jina / Ollama / Doubao);AgentFlow(Dify / Coze / n8n);VectorStore(Pinecone / Qdrant / pgvector / Milvus / Redis)。完整能力列表见 feature-map。