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

Class AgentStudioSimple

examples/legacy_2x/AgentStudioSimple.java:23–88  ·  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 // Only provide the model ID; pass an object when inference parameters are
46 // needed:
47 // .model(ModelConfig.builder().id("qwen3-max").temperature(0.7).build())
48 .model("qwen-plus")
49 .systemPrompt("你是一个简洁的助手。")
50 .build());
51 System.out.println("Agent: " + agent.getId());
52
53 // 2. Create Session
54 Session session =
55 client.sessions().create(SessionCreateParam.builder().agent(agent.getId()).build());
56 System.out.println("Session: " + session.getId());
57
58 // 3. Send message
59 client
60 .sessions()
61 .events()
62 .send(session.getId(), Collections.singletonList(ClientEvents.userMessage("你好")));
63
64 // 4. Stream reply
65 try (AgentStudioEventStream stream = client.sessions().events().stream(session.getId())) {
66 for (Message event : stream) {
67 if ("message".equals(event.getType()) && event.getContent() != null) {
68 for (ContentBlock block : event.getContent()) {
69 if (block instanceof ContentBlock.Text) {
70 System.out.print(((ContentBlock.Text) block).getText());
71 }
72 }
73 } else if ("session_status".equals(event.getType())) {
74 Session.StopReason stopReason = event.getStopReason();
75 if (stopReason != null) {
76 System.out.println("\nstop_reason: " + stopReason.getType());
77 }
78 break;
79 }
80 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected