My attempt at creating generative agent simulations for RPG games or research
Generative agents are virtual characters that can learn and adapt to their environment. Using LLM models such as ChatGPT or StableLM, agents are able to observe their surroundings, store memories, and react to state changes in the world
Generative Agents save time programming interactions by hand and make NPC's more realistic / dynamic
- Download and install the Java server.
- Use the JavaScript SDK to create your own generative agent.
- Connect your generative agent to the server.
- Start the server and let your generative agent run in your game.
Visit the package on npm https://www.npmjs.com/package/smallville
Start a new javascript project
npm init
npm i smallville
Download the compiled jar from releases
Create new agents (as seen in the example project) example project
const sim = new Smallville({
host: "http://localhost:8080", // host of the server
stateHandler: function(state) {
//in here you would update the location of the agent using your own pathfinding algorithm
const currentLocation = state.agents[0].getCurrentLocation()
const newLocation = state.agents[0].getNextLocation()
const emoji = state.agents[0].getEmoji()
const activity = state.agents[0].getCurrentActivity()
const updatedLocations = state.locations;
const conversations = state.conversations;
console.log('[State Change]: The simulation has been updated')
},
});
Create new location trees
sim.createLocation({
name: 'Barn',
description: 'An empty barn'
})
sim.createLocation({
name: 'Hay Pile',
parent: 'Barn',
description: 'A hay pile',
state: 'Full'
})
Add new agents and initialize their memory stream with starting memories
sim.createAgent({
name: 'John',
location: 'Barn: Hay Pile',
memories: [
"John is a farmer at the Barn",
"John is a nice and outgoing person"
]
})
Increment the time clock. The simulation will get the current time and update the agents state
sim.next();
Keep calling sim.next whenever you want to update the simulation step
To add new observations to the agent which they will prompt a reaction to use the following method
sim.addObservation({name: "Full Agent Name", observation: "memory description", reactable: true})
Such observations such as encountering another agent or discovering a location should make use of this
For observatonal purposes, you can also ask an agent a question which will use their relevant memories to answer the question
sim.askQuestion("John", "What do you do in your free time")
This will not store the question in the agents memory unless you call sim.addObservation()
Furthermore, locations should be given as a full tree. ex)
location: "Island: Red House: desk"
And the leaf node (in this example desk), should have a state, although it is not necessary. Adding states to leaf locations enables the agents to interact with the world. To get the leaf location and move the agent to the location you can use our utility function getLeafLocation(location)
or make your own method.
Running the Server The Smallville World Simulator comes with a Java 17 server that you can use to store the simulation data.
Run the following command in the same directory as your jar file downloaded from releases
java -jar smallville-server.jar --api-key <OPEN_AI_KEY> --port 8080
The server will start on the default port 8080 unless specified otherwise
The example is under the example directory. Start a node server on that port and run the java server to start the simulation. This example isn't finished yet but is a basic example of how to get started. example javascript project
- Reflections
- Improve conversations
- Finish example game
- Improve memory retrieval
- Improve token embeddings
- Add option to use StableLM
- Bug fixes on location state updates
- Work on a way around possible timeouts from long request wait times
- Improve test coverage
Code based on Generative Agents: Interactive Simulacra of Human Behavior https://arxiv.org/pdf/2304.03442.pdf
If you need help getting started with smallville you can join our community discord https://discord.gg/ktXPsgbFp5