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

Class AgentStudioSimple

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected