- 3 Minutes Wednesdays
- Posts
- R Blackmagic for LLM Prompting
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:
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 š)
Generate Insights in Minutes, not Hours: This comprehensive course teaches you to handle data faster, smarter, and more efficiently.
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