Friday, August 30, 2024

gptel: Mindblowing integration between Emacs and ChatGPT

ChatGPT and its LLM brethren have been a potent source of FOMO: the AI revolution is happening, and I feel like I'm forever late to the party. After getting comfortable with OpenAI's web interface and coding my own command line tool, I figured it was time to complete the trilogy by integrating an LLM AI into Emacs.

As with many topics, there were a number of available Emacs packages, and it wasn't immediately obvious which was 'the best.' And then I watched this (to me) blandly named YouTube video: Every LLM in Emacs, with gptel and was hooked.

This 17 minute video blew my mind. If you're remotely interested in emacs and LLMs, stop reading this post and go watch this video. Heck, watch it a couple of times. In it, the gptel package developer goes through the why and how of gptel, and demonstrates some next-level uses of this package.

So gptel is my answer for adding them magic of ChatGPT to Emacs. gptel can be used both interactively and programmatically. I've found the former capability let's me have a running conversation with an 'assistant.' I can get answers to coding, writing or any other question on my mind without having to leave Emacs. Score.

But interactive use is just the beginning. By leveraging gptel-request, it's possible to automate common patterns. For example, I can set the region to include a word and run bs-gptel-define-word. In a few moments, ChatGPT will come back with a definition, examples, synonyms and antonyms. Here's the definition this function reports for FOMO:

In some respects, this is hardly exciting. There are a number of dictionary services are already integrated into Emacs, so why bother hand coding another? Yet, using gptel has two advantages. First, by changing the prompt, I can easily adjust the output. For example, here's the FOMO definition with a prompt that asks for active and passive voice examples:

The other benefit of using gptel for definitions is that the universe of what's considered a definable term is far larger. For example, I can ask for the 'definition' of a CSS property box-sizing: border-box.

Heck, it even handles slang. Here's the definition of slang: what the sigma

This would have been handy when we were traveling with our nieces and nephew, and this phrase came up repeatedly.

Another example of automating a task by leveraging gptel is gptel-proof. This is an early attempt at coding a proofreading tool within Emacs. The package works by taking an arbitrary region of text and asking ChatGPT to fix the spelling and grammar. The result is embedded back in the original document, surrounded by the familiar version control conflict markers. For example, here's what I see when I proofread this paragraph.

By using the conflict markers, I can use M-x vc-resolve-conflicts, to interactively compare and adopt the improved text.

What I like about this approach is that I don't need to blindly accept the LLM's changes. By using ediff, I'm combining both the magic of AI with the magic of emacs, to make, well, more magic. It's magical. A

This all feels like a very awkward start to using such a game-changing capability. But, at least I've started!

Here's some additional inspiration on the topic: Integrate Emacs with ChatGPT or any LLM, Powerful AI Prompts I have Known and Loved - that you can use, and Emacs pinky Saver: a voice driving GPT4 co-worker for emacs. together.ai is a platform for letting me interact with more LLMs than just ChatGPT.

No comments:

Post a Comment