MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / AgentStudioSimple

Class AgentStudioSimple

samples/AgentStudioSimple.java:23–87  ·  view source on GitHub ↗

AgentStudio quick start: create agent, session, send message, stream reply. Prerequisites: set DASHSCOPE_API_KEY env var, and either DASHSCOPE_WORKSPACE env var or pass workspace to the constructor. export DASHSCOPE_API_KEY=sk-xxx export DASHSCOPE_WORKSPACE=ws_xxxxxxxxxxxx

Source from the content-addressed store, hash-verified

21 * </pre>
22 */
23public class AgentStudioSimple {
24
25 public static void main(String[] args) throws Exception {
26 // Option 1: apiKey + workspace (production)
27 AgentStudioClient client = new AgentStudioClient("sk-xxx", "ws_xxxxxxxxxxxx");
28
29 // Option 2: all from env vars (DASHSCOPE_API_KEY + DASHSCOPE_WORKSPACE)
30 // AgentStudioClient client = new AgentStudioClient();
31
32 // Option 3: custom base URL
33 // AgentStudioClient client = AgentStudioClient.builder()
34 // .apiKey("sk-xxx")
35 // .baseUrl("https://your-custom-host/api/v1/agentstudio")
36 // .build();
37
38 // 1. Create Agent
39 Agent agent =
40 client
41 .agents()
42 .create(
43 AgentCreateParam.builder()
44 .name("demo-agent")
45 // 只给模型 id;需要推理参数时改传对象:
46 // .model(ModelConfig.builder().id("qwen3-max").temperature(0.7).build())
47 .model("qwen-plus")
48 .systemPrompt("你是一个简洁的助手。")
49 .build());
50 System.out.println("Agent: " + agent.getId());
51
52 // 2. Create Session
53 Session session =
54 client.sessions().create(SessionCreateParam.builder().agent(agent.getId()).build());
55 System.out.println("Session: " + session.getId());
56
57 // 3. Send message
58 client
59 .sessions()
60 .events()
61 .send(session.getId(), Collections.singletonList(ClientEvents.userMessage("你好")));
62
63 // 4. Stream reply
64 try (AgentStudioEventStream stream = client.sessions().events().stream(session.getId())) {
65 for (Message event : stream) {
66 if ("message".equals(event.getType()) && event.getContent() != null) {
67 for (ContentBlock block : event.getContent()) {
68 if (block instanceof ContentBlock.Text) {
69 System.out.print(((ContentBlock.Text) block).getText());
70 }
71 }
72 } else if ("session_status".equals(event.getType())) {
73 Session.StopReason stopReason = event.getStopReason();
74 if (stopReason != null) {
75 System.out.println("\nstop_reason: " + stopReason.getType());
76 }
77 break;
78 }
79 }
80 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected