-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathchatgpt-shell-perplexity.el
234 lines (200 loc) · 9.17 KB
/
chatgpt-shell-perplexity.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
;;; chatgpt-shell-perplexity.el --- Perplexity-specific logic -*- lexical-binding: t -*-
;; Copyright (C) 2023 Alvaro Ramirez
;; Author: Alvaro Ramirez https://xenodium.com
;; URL: https://github.com/xenodium/chatgpt-shell
;; Package-Requires: ((emacs "29.1") (shell-maker "0.72.1"))
;; This package 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, or (at your option)
;; any later version.
;; This package 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 GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Adds Perplexity specifics for `chatgpt-shell'.
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'let-alist)
(require 'shell-maker)
(require 'map)
(require 'seq)
(require 'subr-x)
(defvar chatgpt-shell-proxy)
(declare-function chatgpt-shell-openai--make-payload "chatgpt-shell-openai")
(cl-defun chatgpt-shell-perplexity-make-model (&key version
short-version
token-width
context-window)
"Create an Perplexity model.
Set VERSION, SHORT-VERSION, TOKEN-WIDTH, CONTEXT-WINDOW and
VALIDATE-COMMAND handler."
(unless version
(error "Missing mandatory :version param"))
(unless token-width
(error "Missing mandatory :token-width param for %s" version))
(unless context-window
(error "Missing mandatory :context-window param for %s" version))
(unless (integerp token-width)
(error ":token-width must be an integer"))
(unless (integerp context-window)
(error ":context-window must be an integer"))
`((:provider . "Perplexity")
(:label . "Perplexity")
(:version . ,version)
(:short-version . ,short-version)
(:token-width . ,token-width)
(:context-window . ,context-window)
(:headers . chatgpt-shell-perplexity--make-headers)
(:handler . chatgpt-shell-perplexity--handle-perplexity-command)
(:filter . chatgpt-shell-perplexity--extract-perplexity-response)
(:payload . chatgpt-shell-openai--make-payload)
(:url . chatgpt-shell-perplexity--make-url)
(:validate-command . chatgpt-shell-perplexity--validate-command)))
(defcustom chatgpt-shell-perplexity-key nil
"Perplexity API key as a string or a function that loads and returns it."
:type '(choice (function :tag "Function")
(string :tag "String"))
:group 'chatgpt-shell)
(defcustom chatgpt-shell-perplexity-api-url-base "https://api.perplexity.ai"
"Perplexity API's base URL.
API url = base + path.
If you use Perplexity through a proxy service, change the URL base."
:type 'string
:safe #'stringp
:group 'chatgpt-shell)
(defun chatgpt-shell-perplexity-models ()
"Build a list of Perplexity LLM models available."
(list (chatgpt-shell-perplexity-make-model
:version "llama-3.1-sonar-small-128k-online"
:short-version "llama-3.1"
;; TODO: Find a reference.
:token-width 4
;; https://openrouter.ai/perplexity/llama-3.1-sonar-small-128k-online
:context-window 127072)))
(defun chatgpt-shell-perplexity-key ()
"Get the perplexity API key."
(cond ((stringp chatgpt-shell-perplexity-key)
chatgpt-shell-perplexity-key)
((functionp chatgpt-shell-perplexity-key)
(condition-case _err
(funcall chatgpt-shell-perplexity-key)
(error
"KEY-NOT-FOUND")))
(t
nil)))
(cl-defun chatgpt-shell-perplexity--make-headers (&key _model _settings)
"Create the API headers."
(unless (chatgpt-shell-perplexity-key)
(error "Your chatgpt-shell-perplexity-key is missing"))
(list "Content-Type: application/json; charset=utf-8"
(format "Authorization: Bearer %s" (chatgpt-shell-perplexity-key))))
(cl-defun chatgpt-shell-perplexity--handle-perplexity-command (&key model command context shell settings)
"Handle Perplexity shell COMMAND (prompt).
Uses MODEL, CONTEXT, SHELL, and SETTINGS."
(shell-maker-make-http-request
:async t
:url (chatgpt-shell-perplexity--make-url :model model
:settings settings)
:proxy chatgpt-shell-proxy
:data (chatgpt-shell-openai--make-payload :model model
:context
(append
context
(list (cons command nil)))
:settings settings)
:headers (chatgpt-shell-perplexity--make-headers)
:filter #'chatgpt-shell-perplexity--extract-perplexity-response
:shell shell))
(cl-defun chatgpt-shell-perplexity--make-url (&key _command _model _settings)
"Create the API URL using MODEL and SETTINGS."
(concat chatgpt-shell-perplexity-api-url-base
"/chat/completions"))
(defun chatgpt-shell-perplexity--extract-perplexity-response (raw-response)
"Extract Perplexity response from RAW-RESPONSE.
When Perplexity responses are streamed, they arrive in the form:
data: {...json...}
data: {...jdon...}
Otherwise:
{...json...}."
;; Non-streamed
(if-let* ((whole (shell-maker--json-parse-string raw-response))
(response (or (let-alist whole
.error.message)
(let-alist whole
(mapconcat (lambda (choice)
(let-alist choice
(cond ((not (string-empty-p .delta.content))
.delta.content)
((not (string-empty-p .message.content))
.message.content)
(t
""))))
.choices "")))))
(let ((citations (let-alist whole
.citations)))
(chatgpt-shell-perplexity--expand-citations
response citations))
;; Streamed
(when-let ((chunks (shell-maker--split-text raw-response)))
(let ((response)
(pending)
(result))
(mapc (lambda (chunk)
(let ((citations))
;; Response chunks come in the form:
;; data: {...}
;; data: {...}
(if-let* ((is-data (equal (map-elt chunk :key) "data:"))
(obj (shell-maker--json-parse-string (map-elt chunk :value)))
(text (or
;; .choices[i].message.content
;; .choices[i].delta.content
(let-alist obj
(mapconcat (lambda (choice)
(let-alist choice
(or .delta.content
.message.content)))
.choices "")))))
(progn
(setq citations (let-alist obj
.citations))
(unless (string-empty-p text)
(setq response (concat response
(chatgpt-shell-perplexity--expand-citations
text citations)))))
(setq pending (concat pending
(or (map-elt chunk :key) "")
(map-elt chunk :value))))))
chunks)
(setq result
(list (cons :filtered (unless (string-empty-p response)
response))
(cons :pending pending)))
result))))
(defun chatgpt-shell-perplexity--expand-citations (text citations)
"Expand citation placeholders in TEXT using CITATIONS."
(if (and text citations)
(replace-regexp-in-string
"\\[\\([0-9]+\\)\\]"
(lambda (match)
(let* ((index (string-to-number (match-string 1 match)))
(url (nth (1- index)
;; Append converts arrays to lists.
(append citations nil))))
(when url
(format " [%d](%s)" index url))))
text)
text))
(defun chatgpt-shell-perplexity--validate-command (_command _model _settings)
"Return error string if command/setup isn't valid."
(unless chatgpt-shell-perplexity-key
"Variable `chatgpt-shell-perplexity-key' needs to be set to your key.
Try M-x set-variable chatgpt-shell-perplexity-key
or
(setq chatgpt-shell-perplexity-key \"my-key\")"))
(provide 'chatgpt-shell-perplexity)
;;; chatgpt-shell-perplexity.el ends here