@@ -11,16 +11,14 @@ use ollama_rs::{
11
11
#[ derive( Parser , Debug ) ]
12
12
#[ command( author, version, about, long_about = None ) ]
13
13
struct Args {
14
- /// Maximum value (exclusive) for random number
15
- #[ arg( default_value = "10" ) ]
16
- max : Option < u128 > ,
14
+ /// First argument: either max (if only one provided) or min (if two provided)
15
+ first : Option < u128 > ,
17
16
18
- /// Minimum value (inclusive) for random number
19
- min : Option < u128 > ,
17
+ /// Second argument (optional): max value when min is provided
18
+ second : Option < u128 > ,
20
19
}
21
20
22
21
async fn get_random_number ( min : u128 , max : u128 ) -> Result < u128 > {
23
- // Initialize Ollama client
24
22
let ollama = Ollama :: default ( ) ;
25
23
26
24
let prompt = format ! (
@@ -29,25 +27,19 @@ async fn get_random_number(min: u128, max: u128) -> Result<u128> {
29
27
min, max
30
28
) ;
31
29
32
- // Create generation request with specific options to reduce randomness
33
- let options = GenerationOptions :: default ( )
34
- . temperature ( 0.9 ) // High temperature for more randomness
35
- . top_p ( 0.9 ) // Allow more variety in responses
36
- . top_k ( 40 ) ; // Consider more tokens for more randomness
30
+ let options = GenerationOptions :: default ( ) . temperature ( 0.9 ) . top_p ( 0.9 ) . top_k ( 40 ) ;
37
31
38
32
let request = GenerationRequest :: new ( "mistral" . to_string ( ) , prompt) . options ( options) ;
39
33
40
34
let response =
41
35
ollama. generate ( request) . await . context ( "Failed to generate response from Ollama" ) ?;
42
36
43
- // Parse the response string into a number
44
37
let num = response
45
38
. response
46
39
. trim ( )
47
40
. parse :: < u128 > ( )
48
41
. context ( "Failed to parse number from Ollama response" ) ?;
49
42
50
- // Ensure the number is within bounds
51
43
if num < min || num > max {
52
44
anyhow:: bail!( "Generated number {} is out of bounds [{}, {}]" , num, min, max) ;
53
45
}
@@ -59,18 +51,21 @@ async fn get_random_number(min: u128, max: u128) -> Result<u128> {
59
51
async fn main ( ) {
60
52
let args = Args :: parse ( ) ;
61
53
62
- let ( min, max) = match ( args. min , args. max ) {
63
- ( None , None ) => ( 0 , 10 ) ,
64
- ( None , Some ( max) ) => ( 0 , max) ,
65
- ( Some ( min) , Some ( max) ) => ( min, max) ,
66
- ( Some ( _) , None ) => {
67
- eprintln ! ( "Error: If min is specified, max must also be specified" ) ;
54
+ let ( min, max) = match ( args. first , args. second ) {
55
+ ( None , None ) => ( 0 , 10 ) , // raindom
56
+ ( Some ( max) , None ) => ( 0 , max) , // raindom [max]
57
+ ( Some ( min) , Some ( max) ) => ( min, max) , // raindom [min] [max]
58
+ ( None , Some ( _) ) => {
59
+ eprintln ! ( "Invalid argument pattern. Usage:" ) ;
60
+ eprintln ! ( " raindom" ) ;
61
+ eprintln ! ( " raindom [max]" ) ;
62
+ eprintln ! ( " raindom [min] [max]" ) ;
68
63
process:: exit ( 1 ) ;
69
64
} ,
70
65
} ;
71
66
72
67
if min >= max {
73
- eprintln ! ( "Error: min must be less than max" ) ;
68
+ eprintln ! ( "Error: min ({}) must be less than max ({})" , min , max ) ;
74
69
process:: exit ( 1 ) ;
75
70
}
76
71
0 commit comments