Skip to content

Commit 5d26c9c

Browse files
committed
fix: Handle empty, null, and missing responses from Google Gemini
- Handle cases where the Google Gemini API returns an empty, null, or no response. - Improve error handling and provide more informative error messages. - Update the `google_ai` command to use the `get-input` command for handling user input, similar to the `gcal add` command. - Add a new command `math sample` to generate a random sample from a given set, with optional parameters for sample size, replacement, and probabilities. - Improve the `gcal ai` command to handle cases where the title of a calendar event is written in a naming convention. - Update the command to rewrite the title in normal writing using the `google_ai` command.
1 parent 91da607 commit 5d26c9c

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

ai_tools.nu

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,19 +1673,19 @@ export def google_ai [
16731673
while ($retry_counter <= $max_retries) and $error {
16741674
if $verbose {print ($"attempt #($retry_counter)...")}
16751675
try {
1676-
$answer = (http post -t application/json $url_request $request --allow-errors)
1676+
$answer = http post -t application/json $url_request $request --allow-errors
16771677
$error = false
16781678
}
16791679
$retry_counter = $retry_counter + 1
16801680
sleep 1sec
16811681
}
16821682

1683-
if ($answer | is-empty) {
1683+
if ($answer | is-empty) or ($answer == null) {
16841684
try {
1685-
$answer = (http post -t application/json $url_request $request --allow-errors)
1685+
$answer = http post -t application/json $url_request $request --allow-errors
16861686
}
16871687

1688-
if ($answer | is-empty) and ($model == "gemini-1.5-pro") {
1688+
if (($answer | is-empty) or ($answer == null)) and ($model == "gemini-1.5-pro") {
16891689
let model = "gemini-1.5-flash"
16901690
let url_request = {
16911691
scheme: "https",
@@ -1696,10 +1696,14 @@ export def google_ai [
16961696
}
16971697
} | url join
16981698

1699-
$answer = (http post -t application/json $url_request $request --allow-errors)
1699+
$answer = http post -t application/json $url_request $request --allow-errors
17001700
}
17011701
}
17021702

1703+
if ($answer | is-empty) or ($answer == null) {
1704+
return-error "something went wrong with the server!"
1705+
}
1706+
17031707
if ($model =~ "gemini") {
17041708
return $answer.candidates.content.parts.0.text.0
17051709
} else if ($model =~ "bison") {
@@ -1779,9 +1783,9 @@ export def "gcal ai" [
17791783
--gpt4(-g) #uses gpt-4o
17801784
--gemini(-G) #uses gemini
17811785
] {
1782-
let request = if ($request | is-empty) {$in} else {$request | str join}
1786+
let request = get-input $in $request | str join
17831787
let date_now = date now | format date "%Y.%m.%d"
1784-
let prompt = $request + ".\nPlease consider that today's date is " + $date_now
1788+
let prompt = $request + ".\nPor favor considerar que la fecha de hoy es " + $date_now
17851789

17861790
#get data to make query to gcal
17871791
let gcal_query = (
@@ -1832,7 +1836,7 @@ export def "gcal ai" [
18321836
let where = $gcal_query | get where
18331837
let duration = $gcal_query | get duration
18341838

1835-
let title = google_ai ("if the next text is using a naming convention, rewrite it in normal writing in the original language, i.e., separate words by a space. Only return your response without any commentary on your part, in plain text without any formatting. The text: " + ($gcal_query | get title ))
1839+
let title = google_ai ("if the next text is using a naming convention, rewrite it in normal writing in the original language, i.e., separate words by a space. Only return your response without any commentary on your part, in plain text without any formatting. The text: " + ($gcal_query | get title )) | str trim
18361840

18371841
gcal add $calendar $title $when $where $duration
18381842
},

gcal.nu

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ export def "gcal add" [
2121
where? #location
2222
duration? #duration in minutes
2323
] {
24-
let calendar = get-input (gcal list -r | sort | input list -f (echo-g "Select calendar: ")) $calendar
24+
let calendar = if ($calendar | is-empty) {
25+
gcal list -r | sort | input list -f (echo-g "Select calendar: ")
26+
} else {
27+
$calendar
28+
}
2529

26-
let title = get-input (input (echo-g "title: ")) $title
27-
let when = get-input (input (echo-g "when: ")) $when
28-
let where = get-input (input (echo-g "where: ")) $where
29-
let duration = get-input (input (echo-g "duration: ")) $duration
30+
let title = if ($title | is-empty) {input (echo-g "title: ")} else {$title}
31+
let when = if ($when | is-empty) {input (echo-g "when: ")} else {$when}
32+
let where = if ($where | is-empty) {input (echo-g "where: ")} else {$where}
33+
let duration = if ($duration | is-empty) {input (echo-g "duration: ")} else {$duration}
3034

3135
gcalcli --calendar $"($calendar)" add --title $"($title)" --when $"($when)" --where $"($where)" --duration $"($duration)" --default-reminders
3236
}

jdown.py

100755100644
File mode changed.

maths.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ export def "math sample" [
494494
sample_size:int #size of the sample
495495
set? #set to take the sample from
496496
--replacement(-r) #sample with replacement
497-
--probabilities(-p):list<float> #probability of each element in the set
497+
--probabilities(-p):list #probability of each element in the set
498498
] {
499499
let set = get-input $in $set
500500
let len = $set | length

um.nu

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env nu
2+
3+
export def main [] {
4+
let mounted = sys disks | get mount | find rclone | ansi strip
5+
6+
if ($mounted | length) == 0 {
7+
return "no mounted storages!"
8+
}
9+
10+
$mounted
11+
| each {|drive|
12+
fusermount -u $drive
13+
sleep 1sec
14+
}
15+
}

0 commit comments

Comments
 (0)