1:- module(prolog2gpt,[
2 init_gptkey/0,
3 gpt_models/1, gpt_models/2,
4 gpt_models_detail/2,
5 gpt_extract_data/4,
6 gpt_extract_fields/3,
7 gpt_extract_field_pairs/4,
8 gpt_completions/4, gpt_completions/5,
9 gpt_edits/4, gpt_edits/5,
10 gpt_images_create/3, gpt_images_create/4,
11 gpt_images_edits/4, gpt_images_edits/5,
12 gpt_images_variations/3, gpt_images_variations/4,
13 gpt_embeddings/4, gpt_embeddings/5,
14 gpt_files/1, gpt_files/2,
15 gpt_files_upload/4, gpt_files_upload/5,
16 gpt_files_delete/2, gpt_files_delete/3,
17 gpt_files_retrieve/2, gpt_files_retrieve/3,
18 gpt_files_retrieve_content/2, gpt_files_retrieve_content/3,
19 gpt_fine_tunes/1,gpt_fine_tunes/2,gpt_fine_tunes/3, gpt_fine_tunes/4,
20 gpt_fine_tunes_detail/2, gpt_fine_tunes_detail/3,
21 gpt_fine_tunes_cancel/2, gpt_fine_tunes_cancel/3,
22 gpt_fine_tunes_events/2, gpt_fine_tunes_events/3,
23 gpt_fine_tunes_delete/2, gpt_fine_tunes_delete/3,
24 gpt_moderations/3, gpt_moderations/4
25]).
56:- use_module(library(http/http_open)). 57:- use_module(library(http/http_client)). 58:- use_module(library(http/http_ssl_plugin)). 59:- use_module(library(http/json)). 60:- use_module(library(http/http_json)). 61:- use_module(library(http/json_convert)).
gptkey
) with the key value. Note: execute this predicate
before using any of the others, because the gpt key is needed for authorization
with each gpt api call.
Example use:
:- init_gptkey, current_prolog_flag(gptkey,Key), writeln(Key). Key = sk-manycharactersoftheacturalkeyvalue
75init_gptkey:-
76 getenv('GPTKEY',Key),
77 create_prolog_flag(gptkey,Key,[type(atom)]).
Example use:
:- gpt_models(Models). Models = [babbage,davinci,...]
92gpt_models(Models):- gpt_models(Models,false). 93gpt_models(Models,Raw):- 94 current_prolog_flag(gptkey,Key), 95 http_get('https://api.openai.com/v1/models',Ms, 96 [authorization(bearer(Key)),application/json]), 97 ( Raw=false 98 -> gpt_extract_data(data,id,Ms,Models) 99 ; Models=Ms 100 ).
Example use:
:- gpt_models('text-davinci-003',Details). Details = ... % the JSON term
117gpt_models_detail(Model,Details):-
118 current_prolog_flag(gptkey,Key),
119 atomic_concat('https://api.openai.com/v1/models/',Model,URL),
120 http_get(URL,Details,[authorization(bearer(Key)),application/json]).
Example use:
:- gpt_models(Ms,true), gpt_extract_data(data,id,Ms,Models). Models = ['babbage','text-davinci-001',...]
137gpt_extract_data(Group,Fieldname,json(Data),Result):-
138 member(Group=Fieldlist,Data),
139 gpt_extract_fields(Fieldname,Fieldlist,Result).
Example use:
:- Data=[json([id='babbage',object='model']),json([id='text-davinci-001',object='model'])], gpt_extract_data(data,id,Data,Models). Models = ['babbage','text-davinci-001']
154gpt_extract_fields(_,[],[]):-!. 155gpt_extract_fields(Fieldname,[json(Fields)|Fs],Results):- 156 ( member(Fieldname=R,Fields) 157 -> Results=[R|Res] 158 ; Results=Res 159 ), 160 gpt_extract_fields(Fieldname,Fs,Res).
Example use:
:- Data=[json([id='123',filename=file1]),json([id='345',filename=file2])], gpt_extract_field_pairs(filename,id,Data,FieldPairs). FieldPairs = [file1-'123',file2-'345']
175gpt_extract_field_pairs(_,_,[],[]):-!. 176gpt_extract_field_pairs(Field1,Field2,[json(Fields)|Fs],Results):- 177 ( member(Field1=F1,Fields) 178 -> ( member(Field2=F2,Fields) 179 -> Results = [F1-F2|Res] 180 ; Results = Res 181 ) 182 ; Results = Res 183 ),!, 184 gpt_extract_field_pairs(Field1,Field2,Fs,Res).
Example use:
:- gpt_completions('text-davinci-003','My favourite animal is ',Result,_,[]), Result = ['a dog']
284gpt_completions(Model,Prompt,Result,Options):- 285 gpt_completions(Model,Prompt,Result,false,Options),!. 286 287gpt_completions(Model,Prompt,Result,Raw,Options):- 288 current_prolog_flag(gptkey,Key), 289 atom_json_term(D,json([model=Model,prompt=Prompt|Options]),[]), 290 Data = atom(application/json,D), 291 http_post('https://api.openai.com/v1/completions',Data,ReturnData, 292 [authorization(bearer(Key)),application/json]), 293 ( Raw=false 294 -> gpt_extract_data(choices,text,ReturnData,Result) 295 ; Result= ReturnData 296 ).
Example use:
:- gpt_edit('text-davinci-001','Fix the spelling mistakes',Result,_, [ input='What day of the wek is it?' ]), Result = 'What day of the week is it?'
343gpt_edits(Model,Instruction,Result,Options):- 344 gpt_edits(Model,Instruction,Result,false,Options),!. 345 346gpt_edits(Model,Instruction,Result,Raw,Options):- 347 current_prolog_flag(gptkey,Key), 348 atom_json_term(D,json([model=Model,instruction=Instruction|Options]),[]), 349 Data = atom(application/json,D), 350 http_post('https://api.openai.com/v1/edits',Data,ReturnData, 351 [authorization(bearer(Key)),application/json]), 352 ( Raw=false 353 -> gpt_extract_data(choices,text,ReturnData,Result) 354 ; Result= ReturnData 355 ).
Example use:
:- gpt_images_create('A cute baby sea otter',Result,_,[]), Result = ['https://...'] % url of the resulting image
385gpt_images_create(Prompt,Result,Options):- 386 gpt_images_create(Prompt,Result,false,Options). 387gpt_images_create(Prompt,Result,Raw,Options):- 388 current_prolog_flag(gptkey,Key), 389 atom_json_term(D,json([prompt=Prompt|Options]),[]), 390 Data = atom(application/json,D), 391 http_post('https://api.openai.com/v1/images/generations',Data,ReturnData, 392 [authorization(bearer(Key)),application/json]), 393 ( member(response_format=Format,Options) -> true ; Format=url ), 394 ( Raw=false 395 -> gpt_extract_data(data,Format,ReturnData,Result) 396 ; Result= ReturnData 397 ).
Example use:
:- gpt_images_edits('A cute baby sea otter with a hat','./test/otter.png',Result,_,[]), Result = ['https://...'] % url of the resulting image
432gpt_images_edits(Prompt,Image,Result,Options):- 433 gpt_images_edits(Prompt,Image,Result,false,Options). 434gpt_images_edits(Prompt,Image,Result,Raw,Options):- 435 current_prolog_flag(gptkey,Key), 436 Data = form_data([prompt=Prompt,image=file(Image)|Options]), 437 http_post('https://api.openai.com/v1/images/edits',Data,ReturnData, 438 [authorization(bearer(Key)),application/json]), 439 ( member(response_format=Format,Options) -> true ; Format=url ), 440 ( Raw=false 441 -> gpt_extract_data(data,Format,ReturnData,Result) 442 ; Result= ReturnData 443 ).
variation(s)
of an image.
Example use:
:- gpt_images_variations('./test/otter.png',Result,_,[]), Result = ['https://...'] % url of the resulting image
473gpt_images_variations(Image,Result,Options):- 474 gpt_images_variations(Image,Result,false,Options). 475 476gpt_images_variations(Image,Result,Raw,Options):- 477 current_prolog_flag(gptkey,Key), 478 Data = form_data([image=file(Image)|Options]), 479 http_post('https://api.openai.com/v1/images/variations',Data,ReturnData, 480 [authorization(bearer(Key)),application/json]), 481 ( member(response_format=Format,Options) -> true ; Format=url ), 482 ( Raw=false 483 -> gpt_extract_data(data,Format,ReturnData,Result) 484 ; Result= ReturnData 485 ).
Example use:
:- gpt_embeddings('text-embedding-ada-002','The food was delicious',Result), Result = [0.0023064255,-0.009327292,...]
505gpt_embeddings(Model,Input,Result,Options):- 506 gpt_embeddings(Model,Input,Result,false,Options),!. 507gpt_embeddings(Model,Input,Result,Raw,Options):- 508 current_prolog_flag(gptkey,Key), 509 atom_json_term(D,json([model=Model,input=Input|Options]),[]), 510 Data = atom(application/json,D), 511 http_post('https://api.openai.com/v1/embeddings',Data,ReturnData, 512 [authorization(bearer(Key)),application/json]), 513 ( Raw=false 514 -> gpt_extract_data(data,embedding,ReturnData,Result) 515 ; Result= ReturnData 516 ).
Example use:
:- gpt_files(Result), Result = ['file1.jsonl'-'file-12345','file2.jsonl'-'file-56789']
532gpt_files(Result):- 533 gpt_files(Result,false). 534gpt_files(Result,Raw):- 535 current_prolog_flag(gptkey,Key), 536 http_get('https://api.openai.com/v1/files',json(ReturnData), 537 [authorization(bearer(Key)),application/json]), 538 ( Raw=false 539 -> ( member(data=Files,ReturnData), 540 gpt_extract_field_pairs(filename,id,Files,Result) 541 ) 542 ; Result= json(ReturnData) 543 ).
Example use:
:- gpt_files_upload('./test/tune_answer.jsonl','fine-tune',Result), Result = ['file-XjGxS3KTG0uNmNOK362iJua3']
560gpt_files_upload(File,Purpose,Result,Options):- 561 gpt_files_upload(File,Purpose,Result,false,Options),!. 562gpt_files_upload(File,Purpose,Result,Raw,Options):- 563 current_prolog_flag(gptkey,Key), 564 Data = form_data([file=file(File),purpose=Purpose|Options]), 565 http_post('https://api.openai.com/v1/files',Data,json(ReturnData), 566 [authorization(bearer(Key)),application/json]), 567 ( Raw=false 568 -> (member(id=ID,ReturnData),Result=[ID]) 569 ; Result= json(ReturnData) 570 ).
Example use:
:- gpt_files_delete('file-XjGxS3KTG0uNmNOK362iJua3',Result), Result = ['file-XjGxS3KTG0uNmNOK362iJua3']
587gpt_files_delete(FileID,Result):- 588 gpt_files_delete(FileID,Result,false),!. 589gpt_files_delete(FileID,Result,Raw):- 590 current_prolog_flag(gptkey,Key), 591 atomic_concat('https://api.openai.com/v1/files/',FileID,URL), 592 http_delete(URL,json(ReturnData), 593 [authorization(bearer(Key)),application/json]), 594 ( Raw=false 595 -> (member(id=ID,ReturnData), Result=[ID]) 596 ; Result= json(ReturnData) 597 ).
Example use:
:- gpt_files_retrieve('file-XjGxS3KTG0uNmNOK362iJua3',Result), Result = ['myfile.jsonl']
613gpt_files_retrieve(FileID,Result):- 614 gpt_files_retrieve(FileID,Result,false),!. 615gpt_files_retrieve(FileID,Result,Raw):- 616 current_prolog_flag(gptkey,Key), 617 atomic_concat('https://api.openai.com/v1/files/',FileID,URL), 618 http_get(URL,json(ReturnData), 619 [authorization(bearer(Key)),application/json]), 620 ( Raw=false 621 -> (member(filename=File,ReturnData), Result=[File]) 622 ; Result= json(ReturnData) 623 ).
Example use:
:- gpt_files_retrieve('file-XjGxS3KTG0uNmNOK362iJua3',Result), Result = ['myfile.jsonl']
640gpt_files_retrieve_content(FileID,Result):- 641 gpt_files_retrieve_content(FileID,Result,false),!. 642gpt_files_retrieve_content(FileID,Result,Raw):- 643 current_prolog_flag(gptkey,Key), 644 atomic_list_concat(['https://api.openai.com/v1/files/',FileID,'/content'],URL), 645 http_get(URL,ReturnData, [authorization(bearer(Key))]), 646 ( Raw=false 647 -> (member(filename=File,ReturnData), Result=[File]) 648 ; Result= ReturnData 649 ).
Example use:
:- gpt_fine_tunes('file-XGinujblHPwGLSztz8cPS8XY',Result), Result = ['ft-AF1WoRqd3aJAHsqc9NY7iL8F']
744gpt_fine_tunes(TrainingFile,Result,Options):- 745 gpt_fine_tunes(TrainingFile,Result,false,Options),!. 746gpt_fine_tunes(TrainingFile,Result,Raw,Options):- 747 current_prolog_flag(gptkey,Key), 748 atom_json_term(D,json([training_file=TrainingFile|Options]),[]), 749 Data = atom(application/json,D), 750 http_post('https://api.openai.com/v1/fine-tunes',Data,json(ReturnData), 751 [authorization(bearer(Key)),application/json]), 752 ( Raw=false 753 -> member(id=Result,ReturnData) 754 ; Result= json(ReturnData) 755 ).
Example use:
:- gpt_fine-tunes(Result), Result = ['curie:ft-personal-2022-02-15-04-21-04'-'ft-090asf0asf0',...]
770gpt_fine_tunes(Result):- 771 gpt_fine_tunes(Result,false),!. 772gpt_fine_tunes(Result,Raw):- 773 current_prolog_flag(gptkey,Key), 774 http_get('https://api.openai.com/v1/fine-tunes',json(ReturnData), 775 [authorization(bearer(Key)),application/json]), 776 ( Raw=false 777 -> ( member(data=Models,ReturnData), 778 gpt_extract_field_pairs(fine_tuned_model,id,Models,Result) 779 ) 780 ; Result= json(ReturnData) 781 ).
Example use:
:- gpt_fine_tunes_detail('ft-090asf0asf0',Result), Result = ['curie:ft-personal-2022-02-15-04-21-04']
796gpt_fine_tunes_detail(ID,Result):- 797 gpt_fine_tunes_detail(ID,Result,false),!. 798gpt_fine_tunes_detail(ID,Result,Raw):- 799 current_prolog_flag(gptkey,Key), 800 atomic_concat('https://api.openai.com/v1/fine-tunes/',ID,URL), 801 http_get(URL,json(ReturnData), 802 [authorization(bearer(Key)),application/json]), 803 ( Raw=false 804 -> ( member(fine_tuned_model=TunedModel,ReturnData), 805 Result=[TunedModel] 806 ) 807 ; Result= json(ReturnData) 808 ).
Example use:
:- gpt_fine_tunes_cancel([_-ID]),(ID,Result), Result = ['curie:ft-personal-2022-02-15-04-21-04']
825gpt_fine_tunes_cancel(ID,Result):- 826 gpt_fine_tunes_cancel(ID,Result,false),!. 827gpt_fine_tunes_cancel(ID,Result,Raw):- 828 current_prolog_flag(gptkey,Key), 829 atomic_list_concat(['https://api.openai.com/v1/fine-tunes/',ID,'/cancel'],URL), 830 http_post(URL,[],json(ReturnData), 831 [authorization(bearer(Key)),application/json]), 832 ( Raw=false 833 -> ( member(fine_tuned_model=TunedModel,ReturnData), 834 Result=[TunedModel] 835 ) 836 ; Result= json(ReturnData) 837 ).
Example use:
:- gpt_fine_tunes_events([_-ID]),(ID,Result), Result = ['curie:ft-personal-2022-02-15-04-21-04']
854gpt_fine_tunes_events(ID,Result):- 855 gpt_fine_tunes_events(ID,Result,false),!. 856gpt_fine_tunes_events(ID,Result,Raw):- 857 current_prolog_flag(gptkey,Key), 858 atomic_list_concat(['https://api.openai.com/v1/fine-tunes/',ID,'/events'],URL), 859 http_get(URL,json(ReturnData), 860 [authorization(bearer(Key)),application/json]), 861 ( Raw=false 862 -> ( member(fine_tuned_model=TunedModel,ReturnData), 863 Result=[TunedModel] 864 ) 865 ; Result= json(ReturnData) 866 ).
Example use:
:- gpt_fine_tunes([_-ID]),gpt_fine_tunes_delete(ID,Result), Result = ['ft-XjGxS3KTG0uNmNOK362iJua3']
883gpt_fine_tunes_delete(ID,Result):- 884 gpt_fine_tunes_delete(ID,Result,false),!. 885gpt_fine_tunes_delete(ID,Result,Raw):- 886 current_prolog_flag(gptkey,Key), 887 atomic_concat('https://api.openai.com/v1/models/',ID,URL), 888 http_delete(URL,json(ReturnData), 889 [authorization(bearer(Key)),application/json]), 890 ( Raw=false 891 -> (member(id=ID,ReturnData), Result=[ID]) 892 ; Result= json(ReturnData) 893 ).
Example use:
:- gpt_moderations('I want to kill them',Result), Result = [sexual=false, hate=false, violence=true, 'self-harm'=false, 'sexual/minors'=false, 'hate/threatening'=false, 'violence/graphic'=false].
908gpt_moderations(Input,Result,Options):- 909 gpt_moderations(Input,Result,false,Options). 910gpt_moderations(Input,Result,Raw,Options):- 911 current_prolog_flag(gptkey,Key), 912 atom_json_term(D,json([input=Input|Options]),[]), 913 Data = atom(application/json,D), 914 http_post('https://api.openai.com/v1/moderations',Data,ReturnData, 915 [authorization(bearer(Key)),application/json]), 916 ( Raw=false 917 -> ( gpt_extract_data(results,categories,ReturnData,[json(R)]), 918 maplist(json_pair_boolean,R,Result) 919 ) 920 ; Result= ReturnData 921 ). 922 923json_pair_boolean(Name='@'(Boolean),Name=Boolean):-!. 924json_pair_boolean(Name=Val,Name=Val):-!
Prolog interface to GPT
Introduction
This module provides prolog predicates to call the GPT API.
Large Language Models (LLM) like GPT essentially predict what text comes next, based on learning the (latent) probabilistic relationships between text tokens. By training the model on massive samples of text, the natural language capabilities have improved dramatically in recent years.
However, such language models can benefit from interaction with other types of modules, such as logic engines. To aid in developing such interactions, this library aims to make it easy to interact with GPT directly from Prolog, using predicates that call the GPT API.
The Prolog predicates are based on the OpenAI API Reference: https://platform.openai.com/docs/api-reference
Usage
source
your `~/.bashrc` script to activate the new environment variable).prolog2gpt.pro
Prolog module as usual @author Richard de Rozario @license MIT */