-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp_session_only.py
More file actions
228 lines (200 loc) · 8.83 KB
/
app_session_only.py
File metadata and controls
228 lines (200 loc) · 8.83 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
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
#!/usr/bin/env python
import logging
import logging.config
from typing import Any
from uuid import uuid4, UUID
import json
import gradio as gr
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage
from langgraph.types import RunnableConfig
from pydantic import BaseModel
load_dotenv()
# There are tools set here dependent on environment variables
from graph import graph, model # noqa
FOLLOWUP_QUESTION_NUMBER = 3
TRIM_MESSAGE_LENGTH = 16 # Includes tool messages
USER_INPUT_MAX_LENGTH = 10000 # Characters
with open('logging-config.json', 'r') as fh:
config = json.load(fh)
logging.config.dictConfig(config)
logger = logging.getLogger(__name__)
async def chat_fn(user_input: str, history: dict, input_graph_state: dict, uuid: UUID):
"""
Args:
user_input (str): The user's input message
history (dict): The history of the conversation in gradio
input_graph_state (dict): The current state of the graph. This includes tool call history
uuid (UUID): The unique identifier for the current conversation. This can be used in conjunction with langgraph or for memory
Yields:
str|Any: The output message
dict|Any: The final state of the graph
bool|Any: Whether to trigger follow up questions
We do not use gradio history in the graph since we want the ToolMessage in the history
ordered properly. GraphProcessingState.messages is used as history instead
"""
try:
if "messages" not in input_graph_state:
input_graph_state["messages"] = []
input_graph_state["messages"].append(
HumanMessage(user_input[:USER_INPUT_MAX_LENGTH])
)
input_graph_state["messages"] = input_graph_state["messages"][-TRIM_MESSAGE_LENGTH:]
config = RunnableConfig(
recursion_limit=10,
run_name="user_chat",
configurable={"thread_id": uuid}
)
output: str = ""
final_state: dict | Any = {}
waiting_output_seq: list[str] = []
yield "Processing...", gr.skip(), False
async for stream_mode, chunk in graph.astream(
input_graph_state,
config=config,
stream_mode=["values", "messages"],
):
if stream_mode == "values":
final_state = chunk
elif stream_mode == "messages":
msg, metadata = chunk
if hasattr(msg, "tool_calls") and msg.tool_calls:
for msg_tool_call in msg.tool_calls:
tool_name: str = msg_tool_call['name']
# download_website_text is the name of the function defined in graph.py
if tool_name == "download_website_text":
waiting_output_seq.append("Downloading website text...")
yield "\n".join(waiting_output_seq), gr.skip(), False
elif tool_name == "tavily_search_results_json":
waiting_output_seq.append("Searching for relevant information...")
yield "\n".join(waiting_output_seq), gr.skip(), False
elif tool_name:
waiting_output_seq.append(f"Running {tool_name}...")
yield "\n".join(waiting_output_seq), gr.skip(), False
# print("output: ", msg, metadata)
# assistant_node is the name we defined in the langgraph graph
if metadata['langgraph_node'] == "assistant_node" and msg.content:
output += msg.content
yield output, gr.skip(), False
# Trigger for asking follow up questions
# + store the graph state for next iteration
yield output, dict(final_state), False
# There's a bug in gradio where the message output isn't being fully updated before
# The event is triggered, so try to workaround it by yielding the same output again
yield output, gr.skip(), True
except Exception:
logger.exception("Exception occurred")
user_error_message = "There was an error processing your request. Please try again."
yield user_error_message, gr.skip(), False
def clear():
return dict(), uuid4()
class FollowupQuestions(BaseModel):
"""Model for langchain to use for structured output for followup questions"""
questions: list[str]
async def populate_followup_questions(end_of_chat_response, messages):
"""
This function gets called a lot due to the asynchronous nature of streaming
Only populate followup questions if streaming has completed and the message is coming from the assistant
"""
if not end_of_chat_response or not messages:
return [gr.skip() for _ in range(FOLLOWUP_QUESTION_NUMBER)]
if messages[-1]["role"] == "assistant":
follow_up_questions: FollowupQuestions = await model.with_structured_output(FollowupQuestions).ainvoke([
("system", f"suggest {FOLLOWUP_QUESTION_NUMBER} followup questions for the user to ask the assistant. Refrain from asking personal questions."),
*messages,
])
if len(follow_up_questions.questions) != FOLLOWUP_QUESTION_NUMBER:
raise ValueError("Invalid value of followup questions")
buttons = []
for i in range(FOLLOWUP_QUESTION_NUMBER):
buttons.append(
gr.Button(follow_up_questions.questions[i], visible=True, elem_classes="chat-tab"),
)
return buttons
else:
return [gr.skip() for _ in range(FOLLOWUP_QUESTION_NUMBER)]
CSS = """
footer {visibility: hidden}
.followup-question-button {font-size: 12px }
"""
# We set the ChatInterface textbox id to chat-textbox for this to work
TRIGGER_CHATINTERFACE_BUTTON = """
function triggerChatButtonClick() {
// Find the div with id "chat-textbox"
const chatTextbox = document.getElementById("chat-textbox");
if (!chatTextbox) {
console.error("Error: Could not find element with id 'chat-textbox'");
return;
}
// Find the button that is a descendant of the div
const button = chatTextbox.querySelector("button");
if (!button) {
console.error("Error: No button found inside the chat-textbox element");
return;
}
// Trigger the click event
button.click();
}"""
if __name__ == "__main__":
logger.info("Starting the interface")
with gr.Blocks(title="Langgraph Template", fill_height=True, css=CSS) as app:
uuid_state = gr.State(
uuid4
)
gradio_graph_state = gr.State(
dict()
)
end_of_chat_response_state = gr.State(
bool()
)
chatbot = gr.Chatbot(
type="messages",
scale=1,
)
chatbot.clear(fn=clear, outputs=[gradio_graph_state, uuid_state])
with gr.Row():
followup_question_buttons = []
for i in range(FOLLOWUP_QUESTION_NUMBER):
btn = gr.Button(f"Button {i+1}", visible=False, elem_classes="followup-question-button")
followup_question_buttons.append(btn)
multimodal = False
textbox_component = (
gr.MultimodalTextbox if multimodal else gr.Textbox
)
with gr.Column():
textbox = textbox_component(
show_label=False,
label="Message",
placeholder="Type a message...",
scale=7,
autofocus=True,
submit_btn=True,
stop_btn=True,
elem_id="chat-textbox",
lines=1,
)
chat_interface = gr.ChatInterface(
chatbot=chatbot,
fn=chat_fn,
additional_inputs=[
gradio_graph_state,
uuid_state,
],
additional_outputs=[
gradio_graph_state,
end_of_chat_response_state
],
type="messages",
multimodal=multimodal,
textbox=textbox,
)
def click_followup_button(btn):
buttons = [gr.Button(visible=False) for _ in range(len(followup_question_buttons))]
return btn, *buttons
for btn in followup_question_buttons:
btn.click(fn=click_followup_button, inputs=[btn], outputs=[chat_interface.textbox, *followup_question_buttons]).success(lambda: None, js=TRIGGER_CHATINTERFACE_BUTTON)
chatbot.change(fn=populate_followup_questions, inputs=[end_of_chat_response_state, chatbot], outputs=followup_question_buttons, trigger_mode="once")
app.launch(
server_name="127.0.0.1",
server_port=7860,
)