diff --git a/config-mac.el b/config-mac.el new file mode 100644 index 0000000..b749b3a --- /dev/null +++ b/config-mac.el @@ -0,0 +1,169 @@ +;;; config-mac.el --- Mac-specific configuration -*- lexical-binding: t; -*- + +;; Enable tool use in gptel +(setq gptel-use-tools t) + +;; Always redirect reasoning tokens to a separate buffer +(setq gptel-include-reasoning "*llm-thinking*") + +;;; gptel backend configuration +;; Local llama.cpp server + +(setq gptel-backend (gptel-make-openai "llama.cpp" + :host "localhost:8000" + :protocol "http" + :stream t + :models '(Qwen3-Coder-30B-A3B-Instruct-Q6_K + Qwen3-30B-A3B-Thinking-2507-Q6_K + ;; GLM models (reasoning handled globally via gptel-include-reasoning) + (GLM-4.7-Flash-Q4_K_M + :description "GLM-4.7-Flash 30B MoE Q4 (17GB)" + :request-params (:max_tokens 8192)) + (GLM-4.7-Flash-UD-Q6_K_XL + :description "GLM-4.7-Flash 30B MoE Q6 XL (26GB)" + :request-params (:max_tokens 8192)))) + gptel-model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K) + +;; Prevent ChatGPT from being used as default +(setq-default gptel-model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K) + +;; Additional backend: Ollama for smaller models +(gptel-make-ollama "Ollama" + :host "localhost:11434" + :stream t + :models '(qwen2.5-coder:7b + qwen3:0.6B + (qwen2.5-coder:32b + :description "qwen2.5 coder 32b: large coding model" + :capabilities (tools media) + :mime-types ("image/jpeg" "image/png" "image/svg")))) + +;; Backend for GLM thinking models (uses DeepSeek parser for reasoning_content) +(gptel-make-deepseek "GLM-think" + :host "localhost:8000" + :protocol "http" + :stream t + :key "not-needed" + :models '((GLM-4.7-Flash-UD-Q6_K_XL + :description "GLM-4.7-Flash Q6 XL with thinking" + :request-params (:max_tokens 4096)))) + +;; Presets (these reference the backends defined above) +(gptel-make-preset 'explain + :system "Explain what this code does to a novice programmer." + :model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K + :backend "llama.cpp") + +(gptel-make-preset 'elixir + :system "You are an expert programmer, specialising in Elixir and TDD, you live in Emacs" + :model 'Qwen3-Coder-30B-A3B-Instruct-Q6_K + :backend "llama.cpp") + +;; GLM-4.7-Flash presets +;; GLM requires explicit stop tokens to prevent infinite generation +;; Fast mode: thinking disabled for quick responses +(gptel-make-preset 'glm-fast + :description "GLM-4.7-Flash without thinking (fast)" + :system "You are a coding agent living in Emacs. Be concise. Code only, minimal explanation. No preamble." + :model 'GLM-4.7-Flash-Q4_K_M + :backend "llama.cpp" + :request-params '(:chat_template_kwargs (:enable_thinking :json-false) + :max_tokens 1024 + :stop ["<|user|>" "<|observation|>" "<|endoftext|>"])) + +;; Thinking mode: reasoning streamed to separate buffer +;; Uses GLM-think backend (DeepSeek parser) for reasoning_content extraction +(gptel-make-preset 'glm-think + :description "GLM-4.7-Flash with thinking (reasoning in side buffer)" + :system "Think briefly. Give a short, direct final answer. No verbose explanations." + :model 'GLM-4.7-Flash-UD-Q6_K_XL + :backend "GLM-think" + :request-params '(:max_tokens 2048 + :stop ["<|user|>" "<|observation|>" "<|endoftext|>"])) + +;; GLM search tool - searches org-roam notes via ripgrep +(gptel-make-tool + :name "search_notes" + :function (lambda (query) + (let* ((roam-dir (expand-file-name "~/MyDocuments/qwan/essays/willem")) + (cmd (format "rg --type org -i -C 1 --max-count 2 --max-filesize 100K %s %s 2>/dev/null | head -100" + (shell-quote-argument query) + (shell-quote-argument roam-dir))) + (output (shell-command-to-string cmd))) + (if (string-empty-p output) + (format "No results found for: %s" query) + (if (> (length output) 3000) + (concat (substring output 0 3000) "\n... (truncated)") + output)))) + :description "Search org-roam notes using ripgrep. Returns matching lines with context from the user's personal knowledge base." + :args (list '(:name "query" + :type "string" + :description "Search term or pattern to find in notes")) + :category "org-roam") + +;; GLM search preset - uses the search_notes tool +(gptel-make-preset 'glm-search + :description "GLM-4.7-Flash with org-roam search" + :system "You are a helpful assistant with access to the user's org-roam knowledge base. Use the search_notes tool to find relevant information before answering. Be concise." + :model 'GLM-4.7-Flash-UD-Q6_K_XL + :backend "llama.cpp" + :tools '("search_notes")) + +(defun gptel-glm-search (query) + "Search org-roam notes using GLM-4.7-Flash with tool calling. +Returns answer with org-roam links to sources." + (interactive "sSearch query: ") + (let* ((script (expand-file-name "~/dev/spikes/llm/llama-metal-openwebui/glm_search.py")) + (buf (get-buffer-create "*glm-search*"))) + (with-current-buffer buf + (erase-buffer) + (org-mode) + (yas-minor-mode -1) ; Disable yasnippet to prevent < triggers + (insert (format "* Query: %s\n\nSearching...\n" query))) + (display-buffer buf) + (redisplay) + ;; Run synchronously to ensure all output captured + (let ((output (shell-command-to-string + (format "python3 %s %s 2>/dev/null" + (shell-quote-argument script) + (shell-quote-argument query))))) + (with-current-buffer buf + (erase-buffer) + (insert (format "* Query: %s\n\n" query)) + ;; Remove everything before ==== line + (let ((clean-output (if (string-match "====\n" output) + (substring output (match-end 0)) + output))) + (insert clean-output)) + (goto-char (point-min)))))) + +;; @glm-search directive - searches org-roam with tool calling +(defun my/gptel-glm-search-run (query) + "Run glm-search script and return results." + (message "Searching org-roam for: %s..." query) + (let* ((script (expand-file-name "~/dev/spikes/llm/llama-metal-openwebui/glm_search.py")) + (output (shell-command-to-string + (format "python3 %s %s 2>/dev/null" + (shell-quote-argument script) + (shell-quote-argument query))))) + (if (string-match "====\n" output) + (substring output (match-end 0)) + output))) + +(defun gptel-glm-search-send () + "Search org-roam using @glm-search or @gptel-search query on current line." + (interactive) + (let ((line (buffer-substring-no-properties + (line-beginning-position) (line-end-position)))) + ;; Accept both @glm-search and @gptel-search patterns + (if (string-match "@\\(?:glm\\|gptel\\)-search\\s-+\\(.+\\)$" line) + (let* ((query (string-trim (match-string 1 line))) + (result (my/gptel-glm-search-run query)) + (yas-was-active (bound-and-true-p yas-minor-mode))) + (when yas-was-active (yas-minor-mode -1)) + (end-of-line) + (insert "\n\n" result) + (when yas-was-active (yas-minor-mode 1))) + (message "No @glm-search or @gptel-search query found on current line")))) + +;;; config-mac.el ends here diff --git a/config.el b/config.el index 2a25d18..3987755 100644 --- a/config.el +++ b/config.el @@ -390,8 +390,7 @@ (require 'gptel-integrations)) ;; <- for mcp ;; Load machine-specific configuration (gptel backends, etc.) -;; Create config-local.el with your machine's gptel-backend, gptel-model, and backends -(load! "config-local.el" doom-user-dir t) +(load! "config-mac.el" doom-user-dir t) ;; org-roam v1 port to v2, insert node without opening buffer @@ -405,3 +404,4 @@ (after! flycheck (setq flycheck-haskell-ghc-executable "cabal") (setq flycheck-haskell-runghc-executable "cabal")) +