R Blackmagic for LLM Prompting

Remember how I bamboozled Excel users with my cool PDF automation?

If you want to pull that off too, check out my freshly released "Report Automation With Typst" course.

Guten Tag! šŸ‘‹ 

Many greetings from Munich, Germany. Let’s talk about prompts. That’s important because prompting is everything. You just gotta have the right prompt and the AI will do magic for you. Or at least that’s what AI influencers like to claim…

Personally, I think it makes sense to fill your prompt with specific instructions and relevant data. But beyond that I don’t think that there’s a huge difference whether you use a specific set of words or structures.

But how do we turn arbitrary vectors, lists and data.frames into something we can feed into an LLM? Well, today we’re finding out.

Our goal

Our end goal is this: Create a function glue_prompt() that can be used like the glue() function. But the cool thing is that you can also turn lists and data.frames inside of that into any form you desire for your prompt. Here’s how that can look:

A simple start

To create this function I want to use S7 generics. These give us a powerful mechanism to define how a function should behave based on the function’s input type.

If I were to now use this function with a simple character vector, I’d get the following error:

Defining a method

We haven’t yet told the generic function how to behave when a character vector is passed to it. We can do so by defining a method for characters:

Here, we used x as function argument because that’s how we specified the generic. Take a look at the previous code chunk and notice that we specified "x". This means that generate_prompt() listens to the argument x and will ā€œdispatchā€ a method based on the type of the input for argument x.

This assumes there is a method to dispatch. And we have now made sure that there is a method to dispatch when x is a character vector. This function simply collapses x using commas.

What about other types?

But just because we can deal with characters doesn’t mean that anything else works.

Let’s run a bit of recursive magic on all the types that as.character() can turn into characters.

Here, we say that for either numeric (integer or double), factor or logical vectors we first make them into character vectors and then treat them with generate_prompt() (which we know can dispatch on characters)

What about data.frames?

Well, we can apply the same dispatch logic here as well. We just have to figure out how we want to turn a data.frame object into a text. Luckily for us, there’s the jsonlite::toJSON() function that turns any data.frame into JSON notation.

And now arbitrary collections!

We can turn an arbitrary list of objects into a prompt too. All we have to do is to iterate over the list and apply generate_prompt() which by now can dispatch on almost anything. We might even throw in some opinionated prompt format by using the names of the list as part of a prompt template. And once we have iterated over the list, we can collapse the prompts into one single prompt.

Nice! But how do we turn that into a glue-like function? Well, I’ll tell you next time. In the meantime, if you want to learn more about generic functions, you can check out this video here:

See you next week,
Albert

Whenever you’re ready, there are three I can help you:

  1. Automate Your Data Reports: This course helps data analysts eliminate manual copy-paste reporting by automating PDF reports end-to-end, saving hours every cycle and preventing costly mistakes. (Using the lovely Typst language šŸ˜)

  2. Generate Insights in Minutes, not Hours: This comprehensive course teaches you to handle data faster, smarter, and more efficiently.

  3. Bespoke Data Science Solutions: I’ve helped clients build their own data science solutions. Whether building custom web apps, PDF reports, AI automations or teaching workshops, I’ve got you covered. You can reach out to me via this form (or simply hit reply to this email)

Reply

or to participate.