In the first lesson in this course, we're going to take a look at how you'll get started with the PaLM APIs for code generation. Now, of course, in order to be able to do anything, there's some necessary setup, and I'll guide you through that. The PaLM APIs and their associated tools are continually being updated on the Google generative AI site for developers. And this includes MakerSuite, a fast and easy way for you to prototype with generative AI prompting, as well as Vertex AI which gives you scalability and enterprise-grade privacy, security, and a whole lot more. But the focus of this course will be on the PaLM API, which gives you access to many of the features of Google's large language models via a coding interface. You'll get hands-on with writing code using this API in this course, so let's dive in and see what you're going to need. It's a pretty simple list as we can see. So first of all, you're going to need an API key. And when you're doing this in the real world, you'll get one from the site that I showed you on the last slide, developers.generativeai.google. For the purpose of this course, you don't really need to worry. We've made one for you, but it's something that you're going to need to keep in mind. Next you're going to need the Generative AI Libraries from Google. And at the time of filming, they're available in Node.js, Swift, and Python, as well as with a curl interface. But for this course, I'll be using Python, and I'll show you how to do a pip install. There's a lot in PaLM, including lots of different back-end models that are designed for different purposes. It's a lot of fun to explore them and their naming in particular. You're going to see lots of animal names, and in general, the larger the animal, the larger the model. Let's take a look. First of all, to get your API key, we've provided one for you, but you're going to need a utility to get that. So there's a helper function in here, and I'm just going to do get_api_key to get that helper function. And now in the next cell, what I'm going to do is import the Google generative AI libraries. We're going to call them palm, and we're then going to configure that with the API key. import google.generativeai as palm. Okay, so now that I've done that, I'm going to run this cell. Okay, I've imported the key and again, as I've mentioned earlier on, I'm importing google.generativeai. I'm going to call that palm and palm.configure, passing an api_key parameter to be the results of this API key. All right, so now we're good to go with those. Now, if you were going to do this on your own system that doesn't have the back end, you would have to pip install the google.generativeai stuff and pip install as you know just would work like that. Um if you wanted to do it quietly, pip install -q google.generativeai. it just works like that. So what I'm going to do next is just explore some of these models. So I'm going to do a list for m in palm .list_models. This is a function in the PaLM API that allows you to, as the name suggests, list all the models. And instead of having you watch me type everything, I'm just going to paste in. what happens next, and then we're just going to print the name, the description, and the methods that it supports. So let's run this cell and see what we get. Okay, we see we're getting chat-bison, text-bison, and embedding-gecko. Guess which one are the large models, guess which one's smaller. Right, so we can see that there are two bisons and there's a gecko. And, um, but what we want to do today is to generate text. And we'll see that this chat-bison supports generateMessage, but this text-bison supports generateText. So this is really nice and we know that we're going to be using this model. But there's another function that you can use in here, and as the more models get supported, if there are models, for example, if there are multiple models that support generateText, I can start doing something like models equals m for m in palm.list_models() if generate, oops, generate text. So what I'm saying here is if uh 'generateText' is actually in the supported generation methods, right? We see the generation methods here. Then, what I'm going to do is just print m. Okay, or just show what those models are. So, and then if I run this, Okay, now we can see some more details on this model. text-bison-001 was the only one that actually supported that. So again, this is just some ways that you can start using the PaLM API to understand what's going on. as the API grows and as the supported models grow, you might be getting different results here and you might be able to try a different model and have a little bit more fun with it. As we see, we have like three models end up getting listed out. There's the chat-bison bison-001, the text-bison-001, and the embedding-gecko-001. So you imagine Gecko is going to be smaller. I mentioned it's based on animal sizes. As you're using this um over time, you might be seeing more being added here. Now, one question that you might hear a lot is what's the difference between a chat-bison and a text-bison. So the goal behind a chat-bison is that more optimized for chat scenarios where it keeps track of the context. So you'll ask it something, it'll give an answer, you might follow up, it'll give another answer and you might follow up again. Whereas the text-bison-001 is more optimized for single shot. You're going to give it a prompt, you're going to get an answer and then you're going to move on. We're going to be using that one today because, you know, I find that one generally works much better for code. So now, we've only got one model that supports generateText, so I'm just going to create a variable that points at it. I'll call it model_bison, and that of course is the first one in our models list. Right, so if I just want to output model_bison, just to make sure I'm getting the right one. I run this code and we see I'm getting the same output. Okay, so now that we have our model, let's um just create another helper function and this helper function is going to be the one that just generates text, um so that we don't have to keep writing the same code again and again and again. It's always good practice not to repeat yourself, so it's always good practice not to repeat yourself. It's always good practice not to repeat yourself. So I'm going to start this with from google.api_core import retry. And what the retry library is, it's just something that when you're doing a backend call to something like a like we're doing here to an LLM, for example, that sometimes things can fall out of sync, sometimes you can uh just the your call can, you know, get lost in the intertube somewhere. And instead of you writing a whole bunch of code to retry it and to constantly retry it, this uh can do it with just a very simple decorator like this, retry.Retry. And then we can write our function. So our function is going to be called surprisingly generate_text. And what I'm going to do with generate_text is I'm going to pass it my prompt. And I'm going to pass it the model that I want to use and we've already created something that we call model_bison. And then I'm going to use a temperature. Now, the default temperature for the model is 0.7. Uh, but with this temperature of 0.0, it's going to be a much more deterministic model. so the whatever prompts whatever results I'm getting from prompts here, you should be seeing the same ones. Of course, if you're using a later version and you have a later model, it might have changed differently. And once we've done that, now we just want to return the what uh PaLM is going to give us. So palm.generate_text and passing on the same thing. So prompt equals prompt, model equals model, and temperature equals temperature. So this helps us to have a nice helper function going throughout our code, um so that we just don't have to reinventing the wheel. All right, let me run this cell. Make sure everything's okay. It looks good. So now that we've created the generate_text function, this helper function that you're going to do, I want to just talk briefly and show some slides briefly around how you're going to be able to use this with prompting to be able to generate code. So now, we're going to take our first steps in doing some code generation using the PaLM API. And there's a pattern that I like and we're going to follow that across all of the examples in this short course. The process looks like this. First, we're going to create a prompt. And we'll start with a very simple static string and later show how to template this to make it much more complex and powerful. This contains the basic command that we want to send to the LLM to instruct it to generate some output for us. We'll then use that prompt with the generate_text function that we just created and with the model that we selected to get a completion. It's worth noting here that generally when using LLMs, the output text from the model is what it predicted the next set of tokens would be. So when you pass in a question, the next set of tokens would usually be an answer, and thus it completes the string that you began with the prompt. And that gives rise to this completion as opposed to answer or something else that would be slightly less accurate. And then of course, finally, after the completion is output. And it's up to you how you would do this. In this course we'll just be printing it out in the Jupyter notebook, but in a real app you might be injecting code into an IDE, saving it to a repo, or many other things. So let's explore the code for these steps to do a basic code generation with PaLM. So now that we've done this, let's just do some very, very basic code generation. So I'm going to just give it a very simple prompt. equals... How about show me how to iterate across a list in Python. Sounds like a song, doesn't it? And that's going to be my prompt. Boom. Now, it's assuming a lot here, right? Because it's assuming there is a list, it's assuming it's done in Python, it's assuming there's so many things. And you know, what's this going to look like? Well, let's take a look. So if I say completion equals generate_text and pass it that prompt. What do you think's going to happen? Well, let's see. I'm going to run it. It's going to take a little bit because it's instantiating the APIs. It's making a call to the back end. The back end is generating the stuff for us and, you know, it's all done. You'll if you're not watching the recording and you're doing this in your own notebook, you'll see a little star in the notebook beside it, and when that star turns into the number of the cell, then you'll be able to continue and then you'll be able to do something like print completion.result. And drum roll, boom. We're going to see results like this one. and it's giving us to iterate across a list in Python. You could use the for loop and the syntax would look like this. And here's like if you have a list with ABC, your for loop for item in my_list, print item will give you the output ABC. But one of the really useful things about this as well is that it also finds other ways of doing it. So for example, there's the enumerate function. And you can see how the enumerate function is working on the list. Sorry about the cropping on the screen, but as you look through the code, you'll see how all this kind of stuff is going to work for you. So again, this is very, very simple, very basic code generation, but we're going beyond just creating the code, because I asked for it to show me how to iterate across a list in Python. So it's giving me the code as well as explaining, it's showing me, not just giving me code. And this is the first part and this is the really, really basic code generation. Now, as I showed, I asked it to show me how to iterate across a list in Python. so we got a whole lot of extra stuff. And of course, your typical code generation might just be something along the lines of prompt equals write code to iterate across a list in Python. And, you know, run that, and then let me I'm going to be lazy and I'm just going to copy these. My completion is generate_text. And then print completion.result. And let's see what that gives me. Now, you can see it's just giving me the code, right? I have a a comment, I have comments create the list apple, banana, cherry, and then it's going to just item in list print item. And it only gave me one way of doing it, to iterate using a for loop. So sometimes This is how you prompt and think about how you phrase something is uh your large language model is going to take what you ask it very, very literally. You know, so if you ask it to write code, it'll write code. If you ask it to show you how to do something, you might get some more valuable stuff. um as we did here, where it gave me various options to do it, um and explained them to me as well. So for a little bit of fun, we can also take some of the code that it output, like for example here. And this was Python, giving me a list ABC, and just iterating through that. we can paste that into a cell. See what happens. Boom. Well, it gave me an error because I'm I've brought these things in with the backticks, so let me get rid of those. And then I'll run the code and now we can see that it has actually worked. And if I were to change something, for example, apple banana, cherry. Hopefully, it's actually running the code and it's not just printing ABC. And we can see it actually prints out apple, banana, cherry for me. So, you know, very, very simple code like this is going to work, but one of the things that we will be discussing a lot during the course is that output code is prone to hallucination. So you really, really should be testing your code thoroughly before you use it in any way that's like serious or in production or anything like that. So now it's your turn. Maybe you could try something, you know, prompt equals Show me how to. And what is it that you'd be excited to see? Could it be some common computer science problems, like sorting or counting elements or anything like that. And it's really, really good to experiment with it and also experiment with the English language in the prompt itself as we did here where it said instead of saying show me, we're also saying write code to do something and think about all those different scenarios. Think about how you, if you were having somebody who can do these kind of things, but um takes things very literally and those instructions, how would you phrase your instruction with them? And experiment with it, have some fun with it, and you know, would love to learn about what kind of experiences you're having using it. So after you've tried it out for yourself and you've um come up with some interesting code and all of that kind of thing, it'd be really interesting to see what you've done. We'd love to get some feedback from you. And in the next lesson, what we're going to do is to see how we can make this prompt a little bit more efficient. And we're going to be using something called a string template to do that.