-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathdiscussion.py
130 lines (100 loc) · 4.58 KB
/
discussion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import time
from typing import List, Tuple
from _context import sm
from rich.console import Console
from rich.markdown import Markdown
class MultiAIConversation:
"""Orchestrates conversations between multiple AI models."""
MODEL_SESSIONS = {
"GPT-4o": sm.Session(
llm_provider="openai",
llm_model="gpt-4o",
),
"Grok-Beta": sm.Session(
llm_provider="xai",
llm_model="grok-beta",
),
"Claude-3.5-Sonnet": sm.Session(
llm_provider="anthropic",
llm_model="claude-3-5-sonnet-20241022",
),
}
def __init__(self, topic: str, turns_per_model: int = 1, max_rounds: int = 5):
self.topic = topic
self.turns_per_model = turns_per_model
self.max_rounds = max_rounds
self.conversation_history: List[Tuple[str, str]] = []
self.console = Console()
self.user_name = "Kenneth Reitz"
def _format_system_prompt(self, ai_name: str) -> str:
"""Creates a system prompt for each AI model."""
return f"""You are {ai_name}. You are participating in a thoughtful discussion with other AI models about {self.topic}.
Rules:
1. Be concise but insightful (keep responses under 140 words)
2. Build upon previous points made in the conversation
3. Ask questions to deepen the discussion when appropriate
4. Stay on topic while maintaining your unique perspective
5. Be respectful of other viewpoints while maintaining your distinct voice
Current discussion topic: {self.topic}"""
def _create_conversation(
self, session: sm.Session, ai_name: str
) -> sm.Conversation:
"""Creates a new conversation with appropriate context for an AI model."""
conv = session.create_conversation()
# Add system prompt
conv.add_message(role="user", text=self._format_system_prompt(ai_name))
# Add conversation history
for speaker, message in self.conversation_history[-3:]: # Last 3 messages
conv.add_message(role="user", text=f"{speaker} said: {message}")
return conv
def _print_response(self, ai_name: str, response: str):
"""Pretty prints an AI response using Rich."""
self.console.print(f"\n[bold blue]{ai_name}[/bold blue]:")
self.console.print(Markdown(response))
# Store in history
self.conversation_history.append((ai_name, response))
def _get_user_input(self) -> str:
"""Gets input from the user for the discussion."""
self.console.print("\n[bold green]Your turn! Share your thoughts:[/bold green]")
user_response = input("> ")
self._print_response(self.user_name, user_response)
return user_response
def run_conversation(self):
"""Runs the multi-AI conversation."""
# Get initial thoughts from the human
self.console.print(
f"\n[bold green]Start the discussion about {self.topic}:[/bold green]"
)
self._get_user_input()
for round_num in range(self.max_rounds):
self.console.print(f"\n[bold green]Round {round_num + 1}[/bold green]")
# Let all AI models respond
for model_name, session in self.MODEL_SESSIONS.items():
for turn in range(self.turns_per_model):
conversation = self._create_conversation(session, model_name)
# Add the prompt (simplified since human always starts)
prompt = f"Continue the discussion about {self.topic}, responding to the previous points made."
conversation.add_message(role="user", text=prompt)
# Get and print response
response = conversation.send()
self._print_response(model_name, response.text)
# Small delay to prevent rate limiting
time.sleep(1)
# Then get user input at the end of the round
self._get_user_input()
# Optional: Add a separator between rounds
self.console.print("\n" + "-" * 50)
def have_ai_discussion(turns_per_model: int = 1, max_rounds: int = 3):
"""Convenience function to start an AI discussion."""
# Get topic from user
print("\nWhat topic would you like to discuss?")
topic = input("> ")
debate = MultiAIConversation(
topic=topic, turns_per_model=turns_per_model, max_rounds=max_rounds
)
print(f"\nStarting AI discussion about: {topic}")
print("=" * 50)
debate.run_conversation()
# Example usage
if __name__ == "__main__":
have_ai_discussion(turns_per_model=1, max_rounds=5)