Main

Python AI Choose Your Own Adventure Game - Tutorial

Today, I am going to show you how you can create the most interesting, fun, & simple project you have ever seen. We are going to create an AI choose-your-own-adventure game in under 100 lines of code using Python, powered by OpenAI. Sign up for Astra vector database today. Use your business email address and get from $1000- $3000 in free credits and consulting with your subscription. https://www.datastax.com/lp/astra-registration?utm_medium=youtube_video&utm_source=datastax&utm_campaign=yt_influencers&utm_content=vector_search_tim_ruscica Learn More About Retrieval Augmented Generation (RAG): https://blog.langchain.dev/conversational-retrieval-agents/ Code In This Video: https://github.com/techwithtim/AI-Choose-Your-Own-Adventure-Game OpenAI API Key: https://platform.openai.com/account/api-keys 🎓 *Premium Courses* 🏢 _CourseCareers_ https://coursecareers.com/a/techwithtim 🔗 _BlockchainExpert_ https://algoexpert.io/blockchain (use code “tim”) 💻 _ProgrammingExpert_ https://programmingexpert.io/tim (use code “tim”) 🎓 *Free Courses* 📚 _Introduction To Software Development_ https://coursecareers.com/a/techwithtim 🎞 *Video Resources* ⏳ *Timestamps* 00:00 | Introduction 00:54 | Project Demo 03:52 | The Problem With LLMs 06:16 | The Solution (Vector Databases) 07:39 | Astra Cassandra Database Setup 09:03 | Connecting To The Database 14:54 | Adding LLM Memory 19:00 | Integrating With OpenAI 20:34 | The Prompt 24:15 | OpenAI API Key 24:49 | Using The Model/AI+ 28:03 | Final Game Demo 🔗 *Socials* 📸 _Instagram_ https://www.instagram.com/tech_with_tim 🐦 _Twitter_ https://twitter.com/TechWithTimm 💬 _Discord_ https://discord.gg/twt 🤝 _LinkedIn_ https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌐 _Website_ https://techwithtim.net 💾 _GitHub_ https://github.com/techwithtim *Support* 👕 _Merch_ https://teespring.com/stores/tech-with-tim-merch-shop 💵 _Donations_ https://www.paypal.com/donate?hosted_button_id=CU9FV329ADNT8 🙏 _Patreon_ https://www.patreon.com/techwithtim 🔖 *Tags* - Tech With Tim - Python Coding - Game Development *Hashtags* #techwithtim #python #gamedev

Tech With Tim

5 months ago

in this video I'm going to show you how to build the most interesting fun yet simple python project that you've ever seen now I know that's saying a lot but what we're going to do here is build an AI Choose Your Own Adventure game in under a hundred lines of code now I'm going to walk you through step by step and I promise you even if you're a beginner programmer you're going to be able to follow along here and you're going to have a fantastic project that is going to be a lot of fun to play wit
h now for those of you that just want to dive right into the code I'll leave a time stamp down below but I did want to mention that we're going to be doing this using an open AI GPT model and integrating that with an Apache Cassandra database recently they added Vector support and that means we can actually inject real-time data in a memory buffer into our AI model really really cool stuff I know it seems complicated but I promise I will break it down and it will be crystal clear now let's dive
into a quick demo of the project so I'm going to run the code here quickly though I'll mention that a Choose Your Own Adventure game is a branching game where you have multiple different paths and kind of options that you can choose and each choice affects the choice in the future and eventually you're trying to go down the correct path these were really popular back in the day when I was a kid and I've built a bunch of hard-coded Choose Your Own Adventure games but they can be really tedious to
write out so have a look at this welcome to Whispering Woods it is said that the Lost Gem of strandy Lies hidden in these Woods you must Adventure deep into the depths of the forest to find it before we begin what weapons would you like to take with you now notice I can choose any weapons I want here different from a traditional Chooser and Adventure game so go with maybe Shield and ax okay so I'm going to take those as my weapons very well you've chosen a shield in ax now let us enter the Whis
pering Woods and begin your quest okay let's see what it says next as you make your way through the forest you come across a fork in the path to the left lies a path of darkness and to the right lies a path of light let's go light that seems like the better option okay let's see what brings us you have chosen the path of light you continue down the path until you come to a clearing where a small cottage sits in the middle as you approach the door swings open in a mysterious figure beckons you in
side will you enter yes I will okay let's see what we got here and you step inside and find yourself in a cozy room with a warm fire burning in the corner the mysterious figure greets you and tells you that the Gemma Serenity is hidden deep within the forest he then offers you a choice you can either go at it alone or with this help let's go alone now notice all the stuff you see here I didn't write this is being generated by the AI okay so keep that in mind very well you decide to go in alone d
o you enter the cave or do you turn back enter the cave I'm just trying to get it to a point where I either die or I find the stone so you guys can see how this ends uh you enter the cave as you make your way through uh you see a dragon do you want to sneak past or fight it let's fight the dragon and you attempt to fight the dragon but it proves too powerful for you you are quickly defeated and the dragon devours you whole the end there you go so this whole thing that you saw was generated by AI
in real time as I was typing it and it uses memory so what that means is it knows all of the choices I've made previously so we can generate the next choice using contextual data really really interesting and I'm going to show you exactly how to build this now let's just quickly compare that to a traditional Choose Your Own Adventure game so this is some code I'm just going to quickly scroll through that would be a hard-coded version of that where you need to actually manually enter every singl
e path every single option you need to check for a specific string you couldn't type what I typed where you do like an exclamation point at the end or you have a leading space so you kind of do a weird answer to the question you need to actually hard code all of this stuff out as you can see in here whereas what we're going to do is just give a really detailed prompt that describes how the game should be played and the AI is going to take over and handle all of it for us so I promise we're going
to get into the code but stick with me here for a minute or two where I share with you some important information about how powerful what I'm going to show you actually is and how you can apply this outside of this simple tutorial so when we think about llms and these language models like chat GPT they're very impactful they can do a lot of really interesting stuff and the main benefit to us as developers is that they can reason based on information and a lot of times we'll want to take a model
like this and use it in our application we want to implement generative AI now the main issue in bottleneck we're going to face here is that we don't really have a way at least many people don't know a way to inject real-time data into the model now what that means is that the model can only reason based on the data it was trained on right if it was trained back in September 2021 like chat gbt it doesn't know up-to-date information and it's going to give us kind of hallucinations where it's mis
recalling things it's giving us the wrong data it doesn't know the answer to specific questions because it doesn't have the information to be able to answer what we're asking now at the same time these models have a very hard time remembering large chunks of information and recalling that data accurately go into a chat GPT window ask it a bunch of questions then ask it how many questions have I asked you and you're going to see quite quickly it'll get that wrong a lot even ask it to recall previ
ous questions or prompts that you've given to it and again it's going to hallucinate and it will give you a lot of wrong answers so we as a developer need to mitigate that so that we can actually have a reliable generative AI tool that we can use and that can be production ready and user facing how do we do that well we need to essentially inject the data that we deem accurate and that we want the model to reason in in the prompt and then instruct the model to reason based on that information no
w this is a technique no known as rag or retrieval augmented generation now to go a step further what you can actually do is use the model ask it to tell you the information it needs and then based on its response retrieve the information feed it into the model and have it give you an accurate reply this is a big thing it's very trendy right now again rag rag anyways in this tutorial what I'm going to show you how to do is have a memory for our llm that means that we'll actually allow it to know
all of the previous paths all of the previous replies so it can generate an accurate reply going forward for our Choose Your Own Adventure game so now comes the question how do we do all of that well obviously I'm going to show you here step by step but to be able to provide real-time data to an llm we need to have a very very fast database and in this video we're going to use a vector database provided from data Stacks I've actually teamed up with them for this video they've sponsored it and p
rovide a free Vector database that you guys can use and you'll see exactly exactly why it's kind of the best tool for this type of application anyways what is a vector database well a vector database stores information as vectors in multi-dimensional Space versus a tabular database or like your typical kind of SQL database right this actually allows us to very very quickly search for information because we're searching in a multi-dimensional vector space where we can compare vectors together ver
y quickly relative to searching in an index database that can kind of get out of hand when it gets very very large at small scale obviously none of this matters when you scale to the point where we're dealing with petabytes of data we have all kinds of information we'd want our llm to have access to we need to be able to retrieve that information instantly really really fast right so we need to use a vectorized database to do that or at least a very very fast database and this is the best one th
at I know of I'm going to show you how to set that up but again the vector database stores data in a different representation you can look it up it's actually really interesting how it works and it's kind of the ideal type of tool for something like an llm and the type of application we're going to build so as I was saying what we need for this to work is a vectorized database that can store and retrieve information for us now for that we're going to use the asterisk Sandra database which is a f
ully managed service from datastacks who is the sponsor of this video so click the link in the description you can sign up with them for free you don't need credit card or anything like that and we can make an instance of this database for free and everything will be handled for us typically Vector databases can be complicated to set up but in this case we just press like one or two buttons fill in some info and it will just create it for us and give us all the connection steps that'll walk you
through alright so once you've signed in you should be brought to a page like this and what we can do is go and click create database now we're going to make a vector database again optimized for the AI application database name call this whatever you want I'm just going to call this choose your if we could spell this correctly own adventure looks like I need a language model to help me type for the key space name you can call this whatever you want I'm just going to go with database and then fo
r the free tier you just have one region so just go with whatever the available one is and then same thing we're just going to go with Google cloud or actually I guess you can choose one of these different options but I'm just going to use Google Cloud okay we're going to create this database and it's automatically going to be handled for us it's going to take a few minutes to provision and be set up once it's finished kind of being set up then I will show you how we can connect to it alright so
our database has been created what we're going to do now is go over to connect and we're going to download the appropriate authorization and kind of connection things that we need so what we'll do is we'll follow along with a few steps that are actually down here this is going to allow us to connect to the database and create a place where we'll store the memory for our llm once we have all of that we'll actually connect to open AI we will then generate the prompt and we'll start actually creat
ing the game it's going to take a little bit of time bear with me here I promise this is worth it really really cool application okay so we're going to go from overview to connect we're going to click on generate token we're going to download the token details into the same directory where our python script is so you can see here I'm in vs code you can open any editor you'd like I've created a python file tutorial.pi and inside that same directory very important in the same directory I've put my
Json file now obviously don't share that with people I'm just showing it here I'm going to delete the database afterwards okay next we're going to go to get bundle we're going to click on that we're going to click on our region and we're going to download the secret bundle again to the same directory where our python file is okay now next we need to install a few things but before we do that we are actually going to create a virtual environment where our code is going to live or where our depen
dencies are going to live if you're unfamiliar with a virtual environment this is an isolated container specifically for python where all of the Python packages related to a project are going to be stored very useful when you have a lot of different projects and dependencies anyways what we're going to do here if you're on Windows is you're going to open up Powershell to do that if you're in vs code you can go the little plus button where the arrow is and just click on Powershell if that's not a
n option you can go to the search bar and just type Powershell and you can just run the commands that I'm going to show you from there as long as you're in the correct directory in Powershell now if you are on Mac or Linux then what you're going to do is just open up a normal terminal environment and I'll tell you the corresponding command that you need to run so what we're going to do is we're going to say pip install virtual EnV now this is what will allow us to actually create the virtual env
ironment if you're on Mac or Linux try pip3 install virtual EnV now I'm going to go ahead and run that you can see I already have it installed okay now that we have that we're going to type virtual EnV and then the name of the environment we want to make now typically you'll just call this EnV so I'm going to do EnV and this should be the same command for Mac or Linux okay so you see it created an EnV folder for us up here now what we're going to do is activate the virtual environment now this i
s where the command will differ depending if you're on Mac or Linux if you are on Windows though you're going to type dot slash EnV slash Scripts slash activate Okay go ahead and hit enter and you're going to see a little kind of uh parenthesized EnV here which means you're in the virtual environment now if you're on Mac or Linux you're going to type source and then that same thing so it's going to be dot slash EnV slash grips slash if you do the slashes the right way activate okay and that shou
ld activate the virtual environment for you now to deactivate it you can just type deactivate and you'll get out of it but now that we're inside the virtual environment I'm just going to make this half screen again and we're going to follow along with the installation steps so we're going to go here to python because this is the drive we're using and we're going to first start by installing the Cassandra driver so I'm going to right click there and click on pip install Cassandra driver this is g
oing to take a second once it's finished I'll be right back all right now while we're installing packages there's a few more that we need to install let me make this full screen here these don't come directly from that documentation what we're going to use is something known as Lang chain which is going to allow us to interface with open AI so we're going to type pip install again inside of the virtual environment and we're going to install Casio we're going to install Lang chain like that open
Ai and then tick and it's actually not Tick Tock although it looks like it tick token like that okay so install these Casio Lang chain open AI tick token and that should give us all of the dependencies that we need there might be one or two more but I think that's pretty much it okay so while those install we're gonna go back to half screen here and we are going to now run the next steps here so we already have this directory we don't need that we're just going to go and we're going to copy all
of the code here and we're going to paste this inside of our tutorial script once we have that we can actually close this window and we can start working from this okay so what this is going to do here is connect us to the database now we're going to modify this script slightly but this is just going to essentially test the connection and make sure it's working properly so all we need to do is we just need to go where all these files are and we need to rename them to whatever they are actually n
amed in our directory so you can see here my cloud config secure connect bundle and this is named secure connect Choose Your Own Adventure dot zip okay that's correct that's fine we're going to scroll down you can see this right here this is not correct we need to change this name of our Json file which is choose underscore your underscore if we could do this correctly on adventure like that and then token.json once that's there we're going to open up our terminal from our virtual environment we
're going to run our Python scripts we're going to say Python and then this is called tutorial.pi so I'm going to run that and we're just going to make sure we get some output if we get this output right here or really anything that doesn't tell us an error occurred we're good and we have connected to our database all right so now that this is working we're just going to clean up the code a little bit and start adding some stuff so I'm just going to delete these comments here that we don't need
and I'm going to go and just delete this kind of row code because we're not going to use that either now I'm going to create two variables that we're going to fill in in a second the first is going to be the Astra underscore DB underscore keyspace okay this is going to be whatever you named it when you were creating the database so in this case we called it database we're then going to have another variable here and it's going to be the open AI underscore API underscore key and this is going to
be our openai API key which we'll retrieve later on now though what I'd like to do is I want to connect to the Astrid database or to the Cassandra Astra database whatever you want to call it and I want to initialize memory so I'm going to show you how we create the memory then what we'll need to do is retrieve from memory and inject that into an open AI prompt so we're going to write a fair amount of code here I'll try my best to break it down step by step obviously if I'm too slow for you feel
free to speed up the video so what I'm going to do down here is say message underscore history is equal to Cassandra chat message history okay and we're going to import this from above so we're going to go up here to import and we're going to have a few things we need to import here we're going to say from langchain dot memory import and this is going to be Cassandra chat message history if you see me looking to the side that's because I do have a cheat sheet open I don't have all of this memori
zed obviously but I'm going to type it out with you so I can explain it line by line okay so inside of message history we're going to essentially initialize a place in our Vector database where we're going to store the log of the chat history for this specific application so we're going to say our session underscore ID is equal to and you can make this literally anything you want this is just a unique key that will allow you to retrieve the session later on so if you actually wanted to store the
progress of the choose your adventure game as long as you know what the session ID is and you don't clear the history you can access this at any time you'll see what I mean in a second next we're going to say the session is equal to session which is the connection to our Vector database we're then going to say the key space is equal to because you can have multiple key spaces the Astra DB keyspace and then we're going to say TTL underscore seconds is equal to 3600 this stands for the time to li
ve seconds which means we are going to store all of this for a maximum of 60 minutes if it hits 60 minutes we're going to delete whatever was added inside of here automatically we're then going to say message history dot clear now that's going to clear the message history which is just going to delete all of it that means every time we rerun this application we'll get a new set of memory for the new instance of the game all right now that we've created message history we're going to use a wrappe
r around this that will allow us to easily add to the message history you'll see what I mean but all of this is coming from langchain if you're unfamiliar with Lang chain this essentially allows us to set up pipelines for dealing with models like llms language models Etc and a bunch of other stuff like databases you can see from linkchain.memory import this and this is a wrapper around the vector database right and same thing with a bunch of the other stuff we're going to use now actually from t
he same import line here sorry about the cut there guys we're going to import the conversation buffer memory like that we're then going to come down here and we're going to say that our Cass underscore buff underscore memory is equal to Cassandra not chat message history why do I keep doing Cassandra this is conversation buffer memory and inside of here we're going to have our memory underscore key and you can make this equal to whatever you want but I'm gonna go with chat history and you'll see
where we use that in a second and then I'm going to say my chat memory is equal to the message history now this is going to make it easier for us to load this into the open AI prompt which you're going to see in a second all right so we're actually almost there just bear with me here for a second we're going to go back up here we're gonna import a few things from Lang chain again that we're going to use to interact with open AI we're going to say from Lang chain Dot llms Imports and this is goi
ng to be open AI because that's the one that we're going to use you could use other ones as well we're then going to say from langjain Dot and actually not DOT we're just gonna say from link chain import llm chain and the prompt template okay we're going to go down here we're going to initialize a connection with openai so we're going to say llm is equal to open Ai and we're going to pass the open AI underscore API underscore key equal to the open AI API key okay I think I spelled that correctly
I believe I did so now we have our llm so we'll get this key in a second but for now we'll just write the code next we're going to say llm underscore chain is equal to llm chain and what we're going to do is connect this to our llm so we're going to say llm is equal to llm we're then going to say prompt is equal to and this is going to be a prompt which you'll see in a second and we're going to say our memory is equal to the Cassandra buffer memory so what this is doing Lang chain is just wrapp
ing all the stuff that we need to do with a really easy to use API I know it seems weird because like where are you going to find all this information but this is just right from the documentation okay so that's kind of where I'm copying it from and how I've constructed this project now one thing that we do need to do now is we need to make something known as a prompt template now you may have seen this in some other videos of mine or some other applications but the template is a way where we dy
namically inject data into um kind of Chachi BT or the llm or the prompt right that's what we're doing so I'm going to say prompt is equal to prompt template for the prompt template we specify the input variables that we're going to have which is our dynamic or changing data so we'll say input variables now the first variable is going to be chat history which matches with the name of our memory key okay so what we'll do actually let me just add the next one and then I'll explain this next we're
going to say human input and then we're going to go here and we're going to say the template is equal to template and we're going to define a template here and we'll write this in a second okay so the idea is that we're going to have input variables that we're going to pass into this template you write the input variables like this chat underscore history now when I specify the input variables what will happen is we'll have some value for these we'll pass them into the template and then when we
make a prediction we'll be using a template that contains this Dynamic data so if you go through step by step here right I have my llm chain which now we're going to use our prompt for and we're using our buffer memory the buffer memory key is chat history meaning now when I use the llm it's going to take that memory and it's going to inject it inside of the prompt as the chat history key it's then up to us to provide the human input alright we're almost there now let's fill in the template and
we'll actually start making some predictions now I'm going to copy in the template you can copy this from the link in the description all of the code will be available there but I'm not going to obviously type all of this out and let's read through this template okay so what I've done is I've specified what it is that I want the AI to be kind of responding to or how I want it to act now this template is fully customizable you can change this to be whatever you want and this is really going to be
you defining the rules of your Choose Your Own Adventure story so if I read through this here it says you are now the guide of a mystical Journey at Whispering Woods a traveler named alera seeks the Lost Gem of Serenity you must navigate her through challenges choices and consequences dynamically adapting the tale based on the Traveler's decisions your goal is to create a branching narrative experience where each choice leads to A New Path ultimately determining Alaris fate here are some rules
to follow and this is where you're going to be specific and you're going to tell hey I want you to ask this type of question I want you to ask this first I want you to do this that's where you define the structure of the game it is going to be random but you can make it much more concrete if you make this prompt a lot more detailed so Rules start asking the player to choose some kind of weapons you saw that that's what it did when I asked me have a few paths that lead to success us have some pat
hs that lead to death if the user dies generate a response that explains the death and ends in the text the end I will search for this text to end the game so you're telling with natural language the AI model how you want it to generate a game for you which is really going to be the future of programming kind of a cool way to Showcase that here anyways here is the chat history use this to understand what to say next boom provide the chat history here's the human input and then the AI is going to
generate a response for us hopefully that makes a bit of sense but this is the template that we're now going to pass to the llm which will be chat gbt and now it's going to generate all of this stuff for the game for us we don't need to manually code it like we would have here with like all these if statements and print statements and all that crap we'd have in a traditional game we're almost there what we need to do now is make a prediction but before we can make a prediction we need to have o
ur open API key so let me open up that site let me show you how we generate it alright so to generate your API key from openai you're going to go to platform.openai.com account slash API Keys you can find this pretty easily from the website by clicking over your name if you click that it will say view your API key anyways we're going to take this URL I'll leave this in the description paste it in the browser and we're going to go to create new secret now for this I'm just going to go with game i
t's going to create a secret key obviously I will delete this after the video you're going to copy the key and we're just going to take that and paste that here okay now we have our openai API key now that we have that we can start generating some predictions from our llm so to do that we say llm underscore chain dot predict like that and then we pass to this the human underscore input equal to and then whatever we want this to be so in this case let's just say start the game now what this will
do is return a string to us so we're going to say response is equal to that and then we can print the response now notice that this human input variable matches with the input variable to my prompt template and we could have many other variables if you wanted to add those you just have to specify them here okay so if I go here now and I run my code so it's going to be Python tutorial dot Pi we should just get the output that begins the game for us and then I'll show you how we put this in a loop
okay it actually gives us an issue here it says unknown key space database so it's possible that I entered the wrong keyspace name let me have a look and I'll be right back all right so it looks like I spelled the key space name wrong data BSE hence why it wasn't appearing so let's just go over here and change that to be the correct name I'm sure you guys can spell better than I can so you probably didn't have that issue anyways let's clear and rerun this here and let's finger cross and hope it
gives us the output let's see there you go welcome to Whispering Woods you have come in search of the Lost gem before you embark on your journey you must choose a weapon will you take a sword staff but War ax boom and you see starts the game now what we want to do is put this in a loop and see that memory come into action where it starts generating us through the different paths so let's go down here and let's set up a little bit of a while loop for us so we're going to start by just saying cho
ice is equal to and we're just going to say start like that we're going to say well true okay and then what we're going to do is we're going to generate the response based on the choice now the First Choice I'm just hard coding because I want the first thing the user sees to be some output from the AI so we need to First just give it like a start prompt so it just generates that initial message okay we have that we are then going to say uh choice is equal to inputs we're going to say your reply
like that however before we do that we're just going to look at the response so we're going to print the response I'm going to do dot strip now I'm just going to strip it so it gets rid of any leading or trailing white spaces or new line characters because sometimes the AI generates those we're going to print out the response and then we're going to say if the end in response then we're just going to break now the reason for that is again if we go look at our prompt remember I have a rule here a
nd it says if you die have the end and a period contained in the response so now if that's the case right so if the end is there we're just going to break which is going to end the game for the user that's it now you can make this any search string you want but that's what I told the bot to generate so I'm going to look for that and then we should be good hopefully this makes sense it's a little while loop here but this is just going to run the game infinitely until we win or until we die essent
ially until the AI gives us the end okay so let's go in here zoom in a little bit and let's try this python tutorial.pi if you guys can take a second here there's a little bit of latency and it says welcome to the Whispering Woods um what weapons will you choose let's go with ax and sword okay we're gonna dual wield here chosen ax and a sword good choice you know I must decide what path to take left right or straight let's go left okay you choose left as you venture further down the path you com
e across a large stone wall with a mysterious door what will you do open the door now look at this right you can type anything you want here kick down the door break the door run away like it's so dynamic because of the fact that we're using the AI which is why I really love this project anyways open the door you open the door cautiously and find yourself in a dark and ancient chamber as you explore the chamber you find the chest containing the gem you have success or you've succeeded sorry in y
our quest the end boom ends let's run it one more time okay and by the way if you want it to be longer you could just write in the rules you need to go down at least 10 paths or make 20 decisions before you reach the stone so if you want to make it harder you can just tell the AI make it harder in the prompt right okay welcome to Whispering Woods uh let's bring a shield okay and you've chosen Shield which way do you go let's go left okay and you enter the temple find a large chest you open the c
hest leave it okay and let's see what we get here you leave the chest and continue your journey as you walk through on the path you come across a bridge uh do you want to cross the bridge cross it okay let's see what happens now uh you cross the bridge and make your way to a cottage as you approach the door you hear a loud rumbling suddenly a giant Beast emerges you must fight for your life do you use your Shield or try to run away use the shield so notice it's using a decision I made previously
the shield in this decision right which you can continue to prompt the AI in that main prompt to do you raise your shield and prepare for battle the Beast charge at you but your Shield holds strong and you're able to fend off the attack you defeated the Beast you found the Lost gem congratulations the end okay maybe not the most thrilling end to a story but again you need to instruct it with what you want in the prompt which is really up to you guys so with that said that's going to start wrapp
ing up the video here that's all the code again I'll make it all available from the link in the description so you can just download it and fill in uh the few variables and files and you'll just be good to go and that is how you make an AI Choose Your Own Adventure game I think this was a really really cool project I had a lot of fun building this and I think if you guys adjust this prompt and really add some more rules and give this some more structure you're going to get a really really intere
sting game and a really cool project thanks again to datastacks for sponsoring this video such a pleasure to work with them and really a cool project that we're able to generate here I hope you guys enjoyed and found some value if you did leave a like subscribe and I will see you in the next one [Music]

Comments

@rickaridi

Tim, My sincerest appreciation for your multiple AI related tutorial series. They have been major enablers to me to many others I'm sure. Kudos to you. RickUCkszU2WH9gy1mb0dV-11UJg/ePgfY-K2Kp6Mr8oP1oqAwAc

@stoucan

I made an adventure game today it ended up being really cool with lots of options thanks tim 😊

@Mars5124

I saw this on an ad and I came to watch it. Great video.

@texodus_et6313

This is epic stuff Tim! Zork Nostalgia kicked in! 😊

@samplaying4keeps

I've been wanting to do something like this Tim! Thank you for showing this.

@mohbcn

Thank you Tim for those tutorials they are priceless, i really appreciate your work. 🤯

@djannias

🎯 Key Takeaways for quick navigation: 00:00 📺 The video is about building a Python-based choose your own adventure game using AI. 00:29 🧩 The game integrates an open AI model with an Apache Cassandra database, allowing real-time data injection. 02:30 🕹️ The game dynamically generates story elements and choices based on user interactions with AI-generated content. 04:00 💡 The video discusses the challenges of using language models like GPT-3, including limitations in recalling real-time data. 06:27 🔄 The video mentions using a vectorized database for fast and efficient data retrieval in AI applications. 07:55 🚀 Astra Cassandra database is recommended as a fast and managed database service for this purpose. 09:25 📦 The video provides steps to set up and connect to the Astra Cassandra database. 11:25 ⚙️ Instructions on creating and activating a virtual environment are given for different platforms. 13:26 📝 Code is provided for connecting to the Astra Cassandra database and initializing chat message history. 16:59 🧾 Details on clearing message history and using conversation buffer memory for the game are explained. 18:55 🧰 Setting up the chat memory with message history for use in an OpenAI prompt. 19:24 📦 Importing necessary modules from Lang chain and initializing a connection with OpenAI. 20:27 📝 Creating a prompt template for dynamic data injection and explaining the purpose of input variables. 21:26 🧩 Demonstrating the use of chat history and human input in the prompt template. 22:26 📖 Explaining the customizable template for a choose-your-own-adventure story. 23:24 📜 Defining rules for the AI in the template to structure the game. 24:23 🔑 Generating an OpenAI API key for making predictions with the AI model. 25:22 🔄 Setting up a loop to run the game infinitely and respond to user inputs. 26:22 🚀 Demonstrating the game's dynamic responses based on user input. 27:54 ⏹️ Ending the game when specific conditions are met, as defined in the prompt. Made with HARPA AI

@yutao1982

Easy to follow, great tutorial. This is Instruction FT by using a template and passing in {chat-history}. I am thinking to use a second open-AI bot to fill in the "{human-input}" and see what will happen!! :D Thank you Tim.

@paulthomas1052

Great introduction project to AI using this adventure game. Excellent 😄

@ChalupaTV

This is so crazy for one of my first projects I am literally making a terminal adventure haha this is so cool

@fratzlol

Super interesting ! Quick question though : as the game gets longer, injecting the full conversation history will surpass the llm's context length. Would it be possible to adapt the prompt so {chat_history} is filtered to the information most relevant to {human_prompt} before injecting it to the llm?

@Nohope__

I just finished an Ai course. Once again , we’re on the same page my old tutor.

@severtone263

Amazing project! Twas great to follow along. Finally, someone who can unlock the near-limitless power of AI.

@jasonconsiglio5256

This is an awesome way to interact with chatGPT! Thanks Tim!

@Ninja-wh1ov

Tim you saved my life, I wanna add this in my project😊

@jasonconsiglio5256

At around 12:10 the linux command for activating the env maybe should be "source ./env/bin/activate" instead of "source ./env/Scripts/activate"

@bigloud7067

Wow that’s a really good idea. Gonna try this today

@MeaTLoTioN

Could this be used with a mysql database instead? If so how would you change the code to work with the mysql dB? Would like to remove as many dependencies on other services. Also could this work with a self hosted llama-2 LLM? Again if so could you show another video using mysql and llama-2?

@CherifRahal

you will need to pay for the API, but the video is great, thank you Tim

@DeshonMorgan-rt3hz

for mac its source env/bin/activate to activate the environment