Skip to content

Commit 4f80277

Browse files
committed
refactored prompts, adding templating engine, added configuration options, refactored package names
1 parent 3541ff6 commit 4f80277

File tree

81 files changed

+1053
-831
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+1053
-831
lines changed

smallville/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,5 +176,12 @@
176176
<artifactId>SimpleNLG</artifactId>
177177
<version>4.5.0</version>
178178
</dependency>
179+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
180+
<dependency>
181+
<groupId>com.google.code.gson</groupId>
182+
<artifactId>gson</artifactId>
183+
<version>2.10.1</version>
184+
</dependency>
185+
179186
</dependencies>
180187
</project>

smallville/src/main/java/io/github/nickm980/smallville/Smallville.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import com.beust.jcommander.JCommander;
1010

11-
import io.github.nickm980.smallville.api.server.SmallvilleServer;
11+
import io.github.nickm980.smallville.api.SmallvilleServer;
1212
import io.github.nickm980.smallville.config.CommandLineArgs;
13-
import io.github.nickm980.smallville.config.Config;
13+
import io.github.nickm980.smallville.config.SmallvilleConfig;
1414
import io.github.nickm980.smallville.llm.ChatGPT;
1515
import io.github.nickm980.smallville.nlp.LocalNLP;
1616

@@ -20,7 +20,7 @@ public class Smallville {
2020

2121
public static void main(String[] args) throws IOException {
2222
configureLogs();
23-
23+
2424
CommandLineArgs options = loadArgs(args);
2525

2626
int port = options.getPort();
@@ -29,14 +29,16 @@ public static void main(String[] args) throws IOException {
2929
Settings.setApiKey(key);
3030

3131
LOG.info("Starting server...");
32-
32+
3333
loadConfig();
3434

3535
LocalNLP.preLoad();
36-
37-
startServer(port);
3836

37+
startServer(port);
38+
39+
Updater.checkLatestVersion();
3940
LOG.info("Smallville server started on port " + port);
41+
4042
}
4143

4244
private static CommandLineArgs loadArgs(String[] args) {
@@ -48,8 +50,8 @@ private static CommandLineArgs loadArgs(String[] args) {
4850
}
4951

5052
private static void loadConfig() {
51-
Config.getConfig();
52-
Config.getPrompts();
53+
SmallvilleConfig.getConfig();
54+
SmallvilleConfig.getPrompts();
5355
}
5456

5557
private static void configureLogs() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.github.nickm980.smallville;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
import com.google.gson.Gson;
7+
import com.google.gson.JsonObject;
8+
import okhttp3.OkHttpClient;
9+
import okhttp3.Request;
10+
import okhttp3.Response;
11+
12+
public class Updater {
13+
14+
private static final String VERSION = "v1.0.0";
15+
private final static Logger LOG = LoggerFactory.getLogger(Updater.class);
16+
17+
private Updater() {
18+
19+
}
20+
21+
public static void checkLatestVersion() {
22+
OkHttpClient client = new OkHttpClient();
23+
String url = "https://api.github.com/repos/nickm980/smallville/releases/latest";
24+
Request request = new Request.Builder().url(url).build();
25+
26+
try {
27+
Response response = client.newCall(request).execute();
28+
// Check if the request was successful
29+
if (response.isSuccessful()) {
30+
String body = response.body().string();
31+
32+
Gson gson = new Gson();
33+
JsonObject jsonObject = gson.fromJson(body, JsonObject.class);
34+
String tag = jsonObject.get("tag_name").getAsString();
35+
36+
if (VERSION.compareTo(tag) < 0) {
37+
LOG.warn("Your version of smallville " + VERSION + " is outdated. Latest version: " + tag);
38+
}
39+
}
40+
} catch (Exception e) {
41+
LOG.debug(e.getStackTrace().toString());
42+
}
43+
}
44+
}

smallville/src/main/java/io/github/nickm980/smallville/World.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
1414

15+
import io.github.nickm980.smallville.entities.Agent;
16+
import io.github.nickm980.smallville.entities.Conversation;
17+
import io.github.nickm980.smallville.entities.Dialog;
18+
import io.github.nickm980.smallville.entities.Location;
19+
import io.github.nickm980.smallville.entities.SimulatedLocation;
20+
import io.github.nickm980.smallville.entities.SimulatedObject;
1521
import io.github.nickm980.smallville.exceptions.LocationNotFoundException;
1622
import io.github.nickm980.smallville.exceptions.SmallvilleException;
17-
import io.github.nickm980.smallville.models.Agent;
18-
import io.github.nickm980.smallville.models.Conversation;
19-
import io.github.nickm980.smallville.models.Dialog;
20-
import io.github.nickm980.smallville.models.Location;
21-
import io.github.nickm980.smallville.models.SimulatedLocation;
22-
import io.github.nickm980.smallville.models.SimulatedObject;
2323

2424
/**
2525
* Creates an interactive Simulation for Generative Agents

smallville/src/main/java/io/github/nickm980/smallville/api/server/ExceptionRoutes.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/ExceptionRoutes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api.server;
1+
package io.github.nickm980.smallville.api;
22

33
import java.util.Map;
44
import java.util.NoSuchElementException;

smallville/src/main/java/io/github/nickm980/smallville/api/server/SimulationService.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/SimulationService.java

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api.server;
1+
package io.github.nickm980.smallville.api;
22

33
import java.util.ArrayList;
44
import java.util.Comparator;
@@ -11,27 +11,19 @@
1111

1212
import io.github.nickm980.smallville.Util;
1313
import io.github.nickm980.smallville.World;
14-
import io.github.nickm980.smallville.api.AgentStateResponse;
15-
import io.github.nickm980.smallville.api.ConversationResponse;
16-
import io.github.nickm980.smallville.api.CreateAgentRequest;
17-
import io.github.nickm980.smallville.api.CreateLocationRequest;
18-
import io.github.nickm980.smallville.api.CreateMemoryRequest;
19-
import io.github.nickm980.smallville.api.CreateObjectRequest;
20-
import io.github.nickm980.smallville.api.LocationStateResponse;
21-
import io.github.nickm980.smallville.api.MemoryResponse;
22-
import io.github.nickm980.smallville.api.ModelMapper;
14+
import io.github.nickm980.smallville.api.dto.*;
15+
import io.github.nickm980.smallville.entities.AccessTime;
16+
import io.github.nickm980.smallville.entities.Agent;
17+
import io.github.nickm980.smallville.entities.AgentLocation;
18+
import io.github.nickm980.smallville.entities.Conversation;
19+
import io.github.nickm980.smallville.entities.ObjectState;
20+
import io.github.nickm980.smallville.entities.SimulatedLocation;
21+
import io.github.nickm980.smallville.entities.SimulatedObject;
22+
import io.github.nickm980.smallville.entities.memory.Characteristic;
2323
import io.github.nickm980.smallville.exceptions.AgentNotFoundException;
2424
import io.github.nickm980.smallville.exceptions.LocationNotFoundException;
2525
import io.github.nickm980.smallville.exceptions.SmallvilleException;
2626
import io.github.nickm980.smallville.llm.LLM;
27-
import io.github.nickm980.smallville.models.AccessTime;
28-
import io.github.nickm980.smallville.models.Agent;
29-
import io.github.nickm980.smallville.models.AgentLocation;
30-
import io.github.nickm980.smallville.models.Conversation;
31-
import io.github.nickm980.smallville.models.ObjectState;
32-
import io.github.nickm980.smallville.models.SimulatedLocation;
33-
import io.github.nickm980.smallville.models.SimulatedObject;
34-
import io.github.nickm980.smallville.models.memory.Characteristic;
3527
import io.github.nickm980.smallville.update.UpdateService;
3628

3729
public class SimulationService {
@@ -154,4 +146,8 @@ public void createObject(CreateObjectRequest request) {
154146

155147
world.save(object);
156148
}
149+
150+
public void setGoal(String name, String goal) {
151+
world.getAgent(name).orElseThrow().setGoal(goal);
152+
}
157153
}

smallville/src/main/java/io/github/nickm980/smallville/api/server/SmallvilleServer.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/SmallvilleServer.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api.server;
1+
package io.github.nickm980.smallville.api;
22

33
import java.io.StringWriter;
44
import java.util.HashMap;
@@ -9,14 +9,7 @@
99
import com.github.mustachejava.MustacheFactory;
1010

1111
import io.github.nickm980.smallville.World;
12-
import io.github.nickm980.smallville.api.AgentStateResponse;
13-
import io.github.nickm980.smallville.api.AskQuestionRequest;
14-
import io.github.nickm980.smallville.api.ConversationResponse;
15-
import io.github.nickm980.smallville.api.CreateAgentRequest;
16-
import io.github.nickm980.smallville.api.CreateLocationRequest;
17-
import io.github.nickm980.smallville.api.CreateMemoryRequest;
18-
import io.github.nickm980.smallville.api.CreateObjectRequest;
19-
import io.github.nickm980.smallville.api.LocationStateResponse;
12+
import io.github.nickm980.smallville.api.dto.*;
2013
import io.github.nickm980.smallville.llm.LLM;
2114
import io.javalin.Javalin;
2215

@@ -92,6 +85,17 @@ public void start(int port) {
9285
ctx.json(Map.of("answer", res));
9386
});
9487

88+
app.post("/agents/{name}/goal", (ctx) -> {
89+
SetGoalRequest request = ctx
90+
.bodyValidator(SetGoalRequest.class)
91+
.check((req) -> exists(req.getGoal()), "{goal} cannot be blank")
92+
.get();
93+
94+
service.setGoal(ctx.pathParam("name"), request.getGoal());
95+
96+
ctx.json(Map.of("success", true));
97+
});
98+
9599
app.post("/agents", (ctx) -> {
96100
CreateAgentRequest request = ctx
97101
.bodyValidator(CreateAgentRequest.class)

smallville/src/main/java/io/github/nickm980/smallville/api/AgentStateResponse.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/dto/AgentStateResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api;
1+
package io.github.nickm980.smallville.api.dto;
22

33
public class AgentStateResponse {
44
private String name;

smallville/src/main/java/io/github/nickm980/smallville/api/AskQuestionRequest.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/dto/AskQuestionRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api;
1+
package io.github.nickm980.smallville.api.dto;
22

33
public class AskQuestionRequest {
44

smallville/src/main/java/io/github/nickm980/smallville/api/ConversationResponse.java renamed to smallville/src/main/java/io/github/nickm980/smallville/api/dto/ConversationResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.nickm980.smallville.api;
1+
package io.github.nickm980.smallville.api.dto;
22

33
import java.util.Map;
44

0 commit comments

Comments
 (0)