Spaces:
Sleeping
Sleeping
jedick
commited on
Commit
·
9bf222a
1
Parent(s):
72d25de
Make skip_summarization conditional on successful plot creation
Browse files- .gitignore +4 -3
- PlotMyData/agent.py +21 -6
- README.md +2 -2
.gitignore
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# Any secret files (including secret.openai-api-key)
|
| 2 |
secret.*
|
| 3 |
-
# Copied by Dockerfile from
|
| 4 |
-
|
| 5 |
# Created by entrypoint.sh
|
| 6 |
.Rprofile
|
| 7 |
-
|
|
|
|
|
|
| 1 |
# Any secret files (including secret.openai-api-key)
|
| 2 |
secret.*
|
| 3 |
+
# Copied by Dockerfile from entrypoint.sh
|
| 4 |
+
startup.sh
|
| 5 |
# Created by entrypoint.sh
|
| 6 |
.Rprofile
|
| 7 |
+
# We can ignore __pycache__
|
| 8 |
+
__pycache__
|
PlotMyData/agent.py
CHANGED
|
@@ -188,18 +188,33 @@ def detect_file_type(byte_data: bytes) -> Tuple[str, str]:
|
|
| 188 |
return "image/png", "png"
|
| 189 |
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
async def save_plot_artifact(
|
| 192 |
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict
|
| 193 |
) -> Optional[Dict]:
|
| 194 |
"""
|
| 195 |
Callback function to save plot files as an ADK artifact.
|
| 196 |
"""
|
| 197 |
-
# We just want to see the plot in the conversation;
|
| 198 |
-
# no need for an extra LLM call to tell us it's there.
|
| 199 |
-
# This also prevents the model from trying to rerun the code,
|
| 200 |
-
# so we can directly show the error message.
|
| 201 |
-
tool_context.actions.skip_summarization = True
|
| 202 |
|
|
|
|
| 203 |
if tool.name in ["make_plot", "make_ggplot"]:
|
| 204 |
# In ADK 1.17.0, tool_response is a dict (i.e. result of model_dump method invoked on MCP CallToolResult instance):
|
| 205 |
# https://github.com/google/adk-python/commit/4df926388b6e9ebcf517fbacf2f5532fd73b0f71
|
|
@@ -283,7 +298,7 @@ plot_agent = LlmAgent(
|
|
| 283 |
],
|
| 284 |
before_model_callback=[preprocess_artifact, preprocess_messages],
|
| 285 |
before_tool_callback=catch_tool_errors,
|
| 286 |
-
after_tool_callback=save_plot_artifact,
|
| 287 |
)
|
| 288 |
|
| 289 |
# Create parent agent and assign children via sub_agents
|
|
|
|
| 188 |
return "image/png", "png"
|
| 189 |
|
| 190 |
|
| 191 |
+
async def skip_summarization_for_plot_success(
|
| 192 |
+
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict
|
| 193 |
+
) -> Optional[Dict]:
|
| 194 |
+
"""
|
| 195 |
+
Callback function to turn off summarization if plot succeeded.
|
| 196 |
+
"""
|
| 197 |
+
|
| 198 |
+
# If there was an error making the plot, the LLM tells the user what happened.
|
| 199 |
+
# This happens because skip_summarization is False by default.
|
| 200 |
+
|
| 201 |
+
# But if the plot was created successfully, there's
|
| 202 |
+
# no need for an extra LLM call to tell us it's there.
|
| 203 |
+
if tool.name in ["make_plot", "make_ggplot"]:
|
| 204 |
+
if not tool_response["isError"]:
|
| 205 |
+
tool_context.actions.skip_summarization = True
|
| 206 |
+
|
| 207 |
+
return None
|
| 208 |
+
|
| 209 |
+
|
| 210 |
async def save_plot_artifact(
|
| 211 |
tool: BaseTool, args: Dict[str, Any], tool_context: ToolContext, tool_response: Dict
|
| 212 |
) -> Optional[Dict]:
|
| 213 |
"""
|
| 214 |
Callback function to save plot files as an ADK artifact.
|
| 215 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
|
| 217 |
+
# Look for plot tool (so we don't bother with transfer_to_agent or other functions)
|
| 218 |
if tool.name in ["make_plot", "make_ggplot"]:
|
| 219 |
# In ADK 1.17.0, tool_response is a dict (i.e. result of model_dump method invoked on MCP CallToolResult instance):
|
| 220 |
# https://github.com/google/adk-python/commit/4df926388b6e9ebcf517fbacf2f5532fd73b0f71
|
|
|
|
| 298 |
],
|
| 299 |
before_model_callback=[preprocess_artifact, preprocess_messages],
|
| 300 |
before_tool_callback=catch_tool_errors,
|
| 301 |
+
after_tool_callback=[skip_summarization_for_plot_success, save_plot_artifact],
|
| 302 |
)
|
| 303 |
|
| 304 |
# Create parent agent and assign children via sub_agents
|
README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
---
|
| 2 |
title: PlotMyData
|
| 3 |
emoji: 👀
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|
|
|
|
| 1 |
---
|
| 2 |
title: PlotMyData
|
| 3 |
emoji: 👀
|
| 4 |
+
colorFrom: yellow
|
| 5 |
+
colorTo: purple
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
license: mit
|