karthink / gptel

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

Setting output options programmatically (not using transient) #191

Closed bergheim closed 5 months ago

bergheim commented 5 months ago

Hello,

I am trying to use gptel on a hidden buffer I have set up with some text and then get the output to my current buffer. Do do this I see two options:

  1. Execute gptel-send on the hidden buffer but send the response to the kill-ring, and then use either gptel-post-response-hook or gptel-post-stream-hook to insert the response in my current buffer
  2. Enable gptel-mode in my current buffer, set "Existing session" to my current buffer and stream the response right there. This would be the best option, however it needs to go to point, not to the end of the file

Looking at the source code it seems very entangled with your transient integration, so not sure how to do this programmatically. How should this be setup?

Thanks for a great package!

karthink commented 5 months ago

gptel-send and the transient menu are meant for interactive use only. For custom workflows, there's a more programmable/general API via gptel-request (see the wiki for examples). gptel-request is what the transient menu uses to create queries with custom sources/destinations etc.

Here's a simple version of what you're trying to do

(defun gptel-send-hidden-buffer (hidden-buf)
  "Send the contents of HIDDEN-BUF to an LLM and insert response here."
  (gptel-request (with-current-buffer hidden-buf (buffer-string))))

If you want to do something else with the response besides inserting it at point, you can specify a custom :callback to gptel-request, please check its documentation.

I also realized the function of a couple of the keyword arguments (:buffer and :position) is not clear from the documentation. I'd appreciate any feedback you have about making gptel-request easier to understand and use.

bergheim commented 5 months ago

How did I miss gptel-request! That did it (I love this low-level take you are doing!). Wonderful, thanks! :dancers:

Edit: regarding the two keyword arguments, I did not need to change those, so I had no issues. But I assumed :buffer could just take the buffer-name as a string if I wanted to add that while glossing over it.

karthink commented 5 months ago

But I assumed :buffer could just take the buffer-name as a string if I wanted to add that while glossing over it.

The problem with:buffer and :position is that it's not clear what specifying them even does. Do they affect where the prompt is collected from? Where the response is inserted? Something else? So I'll have to simplify/refine gptel-request at some point -- without causing any breaking changes. Task for future-me to worry about :slightly_smiling_face:, closing this now.