From 0bf95dc6c31820b41cc87d9bac8a24a054296917 Mon Sep 17 00:00:00 2001 From: Brad Nelson Date: Thu, 14 Nov 2024 23:05:24 -0800 Subject: [PATCH] Fix more things. --- examples/gemini/gemini.fs | 58 ++++++++++++++++++++++----------------- examples/gemini/slurp.fs | 3 +- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/examples/gemini/gemini.fs b/examples/gemini/gemini.fs index 8e9ffb8..bcdec6d 100644 --- a/examples/gemini/gemini.fs +++ b/examples/gemini/gemini.fs @@ -32,37 +32,45 @@ vocabulary gemini also json also arrays also gemini definitions }} ; -r| What's the "time"?| askit >json a. cr cr -r| What's the "time"?| askit _s" contents" dict@ 0 a@ _s" parts" dict@ 0 a@ _s" text" dict@ >json a. cr +: reply-text ( a: a -- a ) + json> _s" candidates" dict@ 0 a@ _s" content" dict@ _s" parts" dict@ 0 a@ _s" text" dict@ +; -0 [IF] -_s" sample.txt" slurp json> _s" candidates" dict@ 0 a@ _s" content" dict@ _s" parts" dict@ 0 a@ _s" text" dict@ a. +: 0,c ( a -- a\0 ) 0 _c ,c ; -HTTPClient +DEFINED? HTTPClient [IF] + +_s" /spiffs/gemini_cert" slurp 0,c aconstant cacert + +_s" https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=" + _s" /spiffs/gemini_key" slurp json> _s" key" dict@ ,c 0,c aconstant url + +also HTTPClient NetworkClientSecure.new constant nclient +cacert top adrop nclient NetworkClientSecure.setCACert -: 2constant create , , does> dup cell+ @ swap @ ; +: doquery ( a -- n ) + HTTPClient.new { session } + url top adrop nclient session HTTPClient.beginNC 0= throw + 1 session HTTPClient.setFollowRedirects + 10 session HTTPClient.setRedirectLimit + 0 session HTTPClient.setReuse + z" POST" top session HTTPClient.sendRequest adrop 200 <> throw + session HTTPClient.getStreamPtr { result } + begin result NetworkClient.available while + pad 1024 result NetworkClient.available min result NetworkClient.readBytes + pad swap type + repeat + session HTTPClient.delete + cr +; -s" /spiffs/gemini_cert" slurp-file drop constant cacert -s" /spiffs/gemini_url" slurp-file drop constant url -s" /spiffs/question" slurp-file 2constant question +: ask + nl parse askit >json doquery + top 3 + top >count @ 3 - _s anip reply-text a. cr +; -cacert nclient NetworkClientSecure.setCACert -." loaded cert:" cr -cacert z>s type cr - -HTTPClient.new constant session -." created session" cr -." URL: " url z>s type cr -url nclient session HTTPClient.beginNC ." beginNC: " . cr -1 session HTTPClient.setFollowRedirects -10 session HTTPClient.setRedirectLimit -." set follow redirects and limit of 10" cr -." question: " question type -z" POST" question session HTTPClient.sendRequest ." POSTED: " . cr - -session HTTPClient.getStreamPtr constant result -result NetworkClient.available ." available: " dup . cr +previous [THEN] diff --git a/examples/gemini/slurp.fs b/examples/gemini/slurp.fs index 0a1db35..c4ccffb 100644 --- a/examples/gemini/slurp.fs +++ b/examples/gemini/slurp.fs @@ -18,8 +18,7 @@ also arrays : slurp ( a: filename -- a: content ) top range r/o open-file throw >r adrop - r@ file-size throw ( sz ) - STRING array + r@ file-size throw STRING array top range r@ read-file throw drop r> close-file throw ;