|
9 | 9 |
|
10 | 10 | from ee.hogai.context.context import AssistantContextManager |
11 | 11 | from ee.hogai.graph.insights_graph.graph import InsightsGraph |
| 12 | +from ee.hogai.graph.schema_generator.nodes import SchemaGenerationException |
12 | 13 | from ee.hogai.tool import MaxTool, ToolMessagesArtifact |
| 14 | +from ee.hogai.utils.prompt import format_prompt_string |
13 | 15 | from ee.hogai.utils.types.base import AssistantState |
14 | 16 |
|
15 | 17 | INSIGHT_TOOL_PROMPT = """ |
|
107 | 109 | </system_reminder> |
108 | 110 | """.strip() |
109 | 111 |
|
110 | | -INSIGHT_TOOL_FAILURE_PROMPT = """ |
111 | | -The agent has encountered an error while creating an insight. |
| 112 | +INSIGHT_TOOL_FAILURE_SYSTEM_REMINDER_PROMPT = """ |
112 | 113 | <system_reminder> |
113 | 114 | Inform the user that you've encountered an error during the creation of the insight. Afterwards, try to generate a new insight with a different query. |
114 | 115 | Terminate if the error persists. |
115 | 116 | </system_reminder> |
116 | 117 | """.strip() |
117 | 118 |
|
| 119 | +INSIGHT_TOOL_HANDLED_FAILURE_PROMPT = """ |
| 120 | +The agent has encountered the error while creating an insight. |
| 121 | +
|
| 122 | +Generated output: |
| 123 | +``` |
| 124 | +{{{output}}} |
| 125 | +``` |
| 126 | +
|
| 127 | +Error message: |
| 128 | +``` |
| 129 | +{{{error_message}}} |
| 130 | +``` |
| 131 | +
|
| 132 | +{{{system_reminder}}} |
| 133 | +""".strip() |
| 134 | + |
| 135 | + |
| 136 | +INSIGHT_TOOL_UNHANDLED_FAILURE_PROMPT = """ |
| 137 | +The agent has encountered an unknown error while creating an insight. |
| 138 | +{{{system_reminder}}} |
| 139 | +""".strip() |
| 140 | + |
118 | 141 |
|
119 | 142 | class CreateAndQueryInsightToolArgs(BaseModel): |
120 | 143 | tool_call_id: Annotated[str, InjectedToolCallId, SkipJsonSchema] |
@@ -145,12 +168,23 @@ async def _arun_impl(self, query_description: str, tool_call_id: str) -> tuple[s |
145 | 168 | }, |
146 | 169 | deep=True, |
147 | 170 | ) |
148 | | - dict_state = await graph.ainvoke(new_state) |
| 171 | + try: |
| 172 | + dict_state = await graph.ainvoke(new_state) |
| 173 | + except SchemaGenerationException as e: |
| 174 | + return format_prompt_string( |
| 175 | + INSIGHT_TOOL_HANDLED_FAILURE_PROMPT, |
| 176 | + output=e.llm_output, |
| 177 | + error_message=e.validation_message, |
| 178 | + system_reminder=INSIGHT_TOOL_FAILURE_SYSTEM_REMINDER_PROMPT, |
| 179 | + ), None |
| 180 | + |
149 | 181 | updated_state = AssistantState.model_validate(dict_state) |
150 | 182 | maybe_viz_message, tool_call_message = updated_state.messages[-2:] |
151 | 183 |
|
152 | 184 | if not isinstance(tool_call_message, AssistantToolCallMessage): |
153 | | - return INSIGHT_TOOL_FAILURE_PROMPT, None |
| 185 | + return format_prompt_string( |
| 186 | + INSIGHT_TOOL_UNHANDLED_FAILURE_PROMPT, system_reminder=INSIGHT_TOOL_FAILURE_SYSTEM_REMINDER_PROMPT |
| 187 | + ), None |
154 | 188 |
|
155 | 189 | # If the previous message is not a visualization message, the agent has requested human feedback. |
156 | 190 | if not isinstance(maybe_viz_message, VisualizationMessage): |
|
0 commit comments