-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcui-prompt.el
More file actions
223 lines (201 loc) · 11.5 KB
/
Copy pathcui-prompt.el
File metadata and controls
223 lines (201 loc) · 11.5 KB
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
;;; cui-prompt.el --- Chains of requests to LLM -*- lexical-binding: t; -*-
;; Copyright (C) 2025 github.com/Anoncheg1,codeberg.org/Anoncheg
;; Author: <github.com/Anoncheg1,codeberg.org/Anoncheg>
;; SPDX-License-Identifier: AGPL-3.0-or-later
;;; License
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU Affero General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU Affero General Public License for more details.
;; You should have received a copy of the GNU Affero General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;; Licensed under the GNU Affero General Public License, version 3 (AGPLv3)
;; <https://www.gnu.org/licenses/agpl-3.0.en.html>
;;; Commentary:
;;
;; `cui-agent-call-function' -> `cui-prompt-request-switch' -> `cui-prompt-request-chain'
;;
;; Re1
;; Sys: You a helpful. Give plan of 3 parts to research for answer
;; and do only first part. user: How to make it?
;;
;; "choices": ["message": {"role": "assistant", "content": "To..."}}]
;;
;; Re2
;; Sys: You a helpful. Give plan of 3 parts to research for answer
;; and do only first part.
;; user: How to make it?
;; Assist: Plan and solution for 1) step.
;; user: Research 2-th part and what was missed before.
;;
;; Re3
;; Sys: You a helpful. Give plan of 3 parts to research for answer
;; and do only first part with summary.
;; user: How to make it?
;; Assist: Plan and solution for 1) step.
;; user: Research 2-th part and what was missed before.
;; Assist: sum for 1), new plan, 2) step.
;; user: Research 3-th part and what was missed before, summarize
;; results give final answer.
;; -=-= includes
(require 'cui-block)
(require 'cui-block-msgs)
(require 'cui-block-tags)
(require 'cui-restapi)
(require 'cui-async1)
(require 'cui-timers)
;;; Code:
;; -=-= all
(defvar cui-prompt-chain-list
(list "Give three steps plan. Do only the first step of the plan. Provide tiny seed answer."
"Complete the second step of plan only. Enhance answer."
"Do the third step. Provide a final full answer."))
(defun cui-prompt-collect-chat-research-steps-prompt (commands ind messages &optional default-system-prompt max-tokens)
"Compose messages for LLM for IND step of COMMANDS.
Add to result of `cui-restapi--collect-chat-messages' CoT prompts.
Compose IND request for COMMANDS and ind-1 response.
MESSAGES is result of `cui-restapi-prepare-content'.
IND count from 0. RESP-QUEST is list of string of lengh IND+1 - raw
content of cui block or answer from LLM. We assume that commands and AI
answers except of the first one are already in MESSAGES."
(let* ((recom (if (and cui-restapi-add-max-tokens-recommendation max-tokens)
(cui-restapi--get-length-recommendation max-tokens)))
(comm0 (nth 0 commands))
(comm0 (if (and (= ind 0) recom)
(concat comm0 " " recom)
comm0))
(comm0 (if (and default-system-prompt (not (string-empty-p default-system-prompt)))
(concat default-system-prompt " " comm0)
;; else
comm0))
(comm (nth ind commands))
(comm (if recom (concat comm " " recom) comm))
(sys0 (list :role 'system :content
comm0)))
(apply #'vector sys0 (append messages
;; command after AI answer
(when (> ind 0)
(list (list :role 'system :content comm)))))))
(defun cui-prompt-request-prepare-chain (&rest args)
"Check if there is :chain at cui block parameters and call chain function.
For assiging to `cui-agent-call-function' with all normal ARGS.
Return t if we replace default call implementation
`cui-restapi-request-prepare'."
;; element = (nth 1 args)
(when (not (eql 'x (alist-get :chain (cui-block-get-info (nth 1 args)) 'x)))
(apply #'cui-prompt-request-chain args)
t))
(defun cui-prompt-prepare-chain-prepare (step header-marker noweb-control sys-prompt max-tokens)
"Prepare messages for request in STEP of chain.
Use `cui-prompt-chain-list'.
Arguments
- HEADER-MARKER is a result of `cui-block-get-header-marker' function
for cui block.
- NOWEB-CONTROL SYS-PROMPT MAX-TOKENS, explained in
`cui-restapi-request-prepare' function."
(let* ((messages (with-current-buffer (marker-buffer header-marker)
;; get messages vector
(cui-block-tags-get-content-ai-messages (cui-block-element-by-marker header-marker)
noweb-control
nil ; links-only-last
nil ; not-clear-properties
nil ; cui-block-markers
nil ; disable-tags
'chat)))
(messages (cui-prompt-collect-chat-research-steps-prompt cui-prompt-chain-list
step
messages
sys-prompt
max-tokens))
(messages (cui-block-msgs--modify-vector-content messages #'cui-block-tags-replace 'user))
(messages (cui-block-msgs--modify-vector-content messages #'cui-block-tags--clear-properties 'user))
;; (messages (cui-block--pipeline cui-restapi-after-prepare-messages-hook messages))
)
messages))
(defun cui-prompt-request-chain (req-type element model max-tokens top-p temperature frequency-penalty presence-penalty service stream sys-prompt noweb-control)
"Use :chain parameter to activate and use :step to execute chain of prompt.
Aspects:
1) start and stop reporter at begining and at the end (final callback).
2) error handling: kill reporter, kill tmp buffer, kill timers
Execution Chain:
`cui-restapi-request-llm-retries'
`cui-restapi--url-request-slim'
Modeline notification:
1) `cui-timers--set' used in `cui-restapi-request-llm-retries'.
2) `cui-timers--set' here
3) `cui-timers--progress-reporter-run' - here
For REQ-TYPE, ELEMENT, NOWEB-CONTROL, SYS-PROMPT,
SYS-PROMPT-FOR-ALL-MESSAGES, MODEL, MAX-TOKENS, TOP-P, TEMPERATURE,
FREQUENCY-PENALTY, PRESENCE-PENALTY, SERVICE, STREAM, INFO see
`cui-restapi-request-prepare'."
;; element noweb-control sys-prompt model max-tokens top-p temperature frequency-penalty presence-penalty service _stream &optional _info
;; (if (not (eql 'x (alist-get :chain (cui-block-get-info element) 'x))) ; check if :my exist
(cui--debug "cui-prompt-request-chain service, model, buf: %s %s %s" service model (current-buffer))
;; - My request
(let ((service (or service 'github))
(end-marker (cui-block--get-content-end-marker element))
(header-marker (cui-block-get-header-marker element))
;; (gap-between-requests 3) ; TODO
;; (step (alist-get :step (cui-block-get-info element))) ; Works? not tested TODO
(cui-timers-duration-copy cui-timers-duration)
(cui-timers-retries-copy cui-timers-retries))
(let ((call (lambda (step) ; called 3 times
(lambda (_data callback)
(cui--debug "cui-prompt-request-chain1 step %s" step) ; 0, 1, 2
(cui--debug "cui-prompt-request-chain1 buffer %s" (current-buffer))
(cui--debug "cui-prompt-request-chain1 max-tokens %s header-marker %s sys-prompt %s" max-tokens header-marker sys-prompt)
(let* ((content (cui-prompt-prepare-chain-prepare step header-marker noweb-control sys-prompt max-tokens))
(params (cui-block--pipeline-macro (req-type content element model max-tokens top-p temperature frequency-penalty presence-penalty service stream)
cui-block-msgs-after-prepare-messages-hook)))
(seq-let (_req-type content _element model max-tokens top-p temperature frequency-penalty presence-penalty service _stream) params
;; also save request for timer
(cui-restapi-request-llm-retries service
model
cui-timers-duration-copy ; use current-buffer
callback
:retries cui-timers-retries-copy ; use current-buffer
:messages content
:max-tokens max-tokens
:header-marker header-marker
:temperature temperature
:top-p top-p
:frequency-penalty frequency-penalty
:presence-penalty presence-penalty))))))
(callbackmy (lambda (data callback)
"Called in (current-buffer)."
(when data ; if not data it is fail
(cui--debug "calbackmy %s %s %s" cui-timers--element-marker-variable-dict (current-buffer) data)
(cui-block--insert-single-response end-marker data nil 'not-final)
(run-at-time 0 nil callback data))))
(calbafin (lambda (data _callback)
(when data ; if not data it is fail
(cui--debug "calbafin")
(cui-block--insert-single-response end-marker data t)
(cui-timers--interrupt-current-request (cui-timers--get-keys-for-variable header-marker) #'cui-restapi--stop-tracking-url-request)))))
(cui--debug "cui-prompt-request-chain2 %s %s %s %s" header-marker service model cui-timers-duration)
(condition-case err
(progn
(cui-timers--progress-reporter-run #'cui-restapi--stop-tracking-url-request (* cui-timers-duration cui-timers-retries-copy) )
(cui--debug "cui-prompt-request-chain3")
;; There is a problem that we handle error in callback before timer may be run.
;; And we can't run timer before.
(cui-async1-start nil
(list (funcall call 0)
callbackmy
(funcall call 1)
callbackmy
(funcall call 2)
calbafin))
(cui--debug "cui-prompt-request-chain4"))
(user-error
(funcall cui-restapi-show-error-function (error-message-string err)
header-marker)
(cui-timers--interrupt-current-request (cui-timers--get-keys-for-variable header-marker) #'cui-restapi--stop-tracking-url-request))))))
;;; provide
(provide 'cui-prompt)
;;; cui-prompt.el ends here