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logging
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
from src.llm_boilers import llm_boiler
|
6 |
+
|
7 |
+
|
8 |
+
logging.basicConfig(format="%(asctime)s - %(message)s", level=logging.INFO)
|
9 |
+
logging.warning("READY. App started...")
|
10 |
+
|
11 |
+
|
12 |
+
class Chat:
|
13 |
+
default_system_prompt = "A conversation between a user and an LLM-based AI assistant. The assistant gives helpful and honest answers."
|
14 |
+
system_format = "<|im_start|>system\n{}<|im_end|>\n"
|
15 |
+
|
16 |
+
def __init__(
|
17 |
+
self, system: str = None, user: str = None, assistant: str = None
|
18 |
+
) -> None:
|
19 |
+
if system is not None:
|
20 |
+
self.set_system_prompt(system)
|
21 |
+
else:
|
22 |
+
self.reset_system_prompt()
|
23 |
+
self.user = user if user else "<|im_start|>user\n{}<|im_end|>\n"
|
24 |
+
self.assistant = (
|
25 |
+
assistant if assistant else "<|im_start|>assistant\n{}<|im_end|>\n"
|
26 |
+
)
|
27 |
+
self.response_prefix = self.assistant.split("{}")[0]
|
28 |
+
|
29 |
+
def set_system_prompt(self, system_prompt):
|
30 |
+
# self.system = self.system_format.format(system_prompt)
|
31 |
+
return system_prompt
|
32 |
+
|
33 |
+
def reset_system_prompt(self):
|
34 |
+
return self.set_system_prompt(self.default_system_prompt)
|
35 |
+
|
36 |
+
def history_as_formatted_str(self, system, history) -> str:
|
37 |
+
system = self.system_format.format(system)
|
38 |
+
text = system + "".join(
|
39 |
+
[
|
40 |
+
"\n".join(
|
41 |
+
[
|
42 |
+
self.user.format(item[0]),
|
43 |
+
self.assistant.format(item[1]),
|
44 |
+
]
|
45 |
+
)
|
46 |
+
for item in history[:-1]
|
47 |
+
]
|
48 |
+
)
|
49 |
+
text += self.user.format(history[-1][0])
|
50 |
+
text += self.response_prefix
|
51 |
+
# stopgap solution to too long sequences
|
52 |
+
if len(text) > 4500:
|
53 |
+
# delete from the middle between <|im_start|> and <|im_end|>
|
54 |
+
# find the middle ones, then expand out
|
55 |
+
start = text.find("<|im_start|>", 139)
|
56 |
+
end = text.find("<|im_end|>", 139)
|
57 |
+
while end < len(text) and len(text) > 4500:
|
58 |
+
end = text.find("<|im_end|>", end + 1)
|
59 |
+
text = text[:start] + text[end + 1 :]
|
60 |
+
if len(text) > 4500:
|
61 |
+
# the nice way didn't work, just truncate
|
62 |
+
# deleting the beginning
|
63 |
+
text = text[-4500:]
|
64 |
+
|
65 |
+
return text
|
66 |
+
|
67 |
+
def clear_history(self, history):
|
68 |
+
return []
|
69 |
+
|
70 |
+
def turn(self, user_input: str):
|
71 |
+
self.user_turn(user_input)
|
72 |
+
return self.bot_turn()
|
73 |
+
|
74 |
+
def user_turn(self, user_input: str, history):
|
75 |
+
history.append([user_input, ""])
|
76 |
+
return user_input, history
|
77 |
+
|
78 |
+
def bot_turn(self, system, history, openai_key):
|
79 |
+
conversation = self.history_as_formatted_str(system, history)
|
80 |
+
assistant_response = call_inf_server(conversation, openai_key)
|
81 |
+
# history[-1][-1] = assistant_response
|
82 |
+
# return history
|
83 |
+
history[-1][1] = ""
|
84 |
+
for chunk in assistant_response:
|
85 |
+
try:
|
86 |
+
decoded_output = chunk["choices"][0]["delta"]["content"]
|
87 |
+
history[-1][1] += decoded_output
|
88 |
+
yield history
|
89 |
+
except KeyError:
|
90 |
+
pass
|
91 |
+
|
92 |
+
|
93 |
+
def call_inf_server(prompt, openai_key):
|
94 |
+
model_id = "gpt-3.5-turbo" # "gpt-3.5-turbo-16k",
|
95 |
+
model = llm_boiler(model_id, openai_key)
|
96 |
+
logging.warning(f'Inf via "{model_id}"" for prompt "{prompt}"')
|
97 |
+
|
98 |
+
try:
|
99 |
+
# run text generation
|
100 |
+
response = model.run(prompt, temperature=1.0)
|
101 |
+
logging.warning(f"Result of text generation: {response}")
|
102 |
+
return response
|
103 |
+
|
104 |
+
except Exception as e:
|
105 |
+
# assume it is our error
|
106 |
+
# just wait and try one more time
|
107 |
+
print(e)
|
108 |
+
time.sleep(2)
|
109 |
+
response = model.run(prompt, temperature=1.0)
|
110 |
+
logging.warning(f"Result of text generation: {response}")
|
111 |
+
return response
|
112 |
+
|
113 |
+
|
114 |
+
with gr.Blocks(
|
115 |
+
theme=gr.themes.Soft(),
|
116 |
+
css=".disclaimer {font-variant-caps: all-small-caps;}",
|
117 |
+
) as demo:
|
118 |
+
gr.Markdown(
|
119 |
+
"""<h1><center>Chat with gpt-3.5-turbo</center></h1>
|
120 |
+
This is a lightweight demo of gpt-3.5-turbo conversation completion. It was designed as a template for in-context learning applications to be built on top of.
|
121 |
+
"""
|
122 |
+
)
|
123 |
+
conversation = Chat()
|
124 |
+
with gr.Row():
|
125 |
+
with gr.Column():
|
126 |
+
# to do: change to openaikey input for public release
|
127 |
+
openai_key = gr.Textbox(
|
128 |
+
label="OpenAI Key",
|
129 |
+
value="",
|
130 |
+
type="password",
|
131 |
+
placeholder="sk..",
|
132 |
+
info="You have to provide your own OpenAI API key.",
|
133 |
+
)
|
134 |
+
chatbot = gr.Chatbot().style(height=400)
|
135 |
+
with gr.Row():
|
136 |
+
with gr.Column():
|
137 |
+
msg = gr.Textbox(
|
138 |
+
label="Chat Message Box",
|
139 |
+
placeholder="Chat Message Box",
|
140 |
+
show_label=False,
|
141 |
+
).style(container=False)
|
142 |
+
with gr.Column():
|
143 |
+
with gr.Row():
|
144 |
+
submit = gr.Button("Submit")
|
145 |
+
stop = gr.Button("Stop")
|
146 |
+
clear = gr.Button("Clear")
|
147 |
+
with gr.Row():
|
148 |
+
with gr.Accordion("Advanced Options:", open=False):
|
149 |
+
with gr.Row():
|
150 |
+
with gr.Column(scale=2):
|
151 |
+
system = gr.Textbox(
|
152 |
+
label="System Prompt",
|
153 |
+
value=Chat.default_system_prompt,
|
154 |
+
show_label=False,
|
155 |
+
).style(container=False)
|
156 |
+
with gr.Column():
|
157 |
+
with gr.Row():
|
158 |
+
change = gr.Button("Change System Prompt")
|
159 |
+
reset = gr.Button("Reset System Prompt")
|
160 |
+
with gr.Row():
|
161 |
+
gr.Markdown(
|
162 |
+
"Disclaimer: The gpt-3.5-turbo model can produce factually incorrect output, and should not be solely relied on to produce "
|
163 |
+
"factually accurate information. The gpt-3.5-turbo model was trained on various public datasets; while great efforts "
|
164 |
+
"have been taken to clean the pretraining data, it is possible that this model could generate lewd, "
|
165 |
+
"biased, or otherwise offensive outputs.",
|
166 |
+
elem_classes=["disclaimer"],
|
167 |
+
)
|
168 |
+
with gr.Row():
|
169 |
+
gr.Markdown(
|
170 |
+
"[Privacy policy](https://gist.github.com/samhavens/c29c68cdcd420a9aa0202d0839876dac)",
|
171 |
+
elem_classes=["disclaimer"],
|
172 |
+
)
|
173 |
+
|
174 |
+
submit_event = msg.submit(
|
175 |
+
fn=conversation.user_turn,
|
176 |
+
inputs=[msg, chatbot],
|
177 |
+
outputs=[msg, chatbot],
|
178 |
+
queue=False,
|
179 |
+
).then(
|
180 |
+
fn=conversation.bot_turn,
|
181 |
+
inputs=[system, chatbot, openai_key],
|
182 |
+
outputs=[chatbot],
|
183 |
+
queue=True,
|
184 |
+
)
|
185 |
+
submit_click_event = submit.click(
|
186 |
+
fn=conversation.user_turn,
|
187 |
+
inputs=[msg, chatbot],
|
188 |
+
outputs=[msg, chatbot],
|
189 |
+
queue=False,
|
190 |
+
).then(
|
191 |
+
fn=conversation.bot_turn,
|
192 |
+
inputs=[system, chatbot, openai_key],
|
193 |
+
outputs=[chatbot],
|
194 |
+
queue=True,
|
195 |
+
)
|
196 |
+
stop.click(
|
197 |
+
fn=None,
|
198 |
+
inputs=None,
|
199 |
+
outputs=None,
|
200 |
+
cancels=[submit_event, submit_click_event],
|
201 |
+
queue=False,
|
202 |
+
)
|
203 |
+
clear.click(lambda: None, None, chatbot, queue=False).then(
|
204 |
+
fn=conversation.clear_history,
|
205 |
+
inputs=[chatbot],
|
206 |
+
outputs=[chatbot],
|
207 |
+
queue=False,
|
208 |
+
)
|
209 |
+
change.click(
|
210 |
+
fn=conversation.set_system_prompt,
|
211 |
+
inputs=[system],
|
212 |
+
outputs=[system],
|
213 |
+
queue=False,
|
214 |
+
)
|
215 |
+
reset.click(
|
216 |
+
fn=conversation.reset_system_prompt,
|
217 |
+
inputs=[],
|
218 |
+
outputs=[system],
|
219 |
+
queue=False,
|
220 |
+
)
|
221 |
+
|
222 |
+
|
223 |
+
demo.queue(max_size=36, concurrency_count=14).launch(debug=True)
|