Hello everyone, welcome back to the channel. In today's video we are going to do a small Python project. It's a choose your own adventure project and we will be using the Gemini Flash 2.0 model. I'm choosing Gemini because we have a free API. We can do 15 requests per minute. That should be enough to complete the adventure game. So the game will look something like this. We are going to give prompt to the large language model. Then it's going to come
up with some kind of settings, character, role, story, the game flow as well. So the game flow will basically be some kind of text that will describe what is happening in that section. So in this case we have an introduction and then at the bottom gives us two options. We can type one to go into an asteroid field or we can type two to basically scan from a safe distance. And based on these choices the story will progress. This is similar to books that we used to have back in the days. Like if you've read books that came out in
the 1990s we had type of a book, a genre that says choose your own adventure. Then you would read the first page. At the bottom of that first page it will tell you to go to page 17 if you want to make choice A or go to page 300 if you want to make choice B. And then you would read that part and then at the bottom of that page there will be another choice and you keep on going until you reach the end. I had a lot of fun with those books back in
the days so I will try to replicate it using Python and then using the Gemini AI. You can of course use OpenAI, ChatGPT. You can also use Ollama if you want to. Let's get started. I'm going to use PyCharm to write the code. This is because it's beginner friendly and I will be using the functionality to refactor the code. It's a lot easier to refactor code
inside of PyCharm. I'm using the free version here. You don't need the paid version. You can use any IDE of your choice. So if you are following along using PyCharm, once you start you will get your menu like this where it says welcome to PyCharm and then we have these buttons here. I'm going to click on new project and at the top here is where you want to save your project. Then click on this create a welcome script. It's going to
create a main.py for you and then in here you can choose the type of environment that you would like to use. The Python virtual environment. You can use a Conda virtual environment or you can use a custom environment where you select the type here. I'm going to go with a simple Python virtual environment and you can click on the drop-down to select your Python version. To be honest it doesn't really matter as long as you are above 3.10. So if you have anything above 3.10 just select it from the drop-down and then click on next button. Okay so once it starts you will
see at the bottom that it's going to index the file. Right-click on this ..venv and then make sure that it does not say marked exclusion here. So let me cancel it. Right-click when you click on mark directory as. If it shows excluded here then you need to select it otherwise every time you make some changes it's going to scan this directory and that's going to take a long time. Now this is the main.py that it created for us and it's just some example codes. Now in here I have an example.py and this is a manual version that I basically type in and if I were to do this manually I'll have to go and describe the start of the game the first time that the person is making some choices. If they choose one then it goes into a
specific function. If you choose two it goes into a different function and each function is basically part of the story that's going to happen that choice. Now this is the manual method. Instead we will be using the Gemini AI. So in the description of this video I will have a link that will take you to this page where we can get a quick start for the Gemini API. It tells us here how to install it as well as how to use it but most importantly we have this button here that will get you your API key. So click on this button it will take you to the page where you need to get your API key. Now at
the top here you can see that I am logged in. If you do not have a Google account you will need to create a Google account and then log in. In here it gives you an example of how you can use code to test out your API key. You just need to replace this. And at the bottom here if you have any keys
that you've already generated it will show up. Now for you if you do not have any click on this create API key and it's going to create one like this. Then you just need to copy that API key. You can click on this API key where we have these numbers here. For me it's hidden right now but when you are doing it you will get the full API keys. Just click on the copy button.
Next we'll need to set it up as an environment variable. So click on the start button. Just search for environment variable and select this one here. Now once it's open you will see a section at the top that's for users. So this one here. And then the bottom is for system variable. We
are interested in the top part. Now I've already created mine. You can see it here but you click on this new button here. The name the variable name here is going to be this Gemini API key. So copy it from the page. Paste it here. And then in here is where you're going to paste your API key. And once
you have your API key here click on OK. It's going to create this key for you. Make sure the name is exactly GEMINI_API_KEY. There are no spaces in front or at the back and everything is uppercase. Click on OK to save it. And that's all the setup that we need. Let's go back into this quick start guide and we'll copy this line here. So click on this copy button here. And let's go back into PyCharm. Now in PyCharm on the
left side you will see that we have a terminal. If you are using Windows you can press Alt F12 and that will open the terminal as well. Now by default PyCharm will restart your terminal inside the virtual environment. So this
.venv is this name here. That .venv that we've created automatically. You can verify by typing python -m pip --version and it will tell you where your Python executable is located at. So mine is inside my projects. This is my project name and then we can see it's inside the python .venv package here. So once you confirm it we can paste the entire command. If you want to be extra sure in the front of it you can type in python -m and then paste it. Press Enter. This will install all
the necessary dependencies. You will not see any usual thing that says requirements already satisfied or it's actually downloading and that is because this command has the -q which stands for quiet and quiet means there are no logs. By the way the PyCharm has a Python packages that you can click on and you can also search for it. So the package we are looking for is google-genai and we can see the package here. So
But if you want to install it directly using the PyCharm Python packages then you can search like this click on it and you will get an install button here. Okay so you can use this as documentation. What we are interested in is this section here. So either use the documentation from PyCharm or go into your browser and copy this code here. In the main.py file
let's paste everything and it tells us that we need the API key. Now at the top we are going to import OS. By the way the method of searching for a system variable from the search menu here that is only for Windows. If you are on Mac you will need to export your API key first. So just look for how to export API key on Google or you can ask ChatGPT. It will tell you the steps for your operating system. Now for now
we'll need to extract that API key from system variables. Let's give it a name API_KEY. We'll say OS.getenv() and then it asks us for a key. And this key is a string. Now this string will have to be this exact name that you've used. So make sure you're using the exact name
GEMINI_API_KEY and then add it as a string here. Now I will add an error message here. In case the person forgot to add their gemini API key then I will tell them that hey you do not have a key you just need to set it up. Now let me comment out the rest of the code and let me show you what this will do. Let's go into the terminal once again and we'll
do Python main.py. When I run the code we don't get anything and that is because it found this key. But let's say I change the name now we can see that it's going to give us an error a value error and it says that set up the environmental variable with the API key using the variable name gemini API key. So this code here is basically checking to see if this gemini API underscore key exists. If it does we don't do anything. We
continue with the rest of the code. If it does not we are going to stop the application and then ask the user to add the API key. Now once we have So we'll pass this API key here and this creates a client. So for the genai we have a client. The client has models generate content. The model that we are using is the gemini 2.0 flash and this one like I said you will have 15 requests per minute and there are no limit in terms of how many hours or days that you can use it. As long as you are doing only
15 requests per minute you can use it as many times as you'd like. Now content is the prompt that we usually give to the large language model. So if I go into gemini here and scroll up you can see that I've written a small prompt here. I said let's play a game and I'm describing what I want. I want the theme, settings, characters and then it's supposed to
provide two to three choices at key decision points and I want it to start with an introduction the first choice and then we'll continue based on the user input. Then it goes ahead and give me this. So we want to do this programmatically. I'm going to copy the prompt. I will have the project files in the description in the post so you can go ahead download the post and of course you're going to get the prompt. Let's go to the top here. I'm going to say prompt or we can say initial prompt since the prompt will change based on the story that we are at. I'll open three quotation
mark that is the multi-line functionality of PyCharm and then we can paste everything inside it. So here's the prompt if you don't want to look into the file. I know some of you like to try things out after getting the explanation. So you can type this in. You can change it or modify it for any preferences that you may have. If you like to have sci-fi type of storyline or pirate-like, you can add the instructions in here. So this is initial prompt. I'm going to copy it, go into contents here and paste it and we'll test out to see if we are able to communicate with the API. So once again we'll go into the
terminal and you can type in clear or CLS to clear out the terminal and then python main.py to execute everything. And right now we can see that it's taking longer. That is because it's communicating with Gemini API. So if I scroll up, it says, okay, let's play. We have
the settings. It's a space exploration sci-fi and new commission navigator on the Starship Odyssey. We have the storyline here. It says that it's going to have multiple endings. That's basically the choose your own adventure type. And then writing style is second person perspective, emotive and engaging. Then we have the
story here. Let's scroll down. That's a big introduction. Okay, it says here, you have two choices and precious little time. What do you do? Type one to focus on rerouting power. Type two to prioritizing pinpointing the source of the energy surge. Right
now, I cannot type one to do anything because my code exits right here. So we'll need some way of capturing the user input and then sending that user input back to Gemini. So once we print it out, we can do input. This is how we capture inputs from the user. And we can just say type your choice. And let's
name this as choice. Now once we've captured it, we'll need to send another request like this, actually, like here, just the response part. So let's copy for now. Let's, let's not worry about writing the best code right away. Let's get it to work. And then we can refactor it so that it becomes the proper way of writing code. Now we have another
problem. So this section here is the initial part. Let me add comments. So this is the initial prompt. We are going to get introduction and our first choice. Now depending on the first choice here, the user will make a choice. Now we
need to send the user choice to the model. So if I make choice and replace contact with choice, let me show you what's going to happen. Let's go to the terminal. Before that, let's add a little bit of spaces so that it's easier to see. So triple spaces here. And just before we print the response, I'm going to add a little bit of space so that we know this is the second one. This is
the first and this is the choice. So let's go into the terminal. And it will regenerate the first section. So actually we got the same answer, like the start of the same answer. But you can see that the character now changed. We are no longer that navigator. We are instead this. I'm not sure what this is. And now the choices also change. So we are attempting to repair some
communication devices or we can try to get some survivor hacks. Now it says here, type your choice. So if I type in one, press enter, it's going to go in and send to the model. But you can see here that says, okay, I'm ready. What can I help you with? This is because the model, the way it is, it does not have any context of the previous chat. So it does not know what is happening here. Now you may have heard of a memory buffer. If you are trying to make your
own agent, normally you are going to have a memory buffer that's going to store the past prompts. It can be like the last five prompts. And based on that, the next reply from the model will have context from those past five replies. To do this, we'll need to use the multi-turn conversation. So in here, we
can see that the first chat we have sent message, I have two dogs, then it prints the response. The next time we can ask a different question. And we can say this, then it's going to go in and give us the response. We can also get the messages from
the chat history if we need those messages. So let's copy this code and go back into PyCharm. Now I will replace everything below the API key, paste it. I don't need this from Google because
it's already at the top. I already have it here. So let's remove this. Now we can see that the client is the same. We need to pass
in the API score key. And we create a chat this time. And this chat has sent messages, which will be the message that we are sending.
So if we go into Gemini here, basically what sent message will do, it will send this and then within the same window, it's going to ask us whether we want one or two. And if I type in two, it's going to go and generate the next section. And then we go down, we can go and type in one. Then again, it goes
and generate that next section. So let's go back into PyCharm. This first time here, let's add some comment. This is going to be the
initial prompt. That's going to be our prompt here. Next, we'll replace the example message with our initial prompt. This way, when we
are starting our first prompt, the model knows that we are doing a choose your own adventure type of game. We print the response here. And then again, we are going to do this. So because I want the user to interact with our application, I'm going to once again, add the user choice. So that's going to be choice equals to input, and then we'll just ask them to type in their choice. Now, once we have that, instead of
this example here, we are going to pass in choice, and then we'll print the response. Now, just to separate things again, I'm going to just add like a dummy statement here. So I'm just separating it by three new lines. This way, we know that initial prompt, and then we are going to have our input. Again, three line breaks, and then we are going to have this response. And
then in order to know that this is the history that we are looking at, again, I'm going to separate it. So let's do a test. So let's go into the terminal via or CLS to clear out then Python main dot py, press enter. And again, it's, it's going and hitting that API. Now if I scroll up, here, let's play we have a new setting now cyberpunk mystery, freelance, and then we can see the netrunner name goes. Okay, so that would be an indication that we have a new story now. So this is a new
request, but remember that we are using the chat now. So this should be one chat. So right now we are still within the chat and we have three choices. I'm just going to select one press enter, and it's going to continue the story now. Hopefully, let's scroll up. Okay, so type in
your choice here. And let me check if the story is actually continuing. So we have rusty dragon in the first reply, then it asks us what do you want? I've said one. And then it
says, okay, you are you decided to go straight to rusty dragon. And we can see that the first choice is head straight to rusty dragon. Time is money. And basically, it's going
and telling us the entire story here. And then we have another choice here that says Do you type one accept the job without question, press inspector for more information or play for time. All right, now the next part here we see role user. And this is the chat
history that we are getting here. So let's examine the chat history. This is going to come in handy when we need to grab certain responses from the history. So we can see that the first user history is this prompt, the initial prompt, and then the model here replied with the introduction.
I scroll down, we have the choices here. And then again, we have role user, the user selected one, the model replied here. So at this point, we can copy this section, paste it multiple times, or you can create a while loop just after the first response. So this is what I'm going to do. I'll do a while true. Let's remove this now because we know it's working inside the loop. I'm
going to ask for a choice. Now if choice is, let's say exit, then I want to quit the person want to exit the application. So let's give them a way of exiting the application. Now, otherwise, if it is anything else, we are going to pass the choice to the model. And then it's going to come up with a response and then send us back to choice. So let's see how this works. So let's do Python to make it easier to see what's happening and to find where we are at. I'm going to add an F string here
and I'm just going to say story. So in the terminal, I just need to find a story and then I'll know this is the new response. Let's let me expand the terminal. And once again, Python main dot py. Oh, choice needs to be like this. So choice equals to equals to. Well, let's restart Python main dot py. Now it's going to use the first prompt. So it's outside
the loop right now. It will do initial prompt, give a response and we can see that we have the setting here. We have the character and all of that. Now we have three choices here. So type one, two, actually repeated two. I'm not sure why it's the same thing here. So basically we just have two choices. I'm going to select
one. One says proceed directly down the main corridor. Press enter. Now this choice that we have was inside the while loop. So after doing this, it says,
okay, you choose to proceed down the main corridor. And then we have all of these. Then it says, which way do you want to go? You can continue ahead. You can investigate. So this time I'm going to say to press enter.
Now we can say that it's continuing. So I've said investigate engineering bay. So it's going and doing whatever wants continuing this story. You face a decision. So we can do these two options and let's do So one was about examining the data storage. And you can see that it's still it
has that context. It's able to continue this story. Let's try to continue it and see how far it can go. So I'm going to look for this now. This will be
choice number two. All right. We don't have any enough time. So let me try to continue ahead. Okay. So it's still after. So this is one, two, three,
four, five. So after five choices, it is still has the context. But at some point, you want the story to end. So what you can do is maybe import random and then we'll say random.randint. And then let's say I want the story to end within three to six choices. So I can do three and six like
this. So this random randint, it will select a random integer, including both end points. So it's going to include three and include six. Maybe three is a little bit too short. Let's do four or maybe let's just do three as a small story. And let's do this as index. Or actually let's do stop index. And just after
it will say current index equals to zero. After the user has made a choice, we are going to increment current index by one. So basically we are counting how many choices the user has already made. Let's go to the top here. We'll do an if statement. So if current index is greater or equal to stop index. So if the user has made choices that is equal to or greater than this number, then I will basically manually type in the choice. I would say the next section is
the ending. Continue the story to have an ending. Now based on the choices, you can add like a random choice at the top.
So you can do something like this and you can say good, neutral, and then we'll say bad. And now this one can be endings. Now there are various ways of doing it. Let's say if they choose number one, you can add plus one. If you choose number two, you can subtract one. And then based on that, you can basically select whether it's going to be good, neutral or bad. And instead of just saying continue
the story to have an ending, you can put in front of it. This can be like a variable that would say bad or good or neutral, right? This way the user have like different options. I'm going to leave that as is and just continue implementing what we were doing. So if we have not hit this limit, we are going to continue and just do the normal thing, get the choice from the user and then do all of this. All right, so to recap here, what we've done here is tracking the number of choices. So just for
the test, I'm going to do two and I will do three. So basically the choices here will be two or three, just so that it's faster. Actually, let's just do one. Let's do one. Wait, what's going on? Let's do one or two.
This way we'll get one choice or two choices and then we can test if this logic is working. So I've said the next section is the ending. The model will ask for a choice here. So because of that, I've said choose
yourself and continue the story to its ending. Finish with the end. Then we pass that to the response. If we have not hit this limit, then we are going to continue this part here where the user is going to select the choice. Let's go into terminal and let's verify this. So python main.py.
This is going to be choice number one. This is the first time and this time we are Ilyas. So I'm going to choose one, which is defense. And okay, it says here you
defense. Let's get down. So what do you want to do? I'm guessing we got two as this stop index. So let's activate the suit and say story here. You've activated your suit. What do you want to do? Oh, let's go up. Okay, here. This is the first time prioritize defense. So I said
one, let's scroll down. So here, what do you want to do? I said activate the suit number two and scrolling down, it says, what do you want to do? Keep on going. But I did not get the input this time. It selected one. So it says, okay, I
will choose and continue the story to an ending. And it says here, let's say the choice is type one. So keeping this, it goes in, explain everything. And we
have a story that ends. So the end in here. Now I made a mistake. Let me close the terminal. That is because even though it's ended, it's not breaking out of the loop here. So instead, I'm going to copy this and paste it at the top and then just break here as well. That's a very simple fix. This way, when we hit the end, it's going to break out of this. We don't really need the history this time. So
let's remove this. Now, if we don't hit this, we are going to go down below where the user is going to select. And again, if they want to manually exit, they can type in exit. So let's test this functionality next. Type on
main.py and I will type in exit. And as you can see, we exit out manually. So let's do another one. Type on main.py. Okay, it says, well, we have a new story. Type one, two or three. I'm going to choose one. And now we can see story. I did not get an input. Let
me go back up. So here it says, what do you want to do? And then it says the choices. But this time I don't have any choice to select it. It's selected for me and says, okay, let's finish this. And then
you choose to use your kronosu, temporal shielding, went ahead, completed the story. Everything goes wide, the end, and we are exiting the application. A very simple project, but it's actually fun. So let's increase this. I'm going to do four choices to six. You can increase it as well. You can always
modify these. You can add, like I've showed you, you can add a good ending, neutral ending, bad ending, depending on the choices that the character has done. You can also instruct the model. So we can actually do it here. You will do it in here. So I've said the next section is the ending. Choose yourself and continue the story to its ending. Finish it with the end. Before I say finish it
with the end, I can also say decide for a good neutral or bad ending. So then I'm going to say finish with the end and then we'll just do this. Good, neutral, bad ending. So the model is going to decide whether it's a good ending, neutral ending, or bad ending. Let's actually see this. I will choose 0 and 1. So this, just so that we can see the
model selecting the ending for us. So python main.py. We should not get any choices this time. It should continue all the way until the end. So you can see it asks for choices here. I did not get
any input and says, OK, let's assume type 3 and then says the end, bad ending. And then it's telling me what's happening here. So that's basically something that you can do. And that's how you can make the model choose instead of hard coding it basically. So let's, let's move this random to the top. Now here's where pycharm functionality comes in really handy.
So we can highlight or you can select a particular variable. You can say refactor and we can do move and move it to a different file. Actually, we'll need to create that file first. So we can just create the file like this.
So instead of main.py, I would say let's do props.py. Click on refactor and it's going to move this initial prompt into prompts. And this way you can have as many prompts as you want in here. You can also randomly select a prompt. So let me go back to this here. Instead of initial prompt, what I can do in this file now, I can say initial prompts and this is going to be a list. then I'm going to have prompt one, prompt two, prompt three, and so on.
You can keep on describing it. Then in your main file here, import the initial prompts. Then you will use the random.choice and then pass it initial prompts to it. This will become initial prompts. What's going to happen is that in initial prompts, we have one, two and three prompts.
This random, the choice will randomly select a prompt, our starting prompt. So let's say this prompt here is sci-fi. This one is romance and this one is action, action adventure. Initial prompt will get one of these
randomly selected. And let's say it select the sci-fi one. This will get sent to our model and the model will generate a story sci-fi. Now we can have our initial prompt be number two, which is romance. And then that got passed to the model. And now our story is a romance story.
This way you have multiple initial prompts and you can have variety instead of this, where I'm saying I want fantasy, sci-fi, mystery, horror and stuff like that. Now, additionally, you can keep on refactoring. So you can move this into maybe API.py, for example.
So this will be just the API section. All right. And if I go into API, you can see I have it here.
Now, what we can do here is I can have another one, a third one like this. Instead of Gemini, I can also have my open API key. I can have both API keys. If you are using a local LLM, you can set up your Ollama in here and use which model that you want to use. Once you have all of that, then you just need to pass the model to your main.py here.
Now, of course, this section should also be moved into the AI one. The client and chat should go into AI. And then at the top, we are just importing chat. So right now, we only have the responses. Of course, you can also move all of this and refactor this into a method that I'm going to call main. And at the top here, if name equals to equals to.
And now you have like a clean code. So we have a main. That's our starting point.
Everything else is in their own file. And then we are just doing this. So we can click on the play button here, run main. When you use the PyCharm play button, it will automatically activate your virtual environment. Then it's going to run the main.py here. OK, it says here we go.
We have sci-fi exploration. This is chapter one this time. And make your choices.
So I'm going to select one. And then I'm just going to keep on selecting one until we get the ending. So that's number three. Choice number three right now. So this is number four.
And this time we did not get a end input field. So right now, it's generating the ending. And we can see it here. The end neutral ending. All right. So now we have a really nice choose your own adventure with the Gemini AI.
So hopefully you've enjoyed this one and make your own. Modify the prompt. You can do anything that you want as long as the logic is the same. The only thing that you'll need to watch out is this stop index here. That's the number of choices that you are giving to your your user.
So in here we have six. Don't go too high. Because at some point, the model will need to lay out the past history to make room for new prompts, new responses. So if you go too high, it will start to forget the first few prompts. That's something that will always happen. All right.
So thank you for watching until the very end. Hopefully now you know how to work with the Gemini API key and the API, the Gemini model itself. Remember that we are using this Gemini 2.0 Flash. If you have a Pro account, you can get the Gemini 2.0 and there are other models as well. You can replace this model with any other model.
You can use OpenAI, you can use Claude, you can use Ollama. The model itself doesn't matter. It's just the quality that you'll get. Most of the time, I think ChatGPT and Claude will give you the best responses. Gemini is just below them. And then Ollama, the models, depending on the number of parameters and the size of the model that you're running locally, it might not be that great of an experience.
But it's always something that you can experiment with. As I mentioned, don't forget to add multiple prompts. So the initial prompts that instead of just having sci-fi space exploratory like I was doing, you can have randomness in it.
You can do sci-fi, mystery, horror, or you can even ask the model to choose a theme for the story. That also is something that you can do. All right, there's a lot of things to explore. This, by the way, can be connected with ComfyUI. So you generate a character, you get a description of that character and you send it into the initial prompt.
So that's something else that you can do. All right, so let me know what you guys think about this. And if you are trying to explore other ways of integrating it with your project, I'm interested in knowing what you will do with it. Thank you for watching until the very end.
Have a nice day. Take care. I will see you next time. Bye bye now. Like the Video, Share and Subscribe for more.
2025-04-10 13:49