karthink / gptel

A simple LLM client for Emacs
GNU General Public License v3.0
1.63k stars 151 forks source link

Make changes to gptel-directives propagate into org-mode properties #474

Open nordlow opened 1 week ago

nordlow commented 1 week ago

I'm currently loading gptel as

(use-package gptel                      ;https://github.com/karthink/gptel
  :ensure t :defer t
  :custom
  (gptel-default-mode 'org-mode)
  (gptel-model 'claude-3-5-sonnet-20241022) ;See `gptel--anthropic-models'.
  (gptel-directives
   '((default
      . "You are a large language model living in Emacs and a helpful assistant. Respond concisely.")
     (programming
      . "You are a large language model and a careful programmer.
Provide code and only code as output without any additional text, prompt or note.

When generating C code:
- Use as modern constructs as possible including the
  ones part of the new upcoming C2Y standard that are available in the
  most recent stable version of Clang.

When generating C-like languages like C, C++ and D:
- Formatting should use K&R style with tab character for indentation.
- Variables that aren't mutated after they have been
  initialized (read-only)should be qualified with the attribute `const` as
  in `const int x = 42`.

When generating D code strictly adhere to these rules:

- (member) function templates shall be qualified with _neither_ `pure`,
  `nothrow`, `@safe`, _nor_ `@nogc`,

- formatting shall never insert a new-line before opening curly brace
  ASCII character

.")
     (writing
      . "You are a large language model and a writing assistant. Respond concisely.")
     (chat
      . "You are a large language model and a conversation partner. Respond concisely.")))
  :config
  (dolist (hook '(org-mode-hook markdown-mode-hook))
    (add-hook hook 'gptel-setup-hook:nordlow))
  (add-hook 'gptel-post-response-functions
            'gptel-post-response-adjust:nordlow)
  (require 'gptel-gemini nil t)
  (when (require 'gptel-anthropic nil t)
    (setq gptel-backend (gptel-make-anthropic "Claude"
                          :stream t
                          :key anthropic-api-token)))
  (global-set-key [(control c) (return)] 'gptel-send))

. How do I make new org-mode files choose the programming directive by default?

It would have been useful to able to specify system directive in call to

gptel-make-anthropic

above.

Currently new org-mode files insert

:GPTEL_SYSTEM: You are a large language model living in Emacs and a helpful assistant. Respond concisely.

when I want the programming directive to be inserted. Moreover is newlines automatically converted to \n when inserted into the org-mode file?

karthink commented 1 week ago

How do I make new org-mode files choose the programming directive by default?

Only org-mode files?

It would have been useful to able to specify system directive in call to gptel-make-anthropic above.

The system message is independent of the backend parameters and can be changed at any time, so it doesn't make sense to specify it with the gptel-make-* constructors.

Currently new org-mode files insert...

The active system message is written to the file when you save it. The default directive is active, so it is written.

Moreover is newlines automatically converted to \n when inserted into the org-mode file?

Newlines are converted to \\n (not \n). This is required for serializing Org properties. When they are read from the Org file the newlines are restored.