Skip to content

Commit 8fa1e21

Browse files
committed
bugfix min max
1 parent 2e058c6 commit 8fa1e21

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/main.rs

+15-20
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ use ollama_rs::{
1111
#[derive(Parser, Debug)]
1212
#[command(author, version, about, long_about = None)]
1313
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>,
1716

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>,
2019
}
2120

2221
async fn get_random_number(min: u128, max: u128) -> Result<u128> {
23-
// Initialize Ollama client
2422
let ollama = Ollama::default();
2523

2624
let prompt = format!(
@@ -29,25 +27,19 @@ async fn get_random_number(min: u128, max: u128) -> Result<u128> {
2927
min, max
3028
);
3129

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);
3731

3832
let request = GenerationRequest::new("mistral".to_string(), prompt).options(options);
3933

4034
let response =
4135
ollama.generate(request).await.context("Failed to generate response from Ollama")?;
4236

43-
// Parse the response string into a number
4437
let num = response
4538
.response
4639
.trim()
4740
.parse::<u128>()
4841
.context("Failed to parse number from Ollama response")?;
4942

50-
// Ensure the number is within bounds
5143
if num < min || num > max {
5244
anyhow::bail!("Generated number {} is out of bounds [{}, {}]", num, min, max);
5345
}
@@ -59,18 +51,21 @@ async fn get_random_number(min: u128, max: u128) -> Result<u128> {
5951
async fn main() {
6052
let args = Args::parse();
6153

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]");
6863
process::exit(1);
6964
},
7065
};
7166

7267
if min >= max {
73-
eprintln!("Error: min must be less than max");
68+
eprintln!("Error: min ({}) must be less than max ({})", min, max);
7469
process::exit(1);
7570
}
7671

0 commit comments

Comments
 (0)