emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/llm ea2ec282aa 10/13: Removed /generate endpoint suppor


From: ELPA Syncer
Subject: [elpa] externals/llm ea2ec282aa 10/13: Removed /generate endpoint support based on PR feedback
Date: Wed, 7 Feb 2024 18:58:11 -0500 (EST)

branch: externals/llm
commit ea2ec282aaea2ab40cbf21cc9ea48ea25c714605
Author: Thomas E. Allen <thomas@assistivemachines.com>
Commit: Thomas E. Allen <thomas@assistivemachines.com>

    Removed /generate endpoint support based on PR feedback
---
 llm-ollama.el | 98 +++++++++++++++++++----------------------------------------
 1 file changed, 31 insertions(+), 67 deletions(-)

diff --git a/llm-ollama.el b/llm-ollama.el
index 5d94f65eb1..ec2e3f1ada 100644
--- a/llm-ollama.el
+++ b/llm-ollama.el
@@ -58,11 +58,8 @@ 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.
-
-ENDPOINT is the API endpoint to use, a string. It is optional and
-defaults to `generate'."
-  (scheme "http") (host "localhost") (port 11434) chat-model embedding-model 
(endpoint "generate"))
+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.
@@ -103,58 +100,32 @@ PROVIDER is the llm-ollama provider."
    (llm-request-sync (format "http://localhost:%d/api/embeddings"; (or 
(llm-ollama-port provider) 11434))
                      :data (llm-ollama--embedding-request provider string))))
 
-(defun llm-ollama--chat-request-generate (prompt)
-  "Create the chat request data for the `generate' endpoint.
-PROMPT is the chat prompt to process.
-Returns REQUEST-ALIST with prompt request data."
-  (let (request-alist)
-    (when (llm-chat-prompt-context prompt)
-      (push `("system" . ,(llm-provider-utils-get-system-prompt prompt 
llm-ollama-example-prelude)) request-alist))
-    ;; If the first item isn't an interaction, then it's a conversation which
-    ;; we'll set as the chat context.
-    (when (not (eq (type-of (car (llm-chat-prompt-interactions prompt)))
-                   'llm-chat-prompt-interaction))
-      (push `("context" . ,(car (llm-chat-prompt-interactions prompt))) 
request-alist))
-    (push `("prompt" . ,(string-trim (llm-chat-prompt-interaction-content
-                                      (car (last (llm-chat-prompt-interactions 
prompt))))))
-          request-alist)
-    request-alist))
-
-(defun llm-ollama--chat-request-chat (prompt)
-  "Create the chat request data for the `chat' endpoint.
-PROMPT is the chat prompt to process.
-Returns REQUEST-ALIST with prompt request data."
-  (let (request-alist messages)
+(defun llm-ollama--chat-request (provider prompt)
+  "From PROMPT, create the chat request data to send.
+PROVIDER is the llm-ollama provider to use."
+  (let (request-alist messages options)
+    ;; Create messages from prompt interactions
     (setq messages
           (mapcar (lambda (interaction)
                     `(("role" . ,(symbol-name 
(llm-chat-prompt-interaction-role interaction)))
                       ("content" . ,(llm-chat-prompt-interaction-content 
interaction))))
                   (llm-chat-prompt-interactions prompt)))
+    ;; Add system prompt to messages if it exists
     (when (llm-chat-prompt-context prompt)
       (push `(("role" . "system")
               ("content" . ,(llm-provider-utils-get-system-prompt prompt 
llm-ollama-example-prelude)))
             messages))
+    ;; Add messages to request-alist
     (push `("messages" . ,messages) request-alist)
-    request-alist))
-
-(defun llm-ollama--chat-request (provider prompt)
-  "From PROMPT, create the chat request data to send.
-PROVIDER is the llm-ollama provider to use.
-
-Calls the appropriate helper function to initialize
-`request-alist' depending on whether a `chat' or `generate'
-endpoint is specified in the provider."
-  (let* ((request-alist 
-          (if (string= (llm-ollama-endpoint provider) "chat")
-              (llm-ollama--chat-request-chat prompt)
-            (llm-ollama--chat-request-generate prompt)))
-         (options nil))
+    ;; Add model to request-alist
     (push `("model" . ,(llm-ollama-chat-model provider)) request-alist)
+    ;; Add options to request-alist if they exist
     (when (llm-chat-prompt-temperature prompt)
       (push `("temperature" . ,(llm-chat-prompt-temperature prompt)) options))
     (when (llm-chat-prompt-max-tokens prompt)
       (push `("num_predict" . ,(llm-chat-prompt-max-tokens prompt)) options))
     (when options (push `("options" . ,options) request-alist))
+    ;; Return request-alist
     request-alist))
 
 (defvar-local llm-ollama-current-response ""
@@ -184,16 +155,7 @@ endpoint is specified in the provider."
              current-response
              (concat current-response
                      (mapconcat
-                      (lambda (line)
-                        ;; Process response from /api/chat or
-                        ;; /api/generate endpoint appropriately.
-                        (let* ((json-response (json-read-from-string line))
-                               (response-string
-                                (or (assoc-default 'response json-response)
-                                    (assoc-default 'content
-                                                   (assoc-default 'message 
json-response)))))
-                          response-string))
-                      
+                      (lambda (line) (assoc-default 'content (assoc-default 
'message (json-read-from-string line))))
                       ;; Take from response output last-response to the end. 
This
                       ;; counts only valid responses, so we need to throw out 
all
                       ;; other lines that aren't valid JSON.
@@ -220,7 +182,8 @@ endpoint is specified in the provider."
   ;; we really just need it for the local variables.
   (with-temp-buffer
     (let ((output (llm-request-sync-raw-output 
-                   (llm-ollama--url provider (slot-value provider 'endpoint))
+                   ;; (llm-ollama--url provider (slot-value provider 
'endpoint))
+                   (llm-ollama--url provider "chat")
                    :data (llm-ollama--chat-request provider prompt)
                    ;; ollama is run on a user's machine, and it can take a 
while.
                    :timeout llm-ollama-chat-timeout)))
@@ -233,21 +196,22 @@ endpoint is specified in the provider."
 
 (cl-defmethod llm-chat-streaming ((provider llm-ollama) prompt 
partial-callback response-callback error-callback)
   (let ((buf (current-buffer)))
-    (llm-request-async (llm-ollama--url provider (slot-value provider 
'endpoint))
-      :data (llm-ollama--chat-request provider prompt)
-      :on-success-raw (lambda (response)
-                        (setf (llm-chat-prompt-interactions prompt)
-                              (list (assoc-default 'context 
(llm-ollama--get-final-response response))))
-                        (llm-request-callback-in-buffer
-                         buf response-callback
-                         (llm-ollama--get-partial-chat-response response)))
-      :on-partial (lambda (data)
-                    (when-let ((response 
(llm-ollama--get-partial-chat-response data)))
-                      (llm-request-callback-in-buffer buf partial-callback 
response)))
-      :on-error (lambda (_ _)
-                  ;; The problem with ollama is that it doesn't
-                  ;; seem to have an error response.
-                  (llm-request-callback-in-buffer buf error-callback "Unknown 
error calling ollama")))))
+    ;; (llm-request-async (llm-ollama--url provider (slot-value provider 
'endpoint))
+    (llm-request-async (llm-ollama--url provider "chat")
+                       :data (llm-ollama--chat-request provider prompt)
+                       :on-success-raw (lambda (response)
+                                         (setf (llm-chat-prompt-interactions 
prompt)
+                                               (list (assoc-default 'context 
(llm-ollama--get-final-response response))))
+                                         (llm-request-callback-in-buffer
+                                          buf response-callback
+                                          
(llm-ollama--get-partial-chat-response response)))
+                       :on-partial (lambda (data)
+                                     (when-let ((response 
(llm-ollama--get-partial-chat-response data)))
+                                       (llm-request-callback-in-buffer buf 
partial-callback response)))
+                       :on-error (lambda (_ _)
+                                   ;; The problem with ollama is that it 
doesn't
+                                   ;; seem to have an error response.
+                                   (llm-request-callback-in-buffer buf 
error-callback "Unknown error calling ollama")))))
 
 (cl-defmethod llm-name ((provider llm-ollama))
   (llm-ollama-chat-model provider))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]