- 3 Minutes Wednesdays
- Posts
- 3MW (Using AI Functions With R & {ellmer})
3MW (Using AI Functions With R & {ellmer})
Enjoy 3 Minute Wednesdays at no cost.
This newsletter is brought to you for free. If you want to support my work and advance your R skills at the same time, then you will like my paid offerings:
Guten Tag!
Many greetings from Munich, Germany. Today, we are going to learn about equipping our AI with deterministic tools. This is a fantastic way to equip your AI with certain skills you may need and reduces the risk of hallucinations.
This works by
registering a function with your LLM,
letting the LLM fill out the arguments based on the prompt,
executing the function with the specified arguments in the background, and
then passing the response to the LLM so that it can generate its response.
The {ellmer}
docs summarize this via this neat info-graphic:
Now, this sounds like a lot of steps. But thanks to {ellmer}
they are all automated, we just have to set this up. And this is exactly what I’m going to show you today. So let’s dive in.
Chat first, of course
Like any workflow with {ellmer}
, we need to have a chat object first.
Let’s try out our AI and ask it what the weather at the Washington Monument will be at 5 am local time.
Well, that was a bust. At least ChatGPT was honest and said it doesn’t know how to do that.
Creating a tool
So let us build a good-ol’ function that Gippity can use later on. (Also, look at how cool I can write GPT.) Thankfully, the weather data for the Washington monument is only a few API calls away.
And even better for us, we actually covered that in another 3MW edition. So let me just throw together the code that we used there. You can read up on the details in the old 3MW post.
If you recall, we first make the forecast using the {httr2}
package:
And then we extract the data from the response.
Cool! This shows us all the data our GPT friend needs to generate a meaningful response. Usually, though, LLMs prefer JSON formats. So, let’s bring our data to JSON format.
This doesn’t look as nice to us humans as the tibble format. But apparently GPTs like this more. So, let’s give our AI overlords what they want, shall we? Anyway, let’s wrap all of this into a function.
Register a tool
Now, just because we have created a function doesn’t mean that our chat object knows about that. That’s why we have to register our tool with the register_tool()
method.
Notice that I’ve also used the tool()
function to define a tool. At this point, this tool()
call only adds a description to our function. But we’ll soon see that more stuff can be used for this.
Let’s try again
For now, let’s try to ask GPT again about the weather at the Washington monument.
Aha! Now, GPT can tell us about the weather. But is the reply actually accurate and not some hallucinated non-sense? Let’s check with the data we extracted earlier.
Nice! This looks correct.
Modify location
Now, let’s do something more interesting. Last time, GPT didn’t have to fill any function arguments. So, let’s allow for a couple more locations we can check the weather for.
Notice that I have
created a new argument called
coords
andadded Roxygen2 comments to document the function.
The latter point will be useful in a second. But first, we have to create a new chat with a new system prompt to tell our chat object that it knows specific locations.
Tool registration (again)
Now that we have a new chat, we can register our new function. Remember the Roxygen comments? This lets us pass our function to create_tool_def()
.
In theory, this will send our function (including Roxygen comments) to ChatGPT to generate the tool()
call for us.
Unfortunately, this doesn’t always work perfectly as it is LLM-generated. So, we just take the code and adjust it to what we need. Here, what we have to change is type_unknown()
to type_string()
. Also, the description of the argument could also be more clear.
Now we can take our LLM for a test drive.
And let’s cross-check that with our function.
Hooray! This looks correct. So with that we now how to equip LLMs with functions 🥳
As always, if you have any questions, or just want to reach out, feel free to contact me by replying to this mail or finding me on LinkedIn or on Bluesky.
See you next week,
Albert 👋
Enjoyed this newsletter? Here are other ways I can help you:
Reply