I Built an AI Coding Agent in Python That Creates Apps ๐คฏ
Key Takeaways
Creates an AI coding agent in Python that generates apps using autonomous AI development
Full Transcript
Hello guys, uh want to build uh you know uh AI coding agent live with me. So I I'll show the demo and later we will build the code live. Okay. Uh so I'll just run this Python file python main. py and it will ask me to enter the task. So what I will do is I will just give like you know uh CLI code to build calculator which consists of no uh add subtract subtract uh multiply and divide. So if you see here uh it has already generated the code uh in the generated app. py uh let me open that generated app.py. So guys the code is already there and you know all the things are there. So let me see whether I can run this uh python generated py no add five to you know it is asking me to enter the choice one the first number five second number two and as you see that you know it is successfully generated the code properly right so this we will try to build live now so it is actually whatever the task you have it is actually doing the coding it is not 100% perfect some codes can have some multiple problems now if if I just do the multiplication thing so three five and it is showing as 15 so like that uh it will actually work but you don't need to do any coding right so agent will do the coding part so we will uh you know just do it in live session Okay. So, so as you guys are seeing my screen, uh, so now from scratch we will start how to, you know, um, build an agent that will write its own code based on the task. Okay. Uh, so what I will do, I will just, uh, first I'll create requirements.txt file. Okay. And here I need to give pythonv and grock. These are the two uh require dependencies which I need to take. And after that I need to go to view terminal and as you know I need to create the virtual environment. We'll do that. Uh okay. So command prompt x and now python mnv vnv and as you see in the left hand side the virtual environment got created. So let it create fully. Let it let it it uh completed all the things. Virtual environment got created guys. Now what I need to do is I need to start uh you know uh I need to create another file called env here which has the gro API key. So what I will do is I will take that graph A K A K A K A K A K A K A K A K A K A KPI key API key is the one which I need to take second I'm taking that yeah so it's done key I have pasted here So what I will do in now is that uh I will start creating that uh you know uh I will start downloading the dependencies. So of that requirement txt uh so here I have this and to activate this virtual environment I need to go to scripts activate.pat pad copy relative path and I have activated the virtual environment here. Okay. So once this is done guys uh I need to give pip install minus r requirements txt. Okay. uh so this will actually create uh you know uh the downloading the dependencies of all the whatever I mentioned here python.nv NV and Grock. Okay. Uh so it is installing everything. So once you install this, what we need to do is uh we need to start uh creating one file called env. Let me remove this. And what I'll do is I will just write one file which is main. py which all the code which I have uh I will write here in the main py. So we'll start with the coding coding point of view. So what can be done here? Okay. So first what I will do is I will start import OS that is the first step and next step is import subprocess. I need to import and from env import load env for the environment variables and afterwards you need to call this functions to load the environment uh variables from the env file. Next client equal to uh grock gro API key. So this is actually calling this API key as you guys are aware in my previous sessions also. So this is what I will do os.ktnv I'm calling the rock a key. Okay. So this is I'm assigning to the client and model equal to OS dot get env model name and model name here it will be llama llama 3.1 okay 8b instant so this will be my model for gro which I'm calling here now I need to call the llm llm call how I need to do llm call so defaf I'll create one function ask lm prompt okay uh this is a prompt and response equal to client dot chat dotcomp completions docreate Okay. Uh so here the parameter will be model that is already I have defined the model in the above here the 10th uh line that is a model and messages equal to ro user content and user prompt comma and temperature can mention 2 yeah comma and I'll close the bracket here. So this is ask lm and afterwards I need to return the return response dot choices dot messages dotc content. So this will be my llm call. Okay now I need to uh you know write some of the coding logic here. So where to how to code and other things right like coding uh how to do the clean code right. So for example uh it should generate me the clean code when it is generating the LLM uh generating the you know uh whatever the task I wanted to do. So for that what I will do is I will just write another thing called clean code because the format will be different uh for if anybody is writing it can be having commands or other things right so let me remove all this trimming the commands because when it compiles it should not give any error for me or something right so that's why I'm writing this function where I'm cleaning the code uh where you know it should actually encode uh so this This is the function which I'm using which is clean code which will be having this kind of indicators in the code. Uh so parts equal to parts equal to code do.split you know that icons whatever is there because that those are the comment values or something it will come as part of the gro API keys or grock things. So I wanted to clean it in you know good format. So that's fine. Okay. Next what I need to do I need to extract the code inside the block. So what I will do is I will write another if condition here. If len part uh is uh two I'll write two. Yeah. And code equal to parts one. Okay. And now I need to remove that Python uh if if it is present like Python or some things is present in the code right I need to remove that variables also. So what I will do for that is code equal to look code dot replace python dot strip okay python yeah you can write it dot strip or you can mention here comma one or something so that it will strip all all the values here. Okay. So this is one thing what we need to write guys. So first afterwards what I need to write I need to return. So this is the uh ultimately we need to do for the clean coding things. Okay. Next we need to do the planner things. Planning the things right. So how the code we should write that is a planning things what we need to write. So now def plan task plan task. Okay. Uh so I will write this prompt. I will just show you what I'm writing. Uh this prompt I'm writing here guys. So this is the prompt actually break this task into clear number steps. So for example, if I just to give calculator or build CLA tool for calculator option using addition and not we will start you know break this task into the number steps. I will show you in the output also. Uh so you can uh I can show in a detailed way also but as of now you just uh think about this is like a prompt for the planning things. Okay. Next is the code generator bit which is a very important uh thing code generator. So once planner is done so I will start with a code generator. So for that I need I will just write another function called generate code. Okay, in this I need to write task plan, previous code previous code equal to comma error. If anything error is there let it display that error part also so that we can debug that right now prompt equal to if so this is the prompt uh just copy paste this because this no need to write this because we have multiple prompt here so what I will do is I'll just copy paste all these things you know what I'm giving prompt and all I will copy paste this Okay. So this is a prompt guys. You are an expert Python developer and returns uh only raw Python code. Don't do the commanding word and all at that compilation. It will give the error. So task plan previous code and error. So after that after that it will return the uh llm prompt. Okay. This is the prompt. So now coming to the exeutor part. So this is a coding coding runner part. And now uh code creator part and now it is a executor part exeutor agent. Okay. So now what I will do I need to run this code run this code effectively so that it should actually start running this code uh and also it should start with the agent loop. Let us see whether it will run or not. Otherwise we'll manually see whether it will run or not and we'll see whether it is working properly or not. Okay. So what I will do I will just start with instantiating that values. Okay. Now executor agent what I will start is I will start writing the code uh for the exeutor agent saying run code. Run code is my exeutor agent guys. And I will write here code and I will start with generating the function. So code equal to clean code code. So which I'm calling here. So clean code is one of my function I'm calling here. So that you know it will it should write the clean code here. Okay. And uh I need to write with loop here with open. Okay. I can write you know generated app py. So this is my file which it should start creating if it is not there and I think I need to write this and also I need to add encoding utf if I t def and def.right equal to code. So this is my uh when it's creating the file it will create with this file name generated app py. Okay. So next what I'll do is I will just do try block and result equal to subprocess you know subprocess may you have python and these are the arguments which we have which is generated app captured output text true and time out. what is the time mode for that particular function uh particular uh once you start building that and afterwards I need to return the result one it okay next I need to write except block except exception as e okay return this icon and after that what we need to print the exception that we need to put it Okay. Now actually we need to put start printing the agent loop here. Agent loop. Okay. How many times you want to run the things, right? So that is what this agent. So def run agent. Okay. task comma maximum iters iters or something is equal to two we'll give as of now we'll see uh how it works and also we need to create colon here now print set start like you know uh planning okay let me print that and now plan Next equal to plant task task land task plan task. Okay. Next print plan text. What is present in the plan text? This is how it works. So now this is the run agent loop. And now what I'll do is I'll instantiate the code equal to this one error equal to empty. And now I need to write for loop for I in range max iters you know code equal to generate code whatever it's there okay std out std out comma std er equal to run code code. Okay. If st e r r print. So I am basically running the agent saying that okay uh if if this generating this give this properly things or print the error also right so error error comma steady error. So this is my printing statement here. So afterwards I'll print error equal to std error. So that is my another step. Else what it should do is should print success output success. Success. Yeah. And next print. Maybe in the next line it can print like you know uh final code or something. Final code. Okay. Uh slash m. Okay. And print code here. Whatever you want to code you can print here. Okay. Afterwards you need to put the return statement. Okay. So this is uh all it guys. Uh so we have printed and afterwards if you want to print the iterations also you can print here. So it's like you know uh adding that value for you. So for loop like this print uh failed iteration or something. Okay. Now print code. Let me print it. And here let me make it as max iteration three so that we can see like what is the things. And now I need to call my main function. So which is at the end if name equal to main task equal to input. Enter your enter your task. Okay. Enter your task or enter your um enter to write the code for this. Something like that we can write. So now run agent equal to task. So this is all it guys. So I have almost written everything. Uh so first what we are doing is we are importing the OAS subrocess and other things and we are in instantiating we are declaring the model which model we are using grock API key and now we are doing the LLM call first. So which will be created by Grock. So which we'll do after that whatever the generated code it should be clean code. We are writing that also. And when you're planning the task, you need to give the prompt section. So see this is a planner agent and this is a code generator agent and this is a executor agent. And last we'll have agent loop which will be uh which can be having you know iterations different iterations. Okay. So this is how you need to write your agentic AI uh you know uh anything what you are writing so that you know it will be clear for you guys to understand you know what kind of uh what kind of things we can write so this is how uh the basic thing of you know uh the coding agent right what I'm trying to build so this is how we can build so lot of advanced things are there I'm not going much deeper into that but this is a basic uh coding coding agent what I'm trying to build here for beginners. Okay. So if you want to you know if you don't want to you know just uh write your write the code like in manual way or something you can you guys can use this utility to generate the code for uh you know your whatever you want to do right so just I'll save this uh save this and I will go to view I'll go to terminal and what I will do is now I will just start with this uh cls and I will just start running python main py. Okay, let me see what it will come. Okay, as soon as I write it is asking me to enter the task what you want to do. So what I can do is cla tool for uh calculator including includes add subtract multiply and divide. I'm just giving this sort of idea. We'll see like how it works. Okay. So as soon as I plan it is giving straight away this error saying that you know uh llama 3.1 something is doesn't exist instant is doesn't exist actually uh so what I can do is uh I have my model name so let me copy paste that okay maybe some spelling mistake will be there some spelling Mistake will be there guys. Sorry for that. CLS. Okay. Uh so Python main.py. Let me run again. and CLI tool for calculator which includes add uh sub subtract subtract sorry subtract multiply and divide. So as soon as you see uh it is planned this all the events right defining the program functionality on the calculator tool choose a programming language including Python, Java, C++ set up a development environments. So it started you know uh it has started with this all these approaches and also when you see this cal uh this values right so it has started giving the description also so what you want to do planning the things and all these things right so this is why this is how it is started with the generating the code and if you see here in the terminal it has almost generated the code uh in the generated app section so earlier this file was not there and this file actually it is coming here and you know it is running successfully now currently and it is uh you know really having this value. So what I will do exactly is that I will just start running this uh python generated app. py we'll see what it will come as part of see if you see here it is it is asking us me to you know enter the first number. So it has created the code and it is asking me to uh you know create uh it is asking me to um enter the first number. I will enter three. So what operations you want like you want plus you want minus or something you let me enter plus here. Let me enter the second number. So as you see so it is giving me the uh result as eight. Okay. So like that you can you know give multiple uh you do you want to continue? Yes. So again I can give a number saying like you know uh which is five minus 2. So as you see it is also giving three as a uh integer right. So it's like a basic thing like testing the things whatever I want to do. Uh so let me go to another program. I mean let me give some more uh practical examples for you guys. uh so that you guys will understand how it is creating. So what I will do is I will just give uh one second I'll just give okay uh so let me add uh so what I will do is I will just add some of the examples uh let me check what can what can be the examples. So it can be uh any gaming or something also. Let me see whether if that is working fine or something right. So what I can do is I will just start with you know uh uh open browser show. Okay. Let me do one thing. I will just start with another one which is uh you know building the back end things. Uh I will just create no here. So let me run again python.m main. Uh so let me do cls now. Okay. And let me again run python main. py and my task will be uh you know build a build a flask API okay with one endpoint see these kind of things also we can write guys I mean API things also we can write I'm just showing you one uh calendar one calculator I showed and next thing is I showing this uh uh JSON hello API simply I'm just writing some uh you know text here. So let me see what kind of files it will write. Uh as soon as you see so it is asking me to install flask here. Uh so to run this uh this things we need to have flask. Okay. So what I will do now currently is that I will just go here and I will do flask also but I'll create flask. So I will just install flask as of now. So let me do that cls. So pip install otherwise see guys it will fail because I need to download that dependency even that has advised me to you know download this dependency. Okay. So I have completed this installation of flask currently. Okay. So what I will do I will just do cls again. And what I will do now is that I will just start uh so here also it is write it written this pip install flag since I didn't write that right. So let me remove this uh because I have installed now currently. So what I will do now I will just write Python generated app uh generated app python generated app py and also it's very good that it's uh if any dependencies is not there it's straight away telling that okay giving that instruction please download the dependency so for example here I didn't had that flask and see if you see this right my back end URL got downloaded and if I go to this URL uh just I'll follow this link and also I'll show that URL also for you or I'll copy this this copy This or Second somehow it is showing not phone but this is how this is how you have created your uh development server uh and you have generated this app as well right so and also debugger pin something is there uh but yeah this is how you it is already created creating a code and telling this message as hello API and you as you see the server is also running successfully here right uh but it's showing 4.4 for but uh I think that it has generated uh you know uh whatever the data we want and it is uh ready for you know usaging the for the back end API to create. Okay. So this is how you can just try start uh you know doing some of the things which you can uh you can use for this task. for example uh put it put put in some comments where you know you want to build something and you can also take this code uh from the bit uh github urls I will share that you guys can also try like uh to enter multiple tasks see uh whether you guys can you know do it together I mean to uh do it in a very useful way for your uh uh your approach or you can see whether see because now coding agents are becoming very common here in the AI space where you just want to you know um write the you don't want to write the code you just with prompt prompt techniques you can you know see whether you know how how we can you know run the code or how we can uh you know create the code right so this is where I want you guys to understand this is a basic structure what I have created currently there are there will be more advanced structure multi multi-tool architecture or you know there will be advanced you know technologies or you know initiatives which will be going up but this is the basic for everything. So this is the foundation for everything and this is how people will start creating this uh you know uh LLM uh I mean the coding agents right coding agents. So this is how they will create. So this is how uh this this was about the session of today. So please let me know if you want any uh other things as well. Uh let me try another one if you want. One sec guys. Let me take some other use case. Use case I'll take let let me check what use case I can take. Uh can take some good use cases for you so that you know it will be perfect. Uh what I will do I will just take one interactive one uh this one saying you know uh okay uh I'll just do Python main py as I said it's it's asking to enter the task so build a build a number guessing game in Python. Let me see what it will do. So yes, as you see the code will be I I think in the blink of a second all the code is written here in the generated um app. py. Okay. Uh so this is written. So now what I can do is I just need to uh you know run that code. Somehow it is not running that uh execution things but uh we will manually run and we'll see how it works. Uh yeah so it has created the code uh as you see in the terminal also. So let me run that code and see how we can generate it. Let me do cls for you. Python generated app. py. So guess a number between 1 to 100 it is asking. So let me guess one number which is which is 9. Sorry your number your guess of nine is too low. Okay. Uh 45 too high. Okay. Then it will be between 9 and 45. Yeah. Uh too low. It was telling right. It is still low. Okay. 20. Sorry, your guess of 20 is also too low. It is telling too low. Too low means it should be inside 20 to 45 if I'm not mistaken. So I'll do 30. 30 is also low. So 35. 35 is also low. 40. 40 is too high. So 37. 37 is too low. 38. 38 is also too low. 39. Yes. Congratulations. Your guess of 39 is correct. You made 18 attempts to win this game. So this is how actually uh I took this number to guess uh the value. But yeah, I mean it was a fun game. Uh it was a good game that we have, you know, guess the number after eight attempts. But yeah, I mean these kind of games also we can build and also we can build some of the other things also like whatever error handing things, Python scripts or you know we need to we can organize files into folders by extensions that also we can build. So like that we can build like you know fibonacious scripts. So if you want to have a quick demo of fibonacious scripts let me give that demo as well. So this number game you go right. So what I will do now I will just put it the uh again I need to run python main py. So I'll write fibonachi. See these are the simple things but the only thing what you need to check is how fast it is writing the code. See guys simple. You're going to see it has added that this one. That's it. Done. So let me run this. Okay, maybe it is not entering this main code but it has started to analyze the fibonosis series variable and this is the code which is generated. Okay. So this is how we need to we can start you know writing multiple codes or you know multiple uh initiatives or multiple whatever whatever you want to you know deal with the code right so you can start doing those things and you can you know uh see whether it will help you guys right so this is how easy it to can write the code so generated app py is a system generated one so let me uh so these are the system generated ated one and this is the one which which is writing the code for you and you know generated app. py is the result of all the main py what we are. Okay. So please uh go through this thoroughly uh and let me know if you want to you know extend this to uh multi- aent tool and also if you want UI proper UI right that also we can do in my upcoming sessions and uh so yeah I mean see this is just a basic skeleton what I have built so still more extensively we can work on this to build like a production grade level application we'll try to do that as well right so because uh in PDLC the coding is the main thing in product product development life cycle and next we will try to build the testing agent also where it will combine with the production it the code and afterwards it will uh do the testing also together we'll see like uh like come in upcoming tutorials I will teach you how to build like end to end product development life cycle from the requirements to the end end to end deployment But we'll go gradually but first the primary thing is coding right. So once you get the coding also only you can test that. So that's where uh I wanted you guys to uh see how we are generating this code uh you know live and also in just a blink of a second you will get all the code as you saw in uh as you saw in the demo. So that's where I want you guys to see how we can uh know how please go through this video and uh if you like this video please like and if you want to you know get more content on this video please comment it out uh so that I can see my things also and they can just come back with the all the things whatever you want and if you have any uh doubts or something uh please reach out to me or slightly DM me in my LinkedIn or you in my YouTube channel only directly uh so that I can you know help you guys in uh you know building whatever the agents uh you want right so that is what is the main thing why I am doing this kind of uh sessions because uh this these are the real uh time problems right for example if you want to write the code so we will start writing the code in a very effective manner so if you see this demo you don't want to write the code itself You just give that uh you know what you want what's the task you want the code will autogenerate itself right so that's where uh so that's where that's where we have come that stage we have already came now where you don't want to code uh manually like you don't want to have the logic you don't want to you know have some many things so it will only autogenerate everything with logic and uh you know with uh using AI and everything right so it will start doing all the stuffs so that's where uh it's heading to the uh the trend is heading to with the AI. uh we need to start building we need to start basic of building this kind of agents where uh you will get upskilled uh in terms of uh others right so we will to be uh in that race of building this AI agents so where it will help the organization in a better way uh by doing this stuffs adaptability is also will be there and also uh we can scale it to multiple uh as I told it's just a one agent what I have wrote coding agent but again it can orchestrate with multiple agents like testing agents or testing is another part right requirement gathering is another part requirement writing is another part so those kind of things the product development cycle from starting from the requirement stage till uh the deployment stage to the production so all these things are like end to end process in that process So I have picked this uh coding as one of the agent where we will this is a main core u because with without coding you'll not get that application uh so without requirements you will not do the coding. So what I have done is like we have picked up one uh uh coding agent where it's simple coding agent but it has the capacity to generate the code uh for whatever the uh whatever the task you want to do right so that is where we are heading to uh that is where the industry is also trying to that is where the market of IT sector is also heading to so please try to uh spend at least a hour of time and see this video end to that and you know develop this kind of agents uh so that you know you will get upskilled uh way more than what you want and also if you are liking this video please follow my channel please subscribe to the channel so that I can bring more in innovative uh ideas and innovative thoughts uh in this channel and innovative agents uh like building some production grade production grade grade agents also we can see how we can build and how we can uh you know uh take those things uh into the bigger bigger level right so these kind of basic things how we can take to the bigger level so it's like a foundation next first step second step third step we'll go like that and this is the foundation uh for this uh coding agent and we will try to do more u like a end to end application kind of thing we I have a plan. So we'll go according to that. So each session by session I will teach you guys uh you know uh so that you know we can build one good production grid application in the product development life cycle. Uh so this is what I wanted to convey in this video and if you really like this video please like so that it will reach for more people. So more people will be uh will have should uh mean I mean wanted to require uh this kind of knowledge but if it is not reaching more I it will be like this video will be like sufficed here only and please comment it out. Uh if any queries are there, if any clarifications are there, please comment it out. Or if anybody has um any doubts also, please comment it out. And if you have any feedbacks also, please comment it out. How I can improve or how I can uh how I need to bring new ideas or new thoughts. And if you have any new ideas or thought, please comment it out. And if you like if you please follow my channel for more the more things uh lined up more development agent development is lined up. So please follow my channel for the more updates. Please uh subscribe to my channel and yeah, I mean that's all from uh this video and uh you know if you uh think that okay uh this is adding really a value for you uh you guys I'm really happy uh so at least one or two members if they really uh feel like okay uh this is really adding value uh so I think that is the greatest thing for me uh so because see again like all will be all I can't satisfy everybody. So 100 members, 200 members, uh 300 members, if any whole in that whole group or 1K in that group. So if 200 members or 300 members are getting benefit out of it, I'm so happy and u I'm so uh happy that you know you guys are following my channel, you guys are seeing uh my channel. Uh I am very happy that I'm getting I mean I am um I'm trying to teach you guys but uh yeah even even I am learning in this journey. Uh I'm also not 100% perfect. So if I want to make this video first I need to learn perfectly after that only I can make this kind of videos but even I am learning in this journey guys with you guys. Uh yeah. Uh thank you for all of your support and keep supporting me and uh yeah uh okay, see you in the next video. Um yeah, thank you guys. Bye.
Original Description
Github link-https://github.com/chethannj/AICodeGenerator.git
I built an AI coding agent in Python that can plan tasks, write code, run it, debug errors, and fix itself โ just like Devin ๐คฏ
In this video, I show a full end-to-end demo of an autonomous AI developer using Python and Groq. Watch it think, fail, and improve its own code in real time.
๐ฅ What you'll see:
* Agent planning tasks
* Writing real Python code
* Debugging errors automatically
* Building working apps
This is the future of software development.
#AI #Python #AgenticAI #Devin #CodingAgent #Automation
Watch on YouTube โ
(saves to browser)
Sign in to unlock AI tutor explanation ยท โก30
More on: Agentic Coding
View skill โRelated Reads
๐ฐ
๐ฐ
๐ฐ
๐ฐ
What I Learned Shipping AI Into a Real Financial Workflow
Medium ยท Python
Sprint One: Why We Pivoted MedReach AI to "Data Intelligence First" and Killed the Paid API
Dev.to AI
How I Taught My Autonomous Coding Agent to Survive API Rate Limits
Dev.to ยท yureki_lab
Staying Anonymous on Telegram โ Even When Youโre Running AI Bots
Medium ยท AI
๐
Tutor Explanation
DeepCamp AI