AI Agent: From Prototype to Production

Interpreting Approvals using LLMs

Scott Moss

Scott Moss

Superfilter AI
AI Agent: From Prototype to Production

Check out a free preview of the full AI Agent: From Prototype to Production course

The "Interpreting Approvals using LLMs" Lesson is part of the full, AI Agent: From Prototype to Production course featured in this preview video. Here's what you'd learn in this lesson:

Scott adds a method to the LLM module to run the approval check. The method receives the model, temperature, schema for the response format, and an array of messages. The messages provide a system prompt instructing the LLM to determine if the user approved the request and follows that message with the user's response.

Preview
Close

Transcript from the "Interpreting Approvals using LLMs" Lesson

[00:00:00]
>> Scott Moss: We need a function that, when given a user message, will tell us if the user approved something or disapproved something, cuz we don't know. So it's like, hey, we're gonna make an LLM call that responds back with an object that has an approved property on it, and approved is true or false.

[00:00:16]
And it's just like, did the user say they approve or not? This math reasoning thing, this is an artifact left over. I don't know why I left this here. This is just the name of your schema. When you give OpenAI API, a schema like this, you have to give it a name, you can put whatever you want here.

[00:00:33]
I guess I copied and pasted this somewhere and just forgot to take this off, but yeah, math reason, you can put whatever you want here. This is just the name of your schema. I'll probably change this to approval or something like that. It's just for OpenAI to know what the name of the schema is for instrumentation purposes.

[00:00:47]
So yeah, let's go do that. So we're gonna go into our LLM file. LLM, we're gonna make a new one up here, or actually I'll do it down here. And it is called runApprovalCheck,
>> Scott Moss: And it's gonna take a user message, which is gonna be a string,
>> Scott Moss: And it's just gonna call OpenAI beta chat completions parse.

[00:01:21]
So I'm gonna say results =openai.beta.chat.completions.parse. The model I'm gonna use this for 4O mini.
>> Scott Moss: I'm gonna put a temperature on 0.1, cuz I don't want any randomness here. For the messages, I'm gonna make a new user message here. So role is user content is user message.
>> Scott Moss: And then we need to give the AI instructions so we can do that in a system prompt before the user message.

[00:02:12]
And then we need to give it a response format. And then we need to return whether or not that was true or not. So let's do that. So we'll make a new message at the beginning of this array. This will be our system prompt. So we'll say role system content are gonna be the instructions.

[00:02:27]
So you are a helpful assistant. Your job is to determine if the
>> Scott Moss: Given user message indicates an approval or not. Something like that, that's probably not good. What the hell did I put here? I put, determine if the user approved the image generation. If you are not sure, then it is not approved.

[00:03:06]
That's so much better. Let me copy that one [LAUGH]. I think I e-mailed that one and got it to there. So I'm gonna do that.
>> Scott Moss: Determine if the user approved the image generation. If you're not sure, then it's not approved. So I've made this specifically for image generation, which, honestly, it doesn't need to be here, because this has no concept of image generation.

[00:03:30]
It doesn't know anything about that. So this probably doesn't mean anything. I think I put this here because I was obviously gonna write this for image generation. And if the person responded back with something about image generation and their approval, this thing would have been confused. So I specifically put image generation here, which would also mean that eventually I would need a list of tools that need approvals here.

[00:03:52]
And then I would loop over those names here to be whether or not they approve image generation or this, or this, or this, so it knows the name of every tool that needs an approval, but I only have one, so I just hard coded it. Okay, and then the next thing we need is a response format.

[00:04:08]
So response format uses this zod response format helper, and it takes in an object, c.object like this, and then the second one again I told you the name of the schema I'm just gonna call it approval. And it's only gonna take in, was it approved or not? And I'm gonna say that's a boolean, and I'll give a description, did the user approve the action or not.

[00:04:45]
And do I not have zod here? Let me import zod.
>> Scott Moss: Cool.
>> Scott Moss: So we have that, this is what gives us the structured output along with the beta parse.
>> Scott Moss: And then, now I need to return, and it should be type check now, so now I can say result.choices[0].message.parsed, and that, as you can see, is an object with an approved property owner, approved.

[00:05:30]
So only reason this wouldn't be here is if it was refusal instead.
>> Scott Moss: So obviously you would check for refusal. We're not gonna do that. We're just gonna assume that it's there.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now