Spaces:
Sleeping
Sleeping
Commit
Β·
3996e79
1
Parent(s):
72ef847
add node name to response
Browse files- src/agent.py +2 -2
- src/ui.py +27 -5
src/agent.py
CHANGED
@@ -74,7 +74,7 @@ class AudioAgent:
|
|
74 |
stream_mode="messages"
|
75 |
):
|
76 |
if msg.content:
|
77 |
-
yield msg.content
|
78 |
|
79 |
async def main():
|
80 |
agent = AudioAgent()
|
@@ -83,7 +83,7 @@ async def main():
|
|
83 |
print("β", reply)
|
84 |
|
85 |
# streaming example
|
86 |
-
async for msg in agent.stream_chat("Explain how audio normalization works."):
|
87 |
print(msg, end="", flush=True)
|
88 |
|
89 |
if __name__ == "__main__":
|
|
|
74 |
stream_mode="messages"
|
75 |
):
|
76 |
if msg.content:
|
77 |
+
yield msg.content, metadata["langgraph_node"]
|
78 |
|
79 |
async def main():
|
80 |
agent = AudioAgent()
|
|
|
83 |
print("β", reply)
|
84 |
|
85 |
# streaming example
|
86 |
+
async for msg, node in agent.stream_chat("Explain how audio normalization works."):
|
87 |
print(msg, end="", flush=True)
|
88 |
|
89 |
if __name__ == "__main__":
|
src/ui.py
CHANGED
@@ -19,7 +19,7 @@ def user_input(user_message, history):
|
|
19 |
|
20 |
async def bot_response(history):
|
21 |
"""
|
22 |
-
Generate bot response with streaming
|
23 |
"""
|
24 |
if not history or history[-1]["role"] != "user":
|
25 |
return
|
@@ -35,17 +35,39 @@ async def bot_response(history):
|
|
35 |
history.append({"role": "assistant", "content": ""})
|
36 |
yield history
|
37 |
|
|
|
|
|
|
|
|
|
|
|
38 |
# Stream the response
|
39 |
-
async for chunk in agent.stream_chat(user_message):
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
yield history
|
42 |
|
43 |
except Exception as e:
|
44 |
# Update the last message with error or add error message
|
45 |
if history and history[-1]["role"] == "assistant":
|
46 |
-
history[-1]["content"] = f"Error
|
47 |
else:
|
48 |
-
history.append({"role": "assistant", "content": f"Error
|
49 |
yield history
|
50 |
|
51 |
def bot_response_sync(history):
|
|
|
19 |
|
20 |
async def bot_response(history):
|
21 |
"""
|
22 |
+
Generate bot response with streaming, organizing content by graph nodes
|
23 |
"""
|
24 |
if not history or history[-1]["role"] != "user":
|
25 |
return
|
|
|
35 |
history.append({"role": "assistant", "content": ""})
|
36 |
yield history
|
37 |
|
38 |
+
# Track current node and organize content by nodes
|
39 |
+
current_content = ""
|
40 |
+
current_node = None
|
41 |
+
nodes_content = {}
|
42 |
+
|
43 |
# Stream the response
|
44 |
+
async for chunk, node_name in agent.stream_chat(user_message):
|
45 |
+
# If we encounter a new node, update the display structure
|
46 |
+
if node_name != current_node:
|
47 |
+
current_node = node_name
|
48 |
+
if node_name not in nodes_content:
|
49 |
+
nodes_content[node_name] = ""
|
50 |
+
|
51 |
+
# Add chunk to the current node's content
|
52 |
+
if chunk:
|
53 |
+
nodes_content[node_name] += chunk
|
54 |
+
|
55 |
+
# Build the formatted content with node headers
|
56 |
+
formatted_content = ""
|
57 |
+
for node, content in nodes_content.items():
|
58 |
+
if content.strip(): # Only show nodes that have content
|
59 |
+
formatted_content += f"**π§ {node}**\n\n{content}\n\n"
|
60 |
+
|
61 |
+
# Update the chat history
|
62 |
+
history[-1]["content"] = formatted_content.rstrip()
|
63 |
yield history
|
64 |
|
65 |
except Exception as e:
|
66 |
# Update the last message with error or add error message
|
67 |
if history and history[-1]["role"] == "assistant":
|
68 |
+
history[-1]["content"] = f"β **Error**: {str(e)}"
|
69 |
else:
|
70 |
+
history.append({"role": "assistant", "content": f"β **Error**: {str(e)}"})
|
71 |
yield history
|
72 |
|
73 |
def bot_response_sync(history):
|