1+ import os
2+ from typing import Optional
3+ from dataclasses import dataclass
4+ import random
5+ import string
6+
7+ def generate_random_string (length : int ) -> str :
8+ """Generates a random string of letters and digits."""
9+ if not isinstance (length , int ) or length <= 0 :
10+ raise ValueError ("Length must be a positive integer." )
11+ characters = string .ascii_letters + string .digits
12+ return '' .join (random .choice (characters ) for i in range (length ))
13+
14+ class StopReasons :
15+ ALL_INSTRUCTIONS_ADHERED = "all_instructions_adhered"
16+ MAX_ITERATIONS_REACHED = "max_iterations_reached"
17+ CONTINUE = "instructions_failed_continue_reprompting"
18+ CONTINUE_TOXICITY = "toxicity_detect_continue_reprompting"
19+
20+ ## limits
21+ LATENCY_LIMIT_EXCEEDED = "latency_limit_exceeded"
22+
23+ ##errors
24+ REPROMPTING_FAILED = "reprompting_failed"
25+ UNKNOWN_ERROR = "unknown_error"
26+
27+ @dataclass
28+ class RepromptingConfig :
29+ """
30+ Configuration for the automated re-prompting pipeline.
31+
32+ Attributes:
33+ publish (bool): Whether to publish results to app.aimon.ai.
34+ max_iterations (int): Maximum number of re-prompting iterations (1 initial + N retries).
35+ aimon_api_key (Optional[str]): API key for AIMon integration. Defaults to "AIMON_API_KEY" env var.
36+ model_name (Optional[str]): Model identifier for telemetry. Defaults to "aimon-react-model-{rand}".
37+ application_name (Optional[str]): Application identifier for telemetry. Defaults to "aimon-react-application-{rand}".
38+ return_telemetry (bool): Whether to include per-iteration telemetry in the response.
39+ return_aimon_summary (bool): Whether to include a human-readable caption summarizing re-prompting. (e.g.: 2 iterations, 0 failed instructions)
40+ latency_limit_ms (Optional[int]): Maximum cumulative latency (ms) before aborting. None = no limit.
41+ user_model_max_retries (Optional[int]): Max retries for user model calls. Defaults to 2.
42+ feedback_model_max_retries (Optional[int]): Max retries for feedback model calls. Defaults to 2.
43+ """
44+ publish : bool = False
45+ max_iterations : int = 2
46+ if max_iterations < 1 :
47+ raise ValueError ("Max iterations must be greater than 0" )
48+ aimon_api_key : Optional [str ] = os .getenv ("AIMON_API_KEY" ) or "default_api_key"
49+ if aimon_api_key == "default_api_key" :
50+ raise ValueError ("AIMON_API_KEY environment variable is not set and no fallback value is provided." )
51+ model_name : Optional [str ] = "aimon-react-model-" + generate_random_string (5 )
52+ application_name : Optional [str ] = "aimon-react-application-" + generate_random_string (5 )
53+ return_telemetry : bool = False
54+ return_aimon_summary : bool = False
55+ latency_limit_ms : Optional [int ] = None
56+ user_model_max_retries : Optional [int ] = 2
57+ feedback_model_max_retries : Optional [int ] = 2
58+
59+
0 commit comments