forked from CloudWaddie/LMArenaBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_interactive.py
More file actions
221 lines (179 loc) · 7.07 KB
/
chat_interactive.py
File metadata and controls
221 lines (179 loc) · 7.07 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""
Interactive chat script using OpenAI Python library with LMArena Bridge
Allows you to have a conversation with any model available through the bridge
"""
from openai import OpenAI
import sys
# Configuration
BASE_URL = "http://localhost:8000/api/v1"
API_KEY = "sk-lmab-50664b6f-87c7-4115-b630-eb38a9b55021" # Replace with your API key
def list_available_models(client):
"""List all available models"""
try:
models = client.models.list()
return [model.id for model in models.data]
except Exception as e:
print(f"Error fetching models: {e}")
return []
def chat_session(client, model_name):
"""Run an interactive chat session"""
print(f"\n{'='*60}")
print(f"🤖 Chat Session with {model_name}")
print(f"{'='*60}")
# Ask for system prompt
print("\n📋 System Prompt (optional):")
print("Set the behavior/personality of the assistant.")
print("Press Enter to skip, or type your system prompt below:")
system_prompt = input("System: ").strip()
conversation_history = []
# Add system message if provided
if system_prompt:
conversation_history.append({
"role": "system",
"content": system_prompt
})
print(f"✅ System prompt set: {system_prompt[:50]}{'...' if len(system_prompt) > 50 else ''}")
print(f"\n{'='*60}")
print("Type your messages below. Commands:")
print(" - 'exit' or 'quit' to end the session")
print(" - 'clear' to start a new conversation")
print(" - 'system' to view/change system prompt")
print(" - 'models' to switch models")
print(f"{'='*60}\n")
while True:
# Get user input
try:
user_input = input("You: ").strip()
except (EOFError, KeyboardInterrupt):
print("\n\n👋 Goodbye!")
break
# Handle commands FIRST before adding to history
if not user_input:
continue
if user_input.lower() in ['exit', 'quit']:
print("\n👋 Goodbye!")
break
if user_input.lower() == 'clear':
# Keep system prompt if it exists
system_msg = None
if conversation_history and conversation_history[0]["role"] == "system":
system_msg = conversation_history[0]
conversation_history = []
if system_msg:
conversation_history.append(system_msg)
print("\n🔄 Conversation cleared!\n")
continue
if user_input.lower() == 'system':
# Show or update system prompt
current_system = None
if conversation_history and conversation_history[0]["role"] == "system":
current_system = conversation_history[0]["content"]
print(f"\n📋 Current system prompt:\n{current_system}\n")
else:
print("\n📋 No system prompt set.\n")
print("Enter new system prompt (or press Enter to keep current):")
new_system = input("System: ").strip()
if new_system:
# Remove old system message if exists
if conversation_history and conversation_history[0]["role"] == "system":
conversation_history.pop(0)
# Add new system message at the start
conversation_history.insert(0, {
"role": "system",
"content": new_system
})
print(f"✅ System prompt updated: {new_system[:50]}{'...' if len(new_system) > 50 else ''}\n")
else:
print("System prompt unchanged.\n")
continue
if user_input.lower() == 'models':
return 'switch_model'
# Add user message to history (only if not a command)
conversation_history.append({
"role": "user",
"content": user_input
})
# Get response from API with streaming
try:
print("Assistant: ", end="", flush=True)
stream = client.chat.completions.create(
model=model_name,
messages=conversation_history,
stream=True # Enable streaming
)
assistant_message = ""
for chunk in stream:
if chunk.choices[0].delta.content is not None:
content = chunk.choices[0].delta.content
print(content, end="", flush=True)
assistant_message += content
print() # New line after streaming completes
# Add assistant response to history
conversation_history.append({
"role": "assistant",
"content": assistant_message
})
print() # Empty line for readability
except Exception as e:
print(f"\n❌ Error: {e}\n")
# Remove the failed user message from history
conversation_history.pop()
def select_model(client, models):
"""Let user select a model"""
print("\n📋 Available Models:")
print("-" * 60)
for i, model in enumerate(models, 1):
print(f"{i}. {model}")
print("-" * 60)
while True:
try:
choice = input("\nSelect a model number (or 'q' to quit): ").strip()
if choice.lower() == 'q':
return None
choice_num = int(choice)
if 1 <= choice_num <= len(models):
return models[choice_num - 1]
else:
print(f"Please enter a number between 1 and {len(models)}")
except ValueError:
print("Please enter a valid number or 'q' to quit")
except (EOFError, KeyboardInterrupt):
print("\n")
return None
def main():
"""Main function"""
print("=" * 60)
print("🚀 LMArena Bridge - Interactive Chat")
print("=" * 60)
# Initialize OpenAI client
try:
client = OpenAI(
api_key=API_KEY,
base_url=BASE_URL
)
print("✅ Connected to LMArena Bridge")
except Exception as e:
print(f"❌ Failed to initialize client: {e}")
return
# Get available models
print("📡 Fetching available models...")
models = list_available_models(client)
if not models:
print("❌ No models available. Please check your API key and server status.")
return
print(f"✅ Found {len(models)} models")
# Main loop
while True:
selected_model = select_model(client, models)
if selected_model is None:
print("\n👋 Goodbye!")
break
result = chat_session(client, selected_model)
if result != 'switch_model':
break
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
sys.exit(0)