-
Notifications
You must be signed in to change notification settings - Fork 186
/
Copy pathgptel-openai-extras.el
276 lines (228 loc) · 10.3 KB
/
gptel-openai-extras.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
;;; gptel-openai-extras.el --- Extensions to the OpenAI API -*- lexical-binding: t; -*-
;; Copyright (C) 2023 Karthik Chikmagalur
;; Authors: Karthik Chikmagalur <karthikchikmagalur@gmail.com> and pirminj
;; This program 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.
;; 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This file adds support for Privategpt's Messages API and
;; Perplexity's Citations feature to gptel
;;; Code:
(require 'cl-generic)
(eval-when-compile
(require 'cl-lib))
(require 'map)
(require 'gptel)
(defvar json-object-type)
(declare-function prop-match-value "text-property-search")
(declare-function text-property-search-backward "text-property-search")
(declare-function json-read "json" ())
;;; Privategpt (Messages API)
(cl-defstruct (gptel-privategpt (:constructor gptel--make-privategpt)
(:copier nil)
(:include gptel-openai))
context sources)
(defun gptel--privategpt-parse-sources (response)
(cl-loop with source-alist
for source across (map-nested-elt response '(:choices 0 :sources))
for name = (map-nested-elt source '(:document :doc_metadata :file_name))
for page = (map-nested-elt source '(:document :doc_metadata :page_label))
do (push page (alist-get name source-alist nil nil #'equal))
finally return
(cl-loop for (file-name . file-pages) in source-alist
for pages = (delete-dups (delq nil file-pages))
if pages
collect (format "- %s (page %s)" file-name (mapconcat #'identity pages ", "))
into source-items
else collect (format "- %s" file-name) into source-items
finally return (mapconcat #'identity (cons "\n\nSources:" source-items) "\n"))))
;; FIXME(tool) add tool use
(cl-defmethod gptel-curl--parse-stream ((_backend gptel-privategpt) info)
(let* ((content-strs))
(condition-case nil
(while (re-search-forward "^data:" nil t)
(save-match-data
(if (looking-at " *\\[DONE\\]")
(when-let ((sources-string (plist-get info :sources)))
(push sources-string content-strs))
(let ((response (gptel--json-read)))
(unless (or (plist-get info :sources)
(not (gptel-privategpt-sources (plist-get info :backend))))
(plist-put info :sources (gptel--privategpt-parse-sources response)))
(let* ((delta (map-nested-elt response '(:choices 0 :delta)))
(content (plist-get delta :content)))
(push content content-strs))))))
(error
(goto-char (match-beginning 0))))
(apply #'concat (nreverse content-strs))))
;; FIXME(tool) add tool use
(cl-defmethod gptel--parse-response ((_backend gptel-privategpt) response info)
(let ((response-string (map-nested-elt response '(:choices 0 :message :content)))
(sources-string (and (gptel-privategpt-sources (plist-get info :backend))
(gptel--privategpt-parse-sources response))))
(concat response-string sources-string)))
(cl-defmethod gptel--request-data ((_backend gptel-privategpt) prompts)
"JSON encode PROMPTS for sending to ChatGPT."
(let ((prompts-plist
`(:model ,(gptel--model-name gptel-model)
:messages [,@prompts]
:use_context ,(or (gptel-privategpt-context gptel-backend) :json-false)
:include_sources ,(or (gptel-privategpt-sources gptel-backend) :json-false)
:stream ,(or gptel-stream :json-false))))
(when (and gptel--system-message
(not (gptel--model-capable-p 'nosystem)))
(plist-put prompts-plist :system gptel--system-message))
(when gptel-temperature
(plist-put prompts-plist :temperature gptel-temperature))
(when gptel-max-tokens
(plist-put prompts-plist :max_tokens gptel-max-tokens))
;; Merge request params with model and backend params.
(gptel--merge-plists
prompts-plist
(gptel-backend-request-params gptel-backend)
(gptel--model-request-params gptel-model))))
;;;###autoload
(cl-defun gptel-make-privategpt
(name &key curl-args stream key request-params
(header
(lambda () (when-let (key (gptel--get-api-key))
`(("Authorization" . ,(concat "Bearer " key))))))
(host "localhost:8001")
(protocol "http")
(models '(private-gpt))
(endpoint "/v1/chat/completions")
(context t) (sources t))
"Register an Privategpt API-compatible backend for gptel with NAME.
Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST (optional) is the API host, \"api.privategpt.com\" by default.
MODELS is a list of available model names.
STREAM is a boolean to toggle streaming responses, defaults to
false.
PROTOCOL (optional) specifies the protocol, https by default.
ENDPOINT (optional) is the API endpoint for completions, defaults to
\"/v1/messages\".
HEADER (optional) is for additional headers to send with each
request. It should be an alist or a function that retuns an
alist, like:
((\"Content-Type\" . \"application/json\"))
KEY is a variable whose value is the API key, or function that
returns the key.
CONTEXT and SOURCES: if true (the default), use available context
and provide sources used by the model to generate the response.
REQUEST-PARAMS (optional) is a plist of additional HTTP request
parameters (as plist keys) and values supported by the API. Use
these to set parameters that gptel does not provide user options
for."
(declare (indent 1))
(let ((backend (gptel--make-privategpt
:curl-args curl-args
:name name
:host host
:header header
:key key
:models models
:protocol protocol
:endpoint endpoint
:stream stream
:request-params request-params
:url (if protocol
(concat protocol "://" host endpoint)
(concat host endpoint))
:context context
:sources sources)))
(prog1 backend
(setf (alist-get name gptel--known-backends
nil nil #'equal)
backend))))
;;; Perplexity
(cl-defstruct (gptel-perplexity (:constructor gptel--make-perplexity)
(:copier nil)
(:include gptel-openai)))
(cl-defmethod gptel--parse-response ((_backend gptel-perplexity) response info)
"Parse Perplexity response RESPONSE with INFO."
(let ((response-string (map-nested-elt response '(:choices 0 :message :content)))
(citations-string (when-let ((citations (map-elt response :citations)))
(gptel--perplexity-parse-citations citations))))
(concat response-string citations-string)))
(cl-defmethod gptel-curl--parse-stream ((backend gptel-perplexity) info)
"Parse a Perplexity API data stream for BACKEND with INFO.
If available, collect citations at the end and include them with
the response."
(let ((resp (cl-call-next-method)))
(unless (plist-get info :citations)
(save-excursion
(goto-char (point-max))
(when (search-backward (plist-get info :token)
(line-beginning-position) t)
(forward-line 0)
(when (re-search-backward "^data: " nil t)
(goto-char (match-end 0))
(ignore-errors
(when-let* ((chunk (gptel--json-read))
(citations (map-elt chunk :citations)))
(plist-put info :citations t)
(setq resp (concat resp (gptel--perplexity-parse-citations
citations)))))))))
resp))
(defsubst gptel--perplexity-parse-citations (citations)
(let ((counter 0))
(concat "\n\nCitations:\n"
(mapconcat (lambda (url)
(setq counter (1+ counter))
(format "[%d] %s" counter url))
citations "\n"))))
;;;###autoload
(cl-defun gptel-make-perplexity
(name &key curl-args stream key
(header
(lambda () (when-let (key (gptel--get-api-key))
`(("Authorization" . ,(concat "Bearer " key))))))
(host "api.perplexity.ai")
(protocol "https")
(models '(sonar sonar-pro))
(endpoint "/chat/completions")
request-params)
"Register a Perplexity backend for gptel with NAME.
Keyword arguments:
CURL-ARGS (optional) is a list of additional Curl arguments.
HOST (optional) is the API host, \"api.perplexity.ai\" by default.
MODELS is a list of available model names.
STREAM is a boolean to toggle streaming responses.
PROTOCOL (optional) specifies the protocol, https by default.
ENDPOINT (optional) is the API endpoint for completions.
HEADER (optional) is for additional headers to send with each
request. It should be an alist or a function that returns an
alist.
KEY is a variable whose value is the API key, or function that
returns the key.
REQUEST-PARAMS (optional) is a plist of additional HTTP request
parameters."
(declare (indent 1))
(let ((backend (gptel--make-perplexity
:curl-args curl-args
:name name
:host host
:header header
:key key
:models models
:protocol protocol
:endpoint endpoint
:stream stream
:request-params request-params
:url (if protocol
(concat protocol "://" host endpoint)
(concat host endpoint)))))
(prog1 backend
(setf (alist-get name gptel--known-backends
nil nil #'equal)
backend))))
(provide 'gptel-openai-extras)
;;; gptel-openai-extras.el ends here