Skip to content

Commit

Permalink
refactor some functions and adjust rates
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason committed Feb 25, 2024
1 parent 3a62c08 commit 688222e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 31 deletions.
19 changes: 19 additions & 0 deletions prompts/general/evaluate_progress.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
About {{agent}}:
{{agent.getSelfContext()}}

{{agent}}'s goal is {{agent.goal}}.
And {{agent}}'s recent memories:
{{relevant_memories_string}}
Rate your progress towards your goal with an integer from 1-5, 1 being bad, 5 being good.


Write in the first person from the point of view of {{agent}}.
Write in natural short response style.
{% if random_prime %}
Think about the word "{{random_prime}}".
{% endif %}
Base your evaluation on your past memories only.
Format your response like:
Explanation: I didnt get my groceries today
Score: 1

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ SQLAlchemy==2.0.23
vllm==0.2.6
python-Levenshtein
jsonlines
nltk
41 changes: 16 additions & 25 deletions src/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,16 @@ def update_goals(self):
relevant_memories = self.getMemories(None,unix_to_strftime(self.matrix.unix_time))
relevant_memories_string = "\n".join(f"Memory {i + 1}: \"{memory}\"" for i, memory in enumerate(relevant_memories)) if relevant_memories else ""
prompt = f'''
you are a character in a world.
{self.getSelfContext()}
goal:"{self.goal}".
And {self}'s recent memories:
{relevant_memories_string}
Write out what my new goal should be in a short sentence given recent memories.
Take into account what happened recently to help you focus.
The goal should be something a real human would follow.
Take into account what happened recently to help you focus as your goal may need to change given circumstances.
Write in the first person from the point of view of {self}.
Write out the goal only in json:
{{"goal":"goal"}}
'''
Express your goal using only the action or outcome. Avoid adding phrases like 'my goal should be'
'''
msg = llm.generate(prompt)
self.addMemory("observation",f"I updated my goal to be \"{msg}\"", unix_to_strftime(self.matrix.unix_time), random.randint(5, 8))
self.goal = msg
Expand All @@ -96,26 +94,17 @@ def ask_meta_questions(self, timestamp):
m = re.compile('([\d]+\. )(.*?)(?=([\d]+\.)|($))', re.DOTALL).findall(msg)
self.meta_questions.extend(x[1] for x in m if x[1] not in self.meta_questions)

def evaluate_progress(self):
def evaluate_progress(self,opts={}):
relevant_memories = self.getMemories(self.goal, unix_to_strftime(self.matrix.unix_time))
relevant_memories_string = "\n".join(f"Memory {i + 1}:\n{memory}" for i, memory in enumerate(relevant_memories)) if relevant_memories else ""
prompt = f'''
About {self}:
{self.getSelfContext()}
{self}'s goal is {self.goal}.
And {self}'s recent memories:
{relevant_memories_string}
Rate your progress towards your goal with an integer from 1-5, 1 being bad, 5 being good.
Write in the first person from the point of view of {self}.
Write in natural short response style.
Format your response like:
Explanation: I didnt get my groceries today
Score: 1
'''
msg = llm.generate(prompt, f"How am I doing?")
random_prime = opts.get("random_prime",False)
variables = {
'agent': self,
'random_prime': random_common_word(),
'selfContext': self.getSelfContext()
}
#msg = llm.generate(prompt, f"How am I doing?")
msg = llm.prompt("evaluate_progress", variables)
explanation_match = re.search(r"Explanation:\s*(.+)", msg)
explanation = explanation_match.group(1) if explanation_match else None
match = re.search(r"Score:\s*(\d+)", msg)
Expand Down Expand Up @@ -698,7 +687,8 @@ def __init__(self, dictionary):
for mem in sorted(self.memory, key=lambda x: x.overall_score, reverse=True)[:n]:
# Last accessed now
mem.last_accessed_at = timestamp
relevant_memories.append(mem.content)
relevant_memories.append(mem)
#relevant_memories.append(mem.content)

return relevant_memories

Expand All @@ -715,6 +705,7 @@ def __str__(self, verbose=False):
else:
return f"{self.name}"


def find_path(start, target, valid_positions):
start_time = time.time()
print("Path finding started")
Expand Down
13 changes: 10 additions & 3 deletions src/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ def __init__(self, config={}):
self.id = config.get("id", str(uuid.uuid4()))
if self.id == None:
self.id = str(uuid.uuid4())
print(f"matrix id is {self.id}")
print(f"you can view replay at http://localhost:3000/?sim_id={self.id}")

self.scenario_file = config.get("scenario")
self.environment_file = config.get("environment")
self.agents = []
Expand Down Expand Up @@ -76,6 +79,7 @@ def __init__(self, config={}):
#then agent initialization
# Build Environment
self.background = None
print(config)

def boot(self):
# Add Agents
Expand Down Expand Up @@ -233,7 +237,7 @@ def add_to_logs(self,obj):
with jsonlines.open(file, mode='a') as writer:
writer.write(json.dumps(obj))
stream = f"{obj['sim_id']}_stream"
queue = f"{obj['sim_id']}_queue"
queue = f"{obj['sim_id']}"
wtf = json.loads(json.dumps(obj, default=str))
#redis_connection.xadd(stream, wtf)
redis_connection.lpush(queue,json.dumps(obj))
Expand Down Expand Up @@ -445,7 +449,7 @@ def llm_action(self, agent, unix_time):
if self.allow_reflect_flag == 1 and agent.recent_memories_importance() > self.reflect_threshold:
agent.reflect(unix_to_strftime(unix_time))

if self.allow_meta_flag == 1 and random.randint(0,100) < 50:
if self.allow_meta_flag == 1 and random.randint(0,100) < 25:
agent.evaluate_progress()

agent.conversation_cooldown -= 1
Expand Down Expand Up @@ -563,11 +567,14 @@ def llm_action(self, agent, unix_time):
for witness in witnesses:
witness.addMemory("perceived", f"{a} was murdered by {agent} at {self.environment.get_area_from_coordinates(a.x, a.y)} {self.environment.get_location_from_coordinates(a.x, a.y)}", unix_to_strftime(unix_time), 9)

memory = agent.addMemory("decision",f"I decided to {decision} because {explanation}",unix_to_strftime(unix_time),random.randint(1,4))
memory = None
#memory = agent.addMemory("decision",f"I decided to {decision} because {explanation}",unix_to_strftime(unix_time),random.randint(1,4))
if memory and memory.importance >= 6:
agent.update_goals()
return agent



def agent_action(self, agent, unix_time):
if agent.status == "dead":
return agent
Expand Down
3 changes: 2 additions & 1 deletion src/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ def __init__(self, kind, content, created_at, last_accessed_at, score=None,embed
def to_dict(self):
return vars(self)

def __str__(self, verbose=True):
def __str__(self, verbose=False):
if verbose:
return (
f"Memory(c={self.content}, "
f"t={self.kind}, "
f"rel={self.relevancy_score}, "
f"rec={self.recency_score}, "
f"i={self.importance}, "
f"o={self.overall_score}, "
f"c_at={self.created_at}, "
f"a_at={self.last_accessed_at})"
)
Expand Down
17 changes: 15 additions & 2 deletions utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import redis
import difflib
import random
from datetime import datetime
import json
from urllib.parse import urlparse
Expand All @@ -14,6 +15,11 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, column_property
import requests

import nltk
nltk.download('brown')
from nltk.corpus import brown

try:
from vllm import LLM, SamplingParams
except ImportError:
Expand All @@ -34,6 +40,11 @@ def pd(msg):
def unix_to_strftime(unix_time):
return datetime.fromtimestamp(unix_time).strftime("%Y-%m-%d %H:%M:%S")

def random_common_word():
#TODO store the array in memory
common_words = [word.lower() for word in brown.words() if len(word) > 2 and word.isalpha()]
random_common_word = random.choice(common_words)
return random_common_word
'''
Llm class wrapper
Expand Down Expand Up @@ -166,8 +177,8 @@ def generate(self, prompt, fallback="Llm Error"):
self.call_times.append(end_time - start_time)
if len(self.urls) > 1:
pd(f"current url {current_url}")
#print(f"INPUT:\n {prompt}")
#print(f"OUTPUT:\n {msg}")
print(f"INPUT:\n {prompt}")
print(f"OUTPUT:\n {msg}")
pd(f"runtime: {end_time - start_time}")
return msg

Expand Down Expand Up @@ -249,13 +260,15 @@ def prompt(self, prompt_name, variables=None, fallback="Llm Error"):
output = self.generate(new_prompt, fallback)
return output


llm = Llm()
'''
REDIS
'''
redis_connection = redis.Redis.from_url(REDIS_URL)

def print_and_log(content, key):
return
redis_log(content, key)
print(content)

Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions web/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
REDIS_URL=redis://localhost:6379
NEXT_PUBLIC_API_URL=https://replicantlife.com/api/v1/replays

0 comments on commit 688222e

Please sign in to comment.