-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathllm-ollama.el
217 lines (181 loc) · 9.59 KB
/
llm-ollama.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
;;; llm-ollama.el --- llm module for integrating with Ollama. -*- lexical-binding: t; package-lint-main-file: "llm.el"; -*-
;; Copyright (c) 2023-2025 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 Ollama, an
;; interface to running LLMs locally. Ollama can be found at https://ollama.ai/.
;;; Code:
(require 'cl-lib)
(require 'llm)
(require 'llm-provider-utils)
(require 'llm-models)
(require 'json)
(require 'plz-media-type)
(defgroup llm-ollama nil
"LLM implementation for Ollama."
:group 'llm)
(defcustom llm-ollama-example-prelude "Examples of how you should respond follow."
"The prelude to use for examples in Ollama chat prompts."
:type 'string
:group 'llm-ollama)
(defcustom llm-ollama-chat-timeout 300
"Timeout for sync ollama chat calls."
:type 'integer
:group 'llm-ollama)
(cl-defstruct (llm-ollama (:include llm-standard-full-provider))
"A structure for holding information needed by Ollama's API.
SCHEME is the http scheme to use, a string. It is optional and
default to `http'.
HOST is the host that Ollama is running on. It is optional and
default to localhost.
PORT is the localhost port that Ollama is running on. It is optional.
CHAT-MODEL is the model to use for chat queries. It is required.
EMBEDDING-MODEL is the model to use for embeddings. It is required."
(scheme "http") (host "localhost") (port 11434) chat-model embedding-model)
;; Ollama's models may or may not be free, we have no way of knowing. There's no
;; way to tell, and no ToS to point out here.
(cl-defmethod llm-nonfree-message-info ((provider llm-ollama))
(ignore provider)
nil)
(defun llm-ollama--url (provider method)
"With ollama PROVIDER, return url for METHOD."
(format "%s://%s:%d/api/%s" (llm-ollama-scheme provider )(llm-ollama-host provider)
(llm-ollama-port provider) method))
(cl-defmethod llm-provider-embedding-url ((provider llm-ollama) &optional _)
(llm-ollama--url provider "embed"))
(cl-defmethod llm-provider-chat-url ((provider llm-ollama))
(llm-ollama--url provider "chat"))
(cl-defmethod llm-provider-chat-timeout ((_ llm-ollama))
llm-ollama-chat-timeout)
(cl-defmethod llm-provider-embedding-extract-error ((_ llm-ollama) response)
(assoc-default 'error response))
(cl-defmethod llm-provider-chat-extract-error ((_ llm-ollama) response)
(assoc-default 'error response))
(cl-defmethod llm-provider-embedding-request ((provider llm-ollama) string)
"Return the request to the server for the embedding of STRING.
PROVIDER is the llm-ollama provider."
`(:input ,string
:model ,(llm-ollama-embedding-model provider)))
(cl-defmethod llm-provider-batch-embeddings-request ((provider llm-ollama) strings)
(llm-provider-embedding-request provider strings))
(cl-defmethod llm-provider-embedding-extract-result ((_ llm-ollama) response)
"Return the embedding from the server RESPONSE."
(aref (assoc-default 'embeddings response) 0))
(cl-defmethod llm-provider-batch-embeddings-extract-result ((_ llm-ollama) response)
(append (assoc-default 'embeddings response) nil))
(cl-defmethod llm-provider-chat-extract-result ((_ llm-ollama) response)
"Return the chat response from the server RESPONSE."
(assoc-default 'content (assoc-default 'message response)))
(defun llm-ollama--response-format (format)
"Return the response format for FORMAT."
(if (eq format 'json)
:json
(llm-provider-utils-convert-to-serializable format)))
(cl-defmethod llm-provider-chat-request ((provider llm-ollama) prompt streaming)
(llm-provider-utils-combine-to-system-prompt prompt llm-ollama-example-prelude)
(let (request-plist messages options)
(setq messages
(vconcat (mapcar (lambda (interaction)
(let* ((role (llm-chat-prompt-interaction-role interaction))
(content (llm-chat-prompt-interaction-content interaction))
(content-text "")
(images nil))
(if (stringp content)
(setq content-text content)
(if (eq 'user role)
(dolist (part (llm-multipart-parts content))
(if (llm-media-p part)
(setq images (append images (list part)))
(setq content-text (concat content-text part))))
(setq content-text (json-serialize content))))
(append
`(:role ,(symbol-name role)
:content ,content-text)
(when images
`(:images
,(vconcat (mapcar (lambda (img) (base64-encode-string (llm-media-data img) t))
images)))))))
(llm-chat-prompt-interactions prompt))))
(setq request-plist (plist-put request-plist :messages messages))
(setq request-plist (plist-put request-plist :model (llm-ollama-chat-model provider)))
(when (and streaming (llm-chat-prompt-tools prompt))
(signal 'not-implemented
"Ollama does not support streaming with function calls"))
(when (llm-chat-prompt-tools prompt)
(setq request-plist (plist-put
request-plist :tools
(vconcat (mapcar #'llm-provider-utils-openai-tool-spec
(llm-chat-prompt-tools prompt))))))
(when (llm-chat-prompt-response-format prompt)
(setq request-plist (plist-put request-plist :format
(llm-ollama--response-format
(llm-chat-prompt-response-format prompt)))))
(setq request-plist (plist-put request-plist :stream (if streaming t :false)))
(when (llm-chat-prompt-temperature prompt)
(setq options (plist-put options :temperature (llm-chat-prompt-temperature prompt))))
(when (llm-chat-prompt-max-tokens prompt)
(setq options (plist-put options :num_predict (llm-chat-prompt-max-tokens prompt))))
(setq options (append options (llm-provider-utils-non-standard-params-plist prompt)))
(when options
(setq request-plist (plist-put request-plist :options options)))
request-plist))
(cl-defmethod llm-provider-extract-tool-uses ((_ llm-ollama) response)
(mapcar (lambda (call)
(let ((function (cdar call)))
(make-llm-provider-utils-tool-use
:name (assoc-default 'name function)
:args (assoc-default 'arguments function))))
(assoc-default 'tool_calls (assoc-default 'message response))))
(cl-defmethod llm-provider-populate-tool-uses ((_ llm-ollama) prompt tool-uses)
(llm-provider-utils-append-to-prompt
prompt
(vconcat (mapcar (lambda (tool-use)
`(:function (:name ,(llm-provider-utils-tool-use-name tool-use)
:arguments ,(json-serialize
(llm-provider-utils-tool-use-args tool-use)))))
tool-uses))))
(cl-defmethod llm-provider-streaming-media-handler ((_ llm-ollama) msg-receiver _ _)
(cons 'application/x-ndjson
(plz-media-type:application/x-ndjson
:handler (lambda (data)
(when-let ((response (assoc-default
'content
(assoc-default 'message data))))
(funcall msg-receiver response))))))
(cl-defmethod llm-name ((provider llm-ollama))
(or (llm-ollama-chat-model provider)
(llm-ollama-embedding-model provider)))
(cl-defmethod llm-chat-token-limit ((provider llm-ollama))
(llm-provider-utils-model-token-limit (llm-ollama-chat-model provider)
2048))
(cl-defmethod llm-capabilities ((provider llm-ollama))
(append '(streaming json-response)
(when (and (llm-ollama-embedding-model provider)
(let ((embedding-model (llm-models-match
(llm-ollama-embedding-model provider))))
(and embedding-model
(member 'embedding (llm-model-capabilities embedding-model)))))
'(embeddings embeddings-batch))
(when-let* ((model (llm-ollama-chat-model provider))
(chat-model (llm-models-match model))
(capabilities (llm-model-capabilities chat-model)))
(append
(when (member 'tool-use capabilities) '(function-calls))
(seq-intersection capabilities '(image-input))))))
(provide 'llm-ollama)
;;; llm-ollama.el ends here