AI Agents Tutorial For Beginners

161.06k views4047 WordsCopy TextShare
codebasics
In this beginners friendly lecture, we will build AI agents with llama3.3 and PhiData framework. We ...
Video Transcript:
in this video we are going to build AI agent using open source llm Lama 3. 3 and a framework called f data this will be a finance agent which will compare two stocks in terms of analyst recommendations and Company fundamentals later on we will build a team of Agents where One agent will be web search agent second agent will be Finance agent third one will be a team lead which will coordinate between these two agents so it is going to be a very fun tutorial before we get started you need to know what an AI agent is just in case if you don't know please watch this video where I have explained AI agents in a simplified manner in terms of Frameworks we could have used crew AI Lang chain Microsoft autogen Etc we used F data for this because I find F data API to be very clean and minimal also in November 2024 which is last month there were 2. 8 million agents that were created using fi data there are many companies using this framework already so it makes sense that we learn this framework which is rising in terms of popularity as a first step we are going to do some setup before we start building our first agent we are going to use the latest Lama 3.
3 open source model in Gro Cloud you can download this model and run it locally but it's a huge model you need a bigger system to support it the good news is Gro cloud is a free Cloud platform which allows you to use Lama 3. 2 you create an account by going to console. gro.
com I already have an account so I'm already logged in click on API keys and create an API key then save that API key in some safe place make sure you're not sharing that key with somebody else you will also install fi data python module by simply running pip install F data this video is sponsored by code Rabbit Code rabbit is an AI code review platform that helps you significantly reduce your code review time as well as potential bugs it is trusted by thousand plus organizations more than 5 million pull requests have been reviewed by code rabbit if you have any questions on the code review comment you can chat with it and get the answer not only that you can finetune code reviews based on the user feedback they have shared a special coupon code which I have placed in the video description below using this you can get one month free access all right back to our video now I created this folder called Agent tutorials F data where I will save my code I opened it in py Cham and I will create the file called Simple grock agent okay so let's write a simple grock agent for this we need to import couple of things so from F dot agent first of all import agent class then import model import okay now this model supports bunch of models Gro AMA mistal and so on we are using Gro so let's select that and creating agent is so straightforward you will just do agent and supply model argument so your model will be a model on Gro Cloud okay now Gro Cloud supports so many different model so if you look at this particular drop down you can use any of these models I'm going to use the latest 3. 3 70b versatile here you just Supply that same exit string okay Lama 3. 370b versatile so Supply that particular string here and this will be my agent now you need to do API key setup for which you can use environment module so let's uh import this one and call load.
environment if you don't have this module installed once again it's pretty straightforward P install. environment okay make sure you run that command this function expects EnV file so here I'm going to create EnV file where you need to put your key so I will say Gro API key is equal to add your key here I'm going to save my key I'm not going to show you that because it's private many times you know using open AI API gives better performance so I will save open AI key as well I have saved my keys and close that file when I call this function it will create an environment variable called Gro API key and save that key here and when I make a call to this agent when it uses this Lama 3. 3 model it will use that particular key if you don't call this you will get key error so now let's uh give a sample query and you can say print response okay so you can say write two sentence poem for the love between I don't know I'm just making things up these are two Indian Delicacies okay right click run it will take some time to process it but see it worked Dosa and Samosa perfect pair looks like there is a love going on between Dosa and Samosa and it printed the poem accurately so all our setup is okay the query that we gave here there is nothing agentic here is simple prompt that you give okay although I'm calling it agent this is not really an agent I just wanted to taste my connection and my setup now we will create the actual agent for which I will create a new file let's call it Finance agent I will just copy paste this whole code here and now I will enhance this agent so my goal is to uh print a summary of uh analyst recommendation and uh fundamentals between Tesla and Nvidia so I'm giving two stocks to my agent and asking it to kind of compare these two stocks let's say I want to invest in these stocks and I don't know which stock is better so I want to study their analyst recommendation as well as their company fundamentals as you already know this model is uh containing static knowledge so let's say if it is stained until December 2023 it will have knowledge up to that point now you might be running this code in 2025 2026 so you want a latest recommendations for this you need to provide it a tool so agents uses tools to uh fetch the latest information okay so here we are going to provide tools and the tool that we are going to use is why finance Yahoo finance so that is something you can import by going to f tools.
y Finance we have imported this Finance tool and these tools can be multiple so that's why this is an array okay and you create an object of this class you are saying stock price true analyst recommendation true stock fundamentals true and so on some other argument that I want to supply is show tool calls so when internally it is using this tool to retrieve that information I will be able to see those calls then Mark down equal to true I will show you what this means and then instructions uh you use tables to display data so when it prints the report and if it has done comparison I want to see it in a table format and I want to see it using a nice markdown so I don't see those weird characters okay so that's what this argument means I think that's pretty much it let's run this now folks sometimes this may not respond well because Lama 3. 3 although it's a good model it is open source and it is not as reliable as gp4 or other open AI model if you want the best performance you should use open AI API now here it gave a good response it looks like and see it made all these tool calls so the way it worked is when you say summarize and compare analy recommendations for Tesla and Nvidia the llm will try to understand this particular query and it is smart enough because it's a language model it is smart enough to understand that that it needs to somehow pull this data analyst recommendation and fundamentals now when you give that Yahoo finance tool as an input to the agent what happens internally is this so if you control click on y Finance tools what you will find is it will register bunch of functions so it will say self register get current stock price so get current stock price is a function okay so if you look at let me check get stock fundamentals actually so control click so this is part of the five data toolkit you see five tools five finance and here there is a document string so this documentation is being used by llm to understand the purpose of this function so we are registering pretty much like all the functions see get current stock price company info we have registered all these functions and our agent will understand you know what agent will do is it will use this doc string okay so let me go here get stock fundamentals it will use this doc string and it will give it to llm and say hey llm if you need to use this function then this is the purpose so this is very descriptive llm can understand this really well and it can map it to the intent of a user query so going back to this diagram by the way there was a problem initially so I fixed it uh so here this is an agent okay so in agent when you register all these functions right get current stock price get analy recommendation it is using all these doc strings here to enhance its understanding or the knowledge uh of its tools so this is tool one this is tool two so now it knows that okay if you want to get fundamentals or let's say if you want to get analyst recommendation see it can map this user query analyst recommendation with this particular doc strings and it knows that now for these stocks Tesla and Nvidia it needs to uh call this function and in the argument you have the stock symbol So based on all this information this llm is smart enough to map this query to this function and also supply all these arguments okay so it called analyst recommendation for Tesla and Nvidia then stock fundamental for Tesla and Nvidia it retried all all of that and see now it is showing the recommendation for both the companies then it is showing the fundamentals right fundamentals is like your market cap p ratio Etc these are nvidia's fundamentals and it is also comparing them so sometimes it will compare it as a table so right now I'm not seeing a table here but the output will be different uh because this is generative AI U output will be different if you're using once again GPT model you will see a B output so uh agent is as good as the model that you're using sometimes if you're not using the right model and if agent doesn't work then it's not a problem of your agent or your framework agentic framework such as F data it is a problem of most likely underlying problem with that LM that you're using okay so here now see this time second time it actually created this table so this is getting cut off but I think if you increase the width of of it you will see the comparison so Tesla Nvidia what is the market cap what is the p ratio you know you see all this comparison to understand this even better you can supply this argument debug mode equal to True okay let's run this with debug mode true here you will see step by step execution so it uh it is saying that function this from Yahoo finance added to the model all these functions are added to your toolkit then um this is a prompt system prompt right because you have given this instruction let me show you you give this instruction right use tables to display data so use tables to display data then when you say markdown equal to true it will convert that into a system prompt use markdown to format your answers and this is the actual query and then it made this tool call so it call get analyst recommend Commendation with symbol Tesla so from this user query llm is smart enough to understand that these two are symbols right the stock symbols it needs to supply that to get analyst recommendation and once again get analyst recommendation uh documentation is being used here so when you go here uh get analys recommendation this doc string is actually very important from that it figured that it needs stock symbol as an input so that's what it did okay so it call bunch of functions all these functions you know running these functions and then uh it uh just got that data and it processed it see it it got all this data see this is the response from that function it it called them multiple times and it processed all that response see once it has all the response then for llm to process it in a nice output format is pretty straightforward now you might have a question that I can give same question to chat GPT and it also gives a similar answer because it has this web search feature so it will do web search and it will do this comparison well the reason is this is also an agent perplexity is also an agent when you type something in perplexity it will do bunch of searches from internet and it will compose the nice response so perplexity chat GPT when you are using it along with web search these are actually agents behind the scene it is using GPT 40 model but chat GPT is an application is an agent the benefit of using uh framework such as fi data is that you can customize your agents right you can supply uh tools right instead of let's say uh Yahoo tools I trust Bloomberg data you can supply Bloomberg tools if you're working in a Healthcare Company you have your own internal API internal database you can give all of that in Chad GPT you can't do that chat GPT is a public application they have their own bunch of fixed static tools that they use and you have to uh just accept whatever response they give but with the framework like fi data you can totally customize your application let me show you how you can have your own custom tool so the Custom Tool can be as simple as a python function so let's say fire data right now is not a public company okay and you want to compare its uh fundamentals so you can have uh I'm just hardcoding Microsoft as a company because F data doesn't have any fundamentals in Yahoo finance so I'm kind of Faking it but here this function can be given to your tools okay so notice this this is an array I'm giving the second element in the array which is your company symbol and here I'm saying compare Tesla and F data Tesla and F data and in the instruction you can say if you do do not know the company symbol please use this tool even if it is not okay let me just add it in a second line even if it is not a public company okay so F data once again is not publicly trading but I'm giving an instruction that if you don't know the company symbol so which clearly this will not know the company symbol I'm saying use this tool which I have given here even if it is not a public company now this is not guaranteed to work by the way but I'm just showing you an example okay so here it did not work although I gave the instruction um it still uses this F data as a company symbol so if you see see the company symbol it it used is fire data so what I'm going to do is I will U comment out this llama and I will use the open AI model okay so from f. model.
openai import open AI chat so I refer to the document to figure out what is the correct syntax so see open AI key you have to give in your environment file and okay open a chat this is the ID so this is the ID we are giving here let's see if open a I can figure it out so run it again yep it did see for five data it did not know the symbol so it called get company symbol and for five dat it got msft so let me just show you here uh let's see where it is calling that get company symbol see function get company symbol added to the model and uh for the tool company F data see for company F data it call this get company function see that's why was telling your agent is as good as your model Lama 3. 3 was not smart enough to figure it out but GPD 40 is good enough see so for get company symbol it returned that msft symbol okay uh let me just show you so get company symbol is being called here yes see and now you see the msft so just look at this uh detailed trace and you will notice that it is now comparing t TLA with msft okay I mean here is saying Microsoft but I I I hope you're getting this point right I was just Faking It just to demonstrate the usage but you can give let's say 10 different tools here one tool can be calling your company's internal API the other tool con can be retrieving data for from your company's internal database third tool can be calling some public API you can mix and match a variety of use cases is the important thing here is going to be again a doc string so when we registered this Tool uh it understood the purpose of this tool by going over the doc string now I will show you how you can create a team of Agents so right click new python file agent teams here I have created two agents one is web agent and the tool that I have given is duck Dugo duck. go is this uh search engine similar to Google it can search the internet the second agent is finance agent the same agent that we created last time I'm using open AI you can use Lama 3 as well okay and in a team of Agents what you do is you create this kind of agent and you supply team argument and as a list you supply all these agents so web agent Finance agent and so on and the instructions I have similar thing I always include sources and use tables to display data okay so the query I have is summarize analyst recommendation and share the latest news now for the latest news it will use duck ducko to uh get the analyst recommendation it is going to use y Finance tools so it's like a this is more like a team leader okay so this is a team leader he has two people working in his team the first person person is finance agent who will do the work of getting analyst recommendations the second agent is or second team member is a web agent who will do the work of going to internet searching the information for the latest news for NVIDIA so right click and run this all right look at this so it is running the finance agent it is running the web agent uh for finance agent it is saying that retrieve the latest analyst recommend ation for NVIDIA for web agent it is saying find the latest news about Nvidia okay and see these are the recommendations and these are the latest news we can also try using the gro Lama 3.
3 so let's try that so I'm going to commend this code okay so let's uh from f. model. Gro import gr so commment this as well and G grock so now both of the agents are having grock model uh see one approach you can do is for the agents you can give this kind of Cheaper lightweight models and for the team lead you can give uh better model right like the more powerful model but for now I will give the same model to all three previously see this agent will have a default model so if you click on this uh the model there will be a default model that it will be using that's why it was working but anyways now all the three agents two agents and the team Le have Lama 3.
3 so let's run it and folks once again like sometimes if you run it you see an output see I see this kind of error a lot with Lama 3. 3 so you have to run it again if you run it multiple times it will work okay this is not working okay I mean I don't know why this is not working but previous ly this was working okay let me just use openi here use openi for the team lead and for the other okay let's use that see now it worked so sometimes based on the llm behavior you will get different outputs but see you created a team of Agents all the code is on the GitHub in the video description below so please feel free to download and play with it find it also supports this agent UI in the interest of time I'm not going to run this code but I'll will quickly show you what it is you can create this playground dopy file put all your agents okay and kick off this playground by running piy o and then uh running python playground dopy this UI will be running locally on your computer okay and what you can do with this this UI is this see uh when you run it locally it will launch this kind of UI and in this UI you can just ask a general question okay what's your spatial skill and here by the way see the agent that is selected is finance agent and you are using open AI GPT 40 and Local Host 777 is the end point where it is running Finance agent obviously has Financial skills and what's the stock price for Tesla Nvidia and amdc when you give it see it's it's like a chat GPT your local chat GPT which is using this Finance agent that you built so this UI is actually very useful if you want to taste your software uh you take your agents and create this playground.
Copyright © 2025. Made with ♥ in London by YTScribe.com