-
Notifications
You must be signed in to change notification settings - Fork 30
/
llm-claude.el
170 lines (146 loc) · 7.9 KB
/
llm-claude.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
;;; llm-claude.el --- llm module for integrating with Claude -*- lexical-binding: t; package-lint-main-file: "llm.el"; -*-
;; Copyright (c) 2024 Free Software Foundation, Inc.
;; Author: Andrew Hyatt <[email protected]>
;; Homepage: https://github.com/ahyatt/llm
;; SPDX-License-Identifier: GPL-3.0-or-later
;;
;; 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file implements the llm functionality defined in llm.el, for Claude's
;; API.
;;; Code:
(require 'llm)
(require 'llm-provider-utils)
(require 'plz-event-source)
(require 'rx)
;; Models defined at https://docs.anthropic.com/claude/docs/models-overview
(cl-defstruct (llm-claude (:include llm-standard-chat-provider))
(key nil :read-only t)
(chat-model "claude-3-5-sonnet-20241022" :read-only t))
(cl-defmethod llm-nonfree-message-info ((_ llm-claude))
"Return Claude's nonfree ToS."
"https://www.anthropic.com/legal/consumer-terms")
(cl-defmethod llm-provider-prelude ((provider llm-claude))
(unless (llm-claude-key provider)
(error "No API key provided for Claude")))
(defun llm-claude--tool-call (call)
"A Claude version of a function spec for CALL."
`(("name" . ,(llm-function-call-name call))
("description" . ,(llm-function-call-description call))
("input_schema" . ,(llm-provider-utils-openai-arguments
(llm-function-call-args call)))))
(cl-defmethod llm-provider-chat-request ((provider llm-claude) prompt stream)
(let ((request `(("model" . ,(llm-claude-chat-model provider))
("stream" . ,(if stream t :json-false))
;; Claude requires max_tokens
("max_tokens" . ,(or (llm-chat-prompt-max-tokens prompt) 4096))
("messages" .
,(mapcar (lambda (interaction)
`(("role" . ,(pcase (llm-chat-prompt-interaction-role interaction)
('function 'user)
('assistant 'assistant)
('user 'user)))
("content" .
,(if (llm-chat-prompt-interaction-function-call-results interaction)
(mapcar (lambda (result)
`(("type" . "tool_result")
("tool_use_id" .
,(llm-chat-prompt-function-call-result-call-id
result))
("content" .
,(llm-chat-prompt-function-call-result-result result))))
(llm-chat-prompt-interaction-function-call-results interaction))
(llm-chat-prompt-interaction-content interaction)))))
(llm-chat-prompt-interactions prompt)))))
(system (llm-provider-utils-get-system-prompt prompt)))
(when (llm-chat-prompt-functions prompt)
(push `("tools" . ,(mapcar (lambda (f) (llm-claude--tool-call f))
(llm-chat-prompt-functions prompt))) request))
(when (> (length system) 0)
(push `("system" . ,system) request))
(when (llm-chat-prompt-temperature prompt)
(push `("temperature" . ,(llm-chat-prompt-temperature prompt)) request))
(append request (llm-chat-prompt-non-standard-params prompt))))
(cl-defmethod llm-provider-extract-function-calls ((_ llm-claude) response)
(let ((content (append (assoc-default 'content response) nil)))
(cl-loop for item in content
when (equal "tool_use" (assoc-default 'type item))
collect (make-llm-provider-utils-function-call
:id (assoc-default 'id item)
:name (assoc-default 'name item)
:args (assoc-default 'input item)))))
(cl-defmethod llm-provider-populate-function-calls ((_ llm-claude) prompt calls)
(llm-provider-utils-append-to-prompt
prompt
(mapcar (lambda (call)
`((type . "tool_use")
(id . ,(llm-provider-utils-function-call-id call))
(name . ,(llm-provider-utils-function-call-name call))
(input . ,(llm-provider-utils-function-call-args call))))
calls)))
(cl-defmethod llm-provider-chat-extract-result ((_ llm-claude) response)
(let ((content (aref (assoc-default 'content response) 0)))
(if (equal (assoc-default 'type content) "text")
(assoc-default 'text content)
(format "Unsupported non-text response: %s" content))))
(cl-defmethod llm-provider-streaming-media-handler ((_ llm-claude)
msg-receiver _ err-receiver)
(cons 'text/event-stream
(plz-event-source:text/event-stream
:events `((message_start . ignore)
(content_block_start . ignore)
(ping . ignore)
(message_stop . ignore)
(content_block_stop . ignore)
(error . ,(lambda (event)
(funcall err-receiver (plz-event-source-event-data event))))
(content_block_delta
.
,(lambda (event)
(let* ((data (plz-event-source-event-data event))
(json (json-parse-string data :object-type 'alist))
(delta (assoc-default 'delta json))
(type (assoc-default 'type delta)))
(when (equal type "text_delta")
(funcall msg-receiver (assoc-default 'text delta))))))))))
(cl-defmethod llm-provider-headers ((provider llm-claude))
`(("x-api-key" . ,(if (functionp (llm-claude-key provider))
(funcall (llm-claude-key provider))
(llm-claude-key provider)))
("anthropic-version" . "2023-06-01")
("anthropic-beta" . "tools-2024-04-04")))
(cl-defmethod llm-provider-chat-extract-error ((_ llm-claude) response)
(when-let ((err (assoc-default 'error response)))
(format "Error %s: '%s'" (assoc-default 'type err)
(assoc-default 'message err))))
(cl-defmethod llm-provider-chat-url ((_ llm-claude))
"Return the URL for the Claude API."
"https://api.anthropic.com/v1/messages")
(cl-defmethod llm-chat-token-limit ((provider llm-claude))
(llm-provider-utils-model-token-limit (llm-claude-chat-model provider)))
(cl-defmethod llm-name ((_ llm-claude))
"Return the name of the provider."
"Claude")
(cl-defmethod llm-capabilities ((_ llm-claude))
(list 'streaming 'function-calls))
(cl-defmethod llm-provider-append-to-prompt ((_ llm-claude) prompt result
&optional func-results)
;; Claude doesn't have a 'function role, so we just always use assistant here.
;; But if it's a function result, it considers that a 'user response, which
;; needs to be sent back.
(llm-provider-utils-append-to-prompt prompt result func-results (if func-results
'user
'assistant)))
(provide 'llm-claude)
;;; llm-claude.el ends here