Skip to content

Commit

Permalink
add agent builder example (#25)
Browse files Browse the repository at this point in the history
Authored-by: 柳佚 <[email protected]>
  • Loading branch information
Osier-Yi authored Mar 6, 2024
1 parent 996bc94 commit 45c6e1e
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
19 changes: 19 additions & 0 deletions examples/agent_builder/agent_builder_instruct.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Act as a group discussion organizer. Please provide the suitable scenario for discussing this question, and list the roles of the people who need to participate in the discussion in order to answer this question, along with their system prompt to describe their characteristics.
The response must in the format of:
#scenario#: <discussion scenario>
#participants#:
* <participant1 type>: <characteristic description>
* <participant2 type>: <characteristic description>

Here are some examples.
Question: Joy can read 8 pages of a book in 20 minutes. How many hours will it take her to read 120 pages?
Answer:
#scenario#: grade school class discussion
#participants#:
* Instructor: Act as an instructor who is in a class group discussion to guide the student group discussion. Please encourage critical thinking. Encourage participants to think critically and challenge assumptions by asking thought-provoking questions or presenting different perspectives.
* broad-minded-student: Act as a student who is broad-minded and is open to trying new or different ways to solve problems. You are in a group discussion with other student under the guidance of the instructor.
* knowledgeable-student: Act as a knowledgeable student and discuss with others to retrieve more information about the topic. If you do not know the answer to a question, please do not share false information

Please give the discussion scenario and the corresponding participants for the following question:
Question: {question}
Answer:
67 changes: 67 additions & 0 deletions examples/agent_builder/auto-discussion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
"""A simple example for auto discussion: the agent builder automatically\
set up the agents participating the discussion ."""
from tools import load_txt, extract_scenario_and_participants
import agentscope
from agentscope.agents import DialogAgent
from agentscope.pipelines.functional import sequentialpipeline
from agentscope.message import Msg

model_configs = [
{
"model_type": "openai",
"config_name": "gpt-3.5-turbo",
"model": "gpt-3.5-turbo",
"api_key": "xxx", # Load from env if not provided
"organization": "xxx", # Load from env if not provided
"generate_args": {
"temperature": 0.5,
},
},
{
"model_type": "post_api_chat",
"config_name": "my_post_api",
"api_url": "https://xxx",
"headers": {},
"json_args": {},
},
]
agentscope.init(model_configs=model_configs)


# init agent_builder
agent_builder = DialogAgent(
name="agent_builder",
sys_prompt="You're a helpful assistant.",
model_config_name="my_post_api",
)


max_round = 2
query = "Say the pupil of your eye has a diameter of 5 mm and you have a \
telescope with an aperture of 50 cm. How much more light can the \
telescope gather than your eye?"

# get the discussion scenario and participant agents
x = load_txt("examples/agent_builder/agent_builder_instruct.txt").format(
question=query,
)

x = Msg("user", x)
settings = agent_builder(x)
scenario_participants = extract_scenario_and_participants(settings["content"])

# set the agents that participant the discussion
agents = [
DialogAgent(
name=key,
sys_prompt=val,
model_config_name="my_post_api",
)
for key, val in scenario_participants["Participants"].items()
]

# begin discussion
msg = Msg("user", f"let's discuss to solve the question: {query}")
for i in range(max_round):
msg = sequentialpipeline(agents, msg)
81 changes: 81 additions & 0 deletions examples/agent_builder/tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# -*- coding: utf-8 -*-
"""some utils of angent_builder example"""
import re


def load_txt(instruction_file: str) -> str:
"""
load .txt file
Arguments:
instruction_file: str type, which is the .txt file pth
Returns:
instruction: str type, which is the str in the instruction_file
"""
with open(instruction_file, "r", encoding="utf-8") as f:
instruction = f.read()
return instruction


# extract scenario和participants
def extract_scenario_and_participants(content: str) -> dict:
"""
extract the scenario and participants from agent builder's response
Arguments:
content: the agent builders response
Returns:
result: dict
Examples:
content: #scenario#: Astronomy club meeting
#participants#:
* Club Leader: Act as the club leader who is knowledgeable about\
astronomy and optics. You are leading a discussion about the \
capabilities of telescopes versus the human eye. Please provide \
accurate information and guide the discussion.
* Curious Member: Act as a curious club member who is interested \
in astronomy but may not know all the technical details. You are \
eager to learn and ask questions.
* Experienced Astronomer: Act as an experienced astronomer who has \
practical experience using telescopes for stargazing. You can \
provide real-world examples and insights into the topic.
Return:
{'Scenario': 'Astronomy club meeting',
'Participants':
{'Club_Leader': 'Act as the club leader who is knowledgeable \
about astronomy and optics. You are leading a discussion about the \
capabilities of telescopes versus the human eye. Please provide\
accurate information and guide the discussion.',
'Curious_Member': 'Act as a curious club member who is interested \
in astronomy but may not know all the technical details. You are \
eager to learn and ask questions.',
'Experienced_Astronomer': 'Act as an experienced astronomer who has\
practical experience using telescopes for stargazing. You can\
provide real-world examples and insights into the topic.'}}
"""
result = {}
# define regular expression
scenario_pattern = r"#scenario#:\s*(.*)"
participants_pattern = r"\*\s*([^:\n]+):\s*([^\n]+)"

# search and extract scenario
scenario_match = re.search(scenario_pattern, content)
if scenario_match:
result["Scenario"] = scenario_match.group(1).strip()

# search and extract participants
participants_matches = re.finditer(participants_pattern, content)
participants_dict = {}
for match in participants_matches:
participant_type, characteristic = match.groups()
participants_dict[
participant_type.strip().replace(" ", "_")
] = characteristic.strip()
result["Participants"] = participants_dict

return result

0 comments on commit 45c6e1e

Please sign in to comment.