Skip to content

开始使用

This content is not available in your language yet.

本教程将带你完成第一次 Feat AI 调用:引入依赖、创建对话模型、发送一条消息并看到回复。完成后你将掌握 Feat AI 的基本用法。

  • 在 Maven 项目中引入 feat-ai 依赖
  • 使用 FeatAI.chatModel(...) 创建对话模型
  • 调用 chat()chatStream() 与模型交互
  • 区分云端 API(如 Gitee AI)与本地模型(如 Ollama)的配置方式
  • JDK 1.8 或更高版本
  • Maven 3.0 或更高版本
  • 任选其一:
    • Gitee AI:需配置 API Key(环境变量 FEAT_AI_API_KEY
    • Ollama:本地已安装并运行 Ollama,默认 http://localhost:11434

在项目的 pom.xml 中加入 feat-ai 依赖(Feat 父 POM 或版本属性中需包含 feat.version):

pom.xml
<dependency>
<groupId>tech.smartboot.feat</groupId>
<artifactId>feat-ai</artifactId>
<version>${feat.version}</version>
</dependency>

feat-ai 已传递依赖 feat-core,无需单独引入。

本地 Ollama 为例,指定 baseUrl 和模型名即可:

import tech.smartboot.feat.ai.FeatAI;
import tech.smartboot.feat.ai.chat.ChatModel;
public class HelloFeatAI {
public static void main(String[] args) {
ChatModel model = FeatAI.chatModel(opts -> opts
.baseUrl("http://localhost:11434/v1")
.model("qwen2.5:7b")
);
// 下一步将用 model 发送消息
}
}

若使用 Gitee AI,可选用内置的 ChatModelVendor,并确保已设置 FEAT_AI_API_KEY

import tech.smartboot.feat.ai.FeatAI;
import tech.smartboot.feat.ai.chat.ChatModel;
import tech.smartboot.feat.ai.chat.ChatModelVendor;
ChatModel model = FeatAI.chatModel(opts -> opts
.model(ChatModelVendor.GiteeAI.Qwen2_5_72B_Instruct)
);

同步对话chat() 在回调中拿到完整回复与用量信息。

model.chat("解释什么是 RAG", response -> {
System.out.println("回复: " + response.getContent());
System.out.println("用量: " + response.getUsage());
});

流式对话chatStream() 逐段输出,适合长回答或实时展示。

model.chatStream("解释什么是 RAG", content -> {
System.out.print(content);
});

下面是一段基于 Ollama 的完整示例(JDK 8 可运行):

HelloFeatAI.java
import tech.smartboot.feat.ai.FeatAI;
import tech.smartboot.feat.ai.chat.ChatModel;
public class HelloFeatAI {
public static void main(String[] args) {
ChatModel model = FeatAI.chatModel(opts -> opts
.baseUrl("http://localhost:11434/v1")
.model("qwen2.5:7b")
);
model.chatStream("用一句话解释什么是 RAG", content -> {
System.out.print(content);
});
}
}
  • Ollama:先在本机执行 ollama run qwen2.5:7b(或你配置的模型),再运行上述程序,控制台应逐字输出模型回复。
  • Gitee AI:设置好 FEAT_AI_API_KEY 后运行,应得到云端模型的回复;若未设置,会报错提示设置环境变量。
  • 引入 feat-ai 依赖后,通过 FeatAI.chatModel(opts -> ...) 创建对话模型。
  • OllamabaseUrl("http://localhost:11434/v1") + model("模型名")
  • Gitee AImodel(ChatModelVendor.GiteeAI.xxx),并配置 FEAT_AI_API_KEY
  • 同步chat()流式chatStream();后续可在此基础上增加系统提示、函数调用等,见 对话与流式