react - Hello world
2026年1月14日 下午2:35:29
在 solon 项目里添加依赖。也可嵌入到第三方框架生态。
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-ai-agent</artifactId>
</dependency>
1、Helloworld
借助 gitee ai 服务,使用 Qwen3-32B 模型服务。然后开始 ReActAgent(简单体验时和 SimpleAgent 差不多) 编码:
import org.noear.solon.ai.agent.react.ReActAgent;
import org.noear.solon.ai.agent.simple.SimpleAgent;
import org.noear.solon.ai.annotation.ToolMapping;
import org.noear.solon.ai.chat.ChatModel;
import org.noear.solon.ai.chat.tool.MethodToolProvider;
import java.time.LocalDateTime;
public class DemoApp {
public static void main(String[] args) throws Throwable {
ChatModel chatModel = ChatModel.of("https://ai.gitee.com/v1/chat/completions")
.apiKey("***")
.model("Qwen3-32B")
.build();
SimpleAgent robot = SimpleAgent.of(chatModel)
.toolAdd(new MethodToolProvider(new WebSearchTools()))
.build();
String answer = robot.prompt("帮我查一下今年诺贝尔经济学奖得主的最新公开演讲,然后告诉我他演讲中提到的那个中国经济学家(关于债务问题)的主要观点是什么,最后用中文总结一下。")
.call()
.getContent();
System.out.println("Robot 答复: " + answer);
}
public static class WebSearchTools {
WebSearchRepository webSearchRepository = getWebSearchRepository();
@ToolMapping(name = "search", description = "Search the web for the given query.")
public String search(@Param(description = "The query to search for.") String query) throws Throwable {
List<Document> documentList = webSearchRepository.search(query);
return ONode.serialize(documentList);
}
}
}