login
admin
back
chat gpt query
ask the ai a question and save the result in your namespace.
requires having an openai token.
source code
paste this code into a hawk file
protocol: hawk-500
!: :: /text/hawk-500 :: :: ==== chatGPT Query ================ :: :: STEP 0: :: place this file in your hawk namespace :: (probably at /~/config/templates/chat-gpt) :: :: STEP 1: :: you must enter your openAPI secret :: =/ api-key "your-key-here" :: :: STEP 2 (optionally): :: call this code from another page :: :: [%twin /path/to/this/file] :: |^ ?: =((pib:c /) "query") [%shed run-query] [%manx (print parse)] :: :: +$ state $: system-prompt=tape user-prompt=tape result=(unit tape) error=(unit tape) == ++ print |= state ^- manx ;div.prose.page.fc.g3 ;+ disclaimer ;+ (form-prompt system-prompt user-prompt) ;+ (part-result result error) == :: ++ parse ^- state =, mq :^ (zing (text-content (get-id "system" dat:f))) (zing (text-content (get-id "user" dat:f))) =/ output (zing (text-content (get-id "output" dat:f))) ?: =(output "") ~ `output =/ error (zing (text-content (get-id "error" dat:f))) ?: =(error "") ~ `error :: ++ form-prompt |= [system=tape user=tape] ^- manx ;form.fc.g3.p2.br1.bd1.bc0(method "post") =hx-target "#result" =hx-select "#result" ;input.hidden(name "/", value "query"); ;label.fc ;span.p1.s-2.f4: system prompt ;textarea#system.br1.bd1.p2 =placeholder "be concise." =autocomplete "off" =rows "4" =name "/system" ;* ~[;/(system)] == == ;label.fc ;span.p1.s-2.f4: user prompt ;textarea#user.br1.bd1.p2 =placeholder "why practice?" =autocomplete "off" =name "/user" =required "" =rows "5" ;* ~[;/(user)] == == ;button.loader.p-2.br1.bd1.bc0.b2.hover.bold.mono ;span.loaded: GO ;span.loading: == == == ++ part-result |= [result=(unit tape) error=(unit tape)] ;div#result.fc.g3 ;+ refresher ;+ ?~ error ;/("") ;div#error.br1.bd2.bc-1.p3.f-1.scroll-x: {u.error} ;+ ?~ result ;div.p8.f4.tc.s1: no result yet ;div#output.s1.pre-line: {u.result} == ++ disclaimer ;p.p-1.o5.s-1(style "margin: 0;") ; results will apear after >5 seconds == :: ++ refresher ;div.hidden =hx-get "?data" =hx-target "#result" =hx-select "#result" =hx-swap "outerHTML" =hx-trigger "load delay:3s" ;div.loader.mono.tc.wfc.pre.hidden ;span.loaded: ;div.loading: loading == == :: ++ run-query =/ m (strand ,vase) :: =/ system %- crip (pub:c /system tas/'be concise.') :: =/ user %- crip (pub:c /user tas/'why practice?') =/ url 'https://api.openai.com/v1/chat/completions' =/ headers :~ ['Authorization' (crip "Bearer {api-key}")] ['Content-Type' 'application/json'] == =/ body=(unit octs) :- ~ %- as-octs:mimes:html %- en:json:html :- %o %- malt %- limo :~ ['model' s+'gpt-4'] :- 'messages' :- %a ^- (list json) :~ o+(malt (limo ~[['role' s+'system'] ['content' s+system]])) o+(malt (limo ~[['role' s+'user'] ['content' s+user]])) == == ;< ~ bind:m (send-request %'POST' url headers body) ;< res=client-response:iris bind:m take-client-response %- pure:m !> ?+ -.res !! %finished =/ status status-code.response-header.res =/ color ?: (gte status 500) "f-1" ?: (gte status 400) "f-2" ?: (gte status 300) "f-4" "f-3" =/ headers headers.response-header.res (print (trip system) (trip user) (print-completion full-file.res)) == ++ print-completion |= file=(unit [type=@t data=octs]) ^- [(unit tape) (unit tape)] ?~ file [~ `"no file"] ?@ (find "application/json" (trip type.u.file)) [`(trip type.u.file) ~] =/ jon (need (de:json:html +.data.u.file)) =/ hon =, dejs:format %- ou :~ :- %choices %- un %- ar %- ou :~ :- %message %- un %- ou :~ content+(un so) == == == =/ response=(unit (list cord)) (mole |.((hon jon))) ?~ response [~ `(trip +.data.u.file)] [`(trip (of-wain:format u.response)) ~] --