Skip to content

Commit

Permalink
refactored prompts, adding templating engine, added configuration opt…
Browse files Browse the repository at this point in the history
…ions, refactored package names
  • Loading branch information
nickm980 committed May 23, 2023
1 parent 3541ff6 commit 4f80277
Show file tree
Hide file tree
Showing 81 changed files with 1,053 additions and 831 deletions.
7 changes: 7 additions & 0 deletions smallville/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,12 @@
<artifactId>SimpleNLG</artifactId>
<version>4.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

import com.beust.jcommander.JCommander;

import io.github.nickm980.smallville.api.server.SmallvilleServer;
import io.github.nickm980.smallville.api.SmallvilleServer;
import io.github.nickm980.smallville.config.CommandLineArgs;
import io.github.nickm980.smallville.config.Config;
import io.github.nickm980.smallville.config.SmallvilleConfig;
import io.github.nickm980.smallville.llm.ChatGPT;
import io.github.nickm980.smallville.nlp.LocalNLP;

Expand All @@ -20,7 +20,7 @@ public class Smallville {

public static void main(String[] args) throws IOException {
configureLogs();

CommandLineArgs options = loadArgs(args);

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

LOG.info("Starting server...");

loadConfig();

LocalNLP.preLoad();

startServer(port);

startServer(port);

Updater.checkLatestVersion();
LOG.info("Smallville server started on port " + port);

}

private static CommandLineArgs loadArgs(String[] args) {
Expand All @@ -48,8 +50,8 @@ private static CommandLineArgs loadArgs(String[] args) {
}

private static void loadConfig() {
Config.getConfig();
Config.getPrompts();
SmallvilleConfig.getConfig();
SmallvilleConfig.getPrompts();
}

private static void configureLogs() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.nickm980.smallville;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Updater {

private static final String VERSION = "v1.0.0";
private final static Logger LOG = LoggerFactory.getLogger(Updater.class);

private Updater() {

}

public static void checkLatestVersion() {
OkHttpClient client = new OkHttpClient();
String url = "https://api.github.com/repos/nickm980/smallville/releases/latest";
Request request = new Request.Builder().url(url).build();

try {
Response response = client.newCall(request).execute();
// Check if the request was successful
if (response.isSuccessful()) {
String body = response.body().string();

Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(body, JsonObject.class);
String tag = jsonObject.get("tag_name").getAsString();

if (VERSION.compareTo(tag) < 0) {
LOG.warn("Your version of smallville " + VERSION + " is outdated. Latest version: " + tag);
}
}
} catch (Exception e) {
LOG.debug(e.getStackTrace().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.github.nickm980.smallville.entities.Agent;
import io.github.nickm980.smallville.entities.Conversation;
import io.github.nickm980.smallville.entities.Dialog;
import io.github.nickm980.smallville.entities.Location;
import io.github.nickm980.smallville.entities.SimulatedLocation;
import io.github.nickm980.smallville.entities.SimulatedObject;
import io.github.nickm980.smallville.exceptions.LocationNotFoundException;
import io.github.nickm980.smallville.exceptions.SmallvilleException;
import io.github.nickm980.smallville.models.Agent;
import io.github.nickm980.smallville.models.Conversation;
import io.github.nickm980.smallville.models.Dialog;
import io.github.nickm980.smallville.models.Location;
import io.github.nickm980.smallville.models.SimulatedLocation;
import io.github.nickm980.smallville.models.SimulatedObject;

/**
* Creates an interactive Simulation for Generative Agents
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api.server;
package io.github.nickm980.smallville.api;

import java.util.Map;
import java.util.NoSuchElementException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api.server;
package io.github.nickm980.smallville.api;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -11,27 +11,19 @@

import io.github.nickm980.smallville.Util;
import io.github.nickm980.smallville.World;
import io.github.nickm980.smallville.api.AgentStateResponse;
import io.github.nickm980.smallville.api.ConversationResponse;
import io.github.nickm980.smallville.api.CreateAgentRequest;
import io.github.nickm980.smallville.api.CreateLocationRequest;
import io.github.nickm980.smallville.api.CreateMemoryRequest;
import io.github.nickm980.smallville.api.CreateObjectRequest;
import io.github.nickm980.smallville.api.LocationStateResponse;
import io.github.nickm980.smallville.api.MemoryResponse;
import io.github.nickm980.smallville.api.ModelMapper;
import io.github.nickm980.smallville.api.dto.*;
import io.github.nickm980.smallville.entities.AccessTime;
import io.github.nickm980.smallville.entities.Agent;
import io.github.nickm980.smallville.entities.AgentLocation;
import io.github.nickm980.smallville.entities.Conversation;
import io.github.nickm980.smallville.entities.ObjectState;
import io.github.nickm980.smallville.entities.SimulatedLocation;
import io.github.nickm980.smallville.entities.SimulatedObject;
import io.github.nickm980.smallville.entities.memory.Characteristic;
import io.github.nickm980.smallville.exceptions.AgentNotFoundException;
import io.github.nickm980.smallville.exceptions.LocationNotFoundException;
import io.github.nickm980.smallville.exceptions.SmallvilleException;
import io.github.nickm980.smallville.llm.LLM;
import io.github.nickm980.smallville.models.AccessTime;
import io.github.nickm980.smallville.models.Agent;
import io.github.nickm980.smallville.models.AgentLocation;
import io.github.nickm980.smallville.models.Conversation;
import io.github.nickm980.smallville.models.ObjectState;
import io.github.nickm980.smallville.models.SimulatedLocation;
import io.github.nickm980.smallville.models.SimulatedObject;
import io.github.nickm980.smallville.models.memory.Characteristic;
import io.github.nickm980.smallville.update.UpdateService;

public class SimulationService {
Expand Down Expand Up @@ -154,4 +146,8 @@ public void createObject(CreateObjectRequest request) {

world.save(object);
}

public void setGoal(String name, String goal) {
world.getAgent(name).orElseThrow().setGoal(goal);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api.server;
package io.github.nickm980.smallville.api;

import java.io.StringWriter;
import java.util.HashMap;
Expand All @@ -9,14 +9,7 @@
import com.github.mustachejava.MustacheFactory;

import io.github.nickm980.smallville.World;
import io.github.nickm980.smallville.api.AgentStateResponse;
import io.github.nickm980.smallville.api.AskQuestionRequest;
import io.github.nickm980.smallville.api.ConversationResponse;
import io.github.nickm980.smallville.api.CreateAgentRequest;
import io.github.nickm980.smallville.api.CreateLocationRequest;
import io.github.nickm980.smallville.api.CreateMemoryRequest;
import io.github.nickm980.smallville.api.CreateObjectRequest;
import io.github.nickm980.smallville.api.LocationStateResponse;
import io.github.nickm980.smallville.api.dto.*;
import io.github.nickm980.smallville.llm.LLM;
import io.javalin.Javalin;

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

app.post("/agents/{name}/goal", (ctx) -> {
SetGoalRequest request = ctx
.bodyValidator(SetGoalRequest.class)
.check((req) -> exists(req.getGoal()), "{goal} cannot be blank")
.get();

service.setGoal(ctx.pathParam("name"), request.getGoal());

ctx.json(Map.of("success", true));
});

app.post("/agents", (ctx) -> {
CreateAgentRequest request = ctx
.bodyValidator(CreateAgentRequest.class)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

public class AgentStateResponse {
private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

public class AskQuestionRequest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;


public class CreateLocationRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

public class CreateMemoryRequest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

public class CreateObjectRequest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

public class LocationStateResponse {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

import java.time.LocalDateTime;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package io.github.nickm980.smallville.api;
package io.github.nickm980.smallville.api.dto;

import java.util.HashMap;
import java.util.Map;

import io.github.nickm980.smallville.models.Agent;
import io.github.nickm980.smallville.models.Conversation;
import io.github.nickm980.smallville.models.Dialog;
import io.github.nickm980.smallville.models.Location;
import io.github.nickm980.smallville.models.SimulatedObject;
import io.github.nickm980.smallville.models.memory.Characteristic;
import io.github.nickm980.smallville.models.memory.Memory;
import io.github.nickm980.smallville.models.memory.Plan;
import io.github.nickm980.smallville.models.memory.TemporalMemory;
import io.github.nickm980.smallville.entities.Agent;
import io.github.nickm980.smallville.entities.Conversation;
import io.github.nickm980.smallville.entities.Dialog;
import io.github.nickm980.smallville.entities.Location;
import io.github.nickm980.smallville.entities.SimulatedObject;
import io.github.nickm980.smallville.entities.memory.Characteristic;
import io.github.nickm980.smallville.entities.memory.Memory;
import io.github.nickm980.smallville.entities.memory.Plan;
import io.github.nickm980.smallville.entities.memory.TemporalMemory;

public class ModelMapper {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.nickm980.smallville.api.dto;

public class SetGoalRequest {

private String goal;

public String getGoal() {
return goal;
}

public void setGoal(String goal) {
this.goal = goal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ public class GeneralConfig {

private String apiPath;
private String timeFormat;
private String fullTimeFormat;
private String yesterdayFormat;

public String getYesterdayFormat() {
return yesterdayFormat;
}

public void setYesterdayFormat(String yesterdayFormat) {
this.yesterdayFormat = yesterdayFormat;
}

public String getFullTimeFormat() {
return fullTimeFormat;
}

public void setFullTimeFormat(String fullTimeFormat) {
this.fullTimeFormat = fullTimeFormat;
}

public String getTimeFormat() {
return timeFormat;
Expand Down
Loading

0 comments on commit 4f80277

Please sign in to comment.