Spaces:
Running
Running
Commit
Β·
ca5eeb4
1
Parent(s):
c9c133f
Changed thinking streaming
Browse files- src/gradio_utils.py +21 -15
src/gradio_utils.py
CHANGED
|
@@ -92,27 +92,21 @@ def update_map_on_selection(row: pd.Series, df_routes: gr.State) -> Map:
|
|
| 92 |
|
| 93 |
def pull_messages_from_step(step_log, test_mode: bool = True):
|
| 94 |
"""Extract ChatMessage objects from agent steps"""
|
|
|
|
| 95 |
if isinstance(step_log, ActionStep):
|
| 96 |
-
yield
|
| 97 |
if step_log.tool_calls is not None:
|
| 98 |
first_tool_call = step_log.tool_calls[0]
|
| 99 |
used_code = first_tool_call.name == "code interpreter"
|
| 100 |
content = first_tool_call.arguments
|
| 101 |
if used_code:
|
| 102 |
content = f"```py\n{content}\n```"
|
| 103 |
-
yield
|
| 104 |
-
|
| 105 |
-
metadata={"title": "π€ππ"},
|
| 106 |
-
content=str(content),
|
| 107 |
-
)
|
| 108 |
if step_log.observations is not None:
|
| 109 |
-
yield
|
| 110 |
if step_log.error is not None:
|
| 111 |
-
yield
|
| 112 |
-
role="assistant",
|
| 113 |
-
content=str(step_log.error),
|
| 114 |
-
metadata={"title": "π₯ Error"},
|
| 115 |
-
)
|
| 116 |
|
| 117 |
|
| 118 |
# Simplified interaction function
|
|
@@ -130,7 +124,10 @@ def interact_with_agent(agent, prompt, messages, df_routes, additional_args):
|
|
| 130 |
reset_agent_memory=True,
|
| 131 |
additional_args=additional_args,
|
| 132 |
):
|
| 133 |
-
messages.
|
|
|
|
|
|
|
|
|
|
| 134 |
yield (messages, _df_routes, final_message)
|
| 135 |
|
| 136 |
yield (messages, _df_routes, final_message)
|
|
@@ -145,9 +142,18 @@ def stream_to_gradio(
|
|
| 145 |
**kwargs,
|
| 146 |
):
|
| 147 |
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
| 148 |
-
|
|
|
|
| 149 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, **kwargs):
|
| 150 |
-
for
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
yield (message, df_routes, gr.Markdown(value=FINAL_MESSAGE_HEADER , container=True))
|
| 152 |
|
| 153 |
final_answer = step_log # Last log is the run's final_answer
|
|
|
|
| 92 |
|
| 93 |
def pull_messages_from_step(step_log, test_mode: bool = True):
|
| 94 |
"""Extract ChatMessage objects from agent steps"""
|
| 95 |
+
accumulated_thoughts = ""
|
| 96 |
if isinstance(step_log, ActionStep):
|
| 97 |
+
yield (step_log.llm_output, "")
|
| 98 |
if step_log.tool_calls is not None:
|
| 99 |
first_tool_call = step_log.tool_calls[0]
|
| 100 |
used_code = first_tool_call.name == "code interpreter"
|
| 101 |
content = first_tool_call.arguments
|
| 102 |
if used_code:
|
| 103 |
content = f"```py\n{content}\n```"
|
| 104 |
+
yield str(content)
|
| 105 |
+
|
|
|
|
|
|
|
|
|
|
| 106 |
if step_log.observations is not None:
|
| 107 |
+
yield (step_log.observations, "")
|
| 108 |
if step_log.error is not None:
|
| 109 |
+
yield ("", step_log.error)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
# Simplified interaction function
|
|
|
|
| 124 |
reset_agent_memory=True,
|
| 125 |
additional_args=additional_args,
|
| 126 |
):
|
| 127 |
+
if messages.metadata.get("title") == "Error π₯" or messages.metadata.get("title") == "π€ππ" :
|
| 128 |
+
messages[-1] = msg
|
| 129 |
+
else:
|
| 130 |
+
messages.append(msg)
|
| 131 |
yield (messages, _df_routes, final_message)
|
| 132 |
|
| 133 |
yield (messages, _df_routes, final_message)
|
|
|
|
| 142 |
**kwargs,
|
| 143 |
):
|
| 144 |
"""Runs an agent with the given task and streams the messages from the agent as gradio ChatMessages."""
|
| 145 |
+
accumulated_thoughts = ""
|
| 146 |
+
accumulated_errors = ""
|
| 147 |
for step_log in agent.run(task, stream=True, reset=reset_agent_memory, **kwargs):
|
| 148 |
+
for (obs, error) in pull_messages_from_step(step_log, test_mode=test_mode):
|
| 149 |
+
|
| 150 |
+
if len(obs)>0:
|
| 151 |
+
accumulated_thoughts += f"{obs}\n\n"
|
| 152 |
+
message = gr.ChatMessage(role="assistant", metadata={"title": "π€ππ"}, content=str(obs))
|
| 153 |
+
|
| 154 |
+
if len(error)>0:
|
| 155 |
+
accumulated_errors += f"{error}\n\n"
|
| 156 |
+
message = gr.ChatMessage(role="assistant", metadata={"title": "Error π₯"}, content=str(obs))
|
| 157 |
yield (message, df_routes, gr.Markdown(value=FINAL_MESSAGE_HEADER , container=True))
|
| 158 |
|
| 159 |
final_answer = step_log # Last log is the run's final_answer
|