Skip to content

Commit

Permalink
replay mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason committed Feb 8, 2024
1 parent d386c75 commit f9989b2
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,20 +512,20 @@ def setPosition(self, position):
def getPosition(self):
return (self.x, self.y)

def addMemory(self, kind, content, timestamp=None, score=None):
def addMemory(self, kind, content, timestamp=None, score=None,embedding=None):
if timestamp is None:
timestamp = datetime.now()
timestamp = timestamp.strftime("%A %B %d, %Y")

if kind == "observation":
if (self.matrix is not None and self.matrix.allow_observance_flag == 1) or (self.matrix is None and ALLOW_OBSERVANCE == 1):
memory = Memory(kind, content, timestamp, timestamp, score)
if (self.matrix is not None and self.matrix.allow_observance_flag == 1):
memory = Memory(kind, content, timestamp, timestamp, score,embedding)
self.memory.append(memory)
else:
memory = Memory(kind, content, timestamp, timestamp, score)
memory = Memory(kind, content, timestamp, timestamp, score,embedding)
self.memory.append(memory)
if self.matrix:
self.matrix.add_to_logs({"agent_id":self.id,"step_type":"add_memory","kind":kind,"timestamp":timestamp,"last_accessed_at":timestamp,"score":score,"content": content})
self.matrix.add_to_logs({"agent_id":self.id,"step_type":"add_memory","kind":kind,"timestamp":timestamp,"last_accessed_at":timestamp,"score":score,"content": content,"embedding":memory.embedding})

def reflect(self, timestamp, force=False):
relevant_memories = self.memory[-100:]
Expand Down Expand Up @@ -628,7 +628,7 @@ def __init__(self, dictionary):
context_embedding = llm.embeddings(context)

for mem in self.memory:
relevancy_score = Memory.calculateRelevanceScore(mem.embedding_score, context_embedding)
relevancy_score = Memory.calculateRelevanceScore(mem.embedding, context_embedding)
min_relevancy_score = min(min_relevancy_score, relevancy_score)
max_relevancy_score = max(max_relevancy_score, relevancy_score)

Expand Down
55 changes: 54 additions & 1 deletion src/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,59 @@ def boot(self):
zombie = Agent({ "name": f"Zombie_{i}", "kind": "zombie", "actions": ["kill"],"matrix":self })
self.add_agent_to_simulation(zombie)

@classmethod
def from_timeline(cls,src,step=None):
if src.startswith("redis://"):
r = redis_connection.from_url(src)
queue = "josh_queue"
data = r.lrange(queue, 0, -1)
else:
with open(src, 'r') as file:
content = file.read()
data = content.split('\n\n')
matrix = None
current_step = 0
current_substep = 0
for datum in data:
record = json.loads(datum)
if step and record["step"] > step:
break

print(f"at step {record['step']}.{record['substep']}")
if matrix is None and record['step'] == 0 and record['substep'] == 0:
set_globals(record['data'])
matrix = Matrix(record['data'])
elif matrix:
if record['step_type'] == 'agent_init':
config = record
config['matrix'] = matrix
agent = Agent(config)
matrix.add_agent_to_simulation(agent)
elif record['step_type'] == "agent_set":
agent = next((agent for agent in matrix.agents if agent.id == record['agent_id']), None)
if agent:
if record["status"]:
agent.status = record["status"]
elif record['step_type'] == "talk":
pass
# need to do some refactoring with convos....
elif record['step_type'] == "move":
agent = next((agent for agent in matrix.agents if agent.id == record['agent_id']), None)
if agent:
agent.x = record['x']
agent.y = record['y']
elif record['step_type'] == "add_memory":
agent = next((agent for agent in matrix.agents if agent.id == record['agent_id']), None)
if agent:
agent.addMemory(record["kind"],record["wcontent"],record["last_accessed_at"],record["score"])
elif record["step_type"] == "perceived":
pass
else:
print(f"unprocessd step {record['step_type']}")

return(matrix)





Expand Down Expand Up @@ -164,7 +217,7 @@ def add_to_logs(self,obj):

with open("logs.json", "a") as file:
json.dump(obj,file,indent=2)
file.write("\n")
file.write("\n\n")
stream = f"{obj['sim_id']}_stream"
queue = f"{obj['sim_id']}_queue"
wtf = json.loads(json.dumps(obj, default=str))
Expand Down
7 changes: 5 additions & 2 deletions src/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sklearn.metrics.pairwise import cosine_similarity

class Memory:
def __init__(self, kind, content, created_at, last_accessed_at, score=None, memories=[]):
def __init__(self, kind, content, created_at, last_accessed_at, score=None,embedding=None, memories=[]):
self.id = str(uuid.uuid4())
self.kind = kind
self.content = content
Expand All @@ -22,7 +22,10 @@ def __init__(self, kind, content, created_at, last_accessed_at, score=None, memo
self.recency_score = 1
self.relevancy_score = 1
self.overall_score = 1
self.embedding_score = llm.embeddings(content)
if embedding:
self.embedding = embedding
else:
self.embedding = llm.embeddings(content)
#self.simulation_id = simulation_id

#session.add(self)
Expand Down
4 changes: 2 additions & 2 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ def test_embeddings(self):

context_embedding = llm.embeddings(context)
sorted_memory = sorted(agent.memory,
key=lambda mem: Memory.calculateRelevanceScore(mem.embedding_score, context_embedding),
key=lambda mem: Memory.calculateRelevanceScore(mem.embedding, context_embedding),
reverse=True)

print(f"Context: {context}")
for mem in sorted_memory:
print(f"Current Memory: {mem}")
print(f"Relevance Score: {Memory.calculateRelevanceScore(mem.embedding_score, context_embedding)}")
print(f"Relevance Score: {Memory.calculateRelevanceScore(mem.embedding, context_embedding)}")
self.assertTrue(len(sorted_memory) > 0)

def test_talk(self):
Expand Down

0 comments on commit f9989b2

Please sign in to comment.