Skip to content

Commit

Permalink
Merge pull request #18 from jlowin/task-agent
Browse files Browse the repository at this point in the history
Improve relationship between tasks and agents, add moderation
  • Loading branch information
jlowin authored May 10, 2024
2 parents 0b10f4a + 9c44d7b commit 56ead7b
Show file tree
Hide file tree
Showing 15 changed files with 2,443 additions and 343 deletions.
7 changes: 2 additions & 5 deletions examples/choose_a_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@

@ai_flow
def demo():
task = Task("Choose a number between 1 and 100", agents=[a1, a2], result_type=int)

while task.is_incomplete():
a1.run(task)
a2.run(task)
task = Task("choose a number between 1 and 100", agents=[a1, a2], result_type=int)
task.run_until_complete()

return task

Expand Down
76 changes: 76 additions & 0 deletions examples/multi_agent_conversation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
from control_flow import Agent, Task, ai_flow
from control_flow.core.controller.moderators import Moderator

jerry = Agent(
name="Jerry",
description="The observational comedian and natural leader.",
instructions="""
You are Jerry from the show Seinfeld. You excel at observing the quirks of
everyday life and making them amusing. You are rational, often serving as
the voice of reason among your friends. Your objective is to moderate the
conversation, ensuring it stays light and humorous while guiding it toward
constructive ends.
""",
)

george = Agent(
name="George",
description="The neurotic and insecure planner.",
instructions="""
You are George from the show Seinfeld. You are known for your neurotic
tendencies, pessimism, and often self-sabotaging behavior. Despite these
traits, you occasionally offer surprising wisdom. Your objective is to
express doubts and concerns about the conversation topics, often envisioning
the worst-case scenarios, adding a layer of humor through your exaggerated
anxieties.
""",
)

elaine = Agent(
name="Elaine",
description="The confident and independent thinker.",
instructions="""
You are Elaine from the show Seinfeld. You are bold, witty, and unafraid to
challenge social norms. You often take a no-nonsense approach to issues but
always with a comedic twist. Your objective is to question assumptions, push
back against ideas you find absurd, and inject sharp humor into the
conversation.
""",
)

kramer = Agent(
name="Kramer",
description="The quirky and unpredictable idea generator.",
instructions="""
You are Kramer from the show Seinfeld. Known for your eccentricity and
spontaneity, you often come up with bizarre yet creative ideas. Your
unpredictable nature keeps everyone guessing what you'll do or say next.
Your objective is to introduce unusual and imaginative ideas into the
conversation, providing comic relief and unexpected insights.
""",
)

newman = Agent(
name="Newman",
description="The antagonist and foil to Jerry.",
instructions="""
You are Newman from the show Seinfeld. You are Jerry's nemesis, often
serving as a source of conflict and comic relief. Your objective is to
challenge Jerry's ideas, disrupt the conversation, and introduce chaos and
absurdity into the group dynamic.
""",
)


@ai_flow
def demo():
topic = "milk and cereal"
task = Task(
"Discuss a topic; everyone should speak at least once",
agents=[jerry, george, elaine, kramer, newman],
context=dict(topic=topic),
)
task.run_until_complete(moderator=Moderator())


demo()
40 changes: 23 additions & 17 deletions examples/pineapple_pizza.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@

a1 = Agent(
name="Half-full",
instructions="You are an ardent fan and hype-man of whatever topic"
" the user asks you for information on."
" Purely positive, though thorough in your debating skills.",
instructions="""
You are an ardent fan and hype-man of whatever topic
the user asks you for information on.
Purely positive, though thorough in your debating skills.
""",
)
a2 = Agent(
name="Half-empty",
instructions="You are a critic and staunch detractor of whatever topic"
" the user asks you for information on."
" Mr Johnny Rain Cloud, you will find holes in any argument the user puts forth, though you are thorough and uncompromising"
" in your research and debating skills.",
instructions="""
You are a critic and staunch detractor of whatever topic
the user asks you for information on.
Mr Johnny Rain Cloud, you will find holes in any argument
the user puts forth, though you are thorough and uncompromising
in your research and debating skills.
""",
)
# create an agent that will decide who wins the debate
a3 = Agent(name="Moderator")


@ai_flow
def demo():
user_message = "pineapple on pizza"
topic = "pineapple on pizza"

with instructions("one sentence max"):
task = Task(
"All agents must give an argument based on the user message",
agents=[a1, a2],
context={"user_message": user_message},
)
task = Task(
"Discuss the topic",
agents=[a1, a2],
context={"topic": topic},
)
with instructions("2 sentences max"):
task.run_until_complete()

task2 = Task(
"Post a message saying which argument about the user message is more compelling?"
"which argument do you find more compelling?", [a1.name, a2.name], agents=[a3]
)
while task2.is_incomplete():
task2.run(agents=[Agent(instructions="you always pick a side")])
task2.run_until_complete()


demo()
19 changes: 10 additions & 9 deletions examples/readme_example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from control_flow import ai_flow, ai_task, instructions, run_ai
from control_flow import Agent, Task, ai_flow, ai_task, instructions
from pydantic import BaseModel


Expand All @@ -12,7 +12,7 @@ def get_user_name() -> Name:
pass


@ai_task
@ai_task(agents=[Agent(name="poetry-bot", instructions="loves limericks")])
def write_poem_about_user(name: Name, interests: list[str]) -> str:
"""write a poem based on the provided `name` and `interests`"""
pass
Expand All @@ -22,17 +22,18 @@ def write_poem_about_user(name: Name, interests: list[str]) -> str:
def demo():
# set instructions that will be used for multiple tasks
with instructions("talk like a pirate"):
# define an AI task as a function and have it execute it
# define an AI task as a function
name = get_user_name()

# define an AI task inline
interests = run_ai(
"ask user for three interests", cast=list[str], user_access=True
# define an AI task imperatively
interests = Task(
"ask user for three interests", result_type=list[str], user_access=True
)
interests.run_until_complete()

# set instructions for just the next task
with instructions("no more than 8 lines"):
poem = write_poem_about_user(name, interests)
# set instructions for just the next task
with instructions("no more than 8 lines"):
poem = write_poem_about_user(name, interests.result)

return poem

Expand Down
Loading

0 comments on commit 56ead7b

Please sign in to comment.