branch: externals/ellama
commit 42c65d0001746b19fc1fa300e43818c23d6d1669
Merge: e55ed6962d 1d23874ce3
Author: Sergey Kostyaev <[email protected]>
Commit: GitHub <[email protected]>

    Merge pull request #355 from 
s-kostyaev/add-different-response-process-methods
    
    Add response processing control options
---
 README.org  |   3 ++
 ellama.el   | 150 +++++++++++++++++++++++++++++++++++++++++++++++-------------
 ellama.info |  58 ++++++++++++-----------
 3 files changed, 151 insertions(+), 60 deletions(-)

diff --git a/README.org b/README.org
index e5421e5432..138929a8e9 100644
--- a/README.org
+++ b/README.org
@@ -218,6 +218,9 @@ More sofisticated configuration example:
     a corresponding prompt is inserted into a blueprint buffer.
 - ~ellama-community-prompts-update-variables~: Prompt user for values of
     variables found in current buffer and update them.
+- ~ellama-response-process-method~: Configure how LLM responses are processed.
+    Options include streaming for real-time output, async for asynchronous
+    processing, or skipping every N messages to reduce resource usage.
 
 * Keymap
 
diff --git a/ellama.el b/ellama.el
index e1af3090d8..7d71fb4183 100644
--- a/ellama.el
+++ b/ellama.el
@@ -225,6 +225,15 @@ PROMPT is a prompt string."
          (const :tag "By generating name with reasoning LLM based on prompt." 
ellama-generate-name-by-reasoning-llm)
           (function :tag "By custom function")))
 
+(defcustom ellama-response-process-method 'streaming
+  "How to process LLM response.
+If the default streaming method is too resource-heavy, you can try other
+options."
+  :type `(choice
+         (const :tag "Streaming" streaming)
+         (const :tag "Async" async)
+         (integer :tag "Skip every N messages before process one")))
+
 (defcustom ellama-define-word-prompt-template "Define %s"
   "Prompt template for `ellama-define-word'."
   :type 'string)
@@ -1437,39 +1446,114 @@ failure (with BUFFER current).
          (require 'spinner)
          (spinner-start ellama-spinner-type))
        (let* ((handler (ellama--handle-partial insert-text insert-reasoning 
reasoning-buffer))
-              (request (llm-chat-streaming
-                        provider
-                        llm-prompt
-                        handler
-                        (lambda (response)
-                          (let ((text (plist-get response :text))
-                                (reasoning (plist-get response :reasoning)))
-                            (funcall handler response)
-                            (when (or ellama--current-session
-                                      (not reasoning))
-                              (kill-buffer reasoning-buffer))
-                            (with-current-buffer buffer
-                              (accept-change-group ellama--change-group)
-                              (when ellama-spinner-enabled
-                                (spinner-stop))
-                              (if (and (listp donecb)
-                                       (functionp (car donecb)))
-                                  (mapc (lambda (fn) (funcall fn text))
-                                        donecb)
-                                (funcall donecb text))
-                              (when ellama-session-hide-org-quotes
-                                (ellama-collapse-org-quotes))
-                              (setq ellama--current-request nil)
-                              (ellama-request-mode -1))))
-                        (lambda (_ msg)
-                          (with-current-buffer buffer
-                            (cancel-change-group ellama--change-group)
-                            (when ellama-spinner-enabled
-                              (spinner-stop))
-                            (funcall errcb msg)
-                            (setq ellama--current-request nil)
-                            (ellama-request-mode -1)))
-                        t)))
+              (request (pcase ellama-response-process-method
+                         ('async (llm-chat-async
+                                  provider
+                                  llm-prompt
+                                  (lambda (response)
+                                    (let ((text (plist-get response :text))
+                                          (reasoning (plist-get response 
:reasoning)))
+                                      (funcall handler response)
+                                      (when (or ellama--current-session
+                                                (not reasoning))
+                                        (kill-buffer reasoning-buffer))
+                                      (with-current-buffer buffer
+                                        (accept-change-group 
ellama--change-group)
+                                        (when ellama-spinner-enabled
+                                          (spinner-stop))
+                                        (if (and (listp donecb)
+                                                 (functionp (car donecb)))
+                                            (mapc (lambda (fn) (funcall fn 
text))
+                                                  donecb)
+                                          (funcall donecb text))
+                                        (when ellama-session-hide-org-quotes
+                                          (ellama-collapse-org-quotes))
+                                        (setq ellama--current-request nil)
+                                        (ellama-request-mode -1))))
+                                  (lambda (_ msg)
+                                    (with-current-buffer buffer
+                                      (cancel-change-group 
ellama--change-group)
+                                      (when ellama-spinner-enabled
+                                        (spinner-stop))
+                                      (funcall errcb msg)
+                                      (setq ellama--current-request nil)
+                                      (ellama-request-mode -1)))
+                                  t))
+                         ('streaming (llm-chat-streaming
+                                      provider
+                                      llm-prompt
+                                      handler
+                                      (lambda (response)
+                                        (let ((text (plist-get response :text))
+                                              (reasoning (plist-get response 
:reasoning)))
+                                          (funcall handler response)
+                                          (when (or ellama--current-session
+                                                    (not reasoning))
+                                            (kill-buffer reasoning-buffer))
+                                          (with-current-buffer buffer
+                                            (accept-change-group 
ellama--change-group)
+                                            (when ellama-spinner-enabled
+                                              (spinner-stop))
+                                            (if (and (listp donecb)
+                                                     (functionp (car donecb)))
+                                                (mapc (lambda (fn) (funcall fn 
text))
+                                                      donecb)
+                                              (funcall donecb text))
+                                            (when 
ellama-session-hide-org-quotes
+                                              (ellama-collapse-org-quotes))
+                                            (setq ellama--current-request nil)
+                                            (ellama-request-mode -1))))
+                                      (lambda (_ msg)
+                                        (with-current-buffer buffer
+                                          (cancel-change-group 
ellama--change-group)
+                                          (when ellama-spinner-enabled
+                                            (spinner-stop))
+                                          (funcall errcb msg)
+                                          (setq ellama--current-request nil)
+                                          (ellama-request-mode -1)))
+                                      t))
+                         ((pred integerp)
+                          (let* ((cnt 0)
+                                 (skip-handler
+                                  (lambda (request)
+                                    (if (= cnt ellama-response-process-method)
+                                        (progn
+                                          (funcall handler request)
+                                          (setq cnt 0))
+                                      (cl-incf cnt)))))
+                            (llm-chat-streaming
+                             provider
+                             llm-prompt
+                             skip-handler
+                             (lambda (response)
+                               (let ((text (plist-get response :text))
+                                     (reasoning (plist-get response 
:reasoning)))
+                                 (funcall handler response)
+                                 (when (or ellama--current-session
+                                           (not reasoning))
+                                   (kill-buffer reasoning-buffer))
+                                 (with-current-buffer buffer
+                                   (accept-change-group ellama--change-group)
+                                   (when ellama-spinner-enabled
+                                     (spinner-stop))
+                                   (if (and (listp donecb)
+                                            (functionp (car donecb)))
+                                       (mapc (lambda (fn) (funcall fn text))
+                                             donecb)
+                                     (funcall donecb text))
+                                   (when ellama-session-hide-org-quotes
+                                     (ellama-collapse-org-quotes))
+                                   (setq ellama--current-request nil)
+                                   (ellama-request-mode -1))))
+                             (lambda (_ msg)
+                               (with-current-buffer buffer
+                                 (cancel-change-group ellama--change-group)
+                                 (when ellama-spinner-enabled
+                                   (spinner-stop))
+                                 (funcall errcb msg)
+                                 (setq ellama--current-request nil)
+                                 (ellama-request-mode -1)))
+                             t))))))
          (with-current-buffer buffer
            (setq ellama--current-request request)))))))
 
diff --git a/ellama.info b/ellama.info
index f2e7da9c17..006f9ad305 100644
--- a/ellama.info
+++ b/ellama.info
@@ -320,6 +320,10 @@ File: ellama.info,  Node: Commands,  Next: Keymap,  Prev: 
Installation,  Up: Top
      buffer.
    • ‘ellama-community-prompts-update-variables’: Prompt user for values
      of variables found in current buffer and update them.
+   • ‘ellama-response-process-method’: Configure how LLM responses are
+     processed.  Options include streaming for real-time output, async
+     for asynchronous processing, or skipping every N messages to reduce
+     resource usage.
 
 
 File: ellama.info,  Node: Keymap,  Next: Configuration,  Prev: Commands,  Up: 
Top
@@ -1418,33 +1422,33 @@ Tag Table:
 Node: Top1379
 Node: Installation3613
 Node: Commands8621
-Node: Keymap14965
-Node: Configuration17798
-Node: Context Management23397
-Node: Transient Menus for Context Management24305
-Node: Managing the Context25919
-Node: Considerations26694
-Node: Minor modes27287
-Node: ellama-context-header-line-mode29275
-Node: ellama-context-header-line-global-mode30100
-Node: ellama-context-mode-line-mode30820
-Node: ellama-context-mode-line-global-mode31668
-Node: Ellama Session Header Line Mode32372
-Node: Enabling and Disabling32941
-Node: Customization33388
-Node: Ellama Session Mode Line Mode33676
-Node: Enabling and Disabling (1)34261
-Node: Customization (1)34708
-Node: Using Blueprints35002
-Node: Key Components of Ellama Blueprints35621
-Node: Creating and Managing Blueprints36228
-Node: Variable Management37209
-Node: Keymap and Mode37678
-Node: Transient Menus38614
-Node: Running Blueprints programmatically39160
-Node: Acknowledgments39747
-Node: Contributions40460
-Node: GNU Free Documentation License40844
+Node: Keymap15207
+Node: Configuration18040
+Node: Context Management23639
+Node: Transient Menus for Context Management24547
+Node: Managing the Context26161
+Node: Considerations26936
+Node: Minor modes27529
+Node: ellama-context-header-line-mode29517
+Node: ellama-context-header-line-global-mode30342
+Node: ellama-context-mode-line-mode31062
+Node: ellama-context-mode-line-global-mode31910
+Node: Ellama Session Header Line Mode32614
+Node: Enabling and Disabling33183
+Node: Customization33630
+Node: Ellama Session Mode Line Mode33918
+Node: Enabling and Disabling (1)34503
+Node: Customization (1)34950
+Node: Using Blueprints35244
+Node: Key Components of Ellama Blueprints35863
+Node: Creating and Managing Blueprints36470
+Node: Variable Management37451
+Node: Keymap and Mode37920
+Node: Transient Menus38856
+Node: Running Blueprints programmatically39402
+Node: Acknowledgments39989
+Node: Contributions40702
+Node: GNU Free Documentation License41086
 
 End Tag Table
 

Reply via email to