Skip to content

Commit

Permalink
support write abstract method for agent (#25)
Browse files Browse the repository at this point in the history
* Implemented write abstract method for agent

* fix merge error

* add communicate func

---------

Co-authored-by: Haofei Yu <[email protected]>
  • Loading branch information
timsanders256 and lwaekfjlk authored May 11, 2024
1 parent 41d15e7 commit 84492ef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
6 changes: 4 additions & 2 deletions research_town/agents/agent_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
generate_ideas,
summarize_research_direction,
summarize_research_field,
write_paper_abstract,
)


Expand Down Expand Up @@ -180,8 +181,9 @@ def generate_idea(

return ideas

def write_paper(self, input: Dict[str, str], external_data: Dict[str, str]) -> str:
return "writing paper"
def write_paper(self, input: List[str], external_data: Dict[str, Dict[str, List[str]]]) -> str:
paper_abstract = write_paper_abstract(input, external_data)
return paper_abstract[0]

def review_paper(self, input: Dict[str, str], external_data: Dict[str, str]) -> str:
return "review comments"
Expand Down
51 changes: 50 additions & 1 deletion research_town/agents/agent_prompting.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,55 @@ def summarize_research_direction(personal_info: str) -> List[str]:
content = completion.choices[0].message["content"]
return [content]


def write_paper_abstract(ideas: List[str], external_data: Dict[str, Dict[str, List[str]]]):
"""
Write paper using ideas from list, and external data (published papers)
"""
ideas_serialize = [f"{i}: {idea}" for i, idea in enumerate(ideas)]
ideas_serialize_all = "\n".join(ideas_serialize)

papers_serialize = []

for i, timestamp in enumerate(external_data.keys()):
abstracts = external_data[timestamp]['abstract']
formatted_abstracts = '\nAbstract: '.join(abstracts)
paper_entry = f"Time: {timestamp}\nAbstract: {formatted_abstracts}\n"
papers_serialize.append(paper_entry)

papers_serialize_all = "\n\n".join(papers_serialize)

prompt_qa = (
"Please write a paper based on the following ideas and external data. To save time, you only need to write the abstract. "
"You might use two or more of these ideas if they are related and works well together. "
"Here are the ideas: {ideas_serialize_all}"
"Here are the external data, which is a list abstracts of related papers: {papers_serialize_all}"
)

openai.api_key = KEY
input = {"ideas_serialize_all": ideas_serialize_all, "papers_serialize_all": papers_serialize_all}

prompt = prompt_qa.format_map(input)
try:
completion = openai.ChatCompletion.create(
model=llm_model,
messages=[{"role": "user", "content": prompt}],
temperature=0,
seed=42,
top_p=0,
)
except Exception:
time.sleep(20)
completion = openai.ChatCompletion.create(
model=llm_model,
messages=[{"role": "user", "content": prompt}],
temperature=0,
seed=42,
top_p=0,
)
content = completion.choices[0].message["content"]
return [content]

def communicate_with_multiple_researchers(input: Dict[str, str]):
"""
This is a single-round chat method. One that contains a chat history can better enable
Expand Down Expand Up @@ -163,4 +212,4 @@ def communicate_with_multiple_researchers(input: Dict[str, str]):
top_p=0,
)
content = completion.choices[0].message["content"]
return [content]
return [content]
2 changes: 1 addition & 1 deletion research_town/envs/env_paper_submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def step(self) -> None:
agent.read_paper(external_data=external_data, domain="machine learning")
agent.find_collaborators({})
agent.generate_idea(external_data=external_data, domain="machine learning")
agent.write_paper({}, {})
agent.write_paper([], {})

self.submit_paper()

Expand Down
6 changes: 6 additions & 0 deletions tests/test_agent_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def test_communicate():
assert isinstance(response, str)
assert response != ""

def test_write_paper_abstract():
research_agent = BaseResearchAgent("Jiaxuan You")
abstract = research_agent.write_paper(["We can simulate the scientific research pipeline with agents."], {"2024-04":{"abstract":["Believable proxies of human behavior can empower interactive applications ranging from immersive environments to rehearsal spaces for interpersonal communication to prototyping tools. In this paper, we introduce generative agents--computational software agents that simulate believable human behavior. Generative agents wake up, cook breakfast, and head to work; artists paint, while authors write; they form opinions, notice each other, and initiate conversations; they remember and reflect on days past as they plan the next day. To enable generative agents, we describe an architecture that extends a large language model to store a complete record of the agent's experiences using natural language, synthesize those memories over time into higher-level reflections, and retrieve them dynamically to plan behavior. We instantiate generative agents to populate an interactive sandbox environment inspired by The Sims, where end users can interact with a small town of twenty five agents using natural language. In an evaluation, these generative agents produce believable individual and emergent social behaviors: for example, starting with only a single user-specified notion that one agent wants to throw a Valentine's Day party, the agents autonomously spread invitations to the party over the next two days, make new acquaintances, ask each other out on dates to the party, and coordinate to show up for the party together at the right time. We demonstrate through ablation that the components of our agent architecture--observation, planning, and reflection--each contribute critically to the believability of agent behavior. By fusing large language models with computational, interactive agents, this work introduces architectural and interaction patterns for enabling believable simulations of human behavior. "]}})
assert isinstance(abstract, str)
assert abstract != ""

'''
def test_read_paper():
external_data = {"2021-01-01": {"abstract": ["This is a paper"]}}
Expand Down

0 comments on commit 84492ef

Please sign in to comment.