-
Notifications
You must be signed in to change notification settings - Fork 79
/
chatgpt-shell-anthropic.el
234 lines (205 loc) · 9.65 KB
/
chatgpt-shell-anthropic.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-anthropic.el --- Anthropic-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 "28.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 Anthropic specifics for `chatgpt-shell'.
;;; Code:
(eval-when-compile
(require 'cl-lib))
(require 'shell-maker)
(require 'map)
(defvar chatgpt-shell-proxy)
(declare-function chatgpt-shell-previous-source-block "chatgpt-shell")
(defcustom chatgpt-shell-anthropic-key nil
"Anthropic 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-anthropic-api-url-base "https://api.anthropic.com"
"Anthropic API's base URL.
API url = base + path.
If you use Claude through a proxy service, change the URL base."
:type 'string
:safe #'stringp
:group 'chatgpt-shell)
(cl-defun chatgpt-shell-anthropic--make-model (&key version
short-version
token-width
max-tokens
context-window)
"Create an Anthropic model.
Set VERSION, SHORT-VERSION, TOKEN-WIDTH, MAX-TOKENS, CONTEXT-WINDOW and
VALIDATE-COMMAND handler."
(unless version
(error "Missing mandatory :version param"))
(unless token-width
(error "Missing mandatory :token-width param"))
(unless max-tokens
(error "Missing mandatory :max-tokens param"))
(unless context-window
(error "Missing mandatory :context-window 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))
`((:provider . "Anthropic")
(:label . "Claude")
(:path . "/v1/messages")
(:version . ,version)
(:max-tokens . ,max-tokens)
(:short-version . ,short-version)
(:token-width . ,token-width)
(:context-window . ,context-window)
(:handler . chatgpt-shell-anthropic--handle-claude-command)
(:filter . chatgpt-shell-anthropic--extract-claude-response)
(:payload . chatgpt-shell-anthropic--make-payload)
(:url . chatgpt-shell-anthropic--make-url)
(:headers . chatgpt-shell-anthropic--make-headers)
(:url-base . chatgpt-shell-anthropic-api-url-base)
(:key . chatgpt-shell-anthropic-key)
(:validate-command . chatgpt-shell-anthropic--validate-command)))
(defun chatgpt-shell-anthropic-models ()
"Build a list of Anthropic LLM models available."
(list
;; https://docs.anthropic.com/en/docs/about-claude/models#model-comparison-table
;; A token is equivalent to _about_ 4 characters.
(chatgpt-shell-anthropic--make-model :version "claude-3-5-sonnet-20241022"
:short-version "3-5-sonnet-20241022"
:token-width 4
:max-tokens 8192
:context-window 200000)
(chatgpt-shell-anthropic--make-model :version "claude-3-5-haiku-20241022"
:short-version "3-5-haiku-20241022"
:token-width 4
:max-tokens 8192
:context-window 200000)
(chatgpt-shell-anthropic--make-model :version "claude-3-opus-20240229"
:short-version "3-opus-20240229"
:token-width 4
:max-tokens 4096
:context-window 200000)))
(cl-defun chatgpt-shell-anthropic--make-url (&key _command model _settings)
"Create the API URL using MODEL and SETTINGS."
(concat (symbol-value (or (map-elt model :url-base)
(error "Model :url-base not found")))
(or (map-elt model :path)
(error "Model :path not found"))))
(defun chatgpt-shell-anthropic--validate-command (_command _model _settings)
"Return error string if command/setup isn't valid."
(unless chatgpt-shell-anthropic-key
"Variable `chatgpt-shell-anthropic-key' needs to be set to your key.
Try M-x set-variable chatgpt-shell-anthropic-key
or
(setq chatgpt-shell-anthropic-key \"my-key\")"))
(defun chatgpt-shell-anthropic-key ()
"Get the Anthropic API key."
(cond ((stringp chatgpt-shell-anthropic-key)
chatgpt-shell-anthropic-key)
((functionp chatgpt-shell-anthropic-key)
(condition-case _err
(funcall chatgpt-shell-anthropic-key)
(error
"KEY-NOT-FOUND")))
(t
nil)))
(cl-defun chatgpt-shell-anthropic--make-headers (&key _model _settings)
"Create the API headers."
(unless (chatgpt-shell-anthropic-key)
(error "Your chatgpt-shell-anthropic-key is missing"))
(list "Content-Type: application/json; charset=utf-8"
(concat "x-api-key: " (chatgpt-shell-anthropic-key))
"anthropic-version: 2023-06-01"))
(cl-defun chatgpt-shell-anthropic--make-payload (&key model context settings)
"Create the API payload using MODEL CONTEXT and SETTINGS."
(let ((context (mapcan (lambda (l)
(when (cdr l)
`(((role . "user")
(content . ,(car l)))
((role . "assistant")
(content . ,(cdr l))))))
context))
(command `(((role . "user")
(content . ,(caar (last context)))))))
(append
(when (map-elt settings :system-prompt)
`((system . ,(map-elt settings :system-prompt))))
`((max_tokens . ,(or (map-elt model :max-tokens)
(error "Missing %s :max-tokens" (map-elt model :name))))
(model . ,(map-elt model :version))
(stream . ,(if (map-elt settings :streaming) 't :false))
(messages . ,(vconcat
(append
context
command)))))))
(cl-defun chatgpt-shell-anthropic--handle-claude-command (&key model command context shell settings)
"Handle Claude shell COMMAND (prompt) using MODEL, CONTEXT, SHELL, and SETTINGS."
(shell-maker-make-http-request
:async t
:url (chatgpt-shell-anthropic--make-url :model model
:settings settings)
:proxy chatgpt-shell-proxy
:data (chatgpt-shell-anthropic--make-payload :model model
:context
(append
context
(list (cons command nil)))
:settings settings)
:headers (chatgpt-shell-anthropic--make-headers)
:filter #'chatgpt-shell-anthropic--extract-claude-response
:shell shell))
(defun chatgpt-shell-anthropic--extract-claude-response (raw-response)
"Extract Claude response from RAW-RESPONSE."
(if-let* ((whole (shell-maker--json-parse-string raw-response))
(response (or (let-alist whole
.error.message)
(let-alist whole
(mapconcat (lambda (content)
(let-alist content
.text))
.content)))))
response
(if-let ((chunks (shell-maker--split-text raw-response)))
(let ((response)
(pending)
(result))
(mapc (lambda (chunk)
;; Response chunks come in the form:
;; event: message_start
;; data: {...}
;; event: content_block_start
;; data: {...}
(if-let* ((is-data (equal (map-elt chunk :key) "data:"))
(obj (shell-maker--json-parse-string (map-elt chunk :value)))
(text (let-alist obj
(or .text
.content_block.text
.delta.text
.error.message
""))))
(unless (string-empty-p text)
(setq response (concat response text)))
(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)
(list (cons :filtered nil)
(cons :pending raw-response)))))
(provide 'chatgpt-shell-anthropic)
;;; chatgpt-shell-anthropic.el ends here