-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.lisp
executable file
·1623 lines (1381 loc) · 57.9 KB
/
client.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package :gpt)
(defparameter *default-key-pathname* "~/openai/key")
(defvar *default-version* :v1)
(defvar *default-server* "api.openai.com")
(defvar *key*)
(defun read-key-file (filename)
(with-open-file (fs filename :direction :input :element-type 'character)
(loop with char
with string = (make-array 64 :adjustable t :fill-pointer 0 :element-type 'character)
do (setq char (read-char fs nil :eof))
when (eq char :eof)
do (return (string-trim '(#\Return #\Newline #\Space) string))
do (vector-push-extend char string))))
(defun set-key-path (&optional (pathname *default-key-pathname*))
(setf *key* (read-key-file pathname)))
(set-key-path)
(defun json-as-list (jso)
(cond ((eq jso :null) nil)
((typep jso 'st-json:jso)
(let ((data (st-json:getjso "data" jso)))
(if data data jso)))
((listp jso) jso)
(t jso)))
(defun json-as-boolean (jso)
(cond ((eq jso :false) nil)
((eq jso :true) t)
(t jso)))
(defun validate-response-format-parameter (arg)
(cond ((or (string-equal arg "url")
(eq arg :url))
"url")
((or (string-equal arg "b64_json")
(string-equal arg "b64-json")
(eq :b64_json arg)
(eq :b64-json arg))
"b64_json")
((or (string-equal arg "json")
(eq arg :json))
"json")
((or (string-equal arg "verbose-json")
(string-equal arg "verbose_json")
(eq arg :verbose-json))
"verbose_json")
((or (string-equal arg "srt")
(eq arg :srt))
"srt")
((or (string-equal arg "vtt")
(eq arg :vtt))
"vtt")
(t (error "invalid response-format ~S" arg))))
(defun make-request-url (server version service-point &rest args)
(concatenate 'string "https://" server "/" (string-downcase (format nil "~A" version))
(apply #'format nil service-point args)))
(defun make-request-arguments (key &optional (content nil content-present-p))
(cond ((null content-present-p) ;; GET request
(list :additional-headers (list (cons :authorization (concatenate 'string "Bearer " key)))
:want-stream t))
(t (list :method :post
:additional-headers (list (cons :authorization (concatenate 'string "Bearer " key)))
:want-stream t
:content-type "application/json"
:content (st-json:write-json-to-string content)))))
(defun stringify (thing)
(string-downcase (format nil "~A" thing)))
(defun check-for-error (jso)
(let ((error (st-json:getjso "error" jso)))
(when error
(let ((message (st-json:getjso "message" error))
(type (st-json:getjso "type" error)))
(error "GPT error: ~A: ~A" type message)))))
(defun list-models (&key
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/models"))
"Lists the currently available models, and provides basic information about each one such as the owner and availability."
(let ((jso (st-json:read-json
(apply #'drakma:http-request (make-request-url server version service-point) (make-request-arguments key)))))
(check-for-error jso)
(let ((jso-list (st-json:getjso "data" jso)))
(mapcar #'(lambda (object)
(make-model object))
jso-list))))
(defclass permission ()
((id :accessor id)
(object :accessor object)
(created :accessor created)
(allow-create-engine :accessor allow-create-engine)
(allow-sampling :accessor allow-sampling)
(allow-logprobs :accessor allow-logprobs)
(allow-search-indices :accessor allow-search-indices)
(allow-view :accessor allow-view)
(allow-fine-tuning :accessor allow-fine-tuning)
(organization :accessor organization)
(group :accessor group)
(blocking? :accessor blocking?)))
(defun make-permission (jso)
(let ((permission (make-instance 'permission)))
(setf (id permission) (st-json:getjso "id" jso)
(object permission) (st-json:getjso "object" jso)
(created permission) (st-json:getjso "created" jso)
(allow-create-engine permission) (st-json:getjso "allow_create_engine" jso)
(allow-logprobs permission) (st-json:getjso "allow_logprobs" jso)
(allow-search-indices permission) (st-json:getjso "allow_search_indices" jso)
(allow-view permission) (st-json:getjso "allow_view" jso)
(allow-fine-tuning permission) (st-json:getjso "allow_fine_tuning" jso)
(organization permission) (st-json:getjso "organization" jso)
(group permission) (st-json:getjso "group" jso)
(blocking? permission) (st-json:getjso "blocking" jso))
permission))
(defclass model ()
((id :accessor id)
(object :accessor object)
(created :accessor created)
(permission :accessor permission)
(root :accessor root)
(parent :accessor parent)))
(defun make-model (jso)
(let ((model (make-instance 'model)))
(setf (id model) (st-json:getjso "id" jso)
(object model) (st-json:getjso "object" jso)
(created model) (st-json:getjso "created" jso)
(permission model) (mapcar #'make-permission (st-json:getjso "permission" jso))
(root model) (st-json:getjso "root" jso)
(parent model) (st-json:getjso "parent" jso))
model))
(defmethod print-object ((object model) stream)
(print-unreadable-object (object stream :type t)
(princ (id object) stream)))
(defmethod get-model ((model model) &rest args)
(apply #'get-model (id model) args))
(defmethod get-model (model &rest args
&key
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/models/~A"))
"Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
arguments:
`model' The ID of the model to use for this request, a string or a symbol."
(declare (ignore args))
(assert (or (symbolp model) (stringp model)))
(let ((jso (st-json:read-json
(apply #'drakma:http-request (make-request-url server version service-point (stringify model)) (make-request-arguments key)))))
(check-for-error jso)
(make-model jso)))
(defgeneric create-completion (model &rest args)
(:documentation "Creates a completion for the provided prompt and parameters
`model'
model, string or symbol
Required
ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
`prompt'
string or list of strings
Optional
Defaults to \"<|endoftext|>\"
The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
`suffix'
string
Optional
Defaults to nil
The suffix that comes after a completion of inserted text.
`max-tokens'
integer
Optional
Defaults to 16
The maximum number of tokens to generate in the completion.
The token count of your prompt plus max-tokens cannot exceed the model's context length. Most models have a context length of 2048 tokens (except for the newest models, which support 4096).
`temperature'
number
Optional
Defaults to 1
What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top_p but not both.
`top-p'
number
Optional
Defaults to 1
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
`n'
integer
Optional
Defaults to 1
How many completions to generate for each prompt.
Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
`stream'
boolean
Optional
Defaults to nil
Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
`logprobs'
integer
Optional
Defaults to nil
Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. For example, if logprobs is 5, the API will return a list of the 5 most likely tokens. The API will always return the logprob of the sampled token, so there may be up to logprobs+1 elements in the response.
The maximum value for logprobs is 5. If you need more than this, please contact us through our Help center and describe your use case.
`echo'
boolean
Optional
Defaults to nil
Echo back the prompt in addition to the completion
`stop'
string or sequence
Optional
Defaults to nil
Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
`presence-penalty'
number
Optional
Defaults to 0
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
See more information about frequency and presence penalties.
`frequency-penalty'
number
Optional
Defaults to 0
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
See more information about frequency and presence penalties.
`best-of'
integer
Optional
Defaults to 1
Generates `best-of' completions server-side and returns the \"best\" (the one with the highest log probability per token). Results cannot be streamed.
When used with `n', `best-of' controls the number of candidate completions and `n' specifies how many to return `best-of' must be greater than `n'.
Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
`logit-bias'
plist
Optional
Defaults to nil
Modify the likelihood of specified tokens appearing in the completion.
Accepts a plist that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this tokenizer tool (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
As an example, you can pass (list \"50256\" -100) to prevent the \"<|endoftext|>\" token from being generated.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."))
(defmethod create-completion ((model model) &rest args)
(apply #'create-completion (id model) args))
(defmethod create-completion (model &rest args
&key
(prompt nil prompt-present-p)
(suffix nil suffix-present-p)
(max-tokens nil max-tokens-present-p)
(temperature nil temperature-present-p)
(top-p nil top-p-present-p)
(n nil n-present-p)
(stream nil stream-present-p)
(logprobs nil logprobs-present-p)
(echo nil echo-present-p)
(stop nil stop-present-p)
(presence-penalty nil presence-penalty-present-p)
(frequency-penalty nil frequency-penalty-present-p)
(best-of nil best-of-present-p)
(logit-bias nil logit-bias-present-p)
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/completions"))
(declare (ignore args))
(assert (or (symbolp model) (stringp model)))
(let* ((content (apply #'st-json:jso
"model" (stringify model)
(append
(when prompt-present-p
(assert (listp prompt))
(list "prompt" prompt))
(when suffix-present-p
(assert (stringp suffix))
(list "suffix" (format nil "~A" suffix)))
(when max-tokens-present-p
(assert (integerp max-tokens))
(list "max_tokens" max-tokens))
(when temperature-present-p
(assert (numberp temperature))
(assert (<= 0 temperature 2))
(list "temperature" temperature))
(when top-p-present-p
(assert (numberp top-p))
(list "top_p" top-p))
(when n-present-p
(assert (integerp n))
(list "n" n))
(when stream-present-p
(list "stream" stream))
(when logprobs-present-p
(assert (integerp logprobs))
(list "logprobs" logprobs))
(when echo-present-p
(list "echo" echo))
(when stop-present-p
(assert (typep stop 'sequence))
(list "stop" stop))
(when presence-penalty-present-p
(assert (numberp presence-penalty))
(list "presence_penalty" presence-penalty))
(when frequency-penalty-present-p
(assert (numberp frequency-penalty))
(list "frequency_penalty" frequency-penalty))
(when best-of-present-p
(assert (integerp logit-bias))
(list "best_of" best-of))
(when logit-bias-present-p
(assert (listp logit-bias))
(list "logit_bias" logit-bias))
(when user-present-p
(assert (stringp user))
(list "user" user)))))
(response-stream
(apply #'drakma:http-request (make-request-url server version service-point) (make-request-arguments key content))))
(if stream
response-stream
(let ((jso (st-json:read-json response-stream)))
(check-for-error jso)
jso))))
(defgeneric create-chat-completion (model prompt &rest args)
(:documentation "Creates a completion for the chat message. Note, the combined number of tokens in `prompt', `context' `system-instruction', and the generated completion cannot exceed 4096 for model :gpt-3.5-turbo and 2048 for older models.
`model'
string or symbol
Required
ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API.
`prompt'
string
Required
The prompt to generate a completion for.
`context'
an even length list of strings
Optional
Defaults to nil
List should be composed of alternating pairs of prompt and it associated completion, for context.
`system-instruction'
string or null
Optional
Defaults to nil
Should be an instruction to chatgpt of what role it should assume.
`temperature'
number
Optional
Defaults to 1
What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or top-p but not both.
`top-p'
number
Optional
Defaults to 1
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top-p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or temperature but not both.
`n'
integer
Optional
Defaults to 1
How many chat completion choices to generate for each input message.
`stream'
boolean
Optional
Defaults to nil
If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message. See the OpenAI Cookbook for example code.
`stop'
string or sequence
Optional
Defaults to nil
Up to 4 sequences where the API will stop generating further tokens.
`max-tokens'
integer
Optional
Defaults to infinity
The maximum number of tokens to generate in the chat completion.
The total length of input tokens and generated tokens is limited by the model's context length.
`presence-penalty'
number
Optional
Defaults to 0
Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
See more information about frequency and presence penalties.
`frequency-penalty'
number
Optional
Defaults to 0
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
See more information about frequency and presence penalties.
`logit-bias'
plist
Optional
Defaults to nil
Modify the likelihood of specified tokens appearing in the completion.
Accepts a plist that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."))
(defmethod create-chat-completion ((model model) prompt &rest args)
(apply #'create-chat-completion (id model) prompt args))
(defmethod create-chat-completion (model prompt &rest args
&key
(system-instruction nil system-instruction-present-p)
(context nil)
(temperature nil temperature-present-p)
(top-p nil top-p-present-p)
(n nil n-present-p)
(stream nil stream-present-p)
(stop nil stop-present-p)
(max-tokens nil max-tokens-present-p)
(presence-penalty nil presence-penalty-present-p)
(frequency-penalty nil frequency-penalty-present-p)
(logit-bias nil logit-bias-present-p)
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/chat/completions"))
(declare (ignore args))
(assert (or (symbolp model) (stringp model)))
(assert (listp context))
(assert (evenp (length context)))
(flet ((create-messages-argument ()
(mapcar #'(lambda (plist)
(apply #'st-json:jso plist))
(append (when system-instruction-present-p
(list (list "role" "system" "content" system-instruction)))
(loop with list = ()
for (prompt completion) on context by #'cddr
do (push (list "role" "user" "content" (format nil "~A" prompt)) list)
(push (list "role" "assistant" "content" (format nil "~A" completion)) list)
finally (return (nreverse list)))
(list (list "role" "user" "content" prompt))))))
(let* ((content (apply #'st-json:jso
"model" (stringify model)
"messages" (create-messages-argument)
(append
(when temperature-present-p
(assert (numberp temperature))
(assert (<= 0 temperature 2))
(list "temperature" temperature))
(when top-p-present-p
(assert (numberp top-p))
(list "top_p" top-p))
(when n-present-p
(assert (integerp n))
(list "n" n))
(when stream-present-p
(list "stream" (if stream :true :false)))
(when stop-present-p
(assert (typep stop 'sequence))
(list "stop" stop))
(when max-tokens-present-p
(assert (integerp max-tokens))
(list "max_tokens" max-tokens))
(when presence-penalty-present-p
(assert (numberp presence-penalty))
(list "presence_penalty" presence-penalty))
(when frequency-penalty-present-p
(assert (numberp frequency-penalty))
(list "frequency_penalty" frequency-penalty))
(when logit-bias-present-p
(assert (listp logit-bias))
(list "logit_bias" logit-bias))
(when user-present-p
(assert (stringp user))
(list "user" user)))))
#+NIL(pr (princ (st-json:write-json-to-string content)))
(response-stream
(apply #'drakma:http-request (make-request-url server version service-point) (make-request-arguments key content))))
(if stream
response-stream
(let* ((jso (st-json:read-json response-stream)))
(check-for-error jso)
jso)))))
(defun create-edit (instruction &key
(model :text-davici-edit-001)
(input nil input-present-p)
(temperature nil temperature-present-p)
(n nil n-present-p)
(top-p nil top-p-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/edits"))
"Creates a new edit for the provided input, instruction, and parameters.
`instruction'
string
Required
The instruction that tells the model how to edit the prompt.
`model'
string
Optional
ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint.
`input'
string
Optional
Defaults to \"\"
The input text to use as a starting point for the edit.
`n'
integer
Optional
Defaults to 1
How many edits to generate for the input and instruction.
`temperature'
number
Optional
Defaults to 1
What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
We generally recommend altering this or `top-p' but not both.
`top-p'
number
Optional
Defaults to 1
An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
We generally recommend altering this or `temperature' but not both."
(assert (or (symbolp model) (stringp model)))
(assert (stringp instruction))
(let* ((content (apply #'st-json:jso
"instruction" instruction
"model" (stringify model)
(append
(when input-present-p
(assert (stringp input))
(list "input" input))
(when temperature-present-p
(assert (numberp temperature))
(assert (<= 0 temperature 2))
(list "temperature" temperature))
(when n-present-p
(assert (integerp n))
(list "n" n))
(when top-p-present-p
(assert (numberp top-p))
(list "top_p" top-p)))))
(response-stream
(apply #'drakma:http-request (make-request-url server version service-point) (make-request-arguments key content))))
(st-json:read-json response-stream)))
(defun create-image (prompt &key
(n nil n-present-p)
(size nil size-present-p)
(response-format nil response-format-present-p)
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/images/generations"))
"Creates an image given a prompt.
`prompt'
string
Required
A text description of the desired image(s). The maximum length is 1000 characters.
`n'
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
`size'
string
Optional
Defaults to \"1024x1024\"
The size of the generated images. Must be one of \"256x256\", \"512x512\", or \"1024x1024\".
`response-format'
string or symbol
Optional
Defaults to :url
The format in which the generated images are returned. Must be one of :url or :b64-json.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."
(assert (stringp prompt))
(let* ((content (apply #'st-json:jso
"prompt" prompt
(append
(when n-present-p
(assert (integerp n))
(assert (<= 0 n 10))
(list "n" n))
(when size-present-p
(unless (stringp size)
(setq size (format nil "~Ax~A" (elt size 0) (elt size 1))))
(list "size" size))
(when response-format-present-p
(list "response_format" (validate-response-format-parameter response-format)))
(when user-present-p
(assert (stringp user))
(list "user" user)))))
(response-stream
(apply #'drakma:http-request (make-request-url server version service-point) (make-request-arguments key content))))
(st-json:read-json response-stream)))
(defun create-image-edit (image prompt &key
(mask nil mask-present-p)
(n nil n-present-p)
(size nil size-present-p)
(response-format nil response-format-present-p)
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/images/edits"))
"Creates an edited or extended image given an original image and a prompt.
`image'
string
Required
The image to edit. Must be a valid PNG file, less than 4MB, and square. If mask is not provided, image must have transparency, which will be used as the mask.
`prompt'
string
Required
A text description of the desired image(s). The maximum length is 1000 characters.
`mask'
string
Optional
An additional image whose fully transparent areas (e.g. where alpha is zero) indicate where image should be edited. Must be a valid PNG file, less than 4MB, and have the same dimensions as image.
`n'
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
`size'
string
Optional
Defaults to \"1024x1024\"
The size of the generated images. Must be one of \"256x256\", \"512x512\", or \"1024x1024\".
`response-format'
string or symbol
Optional
Defaults to :url
The format in which the generated images are returned. Must be one of :url or :b64-json.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."
(assert (stringp image))
(assert (stringp prompt))
(let* ((content (apply #'st-json:jso
"image" image
"prompt" prompt
(append
(when mask-present-p
(assert (stringp mask))
(list "mask" mask))
(when n-present-p
(assert (integerp n))
(list "n" n))
(when size-present-p
(unless (stringp size)
(setq size (format nil "~Ax~A" (elt size 0) (elt size 1))))
(list "size" size))
(when response-format-present-p
(list "response_format" (validate-response-format-parameter response-format)))
(when user-present-p
(assert (stringp user))
(list "user" user))))))
(st-json:read-json
(apply #'drakma:http-request
(make-request-url server version service-point) (make-request-arguments key content)))))
(defun create-image-variation (image &key
(n nil n-present-p)
(size nil size-present-p)
(response-format nil response-format-present-p)
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/images/variations"))
"Creates a variation of a given image.
`image'
string
Required
The image to use as the basis for the variation(s). Must be a valid PNG file, less than 4MB, and square.
`n'
integer
Optional
Defaults to 1
The number of images to generate. Must be between 1 and 10.
`size'
string
Optional
Defaults to \"1024x1024\"
The size of the generated images. Must be one of \"256x256\", \"512x512\", or \"1024x1024\".
`response-format'
string or symbol
Optional
Defaults to :url
The format in which the generated images are returned. Must be one of :url or :b64-json.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."
(assert (stringp image))
(let* ((content (apply #'st-json:jso
"image" image
(append
(when n-present-p
(assert (integerp n))
(list "n" n))
(when size-present-p
(unless (stringp size)
(setq size (format nil "~Ax~A" (elt size 0) (elt size 1))))
(list "size" size))
(when response-format-present-p
(list "response_format" (validate-response-format-parameter response-format)))
(when user-present-p
(assert (stringp user))
(list "user" user))))))
(st-json:read-json
(apply #'drakma:http-request
(make-request-url server version service-point) (make-request-arguments key content)))))
(defgeneric create-embeddings (model input &rest args)
(:documentation "Creates an embedding vector representing the input text.
`model'
model or string
Required
ID of the model to use. You can use the List models API to see all of your available models, or see our Model overview for descriptions of them.
`input'
string or sequence
Required
Input text to get embeddings for, encoded as a string or array of tokens. To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays. Each input must not exceed 8192 tokens in length.
`user'
string
Optional
A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse."))
(defmethod create-embeddings ((model model) (input string) &rest args)
(apply #'create-embeddings (id model) input args))
(defmethod create-embeddings ((model model) (input sequence) &rest args)
(apply #'create-embeddings (id model) (coerce input 'list) args))
(defmethod create-embeddings (model input &rest args
&key
(user nil user-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/embeddings"))
(declare (ignore args))
(assert (stringp model))
(assert (typep input 'sequence))
(let* ((content (apply #'st-json:jso
"model" (stringify model)
"input" (if (stringp input)
input
(list
(apply #'st-json::jso input)))
(append
(when user-present-p
(assert (stringp user))
(list "user" user))))))
(st-json:read-json
(apply #'drakma:http-request
(make-request-url server version service-point) (make-request-arguments key content)))))
(defun create-transcription (file &key
(model :whisper-1)
(prompt nil prompt-present-p)
(response-format nil response-format-present-p)
(temperature nil temperature-present-p)
(language nil language-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/audio/transcription"))
"Transcribes audio into the input language.
`file'
string
Required
The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
`model'
string
Required
ID of the model to use. Only whisper-1 is currently available.
`prompt'
string
Optional
An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language.
`response-format'
string
Optional
Defaults to :json
The format of the transcript output, in one of these options: :json, :text, :srt, :verbose-json, or :vtt.
`temperature'
number
Optional
Defaults to 0
The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.
`language'
string
Optional
The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency."
(when (typep model 'model)
(setq model (id model)))
(assert (stringp file))
(assert (or (symbolp model) (stringp model)))
(let* ((content (apply #'st-json:jso
"file" file
"model" (stringify model)
(append
(when prompt-present-p
(assert (stringp prompt))
(list "prompt" prompt))
(when response-format-present-p
(list "response_format" (validate-response-format-parameter response-format)))
(when temperature-present-p
(assert (numberp temperature))
(assert (<= 0 temperature 1))
(list "temperature" temperature))
(when language-present-p
(assert (stringp language))
(list "language" language))))))
(st-json:read-json
(apply #'drakma:http-request
(make-request-url server version service-point) (make-request-arguments key content)))))
(defun create-translation (file &key
(model :whisper-1)
(prompt nil prompt-present-p)
(response-format nil response-format-present-p)
(temperature nil temperature-present-p)
(version *default-version*)
(server *default-server*)
(key *key*)
&aux (service-point "/audio/translations"))
"Translates audio into into English.
`file'
string
Required
The audio file to translate, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
`model'
string
Required
ID of the model to use. Only whisper-1 is currently available.
`prompt'
string
Optional
An optional text to guide the model's style or continue a previous audio segment. The prompt should be in English.
`response-format'
string
Optional
Defaults to :json
The format of the transcript output, in one of these options: :json, :text, :srt, :verbose-json, or :vtt.
`temperature'
number
Optional
Defaults to 0
The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit."
(when (typep model 'model)
(setq model (id model)))
(assert (stringp file))
(assert (or (stringp model) (stringp model)))
(let* ((content (apply #'st-json:jso
"file" file
"model" (stringify model)
(append
(when prompt-present-p
(assert (stringp prompt))
(list "prompt" prompt))
(when response-format-present-p
(list "response_format" (validate-response-format-parameter response-format)))
(when temperature-present-p
(assert (numberp temperature))
(assert (<= 0 temperature 1))
(list "temperature" temperature))))))
(st-json:read-json
(apply #'drakma:http-request
(make-request-url server version service-point) (make-request-arguments key content)))))
(defun list-files (&key