Skip to content

Instantly share code, notes, and snippets.

@ahyatt
Last active January 28, 2024 03:15
Show Gist options
  • Save ahyatt/63d0302c007223eaf478b84e64bfd2cc to your computer and use it in GitHub Desktop.
Save ahyatt/63d0302c007223eaf478b84e64bfd2cc to your computer and use it in GitHub Desktop.
llm-flows text replacement example
;; To be used with llm-flows in https://github.com/ahyatt/llm/tree/flows.
(require 'llm)
(require 'llm-flows)
(defun ash/llm-correct-text (start end arg)
(interactive "r\np")
(let* ((initial-context "You are an experienced editor that specializes in clear and correct communication. You will be given a passage of text. Return only a rewrite of that passage, correct in both grammar and spelling, and clearer in meaning. Do not return any other commentary other than the rewrite of the passage, which will be replacing the original text. Keep the formatting style of the original text. For example, if the text is in org-mode format, keep the formatting in the org-mode style instead of using markdown.")
(edited-context
(if (> arg 1)
(read-string-from-buffer "Edit the instruction." initial-context)))
(buf (current-buffer))
(before (buffer-substring-no-properties start end))
(start-marker (make-marker))
(end-marker (make-marker)))
(set-marker start-marker start buf)
(set-marker end-marker end buf)
(start-llm-flows-simple
(make-llm-flows-state
:name "correct text"
:provider llm-flows-provider
:prompt (make-llm-chat-prompt
:context edited-context
:interactions (list (make-llm-chat-prompt-interaction
:role 'user
:content before))
:temperature 0.1)
:user-verifier (lambda (fsm result)
(llm-flows-verify-result-diff fsm before result))
:on-success (lambda (result)
(with-current-buffer buf
(replace-region-contents (marker-position start-marker)
(marker-position end-marker)
(lambda () result))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment