So my immediate block right now in building out the rest api is having no clue where to put a json body into post request. Their is zero documentation that I can find anywhere in the standard library, and the source code doesn’t give much clues either.
I mean, prolog can be an awesome, practical scripting language on-par with python if we want it to. Where is that edge? Let’s push ourselves to it!
On one side, I just want to solve my problem. It shouldn’t have taken an entire afternoon without any success. I may not be the most savvy http-slinging protocol wonk, but I’ve made enough req-res calls in other languages to know that it shouldn’t be this opaque. So I open this up as a challenge. Help me help us write-up an awesome (series) of tutorial that shows others how this language we love so much, can be used for practical things, while also being the most declarative kid on the block. That’s the real nub.
Now for the practical side that I’m stuck on:
The simple request call works just fine. I had this going in no time. But it’s not very interesting to look at a list of models. I’m caching them so that we don’t make repeat calls. I wonder what folks think of this technique?
:- dynamic model_data/1.
models(Models) :-
(call(model_data(Models)) % if model_data is in database
-> model_data(Models) % bind to it
; % otherwise make a request call
base(Base), headers(Headers), api_version(V),
URL = '~w/openai/models?~w' $ [Base, V],
http_open(URL, In, Headers),
json_read_dict(In, Res),
Models = Res.data,
assertz(model_data(Models)) % and store in local db
).
But the real issue is sending post request for the JSON object. This is the approach that has gotten me the furthest so far… I’m getting a 411 error code back, “Length Required”.
completion(Prompt, Model, Response) :-
base(Base), req_headers(Headers), api_version(V),
URL = '~w/openai/deployments/~w/completions?~w' $ [Base, Model, V],
Data = json([prompt=Prompt]),
atom_json_term(A, Data, []),
http_open(URL, Out, [method(post)
| Headers]),
json_write(Out, Data),
close(Out),
http_read_data(Response,_,[json_object(dict)]).
Oh, by the way, don’t mind the $
above… it’s just a functional composition metapredicate from Mndrix’ awesome little library(func) that I use to do atomic concatenation more succinctly.