From 45c6e1e58e6c3dc5930446d2186f07e09528e6e6 Mon Sep 17 00:00:00 2001 From: Osier-Yi Date: Wed, 6 Mar 2024 11:29:11 +0800 Subject: [PATCH] add agent builder example (#25) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Authored-by: 柳佚 --- .../agent_builder/agent_builder_instruct.txt | 19 +++++ examples/agent_builder/auto-discussion.py | 67 +++++++++++++++ examples/agent_builder/tools.py | 81 +++++++++++++++++++ 3 files changed, 167 insertions(+) create mode 100644 examples/agent_builder/agent_builder_instruct.txt create mode 100644 examples/agent_builder/auto-discussion.py create mode 100644 examples/agent_builder/tools.py diff --git a/examples/agent_builder/agent_builder_instruct.txt b/examples/agent_builder/agent_builder_instruct.txt new file mode 100644 index 000000000..5af4faf09 --- /dev/null +++ b/examples/agent_builder/agent_builder_instruct.txt @@ -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#: +#participants#: +* : +* : + +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: \ No newline at end of file diff --git a/examples/agent_builder/auto-discussion.py b/examples/agent_builder/auto-discussion.py new file mode 100644 index 000000000..a6544afea --- /dev/null +++ b/examples/agent_builder/auto-discussion.py @@ -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) diff --git a/examples/agent_builder/tools.py b/examples/agent_builder/tools.py new file mode 100644 index 000000000..04a41f6b5 --- /dev/null +++ b/examples/agent_builder/tools.py @@ -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