init
This commit is contained in:
36
AlgoriAgent/examples/conversation_with_mentions/README.md
Normal file
36
AlgoriAgent/examples/conversation_with_mentions/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
###
|
||||
# Multi-Agent Group Conversation in AgentScope
|
||||
|
||||
This example demonstrates a multi-agent group conversation facilitated by AgentScope. The script sets up a virtual chat room where a user agent interacts with several NPC (non-player character) agents. Participants can utilize a special "@" mention functionality to address specific agents directly.
|
||||
|
||||
## Background
|
||||
|
||||
The conversation takes place in a simulated chat room environment with predefined roles for each participant. Topics are open-ended and evolve based on the user's input and agents' responses.
|
||||
|
||||
## Tested Models
|
||||
|
||||
These models are tested in this example. For other models, some modifications may be needed.
|
||||
- gemini_chat (models/gemini-pro, models/gemini-1.0-pro)
|
||||
- dashscope_chat (qwen-max, qwen-turbo)
|
||||
- ollama_chat (ollama_llama3_8b)
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Fill the next cell to meet the following requirements:
|
||||
- Set your `api_key` in the `configs/model_configs.json` file
|
||||
- Optional: Launch agentscope gradio with `as_gradio main.py`
|
||||
|
||||
## How to Use
|
||||
|
||||
1. Run the script using the command: `python main.py`
|
||||
2. Address specific agents by typing "@" followed by the agent's name.
|
||||
3. Type "exit" to leave the chat.
|
||||
|
||||
## Customization Options
|
||||
|
||||
You can adjust the behavior and parameters of the NPC agents and conversation model by editing the `agent_configs.json` and `model_configs.json` files, respectively.
|
||||
|
||||
### Changing User Input Time Limit
|
||||
|
||||
Adjust the `USER_TIME_TO_SPEAK` variable in the `main.py` script to change the time limit for user input.
|
||||
###
|
||||
@@ -0,0 +1,29 @@
|
||||
[
|
||||
{
|
||||
"class": "DialogAgent",
|
||||
"args": {
|
||||
"name": "Lingfeng",
|
||||
"sys_prompt":"You are Lingfeng, a noble in the imperial court, known for your wisdom and strategic acumen. You often engage in complex political intrigues and have recently suspected the Queen’s adviser of treachery. Your speaking style is reminiscent of classical literature.",
|
||||
"model_config_name": "gpt-4",
|
||||
"use_memory": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"class": "DialogAgent",
|
||||
"args": {
|
||||
"name": "Boyu",
|
||||
"sys_prompt":"You are Boyu, a friend of Lingfeng and an enthusiast of court dramas. Your speech is modern but with a flair for the dramatic, matching your love for emotive storytelling. You've been closely following Lingfeng’s political maneuvers in the imperial court through secret correspondence.",
|
||||
"model_config_name": "gpt-4",
|
||||
"use_memory": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"class": "DialogAgent",
|
||||
"args": {
|
||||
"name": "Haotian",
|
||||
"sys_prompt":"You are Haotian, Lingfeng’s cousin who prefers the open fields to the confines of court life. As a celebrated athlete, your influence has protected Lingfeng in times of political strife. You promote physical training as a way to prepare for life's battles, often using sports metaphors in conversation.",
|
||||
"model_config_name": "gpt-4",
|
||||
"use_memory": true
|
||||
}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
[
|
||||
{
|
||||
"model_type": "openai_chat",
|
||||
"config_name": "gpt-4",
|
||||
"model_name": "gpt-4",
|
||||
"api_key": "xxx",
|
||||
"organization": "xxx",
|
||||
"generate_args": {
|
||||
"temperature": 0.5
|
||||
}
|
||||
},
|
||||
{
|
||||
"model_type": "post_api_chat",
|
||||
"config_name": "my_post_api",
|
||||
"api_url": "https://xxx",
|
||||
"headers": {},
|
||||
"json_args": {}
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,37 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" Group chat utils."""
|
||||
import re
|
||||
from typing import Sequence
|
||||
|
||||
|
||||
def select_next_one(agents: Sequence, rnd: int) -> Sequence:
|
||||
"""
|
||||
Select next agent.
|
||||
"""
|
||||
return agents[rnd % len(agents)]
|
||||
|
||||
|
||||
def filter_agents(string: str, agents: Sequence) -> Sequence:
|
||||
"""
|
||||
This function filters the input string for occurrences of the given names
|
||||
prefixed with '@' and returns a list of the found names.
|
||||
"""
|
||||
if len(agents) == 0:
|
||||
return []
|
||||
|
||||
# Create a pattern that matches @ followed by any of the candidate names
|
||||
pattern = (
|
||||
r"@(" + "|".join(re.escape(agent.name) for agent in agents) + r")\b"
|
||||
)
|
||||
|
||||
# Find all occurrences of the pattern in the string
|
||||
matches = re.findall(pattern, string)
|
||||
|
||||
# Create a dictionary mapping agent names to agent objects for quick lookup
|
||||
agent_dict = {agent.name: agent for agent in agents}
|
||||
|
||||
# Return the list of matched agent objects preserving the order
|
||||
ordered_agents = [
|
||||
agent_dict[name] for name in matches if name in agent_dict
|
||||
]
|
||||
return ordered_agents
|
||||
79
AlgoriAgent/examples/conversation_with_mentions/main.py
Normal file
79
AlgoriAgent/examples/conversation_with_mentions/main.py
Normal file
@@ -0,0 +1,79 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
""" A group chat where user can talk any time implemented by agentscope. """
|
||||
from loguru import logger
|
||||
from groupchat_utils import (
|
||||
select_next_one,
|
||||
filter_agents,
|
||||
)
|
||||
|
||||
import agentscope
|
||||
from agentscope.agents import UserAgent
|
||||
from agentscope.message import Msg
|
||||
from agentscope.msghub import msghub
|
||||
|
||||
USER_TIME_TO_SPEAK = 10
|
||||
DEFAULT_TOPIC = """
|
||||
This is a chat room and you can speak freely and briefly.
|
||||
"""
|
||||
|
||||
SYS_PROMPT = """
|
||||
You can designate a member to reply to your message, you can use the @ symbol.
|
||||
This means including the @ symbol in your message, followed by
|
||||
that person's name, and leaving a space after the name.
|
||||
|
||||
All participants are: {agent_names}
|
||||
"""
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""group chat"""
|
||||
npc_agents = agentscope.init(
|
||||
model_configs="./configs/model_configs.json",
|
||||
agent_configs="./configs/agent_configs.json",
|
||||
project="Conversation with Mentions",
|
||||
)
|
||||
|
||||
user = UserAgent()
|
||||
|
||||
agents = list(npc_agents) + [user]
|
||||
|
||||
hint = Msg(
|
||||
name="Host",
|
||||
content=DEFAULT_TOPIC
|
||||
+ SYS_PROMPT.format(
|
||||
agent_names=[agent.name for agent in agents],
|
||||
),
|
||||
role="assistant",
|
||||
)
|
||||
|
||||
rnd = 0
|
||||
speak_list = []
|
||||
with msghub(agents, announcement=hint):
|
||||
while True:
|
||||
try:
|
||||
x = user(timeout=USER_TIME_TO_SPEAK)
|
||||
if x.content == "exit":
|
||||
break
|
||||
except TimeoutError:
|
||||
x = {"content": ""}
|
||||
logger.info(
|
||||
f"User has not typed text for "
|
||||
f"{USER_TIME_TO_SPEAK} seconds, skip.",
|
||||
)
|
||||
|
||||
speak_list += filter_agents(x.get("content", ""), npc_agents)
|
||||
|
||||
if len(speak_list) > 0:
|
||||
next_agent = speak_list.pop(0)
|
||||
x = next_agent()
|
||||
else:
|
||||
next_agent = select_next_one(npc_agents, rnd)
|
||||
x = next_agent()
|
||||
|
||||
speak_list += filter_agents(x.content, npc_agents)
|
||||
|
||||
rnd += 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user