Hi ,Lan! Hi, Martin!
You built a solution that uses generative AI?
Yes, I used serverless, and it wasn't that hard.
I'm happy to show you.
What does your generative AI solution do, Lan?
Imagine you run a website that lets users book flights and hotels.
You want to create video ads for multiple destinations
and for multiple traveler types.
So if there are 50 destinations and ten
traveler types, that would require 500 video files.
That would take forever to create manually.
That's right.
That's why my team is building a solution called “Generative
AI for Travel Advertisers” to automate the process.
Generative AI knows the top destinations in Singapore, for example.
It can generate the ad text for those sights.
Then the solution can download an image of those sights.
Finally, combining the text and image, we can create video ads
for these destinations.
Very nice.
Did you have to study AI to make this work?
I know I'm not an expert in AI and I wouldn't know where to start.
I'm not an AI expert either.
But it was fairly easy to build this solution
because it just calls the Google Cloud Vertex API.
It simply sends a question to the AI, and the AI responds.
Also, the solution runs on Google Cloud Functions,
so I don't have to do the configuration for the servers.
I just supply the source code and Google Cloud takes care of the rest.
Great.
What does it look like when it runs?
All right, here's the homepage.
I'll click start.
I'll pick Singapore as the destination
and Relaxation Lovers and Nature Goers as the travel audiences.
Then I'll click the
Next button to generate the destinations.
And now the tool is asking Google's generative
AI to find the Singapore destinations?
Yes, the solution is asking AI for the top places
for relaxation lovers and nature goers in Singapore.
Looks like it’s done.
We got three relaxing places and three nature places.
Let’s generate the video ads for them!
And is the any AI doing any work now?
No. The AI is done.
Now the solution runs some code to generate the video files...
and it's done.
I'll click Next and then we get to see the generated video ads.
Wow. That was much quicker than I would have been able
to pick all those places and create those videos manually.
But I'm a developer.
Yes. Martin.
So I'd like to see how you built this.
Can I? Sure. Let's take a look.
First, I needed a question I could ask the AI.
I also needed values for some parameters that the AI asked for.
So I went to Vertex AI
and picked Generative AI Studio and then Language.
I click here to generate a new prompt.
That looks pretty open ended.
You just type there in that box?
Yeah.
So I used this page to try out different prompts.
After some trial and error I arrived at this format: “Provide three places
to visit in Singapore if I like relaxation in the format of ‘place, slogan’.
Each description is less than eight words.
As you can see here, that gives a pretty good result.
Tell me about all the sliders and the other controls there to the right.
So I can change the generative AI model with this dropdown.
I used the default one
Temperature
controls how random the AI response will be.
A low temperature is good for prompts that expect a true or correct response.
A high temperature can lead to a more diverse result.
So I found that a temperature up 0.1 works best for my question.
For the other parameters I used the default values.
Okay, so you got a prompt and parameters that work well.
What was your next step?
So I click Get Code up there.
From there I was able to choose Python, Python Collab or Curl.
Collab (Notebook) is a Jupyter Notebook service hosted by Google.
I like to use it for experimenting and tweaking my code before I deployed it.
Very nice.
So generative AI Studio gave you the AI code for your application
and you tweaked it until you were happy with it,
and then you added some supporting code around it?
Yes. Let's have a look.
Here's the cloud function that generates the places.
I will click the Source tab so we can see the code.
The entry point is the get_recommended_places() Python function.
The function starts with some CORS-related stuff.
That code handles the case when the web page
and the cloud function are served from different domain names, right?
That's right.
The interesting code starts here,
so I loop over every audience and every place.
For every audience and place combination
I call the make_prompt() function.
And that function generates the prompt
or question that you will send to the API.
Exactly. It's a simple function.
It returns the prompt that I came up with in the Generative AI Studio.
And how do you send that prompt that string to the AI?
So I would go back to the get_recommended_places.
The prompt that I got from
make_prompt is stored in the prompt variable.
Then I send the variable to the provide_reasons function.
That function is quite short.
Yes, it only does three things.
One, it creates a text generation model.
That class is provided by Google and I import it at the top of the code.
Two, it calls the predict method on the model.
Three, it returns to response text.
And what would a typical response text look like?
So here is an example.
When I asked the AI for the nature places in Singapore,
it responded with these places.
So I want to separate the name of each place and a description.
And I do that in the parse_text function
that does simple Python string manipulation.
And finally, the
function returns all of the audiences and places to the caller.
And then another piece of code generates videos based on those places?
Yes, that happens in the generate_videos Cloud Function.
It turns out that generating video files
takes more code than calling the AI itself.
Oh, does it?
But no AI is used for generating those video files?
That's right.
The only AI use here is in the provide_reasons function.
Well, I'm no AI expert, but I think I'd be able to write my own
generative AI application based on what you just showed me, Lan.
Yeah. It's a fairly simple process.
We start by trying out different prompts and parameters in the Generative
AI Studio.
When you are happy with them, pick Python collab to experiment
with the code or Python to get all the generated Python code.
Yeah, and the code was pretty straightforward, right?
That's right.
All you have to do is import textGenerationModel.
Create a more instance by calling the from_pretrained method
and then call the predict method on the model instance.
Yeah, I feel inspired to write my own generative AI app!
Thanks for showing us how to do this, Lan!
Thanks for having me, Martin.
And thank you everyone for watching.
If you have any questions for Lan or me, please let us know in the comments.
Also, do let me know
if there are any other serverless topics you'd like to see in future episodes.
I read every single comment.
Until next time!