to lazy for good messages

This commit is contained in:
zastian@mrthoddata.com
2025-06-25 10:38:33 +01:00
parent 3e2f2935d3
commit 1632857b50
6 changed files with 100 additions and 86 deletions

View File

@@ -28,85 +28,86 @@
;;; Code:
(require 'json)
Package-Requires: ((cl-lib "0.5"))
(require 'cl-lib)
(require 'url)
(defgroup ollama nil
"Ollama client for Emacs."
:group 'ollama)
"Ollama client for Emacs."
:group 'ollama)
(defcustom ollama:endpoint "http://localhost:11434/api/generate"
"Ollama http service endpoint."
:group 'ollama
:type 'string)
"Ollama http service endpoint."
:group 'ollama
:type 'string)
(defcustom ollama:model "codellama:13b"
"Ollama model."
:group 'ollama
:type 'string)
"Ollama model."
:group 'ollama
:type 'string)
(defcustom ollama:language "English"
"Language to translate to."
:group 'ollama
:type 'string)
"Language to translate to."
:group 'ollama
:type 'string)
(defun ollama-fetch (url prompt model)
(let* ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/json")))
(url-request-data
(encode-coding-string
(json-encode `((model . ,model) (prompt . ,prompt)))
'utf-8)))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(decode-coding-string
(buffer-substring-no-properties
(point)
(point-max))
'utf-8))))
(let* ((url-request-method "POST")
(url-request-extra-headers
'(("Content-Type" . "application/json")))
(url-request-data
(encode-coding-string
(json-encode `((model . ,model) (prompt . ,prompt)))
'utf-8)))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(decode-coding-string
(buffer-substring-no-properties
(point)
(point-max))
'utf-8))))
(defun ollama-get-response-from-line (line)
(cdr
(assoc 'response
(json-read-from-string line))))
(cdr
(assoc 'response
(json-read-from-string line))))
(defun ollama-prompt (url prompt model)
(mapconcat 'ollama-get-response-from-line
(cl-remove-if #'(lambda (str) (string= str ""))
(split-string (ollama-fetch url prompt model) "\n")) ""))
(mapconcat 'ollama-get-response-from-line
(cl-remove-if #'(lambda (str) (string= str ""))
(split-string (ollama-fetch url prompt model) "\n")) ""))
;;;###autoload
(defun ollama-prompt-line ()
"Prompt with current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (thing-at-point 'line) ollama:model))))
"Prompt with current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (thing-at-point 'line) ollama:model))))
;;;###autoload
(defun ollama-define-word ()
"Find definition of current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "define %s" (thing-at-point 'word)) ollama:model))))
"Find definition of current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "define %s" (thing-at-point 'word)) ollama:model))))
;;;###autoload
(defun ollama-translate-word ()
"Translate current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "translate \"%s\" to %s" (thing-at-point 'word) ollama:language) ollama:model))))
"Translate current word."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "translate \"%s\" to %s" (thing-at-point 'word) ollama:language) ollama:model))))
;;;###autoload
(defun ollama-summarize-region ()
"Summarize marked text."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "summarize \"\"\"%s\"\"\"" (buffer-substring (region-beginning) (region-end))) ollama:model))))
"Summarize marked text."
(interactive)
(with-output-to-temp-buffer "*ollama*"
(princ
(ollama-prompt ollama:endpoint (format "summarize \"\"\"%s\"\"\"" (buffer-substring (region-beginning) (region-end))) ollama:model))))
(provide 'ollama)
;;; ollama.el ends here