Spaces:
Running
Running
File size: 9,545 Bytes
4131ea5 e801179 4131ea5 e801179 4131ea5 e801179 4131ea5 e801179 4131ea5 |
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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
import traceback
from time import sleep
import anthropic
import streamlit as st
import streamlit.components.v1 as components
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
from openai import OpenAI
def create_client(client_class, api_key_name):
if api_key_name not in st.session_state:
return None
return client_class(api_key=st.session_state[api_key_name])
# Create clients
openai_client = create_client(OpenAI, "openai_key")
claude_client = create_client(anthropic.Anthropic, "claude_key")
mistral_client = create_client(MistralClient, "mistral_key")
# Initialize counter
if "counter" not in st.session_state:
st.session_state["counter"] = 0
# Increment counter to trigger javascript so that focus always will be on the input field.
def increment_counter():
st.session_state.counter += 1
# Create debate text from chat history
# First system prompt is added to the debate text.
# Then all the remaining messages are concatenated with preceding role titles. The last role title is the role of the next message.
def create_debate_text(role):
debate_text = ""
debate_text += common_system_prompt + "\n"
turn_titles = {
"openai": "\nChatGPT:",
"mistral": "\nMistral:",
"claude": "\nClaude:",
"user": "\nUser:",
"end": "End of debate",
}
if len(st.session_state.messages) == 0:
debate_text += "\n" + turn_titles[role]
return debate_text
for message in st.session_state.messages:
debate_text += "\n".join(
[turn_titles[message["role"]], message["content"], "\n"]
)
debate_text += "\n\n" + turn_titles[role]
return debate_text
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Create sidebar
with st.sidebar:
st.image("assets/openai.svg", width=20)
openai_api_key = st.text_input("OpenAI API Key", type="password")
openai_system_prompt = st.text_area(
"OpenAI System Prompt",
value="You are ChatGPT. You are agreeing with the debate topic.",
)
if (
"openai_key" not in st.session_state
or openai_api_key != st.session_state.openai_key
) and openai_api_key != "":
st.session_state.openai_key = openai_api_key
openai_client = OpenAI(api_key=openai_api_key)
st.toast("OpenAI API Key is set", icon="✔️")
st.divider()
st.image("assets/Mistral.svg", width=20)
mistral_api_key = st.text_input("mistral API Key", type="password")
mistral_system_prompt = st.text_area(
"mistral System Prompt",
value="You are mistral. You are disagreeing with the debate topic.",
)
if (
"mistral_key" not in st.session_state
or mistral_api_key != st.session_state.mistral_key
) and mistral_api_key != "":
st.session_state.mistral_key = mistral_api_key
mistral_client = MistralClient(api_key=mistral_api_key)
st.toast("mistral API Key is set", icon="✔️")
st.divider()
st.image("assets/claude-ai-icon.svg", width=20)
claude_api_key = st.text_input("Claude API Key", type="password")
claude_system_prompt = st.text_area(
"Claude System Prompt",
value="You are Claude. You are neutral to the debate topic.",
)
if (
"claude_key" not in st.session_state
or claude_api_key != st.session_state.claude_key
) and claude_api_key != "":
st.session_state.claude_key = claude_api_key
claude_client = anthropic.Anthropic(api_key=claude_api_key)
st.toast("Claude API Key is set", icon="✔️")
st.divider()
common_system_prompt = st.text_area(
"Common System Prompt",
value="Following is a conversation from a debate group. You will state your opinion when its your turn. User will transfer participants responses to you and your response to the participants so that you can communicate. When you refer to the opinions of other participants use their name to address them.",
height=300,
)
# Display chat messages from history on app rerun
with st.container(border=True):
for message in st.session_state.messages:
with st.chat_message(message["name"], avatar=message["avatar"]):
st.markdown(message["content"])
with st.container(border=True):
def is_last_message_role(role):
if len(st.session_state.messages) == 0:
return False
return st.session_state.messages[-1]["role"] == role
def get_chatgpt_response():
try:
debate_text = create_debate_text("openai")
completion = openai_client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": openai_system_prompt},
{
"role": "user",
"content": debate_text,
},
],
)
st.session_state.messages.append(
{
"name": "user",
"role": "openai",
"content": completion.choices[0].message.content,
"avatar": "assets/openai.svg",
}
)
except Exception as e:
print(e, traceback.format_exc())
def get_mistral_response():
try:
debate_text = create_debate_text("mistral")
message = mistral_client.chat(
model="mistral-large-latest",
messages=[
ChatMessage(
role="system",
content=mistral_system_prompt,
),
ChatMessage(
role="user",
content=debate_text,
),
],
)
st.session_state.messages.append(
{
"name": "user",
"role": "mistral",
"content": message.choices[0].message.content,
"avatar": "assets/Mistral.svg",
}
)
except Exception as e:
print(e, traceback.format_exc())
def get_claude_response():
try:
debate_text = create_debate_text("claude")
message = claude_client.messages.create(
model="claude-3-sonnet-20240229",
max_tokens=1000,
temperature=0,
system=claude_system_prompt,
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": debate_text,
}
],
},
],
)
st.session_state.messages.append(
{
"name": "user",
"role": "claude",
"content": message.content[0].text,
"avatar": "assets/claude-ai-icon.svg",
}
)
except Exception as e:
print(e, traceback.format_exc())
# React to user input
with st.container():
if prompt := st.chat_input(
"Start the conversation.", on_submit=increment_counter
):
st.session_state.messages.append(
{"name": "user", "role": "user", "content": prompt, "avatar": "❔"}
)
st.rerun()
with st.container(border=False):
col1, col2, col3 = st.columns(3)
with col1:
st.button(
"ChatGPT",
on_click=get_chatgpt_response,
disabled=is_last_message_role("openai"),
type="primary",
)
with col2:
st.button(
"Mistral",
on_click=get_mistral_response,
disabled=is_last_message_role("mistral"),
type="primary",
)
with col3:
st.button(
"Claude",
on_click=get_claude_response,
disabled=is_last_message_role("claude"),
type="primary",
)
col1, col2 = st.columns(2)
with col1:
st.button(
"Clear chat", on_click=lambda: st.session_state.pop("messages", None)
)
with col2:
# save chat history to file
st.download_button(
"Save chat history",
data=create_debate_text("end"),
file_name="chat_history.txt",
mime="text/plain",
)
custom_css = """
<style>
.stButton{
display: flex;
justify-content: center;
align-items: center;
}
</style>
"""
st.markdown(custom_css, unsafe_allow_html=True)
components.html(
f"""
<div>some hidden container</div>
<p>{st.session_state.counter}</p>
<script>
console.log("Hello from the other side");
var input = window.parent.document.querySelectorAll("textarea[type=textarea]");
console.log(input);
for (var i = 0; i < input.length; ++i) {{
console.log(input[i]);
input[i].focus();
}}
</script>
""",
height=0,
)
|