Protected method to call the language model with automatic retry logic. This is the primary interface for agents to interact with their language model. It provides robust error handling with exponential backoff retries, automatic selection between text and structure
(self,
prompt: str,
system_prompt: Optional[str] = None,
schema: Optional[Dict[str, Any]] = None,
temperature: Optional[float] = None)
| 257 | } |
| 258 | |
| 259 | async def _call_model(self, |
| 260 | prompt: str, |
| 261 | system_prompt: Optional[str] = None, |
| 262 | schema: Optional[Dict[str, Any]] = None, |
| 263 | temperature: Optional[float] = None) -> Union[str, Dict[str, Any]]: |
| 264 | """ |
| 265 | Protected method to call the language model with automatic retry logic. |
| 266 | |
| 267 | This is the primary interface for agents to interact with their language model. |
| 268 | It provides robust error handling with exponential backoff retries, automatic |
| 269 | selection between text and structured (JSON) generation based on the schema |
| 270 | parameter, and comprehensive logging of failures. |
| 271 | |
| 272 | All concrete agent implementations should use this method rather than calling |
| 273 | the model directly to benefit from standardized error handling. |
| 274 | |
| 275 | Args: |
| 276 | prompt (str): The main user prompt describing the task for the model. |
| 277 | Should be clear, specific, and include all necessary context. |
| 278 | system_prompt (Optional[str]): System-level instructions that guide the |
| 279 | model's behavior and response style. If not provided, uses the agent's |
| 280 | default system_prompt from configuration. Defaults to None. |
| 281 | schema (Optional[Dict[str, Any]]): JSON Schema definition for structured |
| 282 | output. When provided, enforces the model to return JSON matching this |
| 283 | schema. When None, returns freeform text. Defaults to None. |
| 284 | temperature (Optional[float]): Sampling temperature for model generation. |
| 285 | Higher values (e.g., 0.8-1.0) increase creativity, lower values |
| 286 | (e.g., 0.1-0.3) increase determinism. If None, uses model default. |
| 287 | |
| 288 | Returns: |
| 289 | Union[str, Dict[str, Any]]: Model's response in one of two formats: |
| 290 | - str: Freeform text response when schema is None |
| 291 | - Dict[str, Any]: Structured JSON response when schema is provided |
| 292 | |
| 293 | Raises: |
| 294 | AgentExecutionError: When model calls fail consistently after exhausting |
| 295 | all retry attempts (max_retries). Contains details of the final error. |
| 296 | |
| 297 | Note: |
| 298 | The method sleeps for 1 second between retry attempts to avoid hammering |
| 299 | the API and potentially triggering rate limits. Consider this latency when |
| 300 | designing time-sensitive operations. |
| 301 | """ |
| 302 | system_prompt = system_prompt or self.system_prompt |
| 303 | remaining_retries = self.max_retries |
| 304 | |
| 305 | while True: |
| 306 | try: |
| 307 | if schema: |
| 308 | return await self.model.generate_json( |
| 309 | prompt=prompt, |
| 310 | schema=schema, |
| 311 | system_prompt=system_prompt, |
| 312 | temperature=temperature |
| 313 | ) |
| 314 | else: |
| 315 | return await self.model.generate( |
| 316 | prompt=prompt, |
no test coverage detected