Skip to content

Commit 0d4cdc7

Browse files
authored
refactor: split examples (#122)
1 parent ee20255 commit 0d4cdc7

56 files changed

Lines changed: 913 additions & 854 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2024-2025 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ You may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<project xmlns="http://maven.apache.org/POM/4.0.0"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
21+
<modelVersion>4.0.0</modelVersion>
22+
23+
<parent>
24+
<groupId>io.agentscope</groupId>
25+
<artifactId>agentscope-examples</artifactId>
26+
<version>${revision}</version>
27+
</parent>
28+
29+
<artifactId>agentscope-examples-advanced</artifactId>
30+
<packaging>jar</packaging>
31+
32+
<name>AgentScope Examples - Advanced</name>
33+
<description>Advanced examples for AgentScope Java SDK (RAG, Mem0, Studio)</description>
34+
35+
<dependencies>
36+
<!-- AgentScope Core -->
37+
<dependency>
38+
<groupId>io.agentscope</groupId>
39+
<artifactId>agentscope-core</artifactId>
40+
</dependency>
41+
42+
<!-- AgentScope Extensions -->
43+
<dependency>
44+
<groupId>io.agentscope</groupId>
45+
<artifactId>agentscope-extensions-rag-bailian</artifactId>
46+
</dependency>
47+
48+
<dependency>
49+
<groupId>io.agentscope</groupId>
50+
<artifactId>agentscope-extensions-rag-simple</artifactId>
51+
</dependency>
52+
53+
<dependency>
54+
<groupId>io.agentscope</groupId>
55+
<artifactId>agentscope-extensions-mem0</artifactId>
56+
</dependency>
57+
58+
<dependency>
59+
<groupId>io.agentscope</groupId>
60+
<artifactId>agentscope-extensions-studio</artifactId>
61+
</dependency>
62+
63+
<!-- Spring Boot for web examples -->
64+
<dependency>
65+
<groupId>org.springframework.boot</groupId>
66+
<artifactId>spring-boot-starter-webflux</artifactId>
67+
</dependency>
68+
</dependencies>
69+
70+
</project>

agentscope-examples/src/main/java/io/agentscope/examples/BailianRAGExample.java renamed to agentscope-examples/advanced/src/main/java/io/agentscope/examples/advanced/BailianRAGExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.agentscope.examples;
16+
package io.agentscope.examples.advanced;
1717

1818
import io.agentscope.core.ReActAgent;
1919
import io.agentscope.core.agent.user.UserAgent;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.examples.advanced;
17+
18+
import io.agentscope.core.ReActAgent;
19+
import io.agentscope.core.message.Msg;
20+
import io.agentscope.core.message.MsgRole;
21+
import io.agentscope.core.message.TextBlock;
22+
import io.agentscope.examples.advanced.util.MsgUtils;
23+
import java.io.BufferedReader;
24+
import java.io.IOException;
25+
import java.io.InputStreamReader;
26+
27+
/**
28+
* Utility class providing common functionality for examples.
29+
*/
30+
public class ExampleUtils {
31+
32+
private static final BufferedReader reader =
33+
new BufferedReader(new InputStreamReader(System.in));
34+
35+
private ExampleUtils() {}
36+
37+
/**
38+
* Gets the DashScope API key from environment variable. Exits if not set.
39+
*
40+
* @return The API key
41+
*/
42+
public static String getDashScopeApiKey() {
43+
String apiKey = System.getenv("DASHSCOPE_API_KEY");
44+
if (apiKey == null || apiKey.isEmpty()) {
45+
System.err.println("Error: DASHSCOPE_API_KEY environment variable not set.");
46+
System.err.println("Please set it with: export DASHSCOPE_API_KEY=your_api_key");
47+
System.exit(1);
48+
}
49+
return apiKey;
50+
}
51+
52+
/**
53+
* Prints a welcome message for an example.
54+
*
55+
* @param title The title of the example
56+
* @param description The description of what the example demonstrates
57+
*/
58+
public static void printWelcome(String title, String description) {
59+
System.out.println("\n" + "=".repeat(80));
60+
System.out.println(title);
61+
System.out.println("=".repeat(80));
62+
System.out.println(description);
63+
System.out.println("=".repeat(80) + "\n");
64+
}
65+
66+
/**
67+
* Starts an interactive chat session with the given agent.
68+
*
69+
* @param agent The agent to chat with
70+
* @throws IOException If reading input fails
71+
*/
72+
public static void startChat(ReActAgent agent) throws IOException {
73+
System.out.println("Chat started! Type 'exit' to quit.\n");
74+
75+
while (true) {
76+
System.out.print("You: ");
77+
String input = reader.readLine();
78+
79+
if (input == null || input.trim().equalsIgnoreCase("exit")) {
80+
System.out.println("\nGoodbye!");
81+
break;
82+
}
83+
84+
if (input.trim().isEmpty()) {
85+
continue;
86+
}
87+
88+
Msg userMsg =
89+
Msg.builder()
90+
.role(MsgRole.USER)
91+
.content(TextBlock.builder().text(input.trim()).build())
92+
.build();
93+
94+
System.out.print("\nAssistant: ");
95+
try {
96+
Msg response = agent.call(userMsg).block();
97+
if (response != null) {
98+
System.out.println(MsgUtils.getTextContent(response));
99+
}
100+
System.out.println();
101+
} catch (Exception e) {
102+
System.err.println("\nError: " + e.getMessage());
103+
}
104+
}
105+
}
106+
}

agentscope-examples/src/main/java/io/agentscope/examples/Mem0Example.java renamed to agentscope-examples/advanced/src/main/java/io/agentscope/examples/advanced/Mem0Example.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.agentscope.examples;
16+
package io.agentscope.examples.advanced;
1717

1818
import io.agentscope.core.ReActAgent;
1919
import io.agentscope.core.agent.user.UserAgent;

agentscope-examples/src/main/java/io/agentscope/examples/RAGExample.java renamed to agentscope-examples/advanced/src/main/java/io/agentscope/examples/advanced/RAGExample.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.agentscope.examples;
16+
package io.agentscope.examples.advanced;
1717

1818
import io.agentscope.core.ReActAgent;
1919
import io.agentscope.core.embedding.EmbeddingModel;
@@ -62,12 +62,12 @@ public static void main(String[] args) throws Exception {
6262
.modelName("text-embedding-v3")
6363
.dimensions(EMBEDDING_DIMENSIONS)
6464
.build();
65-
System.out.println("✓ Embedding model created\n");
65+
System.out.println("Embedding model created\n");
6666

6767
// Create vector store
6868
System.out.println("Creating vector store...");
6969
VDBStoreBase vectorStore = InMemoryStore.builder().dimensions(1024).build();
70-
System.out.println("✓ Vector store created\n");
70+
System.out.println("Vector store created\n");
7171

7272
// Create knowledge base
7373
System.out.println("Creating knowledge base...");
@@ -76,12 +76,12 @@ public static void main(String[] args) throws Exception {
7676
.embeddingModel(embeddingModel)
7777
.embeddingStore(vectorStore)
7878
.build();
79-
System.out.println("✓ Knowledge base created\n");
79+
System.out.println("Knowledge base created\n");
8080

8181
// Add documents to knowledge base
8282
System.out.println("Adding documents to knowledge base...");
8383
addSampleDocuments(knowledge);
84-
System.out.println("✓ Documents added\n");
84+
System.out.println("Documents added\n");
8585

8686
// Demonstrate Agentic Mode
8787
System.out.println("\n=== Agentic RAG Mode ===");

agentscope-examples/src/main/java/io/agentscope/examples/StudioExample.java renamed to agentscope-examples/advanced/src/main/java/io/agentscope/examples/advanced/StudioExample.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package io.agentscope.examples;
16+
package io.agentscope.examples.advanced;
1717

1818
import io.agentscope.core.ReActAgent;
1919
import io.agentscope.core.formatter.dashscope.DashScopeChatFormatter;
@@ -31,20 +31,20 @@ public static void main(String[] args) throws Exception {
3131
// Get API key
3232
String apiKey = ExampleUtils.getDashScopeApiKey();
3333

34-
System.out.println("🚀 Starting Studio Example...\n");
34+
System.out.println("Starting Studio Example...\n");
3535

3636
// Initialize Studio
37-
System.out.println("📡 Connecting to Studio at http://localhost:3000...");
37+
System.out.println("Connecting to Studio at http://localhost:3000...");
3838
StudioManager.init()
3939
.studioUrl("http://localhost:3000")
4040
.project("JavaExamples")
4141
.runName("studio_demo_" + System.currentTimeMillis())
4242
.initialize()
4343
.block();
44-
System.out.println("✓ Connected to Studio\n");
44+
System.out.println("Connected to Studio\n");
4545

4646
// Create agent with Studio hook
47-
System.out.println("🤖 Creating agent with Studio integration...");
47+
System.out.println("Creating agent with Studio integration...");
4848
ReActAgent agent =
4949
ReActAgent.builder()
5050
.name("Assistant")
@@ -56,21 +56,21 @@ public static void main(String[] args) throws Exception {
5656
.formatter(new DashScopeChatFormatter())
5757
.build())
5858
.build();
59-
System.out.println("✓ Agent created\n");
59+
System.out.println("Agent created\n");
6060

6161
// Create user agent
62-
System.out.println("👤 Creating user agent...");
62+
System.out.println("Creating user agent...");
6363
StudioUserAgent user =
6464
StudioUserAgent.builder()
6565
.name("User")
6666
.studioClient(StudioManager.getClient())
6767
.webSocketClient(StudioManager.getWebSocketClient())
6868
.build();
69-
System.out.println("✓ User agent ready\n");
69+
System.out.println("User agent ready\n");
7070

7171
// Conversation loop
72-
System.out.println("💬 Starting conversation (type 'exit' to quit)");
73-
System.out.println("🌐 Open http://localhost:3000 to interact\n");
72+
System.out.println("Starting conversation (type 'exit' to quit)");
73+
System.out.println("Open http://localhost:3000 to interact\n");
7474

7575
try {
7676
Msg msg = null;
@@ -80,7 +80,7 @@ public static void main(String[] args) throws Exception {
8080
msg = user.call(msg).block();
8181

8282
if (msg == null || "exit".equalsIgnoreCase(msg.getTextContent())) {
83-
System.out.println("\n👋 Conversation ended");
83+
System.out.println("\nConversation ended");
8484
break;
8585
}
8686

@@ -96,9 +96,9 @@ public static void main(String[] args) throws Exception {
9696
turn++;
9797
}
9898
} finally {
99-
System.out.println("\n🧹 Shutting down...");
99+
System.out.println("\nShutting down...");
100100
StudioManager.shutdown();
101-
System.out.println("✓ Done\n");
101+
System.out.println("Done\n");
102102
}
103103
}
104104
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.agentscope.examples.advanced.util;
17+
18+
import io.agentscope.core.message.ContentBlock;
19+
import io.agentscope.core.message.Msg;
20+
import io.agentscope.core.message.TextBlock;
21+
import java.util.List;
22+
23+
/**
24+
* Utility class for handling Msg objects.
25+
*/
26+
public final class MsgUtils {
27+
28+
private MsgUtils() {
29+
// Utility class, no instantiation
30+
}
31+
32+
/**
33+
* Extracts text content from a Msg object.
34+
*
35+
* @param msg the message to extract text from
36+
* @return the concatenated text content, or empty string if null
37+
*/
38+
public static String getTextContent(Msg msg) {
39+
if (msg == null) {
40+
return "";
41+
}
42+
43+
List<ContentBlock> content = msg.getContent();
44+
if (content == null || content.isEmpty()) {
45+
return "";
46+
}
47+
48+
StringBuilder sb = new StringBuilder();
49+
for (ContentBlock block : content) {
50+
if (block instanceof TextBlock textBlock) {
51+
String text = textBlock.getText();
52+
if (text != null) {
53+
sb.append(text);
54+
}
55+
}
56+
}
57+
return sb.toString();
58+
}
59+
}

agentscope-examples/src/main/resources/logback.xml renamed to agentscope-examples/advanced/src/main/resources/logback.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
</root>
3232

3333
</configuration>
34+

0 commit comments

Comments
 (0)