ontocast.agent.common¶
call_llm_with_retry(llm_tool, prompt, parser, prompt_kwargs, max_retries=3, retry_error_feedback=True, llm_graph_format=None)
async
¶
Call LLM and parse response with automatic retry on parsing failures.
This utility function implements a common pattern across agent functions: 1. Call LLM with a prompt 2. Parse the response 3. Retry if parsing fails (up to max_retries times)
On retry, if retry_error_feedback is True, the error message from the previous attempt is included in the prompt to help the LLM correct its output format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm_tool
|
LLMTool
|
The LLM tool instance to use for generation. |
required |
prompt
|
BasePromptTemplate
|
The prompt template to format and send to the LLM. |
required |
parser
|
BaseOutputParser[T]
|
The output parser to parse the LLM response. |
required |
prompt_kwargs
|
dict[str, Any]
|
Keyword arguments to pass to prompt.format_prompt(). |
required |
max_retries
|
int
|
Maximum number of retry attempts (default: 3). |
3
|
retry_error_feedback
|
bool
|
Whether to include error feedback in retry prompts (default: True). |
True
|
llm_graph_format
|
LLMGraphFormat | None
|
When set, |
None
|
Returns:
| Type | Description |
|---|---|
T
|
The parsed output of type T. |
Raises:
| Type | Description |
|---|---|
Exception
|
If parsing fails after all retry attempts, raises the last parsing error. |
Source code in ontocast/agent/common.py
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 | |
render_suggestions_prompt(suggestions, stage)
¶
Generate prompt templates from the suggestions.
Returns:
| Type | Description |
|---|---|
str
|
Combined string with general and concrete templates. |
str
|
Returns empty string if both fields are empty. |
Source code in ontocast/agent/common.py
strip_json_comments(text)
¶
Remove single-line // comments from JSON-like text while preserving string literals.
The LLM occasionally emits JavaScript-style // comments inside JSON output, which are not valid JSON. This function strips them by scanning the text token by token: JSON string literals (which may contain '//') are kept intact, while bare // … sequences are dropped.
Source code in ontocast/agent/common.py
strip_trailing_commas(text)
¶
Remove trailing commas before } or ] (invalid in strict JSON).