-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
119 lines (86 loc) · 3.5 KB
/
chat.py
File metadata and controls
119 lines (86 loc) · 3.5 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
import openai
import os
from dotenv import load_dotenv, find_dotenv
from utils.text2speech import Voice
from utils.stream import TextStream, VoiceObserver
from prompts.system import jailbreak
from prompts.dungeon_master import dm_init
from prompts.players import player_prompt
import streamlit as st
from streamlit_chat import message
load_dotenv(find_dotenv())
VOICE = False
# Create a TextStream instance
text_stream = TextStream()
# Create a Voice instance
voice = Voice(mute=True)
# Register the VoiceObserver with the TextStream instance
voice_observer = VoiceObserver(voice)
text_stream.register_observer(voice_observer)
# Load your OpenAI API key from an environment variable or secret management service
openai.api_key = os.getenv("OPENAI_API_KEY")
def generate_response(messages):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages,
max_tokens=150,
n=1,
stop=None,
temperature=0.8,
)
message = response.choices[0].message["content"].strip()
return message
def bot_conditioning():
conversation_history = []
# Jailbreak layer
conversation_history.append({"role": "system", "content": jailbreak})
response = generate_response(conversation_history)
text_stream.append(response)
conversation_history.append({"role": "assistant", "content": response})
# DM layer
# TODO: The LLM keeps playing for the Players, Explicitly tell the DM to stop
conversation_history.append({"role": "system", "content": dm_init})
response = generate_response(conversation_history)
text_stream.append(response)
conversation_history.append({"role": "assistant", "content": response})
# Players layer
conversation_history.append({"role": "system", "content": player_prompt})
return conversation_history
def send_message(selected_user, message, conversation_history=[]):
user_input = f"[{selected_user}] {message}"
conversation_history.append({"role": "user", "content": user_input})
chatbot_response = generate_response(conversation_history)
conversation_history.append({"role": "assistant", "content": chatbot_response})
text_stream.append(chatbot_response)
return chatbot_response
def main():
st.title("Chat Interface")
user_options = {"Chris": 1, "Adam": 2, "Peter": 3}
user_colors = {0: "#FFFFFF", 1: "#FF5733", 2: "#33FFBD", 3: "#9A33FF"}
conversation_history = bot_conditioning()
selected_user = st.sidebar.selectbox(
"Choose a user", options=list(user_options.keys())
)
user_id = user_options[selected_user]
chat_log = []
chat_history = st.empty()
message_input = st.text_input("Type your message:")
# parse DM response from conversation_history
for message in conversation_history:
if message["role"] == "assistant":
chat_log.append(("DM", message["content"], user_colors[0]))
if st.button("Send"):
response = send_message(selected_user, message_input, conversation_history)
chat_log.append((selected_user, message_input, user_colors[user_id]))
chat_log.append(("DM", response, user_colors[0]))
# Display the entire chat history
with chat_history:
st.markdown("---")
for user, message, color in chat_log:
st.markdown(
f'<p style="color:{color};"><b>{user}:</b> {message}</p>',
unsafe_allow_html=True,
)
message_input = ""
if __name__ == "__main__":
main()