11use rullm_core:: {
22 ChatRequestBuilder , ConfigBuilder , LlmServiceBuilder , MiddlewareConfig , OpenAIProvider ,
3- RateLimit , config :: RetryPolicy ,
3+ RateLimit ,
44} ;
55use std:: time:: Duration ;
66
@@ -14,16 +14,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1414 // Example 1: Basic middleware stack with defaults
1515 basic_middleware_example ( ) . await ?;
1616
17- // Example 2: Custom retry policy with exponential backoff
18- custom_retry_example ( ) . await ?;
19-
20- // Example 3: Production-ready configuration
17+ // Example 2: Configuration with timeouts and rate limiting
2118 production_config_example ( ) . await ?;
2219
23- // Example 4 : Rate-limited and monitored configuration
20+ // Example 3 : Rate-limited and monitored configuration
2421 rate_limited_example ( ) . await ?;
2522
26- // Example 5 : Custom middleware configuration
23+ // Example 4 : Custom middleware configuration
2724 custom_middleware_config_example ( ) . await ?;
2825
2926 Ok ( ( ) )
@@ -57,91 +54,47 @@ async fn basic_middleware_example() -> Result<(), Box<dyn std::error::Error>> {
5754 Ok ( ( ) )
5855}
5956
60- /// Example 2: Custom retry policy with exponential backoff
61- async fn custom_retry_example ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
62- println ! ( "🔄 Example 2: Custom Retry Policy" ) ;
63-
64- let config = ConfigBuilder :: openai_from_env ( ) ?;
65- let provider = OpenAIProvider :: new ( config) ?;
66-
67- // Create middleware with custom exponential backoff retry policy
68- let mut middleware_stack = LlmServiceBuilder :: new ( )
69- . timeout ( Duration :: from_secs ( 60 ) ) // 60 second timeout
70- . retry ( RetryPolicy :: ExponentialBackoff {
71- initial_delay_ms : 200 , // Start with 200ms
72- max_delay_ms : 10000 , // Cap at 10 seconds
73- multiplier : 2.5 , // Aggressive backoff
74- jitter : true , // Add randomness
75- } )
76- . logging ( )
77- . build ( provider, "gpt-3.5-turbo" . to_string ( ) ) ;
78-
79- let request = ChatRequestBuilder :: new ( )
80- . user ( "Explain quantum computing in simple terms" )
81- . temperature ( 0.7 )
82- . max_tokens ( 150 )
83- . build ( ) ;
84-
85- let response = middleware_stack. call ( request) . await ?;
86-
87- println ! ( "✅ Response: {}" , response. message. content) ;
88- println ! ( "🔄 Retry policy: Exponential backoff with jitter\n " ) ;
89-
90- Ok ( ( ) )
91- }
92-
93- /// Example 3: Production-ready configuration
57+ /// Example 2: Configuration with timeouts and rate limiting
9458async fn production_config_example ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
95- println ! ( "🏭 Example 3: Production Configuration" ) ;
59+ println ! ( "🏭 Example 2: Configuration with Timeouts and Rate Limiting " ) ;
9660
9761 let config = ConfigBuilder :: openai_from_env ( ) ?;
9862 let provider = OpenAIProvider :: new ( config) ?;
9963
100- // Production-ready middleware configuration
64+ // Middleware configuration with timeouts and rate limiting
10165 let mut middleware_stack = LlmServiceBuilder :: new ( )
10266 . timeout ( Duration :: from_secs ( 30 ) ) // Conservative timeout
103- . retry ( RetryPolicy :: ApiGuided {
104- fallback : Box :: new ( RetryPolicy :: ExponentialBackoff {
105- initial_delay_ms : 100 ,
106- max_delay_ms : 5000 ,
107- multiplier : 2.0 ,
108- jitter : true ,
109- } ) ,
110- max_api_delay_ms : 30000 , // Don't wait more than 30 seconds
111- retry_headers : vec ! [ "retry-after" . to_string( ) , "x-ratelimit-reset" . to_string( ) ] ,
112- } )
11367 . rate_limit ( 100 , Duration :: from_secs ( 60 ) ) // 100 requests per minute
114- . logging ( ) // Always log in production
115- . metrics ( ) // Always collect metrics
68+ . logging ( )
69+ . metrics ( )
11670 . build ( provider, "gpt-4" . to_string ( ) ) ;
11771
11872 let request = ChatRequestBuilder :: new ( )
11973 . system ( "You are a helpful assistant for a production application." )
12074 . user ( "How can I optimize my database queries?" )
121- . temperature ( 0.3 ) // More deterministic for production
75+ . temperature ( 0.3 ) // Lower temperature for more deterministic output
12276 . max_tokens ( 300 )
12377 . build ( ) ;
12478
12579 let response = middleware_stack. call ( request) . await ?;
12680
127- println ! ( "✅ Production response received" ) ;
81+ println ! ( "✅ Response received" ) ;
12882 println ! ( "📊 Token usage: {}" , response. usage. total_tokens) ;
129- println ! ( "🛡️ Configuration: API-guided retry, rate limited, fully monitored\n " ) ;
83+ println ! ( "🛡️ Configuration: Rate limited, logged and monitored\n " ) ;
13084
13185 Ok ( ( ) )
13286}
13387
134- /// Example 4 : Rate-limited and monitored configuration
88+ /// Example 3 : Rate-limited and monitored configuration
13589async fn rate_limited_example ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
136- println ! ( "⏱️ Example 4 : Rate Limited Configuration" ) ;
90+ println ! ( "⏱️ Example 3 : Rate Limited Configuration" ) ;
13791
13892 let config = ConfigBuilder :: openai_from_env ( ) ?;
13993 let provider = OpenAIProvider :: new ( config) ?;
14094
14195 // Configuration optimized for rate limiting and monitoring
14296 let mut middleware_stack = LlmServiceBuilder :: new ( )
14397 . timeout ( Duration :: from_secs ( 45 ) )
144- . retry ( RetryPolicy :: Fixed { delay_ms : 1000 } ) // Simple fixed delay
14598 . rate_limit ( 50 , Duration :: from_secs ( 60 ) ) // Conservative rate limit
14699 . logging ( )
147100 . metrics ( )
@@ -184,22 +137,16 @@ async fn rate_limited_example() -> Result<(), Box<dyn std::error::Error>> {
184137 Ok ( ( ) )
185138}
186139
187- /// Example 5 : Custom middleware configuration from struct
140+ /// Example 4 : Custom middleware configuration from struct
188141async fn custom_middleware_config_example ( ) -> Result < ( ) , Box < dyn std:: error:: Error > > {
189- println ! ( "⚙️ Example 5 : Custom Middleware Configuration" ) ;
142+ println ! ( "⚙️ Example 4 : Custom Middleware Configuration" ) ;
190143
191144 let config = ConfigBuilder :: openai_from_env ( ) ?;
192145 let provider = OpenAIProvider :: new ( config) ?;
193146
194147 // Define custom middleware configuration
195148 let middleware_config = MiddlewareConfig {
196149 timeout : Some ( Duration :: from_secs ( 20 ) ) ,
197- retry_policy : Some ( RetryPolicy :: ExponentialBackoff {
198- initial_delay_ms : 500 ,
199- max_delay_ms : 8000 ,
200- multiplier : 1.8 ,
201- jitter : false ,
202- } ) ,
203150 rate_limit : Some ( RateLimit {
204151 requests_per_period : 25 ,
205152 period : Duration :: from_secs ( 60 ) ,
@@ -225,9 +172,7 @@ async fn custom_middleware_config_example() -> Result<(), Box<dyn std::error::Er
225172 "📊 Response length: {} characters" ,
226173 response. message. content. len( )
227174 ) ;
228- println ! (
229- "⚙️ Configuration: Custom timeouts, exponential backoff (no jitter), 25 req/min limit\n "
230- ) ;
175+ println ! ( "⚙️ Configuration: Custom timeouts, 25 req/min limit\n " ) ;
231176
232177 // Display the configuration details
233178 let config = middleware_stack. config ( ) ;
0 commit comments