forked from rksm/org-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg-ai-block.el
241 lines (203 loc) · 10.4 KB
/
org-ai-block.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
;;; org-ai-block.el --- org-ai special block helpers -*- lexical-binding: t; -*-
;; This file is NOT part of GNU Emacs.
;; org-ai.el is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; org-ai.el 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with org-ai.el.
;; If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Defines functions for dealing with #+begin_ai..#+end_ai special blocks
;;; Code:
(require 'org)
(require 'org-element)
(when (and (boundp 'org-protecting-blocks) (listp org-protecting-blocks))
(add-to-list 'org-protecting-blocks "ai"))
(defun org-ai-special-block ()
"Are we inside a #+begin_ai...#+end_ai block?"
(let (org-element-use-cache) ;; with cache enabled we get weird Cached element is incorrect warnings
(cl-loop with context = (org-element-context)
while (and context (not (equal 'special-block (org-element-type context))))
do (setq context (org-element-property :parent context))
finally return context)))
(defun org-ai-get-block-info (&optional context)
"Parse the header of #+begin_ai...#+end_ai block.
`CONTEXT' is the context of the special block. Return an alist of
key-value pairs."
(let* ((context (or context (org-ai-special-block)))
(header-start (org-element-property :post-affiliated context))
(header-end (org-element-property :contents-begin context))
(string (string-trim (buffer-substring-no-properties header-start header-end)))
(string (string-trim-left (replace-regexp-in-string "^#\\+begin_ai" "" string))))
(org-babel-parse-header-arguments string)))
(defun org-ai-get-block-content (&optional context)
"Extracts the text content of the #+begin_ai...#+end_ai block.
`CONTEXT' is the context of the special block."
(let* ((context (or context (org-ai-special-block)))
(content-start (org-element-property :contents-begin context))
(content-end (org-element-property :contents-end context)))
(string-trim (buffer-substring-no-properties content-start content-end))))
(defun org-ai--request-type (info)
"Look at the header of the #+begin_ai...#+end_ai block.
returns the type of request. `INFO' is the alist of key-value
pairs from `org-ai-get-block-info'."
(cond
((not (eql 'x (alist-get :chat info 'x))) 'chat)
((not (eql 'x (alist-get :completion info 'x))) 'completion)
((not (eql 'x (alist-get :image info 'x))) 'image)
(t 'chat)))
(defun org-ai--chat-role-regions ()
"Splits the special block by role prompts."
(if-let* ((context (org-ai-special-block))
(content-start (org-element-property :contents-begin context))
(content-end (org-element-property :contents-end context)))
(let ((result (save-excursion
(goto-char content-start)
(cl-loop with result
while (search-forward-regexp "\\[SYS\\]:\\|\\[ME\\]:\\|\\[AI\\]:" content-end t)
do (push (match-beginning 0) result)
finally return result))))
(if result
(cl-concatenate 'list (list content-start) (reverse result) (list content-end))
(list content-start content-end)))))
(defun org-ai-mark-last-region ()
"Marks the last prompt in an org-ai block."
(interactive)
(when-let* ((regions (reverse (org-ai--chat-role-regions)))
(last-region-end (pop regions))
(last-region-start (pop regions)))
(goto-char last-region-end)
(push-mark last-region-start t t)))
(defun org-ai-mark-region-at-point ()
"Marks the prompt at point."
(interactive)
(when-let* ((regions (org-ai--chat-role-regions))
(start (cl-find-if (lambda (x) (>= (point) x)) (reverse regions)))
(end (cl-find-if (lambda (x) (<= (point) x)) regions)))
(when (= start end)
(setq end (cl-find-if (lambda (x) (< start x)) regions)))
(when (not end)
(setq end start)
(setq start (cl-find-if (lambda (x) (> end x)) (reverse regions))))
(when (and start end)
(goto-char start)
(push-mark end t t)
(cons start end))))
(defun org-ai-kill-region-at-point (&optional arg)
"Kills the prompt at point.
The numeric `ARG' can be used for killing the last n."
(interactive "P")
(cl-loop repeat (or arg 1)
do (when-let ((region (org-ai-mark-region-at-point)))
(cl-destructuring-bind (start . end) region
(kill-region end start)))))
(defun org-ai--collect-chat-messages (content-string &optional default-system-prompt persistant-sys-prompts)
"Takes `CONTENT-STRING' and splits it by [SYS]:, [ME]: and [AI]: markers.
If `PERSISTANT-SYS-PROMPTS' is non-nil, [SYS] prompts are
intercalated. The [SYS] prompt used is either
`DEFAULT-SYSTEM-PROMPT' or the first [SYS] prompt found in
`CONTENT-STRING'."
(with-temp-buffer
(erase-buffer)
(insert content-string)
(goto-char (point-min))
(let* (;; collect all positions before [ME]: and [AI]:
(sections (cl-loop while (search-forward-regexp "\\[SYS\\]:\\|\\[ME\\]:\\|\\[AI\\]:" nil t)
collect (save-excursion
(goto-char (match-beginning 0))
(point))))
;; make sure we have from the beginning if there is no first marker
(sections (if (not sections)
(list (point-min))
(if (not (= (car sections) (point-min)))
(cons (point-min) sections)
sections)))
(parts (cl-loop for (start end) on sections by #'cdr
collect (string-trim (buffer-substring-no-properties start (or end (point-max))))))
;; if no role is specified, assume [ME]
(parts (if (and
(not (string-prefix-p "[SYS]:" (car parts)))
(not (string-prefix-p "[ME]:" (car parts)))
(not (string-prefix-p "[AI]:" (car parts))))
(progn (setf (car parts) (concat "[ME]: " (car parts)))
parts)
parts))
;; create (:role :content) list
(messages (cl-loop for part in parts
collect (cl-destructuring-bind (type &rest content) (split-string part ":")
(let ((type (string-trim type))
(content (string-trim (string-join content ":"))))
(list :role (cond ((string= type "[SYS]") 'system)
((string= type "[ME]") 'user)
((string= type "[AI]") 'assistant)
(t 'assistant))
:content content)))))
(messages (cl-remove-if-not (lambda (x) (not (string-empty-p (plist-get x :content)))) messages))
;; merge messages with same role
(messages (cl-loop with last-role = nil
with result = nil
for (_ role _ content) in messages
if (eql role last-role)
do (let ((last (pop result)))
(push (list :role role :content (string-join (list (plist-get last :content) content) "\n")) result))
else
do (push (list :role role :content content) result)
do (setq last-role role)
finally return (reverse result)))
(starts-with-sys-prompt-p (and messages (eql (plist-get (car messages) :role) 'system)))
(sys-prompt (if starts-with-sys-prompt-p
(plist-get (car messages) :content)
default-system-prompt))
(messages (if persistant-sys-prompts
(cl-loop with result = nil
for (_ role _ content) in messages
if (eql role 'assistant)
do (push (list :role 'assistant :content content) result)
else if (eql role 'user)
do (progn
(push (list :role 'system :content sys-prompt) result)
(push (list :role 'user :content content) result))
finally return (reverse result))
messages)))
(apply #'vector messages))))
;; deal with unspecified prefix
(cl-assert
(equal
(let ((test-string "\ntesting\n [ME]: foo bar baz zorrk\nfoo\n[AI]: hello hello[ME]: "))
(org-ai--collect-chat-messages test-string))
'[(:role user :content "testing\nfoo bar baz zorrk\nfoo")
(:role assistant :content "hello hello")]))
;; sys prompt
(cl-assert
(equal
(let ((test-string "[SYS]: system\n[ME]: user\n[AI]: assistant"))
(org-ai--collect-chat-messages test-string))
'[(:role system :content "system")
(:role user :content "user")
(:role assistant :content "assistant")]))
;; sys prompt intercalated
(cl-assert
(equal
(let ((test-string "[SYS]: system\n[ME]: user\n[AI]: assistant\n[ME]: user"))
(org-ai--collect-chat-messages test-string nil t))
'[(:role system :content "system")
(:role user :content "user")
(:role assistant :content "assistant")
(:role system :content "system")
(:role user :content "user")]))
;; merge messages with same role
(cl-assert
(equal
(let ((test-string "[ME]: hello [ME]: world")) (org-ai--collect-chat-messages test-string))
'[(:role user :content "hello\nworld")]))
;; (comment
;; (with-current-buffer "org-ai-mode-test.org"
;; (org-ai--collect-chat-messages (org-ai-get-block-content))))
;; -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
(provide 'org-ai-block)
;;; org-ai-block.el ends here