karthink / gptel

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

Improve functionality of context handling #453

Open osheari1 opened 3 days ago

osheari1 commented 3 days ago

There seems to be a few areas of improvement with context handling

Potential solutions:

karthink commented 3 days ago

Add functions to remove particular context sets (from Inspect buffer?) and clear the whole context set (from transient menu).

To remove particular context sets you can

when I have 2 chats open, the contexts will be shared across both even if they are totally unrelated.

Context is global by design. It's not clear how you can indicate different context lists per chat buffer to the user in a way that's not very confusing.

Context is also independent of gptel buffers because you can use gptel-send anywhere -- for example, you can include context when doing some refactoring in a project. If the context were made buffer-local, you would have to repeatedly add regions/buffers/files to the buffer-local context in each project buffer you want to use gptel in.

I have to restart emacs to clear it.

As mentioned above, you can clear it from the context inspection buffer or by running gptel-add again. You don't need to restart Emacs.

Context keybindings don't work when evil is enabled (ofc this can be manally fixed).

Transient's keymap should override evil-mode here, not sure what's happening.

osheari1 commented 3 days ago

To remove particular context sets you can

use the context inspection buffer (C in the menu) run gptel-add in a region/buffer that is part of the context, it will be removed instead. Ok got it thanks. I tested this out at bit and it seems to work in some situations but not others.

It works when you are removing the context by calling gptel-context-add on the context region in the buffer itself, but it doesn't work when removing from the context Inspector.

Examples: Adding a specific region in the currently selected buffer and removing with function directly from buffer Highlight region in buffer image

in transient menu, run -r image

run gptel-context-add on selected region (NOT via the context menu) image

Adding a context file from transient menu and removing from inspector In transient menu, select file -f image

Attempt to remove file from context menu image image

Results after removing image

(From inspecting the payload) image

Context is global by design. It's not clear how you can indicate different context lists per chat buffer to the user in a way that's not very confusing.

Yeah this makes sense. Not an issue when you can update the context anyway

Transient's keymap should override evil-mode here, not sure what's happening. I'll look into this and submit a PR if I can find anything. Do you know where in the code is this configured? I found this, but not sure if thats where the evil handling is. https://github.com/karthink/gptel/blob/557f383f445440203b4493104c31383a21005907/gptel-context.el#L364

karthink commented 3 days ago

In the context inspection buffer, you can delete chunks from the context with d, and move around with n and p -- these are the same keybindings as in Dired, buffer-menu, ibuffer etc. The header line explains this:

image

I misunderstood what you meant about evil-mode, I thought the keys in the transient menu (like -b and -f) don't work.

The context inspection buffer does not support evil-mode keybindings, yes. You can switch to Emacs mode in this buffer or add the keybindings to your config.

ultronozm commented 6 hours ago

@osheari1 since you ask about having "per-chat context", it might be worth taking a look at how context is handled in https://github.com/ultronozm/ai-org-chat.el (which uses gptel as a backend, so should hopefully be simple to try out)

osheari1 commented 3 hours ago

Ok cool. This is working thanks :+1: