forked from semiosis/pen.el
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpen-define-prompt-function.el
2750 lines (2381 loc) · 111 KB
/
pen-define-prompt-function.el
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
;;; `pen-define-prompt-function.el` contains the prompting pipeline
;;; - prompt function generation
;;; - templating
;;; - preprocessing
;;; - postprocessing
;;; - analysis
(require 'pen-dni)
(defmacro pen-split-macro-test-inner ()
`(progn
(etv ,testval)))
(defun pen-split-macro-test-define-fun ()
(eval
`(defun split-macro-test-fun ()
,(macroexpand `(pen-split-macro-test-inner)))))
(defun pen-split-macro-test ()
(let ((testval "shane"))
(pen-split-macro-test-define-fun))
(split-macro-test-fun))
;; (comment (pen-split-macro-test))
;; I don't like this either
;; But I have introduced global state for pen-prompt-snc so I can memoize the function without
;; this particular state triggering an update
(defvar pen-snc-ignored-envs nil)
;; Reuse this in the proxy function
(defmacro pen-pfp-data ()
`(let ((data
;; The prompt loses unicode here. I think I need to convert to base64 maybe
;; And if I do, put it just outside pen-encode-string
`(
("PEN_PROXY" . ,final-proxy)
("PEN_PROMPT_FUNCTION" . ,func-name-slug)
("PEN_PROMPT" .
;; Sort this out later
,(pen-encode-string final-prompt)
;; ,(pen-snc "base64" (pen-encode-string final-prompt))
;; ,(pen-snc "base64" final-prompt)
)
("PEN_PROMPT_FULL" .
;; Sort this out later
,(pen-encode-string final-prompt-full)
;; ,(pen-snc "base64" (pen-encode-string final-prompt))
;; ,(pen-snc "base64" final-prompt)
)
("PEN_SUFFIX" .
,(pen-encode-string (or final-suffix "")))
;; ("PEN_PROMPT" . ,(pen-encode-string final-prompt))
("PEN_LM_COMMAND" . ,final-lm-command)
("PEN_MODEL" . ,final-model)
("PEN_WHITESPACE_SUPPORT" . ,(if final-engine-whitespace-support
"y"
""))
;; This must go into last-prompt-data for ink
("PEN_ENGINE" . ,final-engine)
("PEN_API_ENDPOINT" . ,final-api-endpoint)
("PEN_PAYLOADS" . ,final-payloads)
;; TODO Implement query and counterquery for more accurate semantic search
("PEN_QUERY" . ,final-query)
("PEN_COUNTERQUERY" . ,final-counterquery)
("PEN_LOGPROBS" . ,(str final-logprobs))
("PEN_APPROXIMATE_PROMPT_LENGTH" . ,pen-approximate-prompt-token-length)
("PEN_ENGINE_MIN_TOKENS" . ,final-engine-min-tokens)
("PEN_ENGINE_MAX_TOKENS" . ,final-engine-max-tokens)
("PEN_MIN_TOKENS" . ,final-min-tokens)
("PEN_MAX_TOKENS" . ,final-max-tokens)
("PEN_REPETITION_PENALTY" . ,final-repetition-penalty)
("PEN_FREQUENCY_PENALTY" . ,final-frequency-penalty)
("PEN_PRESENCE_PENALTY" . ,final-presence-penalty)
("PEN_LENGTH_PENALTY" . ,final-length-penalty)
("PEN_MIN_GENERATED_TOKENS" . ,final-min-generated-tokens)
("PEN_MAX_GENERATED_TOKENS" . ,final-max-generated-tokens)
("PEN_TEMPERATURE" . ,final-temperature)
("PEN_MODE" . ,final-mode)
("PEN_STOP_SEQUENCE" . ,(pen-encode-string final-stop-sequence t))
;; (json-encode-list (mapcar 'pen-encode-string '("hello my ;\"" "friend")))
;; I still need to use pen-encode-string because of backticks
("PEN_STOP_SEQUENCES" . ,(pen-encode-string (json-encode-list final-stop-sequences) t))
("PEN_VARS" . ,(pen-encode-string (json--encode-alist defs-and-vals-alist) t))
;; TODO Force multiple prompts later
;; Also need multi-prompts to understand different prompt lengths for results
;; ("PEN_PROMPTS" . ,(json-encode-list final-prompts))
;; documents must be a json list of strings
("PEN_DOCUMENTS" . ,(pen-var-value-maybe 'documents))
("PEN_TOP_P" . ,final-top-p)
("PEN_TOP_K" . ,final-top-k)
("PEN_FLAGS" . ,final-flags)
;; 'best of' IS 'top k'
;; ("PEN_BEST_OF" . ,final-best-of)
("PEN_CACHE" . ,(if cache "y" ""))
;; This is also handled by default in pen-sn
("PEN_USER_AGENT" . ,pen-user-agent)
("PEN_TRAILING_WHITESPACE" . ,trailing-whitespace)
("PEN_N_COMPLETIONS" . ,(str final-n-completions))
;; ("PEN_ENGINE_MAX_N_COMPLETIONS" . ,final-engine-max-n-completions)
("PEN_ENGINE_MIN_GENERATED_TOKENS" . ,final-engine-min-generated-tokens)
("PEN_ENGINE_MAX_GENERATED_TOKENS" . ,final-engine-max-generated-tokens)
("PEN_COLLECT_FROM_POS" . ,collect-from-pos)
("PEN_MEMORY_COLLECT_FROM_POS" . ,memory-collect-from-pos)
("PEN_END_POS" . ,end-pos)
("PEN_N_JOBS" . ,final-n-jobs)
("PEN_SEARCH_THRESHOLD" . ,final-search-threshold)
;; ("PEN_QUERY_POS" . ,query-pos)
("PEN_INJECT_GEN_START" . ,(pen-encode-string final-inject-gen-start t)))))
(setq pen-last-prompt-data
(asoc-merge pen-last-prompt-data data))
(setq pen-last-prompt-data
(asoc-merge pen-last-prompt-data (list (cons "PEN_VALS" (pps last-vals))
;; (cons "PEN_END_POS" end-pos)
)))
;; (pen-etv data)
data
;; data
))
(defmacro pen-pfp-resultsdirs ()
`(if (not no-gen)
(progn
(let* ((collation-data data)
(collation-temperature (alist-get "PEN_TEMPERATURE" collation-data nil nil 'equal)))
(cl-loop
for i in (number-sequence 1 final-n-collate)
collect
(progn
(message (concat ,func-name " query " (int-to-string i) "..."))
;; TODO Also handle PEN_N_COMPLETIONS
(let* ((ret
(progn
(setq pen-snc-ignored-envs
(sh-construct-envs
;; This is a bit of a hack for \n in prompts
;; See `pen-restore-chars`
(pen-alist-to-list ignored-data)))
(pen-prompt-snc
(pen-log
(s-join
" "
(list
;; ;; This actually interfered with the memoization!
;; (let ((updval (pen-var-value-maybe 'do-pen-update)))
;; (if updval
;; (concat
;; "export "
;; (sh-construct-envs '(("UPDATE" "y")))
;; "; ")))
;; All parameters are sent as environment variables
(sh-construct-envs
;; This is a bit of a hack for \n in prompts
;; See `pen-restore-chars`
(append (pen-alist-to-list final-envs)
`(("ALSO_EXPORT" ,(sh-construct-envs (pen-alist-to-list final-envs))))
(pen-alist-to-list collation-data)))
;; Currently always updating
"lm-complete")))
i
;; I'm also using memoization of pen-prompt-snc
;; cache
;; pen-sh-update
))))
(message (concat ,func-name " done " (int-to-string i)))
ret
;; Actually, I don't want to do this here.
;; Instead do it here: vim +/"echo \"\$json_results\" | pen-tv" "$HOME/source/git/semiosis/pen.el/scripts/lm-complete"
;; (if (and (sor final-proxy)
;; (sor ret))
;; (let ((jsonresults
;; (vector2list (json-read-from-string (tv ret))))
;; (counter 0))
;; (f-mkdir gen-dir)
;; (loop for r in jsonresults
;; do
;; (progn
;; (setq counter (+ 1 counter))
;; (pen-write-to-file
;; r
;; (f-join gen-dir "splitfile_" (str counter)))))
;; gen-dir)
;; ret)
))
do
(pen-try
;; Update the collation-temperature
(if (sor final-collation-temperature-stepper)
(progn
(setq collation-temperature
(str
(eval-string
(pen-expand-template-keyvals
final-collation-temperature-stepper
`(("temperature" . ,collation-temperature))))))
(evil--add-to-alist
'collation-data
"PEN_TEMPERATURE"
collation-temperature)))
(message "collation temperature stepper failed")))))))
(defmacro pen-pfp-results ()
`(cond
(final-force-results final-force-results)
(final-no-gen (progn
(message "Prompting function aborted")
'("")))
(t (pen-maybe-uniq
final-no-uniq-results
(flatten-once
(cl-loop for rd in resultsdirs
collect
(if (sor rd)
(let* ((processed-results
(-flatten
(->>
(append (glob (concat rd "/split*_*"))
(glob (concat rd "/results*/split*_*"))
(glob (concat rd "/res*.txt"))
(glob (concat rd "/results*/res*.txt")))
(-filter 'string-not-empty-nor-nil-p)
(-filter 'f-exists-p)
(mapcar 'e/cat)
(mapcar
(lambda (r)
(if final-split-patterns
(cl-loop
for stpat in final-split-patterns collect
(s-split stpat r))
(list r)))))))
(processed-results
(->> processed-results
(mapcar (lambda (r)
(cl-loop
for stsq in final-stop-sequences do
(let ((matchpos (pen-string-search (regexp-quote stsq) r)))
(if matchpos
(setq r (s-truncate matchpos r "")))))
r))
(mapcar (lambda (r)
(cl-loop
for stpat in final-stop-patterns do
(let ((matchpos (re-match-p stpat r)))
(if matchpos
(setq r (s-truncate matchpos r "")))))
r))
;; TODO Add multiplexing
;; TODO in iλ? I need an imaginary map function which performs the multiplex
;; I should add this capability manually.
;; Or do I want an =icompose=?
(mapcar (lambda (r) (if (and final-postprocessor (sor final-postprocessor))
(if (string-match "^pf-" final-postprocessor)
(eval `(car (pen-one (apply (str2sym ,final-postprocessor) (list ,r)))))
(pen-sn (concat
(sh-construct-envs (pen-alist-to-list `(("FINAL_PROMPT" . ,final-prompt))))
" "
final-postprocessor)
r))
r)))
(mapcar (lambda (r) (if (or
(and
(or is-interactive
(and (variable-p 'prettify)
prettify))
,prettifier
(sor ,prettifier))
(and (not no-select-result)
,fz-pretty))
(pen-sn ,prettifier r)
r)))
(mapcar (lambda (r) (if (or (not ,no-trim-start)
;; (sor final-inject-gen-start)
)
(s-trim-left r) r)))
(mapcar (lambda (r) (if (not ,no-trim-end) (s-trim-right r) r)))))
(processed-results
(-flatten
(->> processed-results
(mapcar
(lambda (r)
(if final-end-split-patterns
(cl-loop
for stpat in final-end-split-patterns collect
(s-split stpat r))
(list r)))))))
(processed-results
(->> processed-results
(-filter
(lambda (r)
(or
final-engine-whitespace-support
(not (sor trailing-whitespace))
(not (pen-snq (pen-cmd "pen-str" "has-starting-specified-whitespace" trailing-whitespace) r)))))))
(processed-results
(->> processed-results
(-filter
(lambda (r)
(if
(not final-engine-whitespace-support)
(concat trailing-whitespace r)
r)))))
;; (processed-results
;; (mapcar
;; (lambda (r)
;; (if (and (not final-engine-whitespace-support)
;; (sor trailing-whitespace))
;; (s-remove-starting-specified-whitespace r trailing-whitespace)
;; ;; (pen-sn (pen-cmd "pen-str" "remove-starting-specified-whitespace" trailing-whitespace) r)
;; r))
;; processed-results))
(processed-results
(->> processed-results
(-filter
(lambda (r)
(or
(not final-validator)
;; Theoretically, both a shell script and elisp should have access to prompt-length and result-length
(let* ((al `((prompt-length . ,pen-approximate-prompt-token-length)
(gen-length . ,(round (/ (length r)
(or
(pen-num final-approximate-token-char-length)
pen-default-approximate-token-length-divisor))))))
(valr (pen-expand-template-keyvals final-validator al)))
(eval
`(pen-let-keyvals
',al
(if (re-match-p "^(" ,valr)
(eval-string ,valr)
(pen-snq ,valr ,r)))))))))))
processed-results)
(list (message "Try UPDATE=y or debugging")))))))))
(defmacro pen-define-prompt-function-pipeline ()
`(let* (;; Keep in mind this both updates memoization and the bash cache
;; the differences has been confused. Treat as the same
(do-pen-update
(or
;; H-u -- this doesn't work with some interactive functions, such as (interactive (list (read-string "kjlfdskf")))
(>= (prefix-numeric-value current-global-prefix-arg) 4)
;; C-u 0
(= (prefix-numeric-value current-prefix-arg) 0)
(pen-var-value-maybe 'do-pen-update)))
(pen-sh-update
(or
(>= (prefix-numeric-value current-global-prefix-arg) 4)
;; C-u 0
(= (prefix-numeric-value current-prefix-arg) 0)
(pen-var-value-maybe 'pen-sh-update)
do-pen-update))
(cache
(or (pen-var-value-maybe 'cache)
,cache)
;; (and (not do-pen-update)
;; )
)
(do-pen-batch
(pen-var-value-maybe 'do-pen-batch))
(final-expressions)
(final-proxy
(or (pen-var-value-maybe 'proxy)
pen-proxy
,proxy))
(final-path
(let ((fpath
(or (pen-var-value-maybe 'path)
,path)))
(setq pen-last-prompt-data
(asoc-merge pen-last-prompt-data (list (cons "PEN_PROMPT_PATH" fpath))))
fpath))
;; TODO Consider overriding model, temperature and lm-command again
;; based on this value
;; Currently, this is inert.
(final-engine
(str (or
(and
pen-use-human-engine-if-disconnected
(not (pen-internet-connected-p))
(progn
(message "Internet not connected. Prompting human instead. Check terminal.")
"Human"))
(and
(not pen-prompt-force-engine-disabled)
(sor ,force-engine))
(sor pen-force-engine)
(sor (pen-var-value-maybe 'engine))
(sor ,engine)
pen-default-engine)))
(final-temperature)
(final-lm-command)
(final-model)
(final-api-endpoint
(or (pen-var-value-maybe 'api-endpoint)
,api-endpoint))
(final-force-n-jobs
(str (or
,force-n-jobs
(pen-var-value-maybe 'force-n-jobs))))
;; Actually, only override model, temperature and lm-command again if force-engine is set.
;; And with final-force-engine, only override final-model, final-temperature and final-lm-command.
;; Don't override final-'force'-model, etc.
(final-engine
(let* ((engine (ht-get pen-engines (sor final-engine
pen-default-engine)))
(keys (mapcar 'intern (mapcar 'slugify (ht-keys engine))))
(vals (ht-values engine))
(tups (-zip-lists keys vals))
(al (pen-list2alist tups))
(temp (cdr (assoc 'default-temperature al)))
(model (cdr (assoc 'model al)))
(lm-command (cdr (assoc 'lm-command al)))
(force-n-jobs (cdr (assoc 'force-n-jobs al)))
(api-endpoint (cdr (assoc 'api-endpoint al))))
;; (if temp
;; (setq final-temperature temp))
(if model
(setq final-model model))
(if lm-command
(setq final-lm-command lm-command))
(if api-endpoint
(setq final-api-endpoint api-endpoint))
(if force-n-jobs
(setq final-force-n-jobs force-n-jobs))
final-engine))
(final-n-jobs
(str (or
(sor (str final-force-n-jobs))
(pen-var-value-maybe 'n-jobs)
,n-jobs
pen-n-simultaneous-requests)))
(final-flags
(or (pen-var-value-maybe 'flags)
,flags))
(final-cant-n-complete
(or (pen-var-value-maybe 'cant-n-complete)
,cant-n-complete))
(final-evaluator
(or (pen-var-value-maybe 'evaluator)
,evaluator))
(final-variadic-var
(or (pen-var-value-maybe 'variadic-var)
,variadic-var))
(final-delimiter
(or (pen-var-value-maybe 'delimiter)
,delimiter
final-engine-delimiter))
(final-flags
(if final-flags
;; If this is broken then stuff it
(ignore-errors
(mapconcat
(lambda (s) (concat "<" s ">"))
(-filter
'identity
(pen-vector2list final-flags))
" "))
""))
;; hover, info and new-document are related
(final-info
(progn
(comment
(pen-log (concat "(pen-var-value-maybe 'do-etv) " (str (pen-var-value-maybe 'do-etv))))
(pen-log (concat "(pen-var-value-maybe 'info) " (str (pen-var-value-maybe 'info))))
(pen-log (concat "',info " (str ',info)))
(pen-log (concat "(not (pen-var-value-maybe 'no-info)) " (str (not (pen-var-value-maybe 'no-info))))))
(and (or (pen-var-value-maybe 'do-etv)
(pen-var-value-maybe 'info)
',info)
(not (pen-var-value-maybe 'no-info)))))
(final-setup
(if (not (pen-var-value-maybe 'do-pen-batch))
(or (pen-var-value-maybe 'setup)
',setup)))
(final-terminate
(if (not (pen-var-value-maybe 'do-pen-batch))
(or (pen-var-value-maybe 'terminate)
',terminate)))
(final-batch-setup
(if (pen-var-value-maybe 'do-pen-batch)
(or (pen-var-value-maybe 'batch-setup)
',batch-setup)))
(final-batch-terminate
(if (pen-var-value-maybe 'do-pen-batch)
(or (pen-var-value-maybe 'batch-terminate)
',batch-terminate)))
(final-setup
(if final-setup
(eval-string (concat "(progn " final-setup ")"))))
(final-batch-setup
(if final-batch-setup
(eval-string (concat "(progn " final-batch-setup ")"))))
(final-new-document
(and (or (pen-var-value-maybe 'do-etv)
(pen-var-value-maybe 'new-document)
',new-document)
(not (pen-var-value-maybe 'no-new-document))))
(final-utilises-code
(and (or (pen-var-value-maybe 'utilises-code)
',utilises-code)
(not
(or (pen-var-value-maybe 'utilises-code-off)
',utilises-code-off))))
(final-prepend-previous
(and
(not (pen-var-value-maybe 'no-prepend-previous))
(or (pen-var-value-maybe 'prepend-previous)
',prepend-previous)))
(final-hover
(or (pen-var-value-maybe 'hover)
',hover))
(final-linter
(or (pen-var-value-maybe 'linter)
',linter))
(final-formatter
(or (pen-var-value-maybe 'formatter)
',formatter))
(final-collation-temperature-stepper
(or (pen-var-value-maybe 'collation-temperature-stepper)
,collation-temperature-stepper))
(final-engine-whitespace-support
(or
(pen-var-value-maybe 'engine-whitespace-support)
,engine-whitespace-support))
(final-prompt-hist-id
(let ((phi
(or (pen-var-value-maybe 'pen-prompt-hist-id)
(pen-var-value-maybe 'prompt-hist-id))))
(if phi
(setq phi (slugify phi)))
phi))
(final-include-prompt
(or (pen-var-value-maybe 'pen-include-prompt)
(pen-var-value-maybe 'include-prompt)
,include-prompt))
(final-no-gen
(or (pen-var-value-maybe 'pen-no-gen)
(pen-var-value-maybe 'no-gen)
,no-gen))
(final-train-function
(or (pen-var-value-maybe 'pen-train-function)))
(final-force-results
(or (pen-var-value-maybe 'pen-force-results)))
(final-results-analyser
(or (pen-var-value-maybe 'results-analyser)
,results-analyser))
(final-analyse
(and
final-results-analyser
(pen-var-value-maybe 'analyse)))
(final-interpreter
(or (pen-var-value-maybe 'interpreter)
,interpreter))
(final-no-uniq-results
(or (pen-var-value-maybe 'no-uniq-results)
,no-uniq-results))
(final-expand-jinja
(or (pen-var-value-maybe 'expand-jinja)
,expand-jinja))
(final-start-yas
(or (pen-var-value-maybe 'start-yas)
,start-yas))
(final-end-yas
(or (pen-var-value-maybe 'yas)
(pen-var-value-maybe 'end-yas)
,yas
,end-yas))
(final-var-defaults
(or (pen-var-value-maybe 'var-defaults)
',var-defaults))
(final-subprompts
(or (pen-var-value-maybe 'subprompts)
,subprompts))
(final-defs
(or (pen-var-value-maybe 'defs)
',defs))
(final-envs
(pen--htlist-to-alist
(or (pen-var-value-maybe 'envs)
',envs)))
;; Pipelines are just some named shell pipelines that a specific to a prompt
;; that come with the prompt.
(final-pipelines
(or (pen-var-value-maybe 'pipelines)
',pipelines))
;; To use in preprocessors, postprocessor postpostprocessor
(pipelines-varvals
(asoc-merge
final-pipelines))
(final-preprocessors
(or (pen-var-value-maybe 'preprocessors)
',preprocessors))
(final-preprocessors
(if final-preprocessors
(mapcar (lambda (pp) (pen-expand-template-keyvals pp final-pipelines nil final-pipelines)) final-preprocessors)
final-preprocessors))
(final-subprompts-al
(if final-subprompts
(ht->alist (-reduce 'ht-merge (pen-vector2list final-subprompts)))))
(final-force-prompt
(or
override-prompt
(pen-var-value-maybe 'force-prompt)
,force-prompt))
(final-prompt
(or
final-force-prompt
,prompt))
(final-prompt (if final-start-yas
(pen-yas-expand-string final-prompt)
final-prompt))
(final-envs
;; Filter is needed because of ignore-errors
(-filter
'identity
(cl-loop
for atp in final-envs
collect
;; This required an ignore-errors
;; To fix eww.
;; Some image urls would kill lg-generate-alttext.
;; A bad last-final-command is formed
(ignore-errors
(cons
(car atp)
(let ((val (eval
`(pen-let-keyvals
',final-subprompts-al
(eval-string ,(str (cdr atp)))))))
(cond
((and (booleanp val)
val)
"y")
(t (str val)))))))))
(vals
;; If not called interactively then
;; manually run interactive expressions
;; when they exist.
(mapcar 'str
(if (not is-interactive)
(progn
(cl-loop
for sym in ',var-syms
for iarg in ',iargs
collect
(let* ((initval (eval sym)))
(if (and (not initval)
iarg)
(eval iarg)
initval))))
;; Don't include &key pretty
(cl-loop for v in ',var-syms until (eq v '&key) collect (eval v)))))
(last-vals-exprs vals)
(vals
(let ((varvals-sofar))
(cl-loop
;; (-zip-fill nil '(a b c) '(1 2 3) '("a" "b" "c"))
for tp in (-zip-fill nil ',var-syms vals final-var-defaults)
;; cl-loop is opaque to what has been so far set, so I keep track of current vals with varvals-sofar
collect
(let ((sym (first tp))
(val (second tp))
(default (third tp)))
(if (and (not (sor (second tp)))
(sor (third tp)))
;; set the second from the third
;; save to current varvals-sofar and use that in subsequent evaluations
;; TODO if a val is empty, apply the default with the subprompts in scope
(let* ((var-al
(asoc-merge
`((func-name . ,,func-name)
(do-pen-batch . ,do-pen-batch)
(pen-no-select-result . ,no-select-result))
varvals-sofar
final-subprompts-al))
(thowaway var-al)
(valtmp
(eval
;; let* implementation for vals
;; (assoc 'pos-tags (alist2pairs '((pos-tags . "hi"))))
`(pen-let-keyvals
',var-al
(eval-string ,(str default))))))
(pen-alist-set 'varvals-sofar sym valtmp)
valtmp)
val)))))
(last-vals vals)
(final-preprocessors
;; Unfortunately, can't do full template expansion here because we don't have vals. final-preprocessors is needed for vals
(cl-loop for fpp in final-preprocessors collect
(if fpp
(--> fpp
(pen-expand-template-keyvals it (-zip-fill "" ',vars vals))
(pen-expand-template-keyvals it (-zip-fill "" ',var-slugs vals))
(pen-expand-template-keyvals it pipelines-varvals)))))
;; preprocess the values of the parameters
(vals
(cl-loop
for tp in
(-zip-fill nil vals final-preprocessors)
collect
(let* ((v (car tp))
(pp (cdr tp)))
(if (sor final-delimiter)
(let ((sedcmd
(if (re-match-p "\n" final-delimiter)
;; Just avoid this safety measure,
;; if the delim contains a newline,
;; because escaping \n will cause problems
"cat"
(concat
"sed 's/" final-delimiter "/"
(pen-snc "sed 's=.=\\\\\\\\&=g'" final-delimiter)
"/'"))))
(if (sor pp)
(setq pp (concat sedcmd " | " pp))
(setq pp sedcmd))))
(if pp
(pen-sn pp v)
v))))
(final-prompt (if ,repeater
(if (< 0 (length vals))
(concat (pen-awk1 final-prompt)
(string-replace "{}" (str (car (last vals))) ,repeater))
(concat (pen-awk1 final-prompt)
,repeater))
final-prompt))
(var-keyvals (-zip ',vars vals))
(var-keyvals-slugged (-zip ',var-slugs vals))
;; When it comes to adding consistency, I must add consistency based on partial functions.
;; Otherwise, there'd be a single history/training which is prepended to all ifuntions.
;; OK, so how do we specify that?
;; I must specify 'constraint variables' for training .
;; When idefun is defined, specify a =prompt-hist-id=
;; The following needs
(parameter-slug
(s-join "."
(mapcar (lambda (kv)
(let* ((k (car kv))
(v (cdr kv))
(kslug (slugify (s-left 10 k)))
(vslug (slugify (s-left 10 v)))
(khash (s-left 10 (sha1 k)))
(vhash (s-left 10 (sha1 v)))
(kslug (concat kslug "-" khash))
(vslug (concat vslug "-" vhash)))
(concat kslug "_" vslug)))
var-keyvals-slugged)))
;; n-collate currently isn't template expanded
(final-n-collate
(or (pen-var-value-maybe 'n-collate)
,n-collate))
(final-n-max-collate
(or (pen-var-value-maybe 'n-max-collate)
,n-max-collate))
(final-n-target
(or (pen-var-value-maybe 'n-target)
,n-target))
;; (final-pretext
;; (expand-template pretext))
(final-interactive-inject
(or (pen-var-value-maybe 'pen-interactive-inject)
(pen-var-value-maybe 'interactive-inject)
',interactive-inject))
(final-inject-example
(expand-template ,inject-example))
(final-continue-default
(or (pen-var-value-maybe 'pen-continue-default)
(pen-var-value-maybe 'continue-default)
override-prompt
,continue-default))
(final-inject-examples
(cl-loop for e in ',inject-examples collect
(expand-template e)))
(final-inject-gen-start
(expand-template
(or
inject-gen-start
(pen-var-value-maybe 'inject-gen-start)
,inject-gen-start
(and final-interactive-inject
is-interactive
(not final-continue-default)
(if final-inject-examples
(read-string-hist
(concat ,func-name " " parameter-slug " inject: ")
(fz final-inject-examples nil nil
(concat ,func-name " " parameter-slug " inject: ")))
(read-string-hist (concat ,func-name " " parameter-slug " inject: ") final-inject-example))))))
(final-engine-max-n-completions
(expand-template
(str (or (pen-var-value-maybe 'engine-max-n-completions)
,engine-max-n-completions))))
(final-n-completions
(progn
(pen-log "(pen-var-value-maybe 'n-completions)" (pen-var-value-maybe 'n-completions))
(pen-log ",n-completions" (pen-var-value-maybe ,n-completions))
(expand-template
(str (or
(pen-var-value-maybe 'force-n-completions)
;; For some reason, the override is set to 5. Debug this
(pen-var-value-maybe 'n-completions)
,n-completions)))))
(final-n-completions
(progn
;; (pen-log "n-completions" final-n-completions)
;; (pen-log "final-engine-max-n-completions" final-engine-max-n-completions)
;; (pen-log "final-n-completions" (str (pen-hard-bound final-n-completions 1 final-engine-max-n-completions)))
(pen-hard-bound final-n-completions 1 final-engine-max-n-completions)))
(final-n-collate
(if (and
final-cant-n-complete
(nor final-n-collate)
(nor final-n-completions))
(setq final-n-collate
(* (pen-str2num
(or final-n-collate 1))
(pen-str2num
(or
final-n-completions 1))))))
;; min-tokens is not really adjustable
;; without enough tokens to run a prompt, it should fail
;; TODO Make a hard fail here
;; TODO Make a distinction between min and max generated tokens
;; and min and max prompt tokens
;; Can't do this yet anyway, because non-specified engine max becomes 0
;; and that changes max
;; (final-min-tokens (pen-hard-bound
;; final-min-tokens
;; final-engine-min-tokens
;; final-engine-max-tokens))
;; (final-max-tokens (pen-hard-bound
;; final-max-tokens
;; final-engine-min-tokens
;; final-engine-max-tokens))
(final-engine-min-generated-tokens
(pen-str2num
(expand-template
(str (or (pen-var-value-maybe 'engine-min-generated-tokens)
,engine-min-generated-tokens)))))
(final-engine-max-generated-tokens
(pen-str2num
(expand-template
(str (or (pen-var-value-maybe 'engine-max-generated-tokens)
,engine-max-generated-tokens)))))
(final-engine-min-tokens
(pen-str2num
(expand-template
(str (or (pen-var-value-maybe 'engine-min-tokens)
,engine-min-tokens)))))
(final-engine-max-tokens
(pen-str2num
(expand-template
(str (or (pen-var-value-maybe 'engine-max-tokens)
,engine-max-tokens
2048)))))
(final-min-tokens
(pen-str2num
(expand-template
(str (or (pen-var-value-maybe 'min-tokens)
,min-tokens)))))
(final-force-temperature
(or
(pen-var-value-maybe 'force-temperature)
,force-temperature))
(final-logprobs
(or
(pen-var-value-maybe 'logprobs)
,logprobs))
;; This is used for things such as beam search
;; Not a part of a prompt, usually.
;; But the depth of the beam might be a good configure option for a prompt.
(final-force-logprobs
(or
(pen-var-value-maybe 'force-logprobs)
,force-logprobs))
(final-logprobs
(or
(and
pen-logprobs-on
(or
final-force-logprobs
pen-force-logprobs
pen-default-logprobs
(pen-var-value-maybe 'logprobs)
,logprobs))
""))
(final-default-temperature
(expand-template
(str (or (pen-var-value-maybe 'default-temperature)
,default-temperature))))