Ellama: Emacs LLM Integration
Table of Contents
Introduction to Ellama
Ellama is an Emacs package that provides seamless integration with Large Language Models through the Ollama backend. It enables developers to leverage AI capabilities directly within their editing environment for code generation, documentation, refactoring, and interactive assistance. Unlike cloud-based solutions, ellama works with locally-hosted models, ensuring privacy and offline availability.
The package integrates naturally with Emacs workflows, supporting region-based transformations, buffer-wide operations, and interactive queries. It's particularly powerful when combined with org-mode for literate programming and research documentation.
Configuration and Setup with Ollama
First, install Ollama and pull your preferred model. Then configure ellama in your Emacs init file:
;; Install ellama via package.el or use-package (use-package ellama :ensure t :init (setopt ellama-language "English") (require 'llm-ollama) :config (setopt ellama-provider (make-llm-ollama :chat-model "llama3.2:3b" :embedding-model "nomic-embed-text")) (setopt ellama-naming-scheme 'ellama-generate-name-by-llm) (setopt ellama-auto-scroll t))
Key Features and Commands
Ellama provides numerous interactive commands:
ellama-ask-about- Query the LLM about selected regionellama-code-complete- Generate code completionellama-code-improve- Refactor and optimize codeellama-summarize- Create concise summariesellama-translate- Translate text to target languageellama-define-word- Get definitions and explanationsellama-chat- Start interactive conversation
Each command operates on the current region when active, or prompts for input otherwise. Results appear in dedicated buffers with automatic scrolling enabled.
Integration with Org-mode
Ellama excels in org-mode environments, enabling literate programming workflows:
;; Configure org-babel integration (defun ellama-org-babel-execute () "Execute ellama code blocks in org-mode." (interactive) (let ((query (org-element-property :value (org-element-at-point)))) (ellama-ask-about query))) ;; Add keybinding for org-mode (add-hook 'org-mode-hook (lambda () (local-set-key (kbd "C-c C-e") #'ellama-org-babel-execute)))
Use ellama source blocks for embedding AI queries:
What is (+ 2 1 3 4)
Advanced Usage Examples
;; Custom ellama function for code review (defun my-ellama-code-review () "Review selected code for improvements." (interactive) (ellama-ask-about (format "Review this code for best practices, bugs, and improvements:\n\n%s" (buffer-substring-no-properties (region-beginning) (region-end))))) ;; Generate documentation automatically (defun my-ellama-document-function () "Generate documentation for function at point." (interactive) (ellama-code-add "Add comprehensive docstring explaining parameters, return value, and side effects"))