-
Notifications
You must be signed in to change notification settings - Fork 5
/
hallucinate.py
178 lines (137 loc) · 4.88 KB
/
hallucinate.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
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
from dotenv import load_dotenv
load_dotenv()
import json
import os
import requests
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage,
messages_from_dict,
messages_to_dict,
)
@st.cache_resource
def get_chat():
return ChatOpenAI(
model_name=os.environ["OPENAI_MODEL"], temperature=0.7, max_tokens=None
)
@st.cache_data
def get_available_companies():
filenames = os.listdir(os.path.join("trips", "companies"))
return sorted([file.split(".")[0] for file in filenames])
chat = get_chat()
"""
# Trip Saver
There are cases where you might want to force an AI hallucination and then save it
for replay to invite others in to the same context.
"""
def load_trip_from_filename(trip_path):
full_filepath = os.path.join(
trip_path, st.session_state["trip_file_name"] + ".json"
)
with open(full_filepath) as file:
json_str = file.read()
import_json(json.loads(json_str))
with st.form("load_existing"):
trip_file_name = st.selectbox(
"Choose an existing Company Trip",
options=get_available_companies(),
format_func=lambda x: x.title(),
key="trip_file_name",
)
st.form_submit_button(
"Load",
on_click=load_trip_from_filename,
kwargs={"trip_path": os.path.join("trips", "companies")},
)
"""
## System prompt
This is how you'd like the system to behave. You should be very specific, and make sure
that you tell it is okay to provide fake information. After you set this up, you can talk
directly to it.
"""
if "system_locked" not in st.session_state:
st.session_state["system_locked"] = False
system_prompter = st.text_area(
"How should this assistant behave?",
key="system_prompter",
disabled=st.session_state["system_locked"],
)
"""
## Interact
Now you should interact with the assistant you just created.
"""
if "history" not in st.session_state:
st.session_state["history"] = []
else:
st.markdown("### History")
def remove_response_and_prompt(response_index):
prompt_index = response_index - 1
prompt = st.session_state["history"].pop(prompt_index)
print(f"Removing prompt {prompt}")
# Note since the list has shifted up we are using the same index
ai = st.session_state["history"].pop(prompt_index)
print(f"Removing ai result: {ai}")
for index, message in enumerate(st.session_state["history"]):
if isinstance(message, HumanMessage):
st.markdown(f"#### {message.content}")
elif isinstance(message, AIMessage):
st.markdown(message.content)
st.button(
"Remove this prompt",
key=f"remove_{index}",
on_click=remove_response_and_prompt,
args=(index,),
)
if st.session_state["history"]:
number_of_tokens = chat.get_num_tokens_from_messages(st.session_state["history"])
st.markdown(
f"**Number of Tokens used in current conversation**: {number_of_tokens}"
)
def submit_chat():
messages = st.session_state["history"]
if not messages:
messages.append(SystemMessage(content=system_prompter))
messages.append(HumanMessage(content=st.session_state.chat_prompter))
try:
ai_message = chat(messages)
except Exception as ex:
ai_message = AIMessage(
content=f"Hmmm...something went wrong. Try again with a different prompt. {ex}"
)
messages.append(ai_message)
st.session_state["history"] = messages
st.session_state["system_locked"] = True
st.session_state.chat_prompter = ""
with st.form("chat_prompt"):
chat_prompter = st.text_area(
"What would you like to ask your assistant?", key="chat_prompter"
)
st.form_submit_button("Ask", on_click=submit_chat)
def import_json(json_obj):
messages = messages_from_dict(json_obj)
st.session_state["history"] = messages
st.session_state["system_prompter"] = messages[0].content
st.session_state["system_locked"] = True
st.balloons()
def import_json_url():
url = st.session_state["json_url"]
response = requests.get(url)
json = response.json()
return import_json(json)
with st.expander("Additional storage and retrieval options"):
with st.form("save_trip"):
name = st.text_input("What should this be called?")
submitted = st.form_submit_button("Save")
if submitted:
filepath = os.path.join("trips", "user-submitted", name + ".json")
with open(filepath, "w") as f:
f.write(json.dumps(messages_to_dict(st.session_state["history"])))
print(f"Saved: {filepath}")
with st.form("import_json_url"):
json_url = st.text_input("Import from JSON URL", key="json_url")
st.form_submit_button("Import", on_click=import_json_url)
st.markdown("#### Raw JSON")
st.code(json.dumps(messages_to_dict(st.session_state["history"])))