Skip to content

Commit 72cef45

Browse files
Feature(multi agent): new examples for multi agent (#20)
* update human-agent chat * update new method(human-agent and multi agents) * update --------- Co-authored-by: Yining Zhao <zhaoyining5445@gmail.com>
1 parent 75c67b8 commit 72cef45

File tree

18 files changed

+1117
-206
lines changed

18 files changed

+1117
-206
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
git```
1+
# Tiny-Chat
2+
3+
```
24
conda create -n tiny-chat python=3.10
35
conda activate tiny-chat
46
curl -sSL https://install.python-poetry.org | python3
57
poetry install
6-
78
```
89

9-
```
10+
## What's new:
11+
12+
1. You can find some examples [here](/tiny-chat/examples/README.md).

examples/README.md

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Tiny-Chat Examples
2+
3+
This directory showcases various usage examples for Tiny-Chat.
4+
5+
## Example Files
6+
7+
```
8+
examples/
9+
├── README.md
10+
├── two_agents_chat.py # Two-agent conversation
11+
├── human_agent_chat.py # Human-AI interactive chat
12+
└── multi_agents_chat.py # Multi-agent conversation
13+
```
14+
15+
## Running Examples
16+
17+
### Two-Agent Conversation
18+
19+
```bash
20+
python examples/two_agents_chat.py
21+
```
22+
23+
### Human-AI Interactive Chat
24+
25+
```bash
26+
python examples/human_agent_chat.py
27+
```
28+
29+
### Multi-Agent Conversation
30+
31+
```bash
32+
python examples/multi_agents_chat.py
33+
```
34+
35+
## Customizing Examples
36+
37+
You can create your own examples by modifying the following parameters:
38+
39+
### 1. Two-Agent Conversation Customization
40+
41+
In `two_agents_chat.py`, you can modify:
42+
43+
```python
44+
# Agent configurations
45+
agent_configs = [
46+
{
47+
"name": "Your Agent 1 Name",
48+
"agent_number": 1,
49+
"type": "llm",
50+
"model": "gpt-4o-mini",
51+
"goal": "Your Agent 1 Goal",
52+
},
53+
{
54+
"name": "Your Agent 2 Name",
55+
"agent_number": 2,
56+
"type": "llm",
57+
"model": "gpt-4o-mini",
58+
"goal": "Your Agent 2 Goal",
59+
},
60+
]
61+
62+
# Background settings
63+
background = TwoAgentChatBackground(
64+
scenario="Your conversation scenario description",
65+
p1_background="Agent 1 background information",
66+
p2_background="Agent 2 background information",
67+
p1_goal="Agent 1 specific goal",
68+
p2_goal="Agent 2 specific goal",
69+
p1_name="Agent 1 Name",
70+
p2_name="Agent 2 Name",
71+
)
72+
73+
# Conversation parameters
74+
await server.two_agent_run_conversation(
75+
agent_configs=agent_configs,
76+
background=background,
77+
max_turns=10, # Maximum conversation turns
78+
enable_evaluation=True, # Enable evaluation
79+
)
80+
```
81+
82+
### 2. Human-AI Interaction Customization
83+
84+
In `human_agent_chat.py`, you can modify:
85+
86+
```python
87+
# AI agent configuration
88+
agent = LLMAgent(
89+
agent_name="Your AI Assistant Name",
90+
model="gpt-4o-mini",
91+
api_key=api_key,
92+
goal="Your AI Assistant Goal"
93+
)
94+
95+
# Conversation background
96+
background = ChatBackground(
97+
scenario="Your conversation scenario",
98+
p1_background="AI Assistant background setting",
99+
p2_background="User background setting",
100+
p1_goal="AI Assistant goal",
101+
p2_goal="User goal",
102+
p1_name="AI Assistant Name",
103+
p2_name="User Name"
104+
)
105+
```
106+
107+
### 3. Multi-Agent Conversation Customization
108+
109+
Multi-agent conversations support 4 different action ordering modes. You can assign each one in [action_order](/tiny-chat/examples/multi_agents_chat.py#L74):
110+
111+
- **Sequential**: Agents take turns one by one in a fixed order
112+
- **Round-robin**: Agents take turns in a rotating order (A→B→C→A→B→C...)
113+
- **Simultaneous**: All agents act at the same time in each turn
114+
- **Random**: A random agent is selected to act in each turn
115+
116+
## <span style="color: red;">WARNING: Simultaneous mode is Incomplete</span>
117+
118+
In `multi_agents_chat.py`, you can modify:
119+
120+
```python
121+
# Agent configurations (supports 2-3 agents)
122+
agent_configs = [
123+
{
124+
"name": "Agent 1 Name",
125+
"type": "llm",
126+
"model": "gpt-4o-mini",
127+
"goal": "Agent 1 Goal"
128+
},
129+
{
130+
"name": "Agent 2 Name",
131+
"type": "llm",
132+
"model": "gpt-4o-mini",
133+
"goal": "Agent 2 Goal"
134+
},
135+
{
136+
"name": "Agent 3 Name",
137+
"type": "llm",
138+
"model": "gpt-4o-mini",
139+
"goal": "Agent 3 Goal"
140+
}
141+
]
142+
143+
background = MultiAgentChatBackground(
144+
scenario="Your multi-agent scenario description",
145+
agent_configs=[
146+
{
147+
'name': 'Agent 1 Name',
148+
'background': 'Agent 1 detailed background',
149+
'goal': 'Agent 1 specific goal'
150+
},
151+
{
152+
'name': 'Agent 2 Name',
153+
'background': 'Agent 2 detailed background',
154+
'goal': 'Agent 2 specific goal'
155+
},
156+
{
157+
'name': 'Agent 3 Name',
158+
'background': 'Agent 3 detailed background',
159+
'goal': 'Agent 3 specific goal'
160+
}
161+
]
162+
)
163+
164+
# Conversation parameters
165+
await server.multi_agent_run_conversation(
166+
agent_configs=agent_configs,
167+
background=background,
168+
action_order='simultaneous', # sequential, round-robin, simultaneous, random
169+
max_turns=12, # Maximum conversation turns
170+
enable_evaluation=True # Enable evaluation
171+
)
172+
```

examples/human_agent_chat.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Interactive chat script - Chat with AI agents
4+
Usage: python scripts/interactive_chat.py
5+
"""
6+
7+
import asyncio
8+
import logging
9+
import os
10+
import sys
11+
from pathlib import Path
12+
13+
from tiny_chat.agents import LLMAgent
14+
from tiny_chat.messages import Observation, TwoAgentChatBackground
15+
16+
project_root = Path(__file__).parent.parent
17+
sys.path.insert(0, str(project_root))
18+
19+
20+
async def interactive_chat() -> None:
21+
"""Interactive chat with an AI agent"""
22+
23+
logging.getLogger().setLevel(logging.ERROR)
24+
for logger_name in logging.root.manager.loggerDict:
25+
logging.getLogger(logger_name).setLevel(logging.ERROR)
26+
27+
api_key = os.getenv("OPENAI_API_KEY")
28+
if not api_key:
29+
print("Error: OPENAI_API_KEY environment variable is required")
30+
return
31+
32+
agent = LLMAgent(
33+
agent_name="AI Assistant",
34+
model="gpt-4o-mini",
35+
api_key=api_key,
36+
goal="Be helpful, friendly, and engaging in conversation",
37+
)
38+
39+
_background = TwoAgentChatBackground(
40+
scenario="A helpful AI assistant chatting with a human",
41+
p1_background="You are a helpful AI assistant",
42+
p2_background="A human user who wants to chat",
43+
p1_goal="Provide helpful and engaging responses",
44+
p2_goal="Have an interesting conversation",
45+
p1_name="AI Assistant",
46+
p2_name="Human",
47+
)
48+
49+
print("🤖 AI Assistant: Hello! I'm your AI assistant. How can I help you today?")
50+
print("(Type 'quit' or 'q' to exit)")
51+
print("-" * 50)
52+
53+
turn_number = 1
54+
55+
while True:
56+
user_input = input("👤 You: ").strip()
57+
58+
if user_input.lower() in ["quit", "exit", "bye", "q"]:
59+
print("🤖 AI Assistant: Goodbye! It was nice chatting with you!")
60+
break
61+
62+
if not user_input:
63+
continue
64+
65+
observation = Observation(
66+
last_turn=f"Human says: {user_input}",
67+
available_actions=["speak", "none"],
68+
turn_number=turn_number,
69+
)
70+
71+
try:
72+
action = await agent.act(observation)
73+
74+
if action.action_type == "speak":
75+
print(f"AI Assistant: {action.argument}")
76+
else:
77+
print("AI Assistant: [No response]")
78+
79+
except Exception as e:
80+
print(f"AI Assistant: Sorry, I encountered an error: {e}")
81+
82+
turn_number += 1
83+
print("-" * 50)
84+
85+
86+
if __name__ == "__main__":
87+
asyncio.run(interactive_chat())

examples/multi_agents_chat.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import asyncio
2+
import os
3+
import sys
4+
from pathlib import Path
5+
6+
# Add the project root to Python path
7+
project_root = Path(__file__).parent.parent
8+
sys.path.insert(0, str(project_root))
9+
10+
from tiny_chat.messages import MultiAgentChatBackground
11+
from tiny_chat.utils.server import TinyChatServer
12+
13+
14+
async def main():
15+
"""Run a 3-agent conversation"""
16+
17+
# Get API key from environment variable
18+
api_key = os.getenv('OPENAI_API_KEY')
19+
if not api_key:
20+
print('Warning: OPENAI_API_KEY not set. Some features may not work.')
21+
22+
# Create chat server
23+
server = TinyChatServer(api_key=api_key)
24+
25+
# Define 3-agent configurations
26+
agent_configs = [
27+
{
28+
'name': 'Alice',
29+
'type': 'llm',
30+
'model': 'gpt-4o-mini',
31+
'goal': 'Talk about weekend hiking plans',
32+
},
33+
{
34+
'name': 'Bob',
35+
'type': 'llm',
36+
'model': 'gpt-4o-mini',
37+
'goal': 'Share thoughts on a new sci-fi book',
38+
},
39+
{
40+
'name': 'Carol',
41+
'type': 'llm',
42+
'model': 'gpt-4o-mini',
43+
'goal': 'Discuss travel experiences and make everyone laugh',
44+
},
45+
]
46+
47+
# Create background object
48+
background = MultiAgentChatBackground(
49+
scenario='Three friends catching up over coffee',
50+
agent_configs=[
51+
{
52+
'name': 'Alice',
53+
'background': 'Alice is a software engineer who loves hiking',
54+
'goal': 'Talk about weekend hiking plans',
55+
},
56+
{
57+
'name': 'Bob',
58+
'background': 'Bob is a teacher who enjoys reading science fiction',
59+
'goal': 'Share thoughts on a new sci-fi book',
60+
},
61+
{
62+
'name': 'Carol',
63+
'background': 'Carol is a graphic designer who recently returned from a trip to Japan',
64+
'goal': 'Discuss travel experiences and make everyone laugh',
65+
},
66+
],
67+
)
68+
69+
print('Starting multi-agent conversation...')
70+
print('=' * 50)
71+
72+
await server.multi_agent_run_conversation(
73+
agent_configs=agent_configs,
74+
background=background,
75+
action_order='simultaneous', # sequential, round-robin, simultaneous, random
76+
max_turns=12,
77+
enable_evaluation=True,
78+
)
79+
80+
81+
if __name__ == '__main__':
82+
asyncio.run(main())

0 commit comments

Comments
 (0)