init
This commit is contained in:
29
AlgoriAgent/examples/conversation_self_organizing/README.md
Normal file
29
AlgoriAgent/examples/conversation_self_organizing/README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Self-Organizing Conversation Example
|
||||
|
||||
This example will show
|
||||
- How to set up a self-organizing conversation using the `DialogAgent` and `agent_builder`
|
||||
- How to extract the discussion scenario and participant agents from the `agent_builder`'s response
|
||||
- How to conduct a multi-round discussion among the participant agents
|
||||
|
||||
|
||||
## Background
|
||||
|
||||
In this example, we demonstrate how to create a self-organizing conversation where the `agent_builder` automatically sets up the agents participating in the discussion based on a given question. The `agent_builder` provides the discussion scenario and the characteristics of the participant agents. The participant agents then engage in a multi-round discussion to solve the given question.
|
||||
|
||||
|
||||
## Tested Models
|
||||
|
||||
These models are tested in this example. For other models, some modifications may be needed.
|
||||
- `dashscope_chat` with `qwen-turbo`
|
||||
- `ollama_chat` with `llama3_8b`
|
||||
- `gemini_chat` with `models/gemini-1.0-pro-latest`
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Fill the next cell to meet the following requirements
|
||||
- Set up the `model_configs` with the appropriate API keys and endpoints
|
||||
- Provide the path to the `agent_builder_instruct.txt` file in the `load_txt` function
|
||||
- Set the desired `max_round` for the discussion
|
||||
- Provide the `query` or question for the discussion
|
||||
- [Optional] Adjust the `generate_args` such as `temperature` for the `openai_chat` model
|
||||
@@ -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:
|
||||
@@ -0,0 +1,73 @@
|
||||
# -*- 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_chat",
|
||||
"config_name": "gpt-3.5-turbo",
|
||||
"model_name": "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,
|
||||
project="Self-Organizing Conversation",
|
||||
)
|
||||
|
||||
|
||||
# init the self-organizing conversation
|
||||
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/conversation_self_organizing/agent_builder_instruct.txt",
|
||||
).format(
|
||||
question=query,
|
||||
)
|
||||
|
||||
x = Msg("user", x, role="user")
|
||||
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}", role="user")
|
||||
for i in range(max_round):
|
||||
msg = sequentialpipeline(agents, msg)
|
||||
81
AlgoriAgent/examples/conversation_self_organizing/tools.py
Normal file
81
AlgoriAgent/examples/conversation_self_organizing/tools.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user