Provide ability to trace the chain in the chat window
Browse files- app.py +59 -7
- videos/tempfile.mp4 +2 -2
app.py
CHANGED
|
@@ -15,6 +15,11 @@ from langchain.agents import load_tools, initialize_agent
|
|
| 15 |
from langchain.chains.conversation.memory import ConversationBufferMemory
|
| 16 |
from langchain.llms import OpenAI
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
news_api_key = os.environ["NEWS_API_KEY"]
|
| 19 |
tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
|
| 20 |
|
|
@@ -67,19 +72,55 @@ def set_openai_api_key(api_key):
|
|
| 67 |
return chain, llm
|
| 68 |
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def chat(
|
| 71 |
-
inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain]
|
| 72 |
-
):
|
| 73 |
"""Execute the chat functionality."""
|
| 74 |
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
| 75 |
print("inp: " + inp)
|
|
|
|
| 76 |
history = history or []
|
| 77 |
# If chain is None, that is because no API key was provided.
|
| 78 |
output = "Please paste your OpenAI key to use this application."
|
|
|
|
| 79 |
if chain and chain != "":
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
history.append((inp, output))
|
|
|
|
| 83 |
html_video, temp_file = do_html_video_speak(output)
|
| 84 |
return history, history, html_video, temp_file, ""
|
| 85 |
|
|
@@ -114,6 +155,12 @@ def update_selected_tools(widget, state, llm):
|
|
| 114 |
return state, llm, chain
|
| 115 |
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
| 118 |
|
| 119 |
with block:
|
|
@@ -121,6 +168,7 @@ with block:
|
|
| 121 |
history_state = gr.State()
|
| 122 |
chain_state = gr.State()
|
| 123 |
tools_list_state = gr.State(TOOLS_DEFAULT_LIST)
|
|
|
|
| 124 |
|
| 125 |
with gr.Row():
|
| 126 |
with gr.Column():
|
|
@@ -137,6 +185,10 @@ with block:
|
|
| 137 |
htm_video = f'<video width="256" height="256" autoplay muted loop><source src={tmp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
|
| 138 |
video_html = gr.HTML(htm_video)
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
with gr.Column(scale=0.75):
|
| 141 |
chatbot = gr.Chatbot()
|
| 142 |
|
|
@@ -179,8 +231,8 @@ with block:
|
|
| 179 |
|
| 180 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
| 181 |
|
| 182 |
-
message.submit(chat, inputs=[message, history_state, chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
|
| 183 |
-
submit.click(chat, inputs=[message, history_state, chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
|
| 184 |
|
| 185 |
openai_api_key_textbox.change(set_openai_api_key,
|
| 186 |
inputs=[openai_api_key_textbox],
|
|
|
|
| 15 |
from langchain.chains.conversation.memory import ConversationBufferMemory
|
| 16 |
from langchain.llms import OpenAI
|
| 17 |
|
| 18 |
+
# Console to variable
|
| 19 |
+
from io import StringIO
|
| 20 |
+
import sys
|
| 21 |
+
import re
|
| 22 |
+
|
| 23 |
news_api_key = os.environ["NEWS_API_KEY"]
|
| 24 |
tmdb_bearer_token = os.environ["TMDB_BEARER_TOKEN"]
|
| 25 |
|
|
|
|
| 72 |
return chain, llm
|
| 73 |
|
| 74 |
|
| 75 |
+
def run_chain(chain, inp, capture_hidden_text):
|
| 76 |
+
output = ""
|
| 77 |
+
hidden_text = None
|
| 78 |
+
if capture_hidden_text:
|
| 79 |
+
tmp = sys.stdout
|
| 80 |
+
hidden_text_io = StringIO()
|
| 81 |
+
sys.stdout = hidden_text_io
|
| 82 |
+
|
| 83 |
+
output = chain.run(input=inp)
|
| 84 |
+
|
| 85 |
+
sys.stdout = tmp
|
| 86 |
+
hidden_text = hidden_text_io.getvalue()
|
| 87 |
+
|
| 88 |
+
# remove escape characters from hidden_text
|
| 89 |
+
hidden_text = re.sub(r'\x1b[^m]*m', '', hidden_text)
|
| 90 |
+
|
| 91 |
+
# remove "Entering new AgentExecutor chain..." from hidden_text
|
| 92 |
+
hidden_text = re.sub(r"Entering new AgentExecutor chain...\n", "", hidden_text)
|
| 93 |
+
|
| 94 |
+
# remove "Finished chain." from hidden_text
|
| 95 |
+
hidden_text = re.sub(r"Finished chain.", "", hidden_text)
|
| 96 |
+
|
| 97 |
+
# Add newline after "Thought:" "Action:" "Observation:" "Input:" and "AI:"
|
| 98 |
+
hidden_text = re.sub(r"Thought:", "\n\nThought:", hidden_text)
|
| 99 |
+
hidden_text = re.sub(r"Action:", "\n\nAction:", hidden_text)
|
| 100 |
+
hidden_text = re.sub(r"Observation:", "\n\nObservation:", hidden_text)
|
| 101 |
+
hidden_text = re.sub(r"Input:", "\n\nInput:", hidden_text)
|
| 102 |
+
hidden_text = re.sub(r"AI:", "\n\nAI:", hidden_text)
|
| 103 |
+
else:
|
| 104 |
+
output = chain.run(input=inp)
|
| 105 |
+
return output, hidden_text
|
| 106 |
+
|
| 107 |
+
|
| 108 |
def chat(
|
| 109 |
+
inp: str, history: Optional[Tuple[str, str]], chain: Optional[ConversationChain],
|
| 110 |
+
trace_chain: bool):
|
| 111 |
"""Execute the chat functionality."""
|
| 112 |
print("\n==== date/time: " + str(datetime.datetime.now()) + " ====")
|
| 113 |
print("inp: " + inp)
|
| 114 |
+
print("trace_chain: ", trace_chain)
|
| 115 |
history = history or []
|
| 116 |
# If chain is None, that is because no API key was provided.
|
| 117 |
output = "Please paste your OpenAI key to use this application."
|
| 118 |
+
|
| 119 |
if chain and chain != "":
|
| 120 |
+
output, hidden_text = run_chain(chain, inp, capture_hidden_text=trace_chain)
|
| 121 |
+
|
| 122 |
+
history.append((inp, hidden_text if trace_chain else output))
|
| 123 |
+
|
| 124 |
html_video, temp_file = do_html_video_speak(output)
|
| 125 |
return history, history, html_video, temp_file, ""
|
| 126 |
|
|
|
|
| 155 |
return state, llm, chain
|
| 156 |
|
| 157 |
|
| 158 |
+
def update_foo(widget, state):
|
| 159 |
+
if widget:
|
| 160 |
+
state = widget
|
| 161 |
+
return state
|
| 162 |
+
|
| 163 |
+
|
| 164 |
block = gr.Blocks(css=".gradio-container {background-color: lightgray}")
|
| 165 |
|
| 166 |
with block:
|
|
|
|
| 168 |
history_state = gr.State()
|
| 169 |
chain_state = gr.State()
|
| 170 |
tools_list_state = gr.State(TOOLS_DEFAULT_LIST)
|
| 171 |
+
trace_chain_state = gr.State(False)
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column():
|
|
|
|
| 185 |
htm_video = f'<video width="256" height="256" autoplay muted loop><source src={tmp_file_url} type="video/mp4" poster="Masahiro.png"></video>'
|
| 186 |
video_html = gr.HTML(htm_video)
|
| 187 |
|
| 188 |
+
trace_chain_cb = gr.Checkbox(label="Show chain", value=False)
|
| 189 |
+
trace_chain_cb.change(update_foo, inputs=[trace_chain_cb, trace_chain_state],
|
| 190 |
+
outputs=[trace_chain_state])
|
| 191 |
+
|
| 192 |
with gr.Column(scale=0.75):
|
| 193 |
chatbot = gr.Chatbot()
|
| 194 |
|
|
|
|
| 231 |
|
| 232 |
gr.HTML("<center>Powered by <a href='https://github.com/hwchase17/langchain'>LangChain 🦜️🔗</a></center>")
|
| 233 |
|
| 234 |
+
message.submit(chat, inputs=[message, history_state, chain_state, trace_chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
|
| 235 |
+
submit.click(chat, inputs=[message, history_state, chain_state, trace_chain_state], outputs=[chatbot, history_state, video_html, my_file, message])
|
| 236 |
|
| 237 |
openai_api_key_textbox.change(set_openai_api_key,
|
| 238 |
inputs=[openai_api_key_textbox],
|
videos/tempfile.mp4
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e8f7c952f368c8b66d0eace21d820f1549e4fa153088879bc80103b64d3ecdb
|
| 3 |
+
size 307463
|