Skip to content

Commit

Permalink
Merge pull request #111 from s-kostyaev/add-call-chains
Browse files Browse the repository at this point in the history
Add ellama-chain function
  • Loading branch information
s-kostyaev authored May 13, 2024
2 parents 6d4bef6 + a883095 commit 32be5ae
Showing 1 changed file with 77 additions and 28 deletions.
105 changes: 77 additions & 28 deletions ellama.el
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ This filter contains only subset of markdown syntax to be good enough."
:group 'ellama
:type '(sexp :validate 'cl-struct-p))

(defcustom ellama-always-show-chain-steps nil
"Always show ellama chain buffers."
:type 'boolean
:group 'ellama)

(defvar-local ellama--current-session nil)

(defvar ellama--current-session-id nil)
Expand Down Expand Up @@ -1040,6 +1045,50 @@ when the request completes (with BUFFER current)."
(setq ellama--current-request nil)
(ellama-request-mode -1)))))))))

(defun ellama-chain (initial-prompt forms)
"Call chain of FORMS on INITIAL-PROMPT.
Each form is a plist that can contain different options:
:provider PROVIDER - use PROVIDER instead of `ellama-provider'.
:transform FUNCTION - use FUNCTION to transform result of previous step to new
prompt.
:session SESSION - use SESSION in current step.
:chat BOOL - if BOOL use chat buffer, otherwise use temp buffer. Make sense for
last step only.
:show BOOL - if BOOL show buffer for this step."
(let* ((hd (car forms))
(tl (cdr forms))
(provider (or (plist-get hd :provider) ellama-provider))
(transform (or (plist-get hd :transform) #'identity))
(prompt (apply transform (list initial-prompt)))
(session (plist-get hd :session))
(chat (plist-get hd :chat))
(show (or (plist-get hd :show) ellama-always-show-chain-steps chat))
(buf (if (or (and tl (not chat)) (not session))
(get-buffer-create (make-temp-name
(ellama-generate-name provider real-this-command prompt)))
(ellama-get-session-buffer ellama--current-session-id))))
(when show
(display-buffer buf))
(with-current-buffer buf
(funcall ellama-major-mode))
(if chat
(ellama-chat prompt nil :provider provider)
(ellama-stream
prompt
:provider provider
:buffer buf
:session session
:filter (when (derived-mode-p 'org-mode)
#'ellama--translate-markdown-to-org-filter)
:on-done (lambda (res)
(when tl
(ellama-chain res tl)))))))

(defun ellama-chat-done (text)
"Chat done.
Will call `ellama-chat-done-callback' on TEXT."
Expand Down Expand Up @@ -1121,42 +1170,42 @@ ARGS contains keys for fine control.
(interactive "sAsk ellama: ")
(let* ((providers (append
`(("default model" . ellama-provider)
,(if (and ellama-ollama-binary (file-exists-p ellama-ollama-binary))
'("ollama model" . (ellama-get-ollama-local-model))))
,(if (and ellama-ollama-binary (file-exists-p ellama-ollama-binary))
'("ollama model" . (ellama-get-ollama-local-model))))
ellama-providers))
(variants (mapcar #'car providers))
(provider (if current-prefix-arg
(eval (alist-get
(completing-read "Select model: " variants)
providers nil nil #'string=))
(or (plist-get args :provider)
ellama-provider)))
(session (if (or create-session
current-prefix-arg
(and (not ellama--current-session)
(not ellama--current-session-id)))
(ellama-new-session provider prompt)
(or ellama--current-session
(with-current-buffer (ellama-get-session-buffer
ellama--current-session-id)
ellama--current-session))))
(buffer (ellama-get-session-buffer
(ellama-session-id session)))
(file-name (ellama-session-file session))
(translation-buffer (when ellama-chat-translation-enabled
(if file-name
(progn
(find-file-noselect
(ellama--get-translation-file-name file-name)))
(get-buffer-create (ellama-session-id session))))))
(variants (mapcar #'car providers))
(provider (if current-prefix-arg
(eval (alist-get
(completing-read "Select model: " variants)
providers nil nil #'string=))
(or (plist-get args :provider)
ellama-provider)))
(session (if (or create-session
current-prefix-arg
(and (not ellama--current-session)
(not ellama--current-session-id)))
(ellama-new-session provider prompt)
(or ellama--current-session
(with-current-buffer (ellama-get-session-buffer
ellama--current-session-id)
ellama--current-session))))
(buffer (ellama-get-session-buffer
(ellama-session-id session)))
(file-name (ellama-session-file session))
(translation-buffer (when ellama-chat-translation-enabled
(if file-name
(progn
(find-file-noselect
(ellama--get-translation-file-name file-name)))
(get-buffer-create (ellama-session-id session))))))
(if ellama-chat-translation-enabled
(ellama--translate-interaction prompt translation-buffer buffer session)
(display-buffer buffer)
(with-current-buffer buffer
(save-excursion
(goto-char (point-max))
(insert (ellama-get-nick-prefix-for-mode) " " ellama-user-nick ":\n"
(ellama--format-context session) prompt "\n\n"
(ellama--format-context session) (ellama--fill-long-lines prompt) "\n\n"
(ellama-get-nick-prefix-for-mode) " " ellama-assistant-nick ":\n")
(ellama-stream prompt
:session session
Expand Down

0 comments on commit 32be5ae

Please sign in to comment.