用 AgentScope Java 搭建 LLM 应用
AgentScope Java 是一个面向智能体的编程框架,专门用来构建基于大语言模型的应用程序。它把 ReAct 推理、工具调用、记忆管理、多智能体协作这些能力都封装好了,你只需要按自己的场景组合就行。
环境准备
JDK 17 或更高版本,Maven 或 Gradle 构建。在 pom.xml 里加上:
<dependency>
<groupId>io.agentscope</groupId>
<artifactId>agentscope</artifactId>
<version>1.0.1</version>
</dependency>
第一个智能体
先看一个最简单的例子,让智能体打个招呼。
import io.agentscope.core.ReActAgent;
import io.agentscope.core.message.Msg;
import io.agentscope.core.model.DashScopeChatModel;
import io.agentscope.core.message.MsgRole;
import io.agentscope.core.message.TextBlock;
public class FirstAgentExample {
public static void main(String[] args) {
String apiKey = System.getenv("DASHSCOPE_API_KEY");
ReActAgent agent = ReActAgent.builder()
.name("MyFirstAgent")
.sysPrompt("你是一个友好的 AI 助手,总是用简洁的语言回答问题。")
.model(DashScopeChatModel.builder()
.apiKey(apiKey)
.modelName()
.build())
.build();
Msg.builder()
.name()
.role(MsgRole.USER)
.content(TextBlock.builder().text().build())
.build();
agent.call(userMessage).block();
response != ;
System.out.println( + response.getTextContent());
}
}
