blob: 4807fa9c3cb5fd0343824452914d7b34e7afda0a (
plain)
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
|
;;; llm-chat-claude.el --- Implements claude llm intergration -*- lexical-binding: t; -*-
;; This file is not part of GNU Emacs.
;;; License:
;; llm-chat-claude.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.
;; llm-chat-claude.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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Code:
(require 'llm-chat-api)
(defvar llm-chat-claude-models '("claude-3-7-sonnet-latest" "claude-3-5-haiku-latest" "claude-3-5-sonnet-latest" "claude-3-sonnet-20240229" "claude-3-haiku-20240307"))
(cl-defgeneric llm-chat-resolve-model-backend ((backend (eql 'claude))
model)
(if (member model llm-chat-claude-models)
t))
(defun llm-chat-claude-get-model ()
(if (member (llm-chat-get-model) llm-chat-claude-models)
(llm-chat-get-model)
"claude-3-haiku-20240307"))
(defun llm-chat-make-claude-backend ()
(make-llm-chat-backend
:api-endpoint "https://api.anthropic.com/v1/messages"
:api-key (auth-source-pick-first-password :host "api.anthropic.com")
:headers-fn #'llm-chat-claude-headers
:json-fn #'llm-chat-claude-json
:filter-fn #'llm-chat-claude-filter))
(defun llm-chat-claude-json ()
(let ((json-object `((model . ,(llm-chat-claude-get-model))
(system . ,(llm-chat-get-system-prompt))
(messages . ,(cdr (llm-chat-get-history)))
(temperature . ,(llm-chat-get-temperature))
(max_tokens . 4096)
(stream . ,(llm-chat-get-stream?)))))
(json-encode json-object)))
(defun llm-chat-claude-headers (backend)
(list "-H" "anthropic-version: 2023-06-01"
"-H" (concat "x-api-key: " (llm-chat-backend-api-key backend))))
(defun llm-chat-claude-filter (proc string)
(llm-chat-data-filter proc string
#'llm-chat-claude-finished-p
#'llm-chat-claude-parse-content))
(defun llm-chat-claude-finished-p (string)
(let* ((json-object (json-read-from-string string))
(type (alist-get 'type json-object)))
(string-equal type "message_stop")))
(defun llm-chat-claude-parse-content (string)
(when-let* ((json-object (json-read-from-string string))
(type (alist-get 'type json-object))
(delta (alist-get 'delta json-object))
(text (alist-get 'text delta)))
(when (string-equal type "content_block_delta")
text)))
(provide 'llm-chat-claude)
;;; llm-chat-claude.el ends here
|