Gen AI Course | Gen AI Tutorial For Beginners

codebasics www.youtube.com

Brief Summary

Alright, so this generative AI mini-course is all about getting you up to speed with the fundamentals, diving into Langchain (a Python framework for building GenAI apps), and then rolling up your sleeves for two end-to-end projects. The first project uses a commercial GPT model to build an equity news research tool, and the second uses an open-source LLM to create a Q&A tool for the retail sector.

  • GenAI fundamentals
  • Langchain framework
  • Building 2 GenAI projects

Overview

This course will cover Gen AI fundamentals, Langchain, and building two end-to-end Gen AI projects. The first project involves building an equity news research tool using a commercial GPT model. The second project focuses on creating a Q&A tool for the retail industry using an open-source LLM.

What is Gen AI or Generative AI?

AI is divided into generative and non-generative categories. Non-generative AI involves decision-making based on existing data, like diagnosing pneumonia from chest x-rays or assessing loan eligibility. Generative AI, on the other hand, creates new content, such as text, images, video, or audio. A prime example of generative AI is ChatGPT, which can be used for tasks like resume writing, trip planning, and image creation.

Gen AI evolution

The evolution of AI started with statistical machine learning, which predicted outcomes like home prices based on features such as area, bedrooms, and age. Image recognition tasks, like identifying cats or dogs, involved complex features due to unstructured image data. Neural networks then led to deep learning, followed by recurrent neural networks for language translation. Language models, trained on large text corpora, predict the next word in a sequence, as seen in Gmail's autocomplete feature. Self-supervised learning allows training language models without extensive labeled data, using text from sources like Wikipedia. Large language models (LLMs) like GPT-4, which powers ChatGPT, have billions of parameters and are based on the Transformer architecture introduced in the "Attention is All You Need" paper. Various models exist, including Google's BERT, OpenAI's GPT, and image models like DALL-E and Stable Diffusion. OpenAI's SORA is an example of a text-to-video model.

What is LLM (Large Language Model)?

LLMs are computer programs using neural networks to predict the next words in a sentence, similar to a stochastic parrot mimicking conversations. These models are trained on vast datasets like Wikipedia articles and Google News, enabling them to complete sentences on various topics. ChatGPT uses LLMs like GPT-3 or GPT-4, while other examples include Google's PaLM 2 and Meta's LLaMA. LLMs also use reinforcement learning with human feedback (RLHF) to reduce toxic language, with human intervention ensuring less harmful outputs. Despite their power, LLMs lack subjective experience, emotions, and consciousness, relying solely on trained data.

Embeddings, Vector Database

An embedding is a numeric representation of text as a vector, capturing its meaning and enabling mathematical operations with words and sentences. Vector databases store these embeddings, allowing efficient semantic searches that understand the intent behind user queries. Semantic search uses embeddings to find similarities between words and sentences, even without exact keyword matches. Word embedding techniques, like word2vec, represent words numerically, enabling complex arithmetic. Transformer-based embedding techniques are popular in the era of ChatGPT. Vector databases use indexing techniques like locality-sensitive hashing (LSH) to speed up searches, creating buckets of similar-looking embeddings.

Retrieval Augmented Generation

Retrieval Augmented Generation (RAG) involves using a large language model (LLM) to retrieve answers from a database, such as Excel files, PDF documents, or SQL databases. This approach is used when organizations want to build solutions similar to ChatGPT on their specific data. RAG takes a user's question and has the LLM refer to a database to pull the answer, using private internal organizational data or public data sources.

Tooling for Gen AI

Gen AI tooling requires a model (LLM), cloud service, and framework. Commercial models like GPT-4 and open-source models like LLaMA or Mistral are available. Cloud services such as Azure Open AI, Amazon Bedrock, and Google Cloud can be used. Frameworks like Langchain and Hugging Face Transformer library are also essential, along with deep learning libraries like PyTorch and TensorFlow.

Langchain Fundamentals

Langchain is a framework for building applications on top of large language models (LLMs). It addresses limitations of directly calling open AI API, such as cost, limited knowledge, and lack of access to internal data. Langchain provides integration with various models, search engines, and databases. It also offers plug-and-play support for integrating different models. Key components include prompt templates, chains, and sequential chains. Sequential chains allow connecting multiple chains where the output of one chain becomes the input of the next. Agents connect with external tools and use LLM reasoning capabilities to perform tasks. Memory is also a crucial aspect, allowing chatbots to remember past conversations.

End-to-End Project 1: Equity Research Tool

This project builds a news research tool for equity research analysis, using Langchain, open AI, and Streamlit. The tool allows users to input news article URLs and ask questions, retrieving answers based on those articles. The technical architecture involves a document loader, text splitting, and a vector database. The tool uses semantic search to find relevant chunks of text, improving efficiency and saving on open AI costs. The project is built in Streamlit for a proof of concept, with a long-term architecture involving a database injection system and a chatbot UI. Tax loaders, such as text loader, CSV loader, and unstructured URL loader, are used to load data from various sources. Text splitting is performed using character text splitter and recursive text splitter classes. Vector databases, like FAISS, are used for faster search. The retrieval QA with sources chain is used to combine chunks and generate answers.

End-to-End Project 2: Retail Q&A Tool

This project builds a Q&A tool for a t-shirt store, using a mySQL database and Google Palm. The tool converts natural language questions into SQL queries, providing answers from the database. The technical architecture involves Google Palm for SQL conversion, a SQL database chain class, and few short learning. Few short learning uses training data with sample questions and SQL queries, converted into embedding vectors and stored in a vector database. A UI is built in Streamlit. Google Palm is used for its free access, and a custom mySQL prompt is used to guide the LLM. The project demonstrates how to set up the API key for Google Palm and the mySQL database.

Summarize any YouTube video in seconds

Get summaries like this one for any video, in your language, with the free Summ app.

Download on the App Store
Get it on Google Play

Transcript

YouTube Transcript Generator

0:00 welcome to this generative AI mini course first we will understand the Gen AI fundamentals then we will learn Lang chain which is a python framework used for building gen application and in the end we will build two endtoend gen AI projects the first project will be using commercial GPT model where we will build equity news research tool the second project will be using open-source llm model where we will build a Q&A tool in retail industry let's start with the definition of gen ai ai can be categorized into two sections generative ai non-generative ai when you talk about non-generative AI you are dealing with problems such as you have a chest x-ray and you want to find out if this person has pneumonia or not or maybe you have

0:47 some data on person's credit history and you want to figure out if the person should be given a loan or not in these problems you are not creating new content you have data and based on that data you are making certain decisions in the case of generative AI however you are generating new content the classical example is chat GPD chat GPD is a gen application here you can write your resume you can plan your trip you can even create an image of Hulk playing Gujarati dandia in summary generative AI is a category of AI that is associated with generating new content and this new content can be text images video audio Etc let's now look at the evolution of jna in the early days of AI the kind of

1:35 problems that we used to solve was predicting home price based on factors such as the area the bedrooms the age Etc this is called statistical machine learning and the factors which determine the price of the home such as area bedroom age Etc they were called features now these were simple features when it comes to image recognization such as identifying if the image is cat or dog the feature was kind of complex see here the features are the whiskers or the pointy ears that the cat has and the this data which is an image data is just a bunch of pixel so it is unstructured data in the case of house

2:20 price prediction you have structur data area bedroom Etc so therefore these features in an image they are called complex features the cat's face could be in a different angle and this ear can be in let's say in this corner or let's say the legs are not present at all so that makes uh image detection problem much complex and sometimes you can't use statistical ml for this therefore neural networks were invented and that gave birth to deep learning so there was a statistical machine learning before then came deep learning where neural networks was the main approach that we used used after that came recurrent neural network so in recurrent neural network what you have is this kind of network and let's

3:08 say you are trying to translate this particular sentence in a different language let's say you want to translate from English to Hindi what you do is you feed the first word to your neural network so I will feed D to a neural network and it will give me this translation this A1 is nothing but the translation of D in let's say Hindi after that you feed the second word and the translation of previous word to the same network so these are not four different networks okay this is just a time exess it's the same network where you feed the translation of the previous word or previous sentence so that created a kind of a loop within the neural network and it is called recurrent neural network this was used for solving problems like language

3:55 translation and then came more sophisticated problem which was kind of generative in nature so here is the email which I got and in Gmail when I try to respond it will try to autocomplete you just notice that when I say thanks for reaching out here's my availability see it is autoc completing all these words and the way it works is it looks at the content of the email so let's say Angela said hey we have a potential collaboration opportunity do you have time to talk so the network will read this sentence and then uh it will try to predict the probability of the next word so when I'm saying Angela thanks for the probability of uh saying contacting me is higher and probability

4:42 is generally between zero and one let's say contacting me has a probability of 091 I can also say thanks for reaching out and probability of that could be higher I can say thanks for finishing the project but uh in the context of the previous email that Angela send me the probability of this might be lower I'm putting these numbers randomly but you get an idea if Angela had said that uh hey D I have finished the project please check it out in that case I can say Angela thanks for finishing the project so if we had that kind of context then the probability here would be higher but since we're talking about the collaboration the probability of finishing the project is lower so you get an idea here if you pass huge Corpus

5:30 of text and try to build a language understanding you will be able to predict the next word that comes in the sentence and this is called language model it is an AI model that can predict the next word or set of words for a given sequence of words so here I have text from Wikipedia okay and just look at it uh this is the Wikipedia article on India's Freedom Movement and here to train the neural network networ we can create the training pairs from this paragraph we can uh come up with uh a problem that we want to solve and the problem is uh filling the missing word so here I'm saying first Indian nationalist so first Indian nationalist

6:16 to embrace okay so what is the missing word here well nationalist similarly we can come up with this kind of training Pairs and then we give these training pairs to neural network for training okay and this approach of training a neural network is called self-supervised learning so the good point here is that when you want to train a language model you don't need a lot of label data you can take Wikipedia text text from uh news articles text from variety of books and generate these training Pairs and then train the neural network and after that neural network will be able to predict the missing word

7:03 or the word that comes next in the sentence just like how Gmail autocomplete is working and when you feed huge amount of data to this neural network and let's say when you have so many layers in the neural network let's say the neural network is Big what we get is large language model gp4 a model behind chat GPT is a large language model it has 100 75 billion parameters and when I say parameters is nothing but all these weights in the neural network so see this neural network has only 10 parameters imagine a huge network with so many layers having 175 billion parameters the critical breakthrough in uh jni came when this particular paper

7:51 called attention is all you need was published which gave rise to a spatial neural network architecture called Transformer so just to summarize previously we had statistical machine learning then came deep learning which was based on neural network then came recurrent neural network and then came Transformers which is very powerful and talking about Transformers we have variety of architectures or variety of models for example Google has this model called bird open AI came up with this model called GPT if you look at your chat GPT application you will find this model right now it's gp4 so that is the model that they use and it's called

8:36 generative pre-rain Transformer that's a long form of GPT similar to text model we have image models too for example Del stable diffusion Etc this is the image generated by a model called stable diffusion and if you have chat GPD pro version you can generate image for example generating an image of a person selling what on mask look at this image I mean is quite better I know this person is not wearing the mask but still it's doing pretty decent job to summarize we have text to text model such as bird GPT we have text to image model such as di stable diffusion where you give text and generates image and open a SORA is an example of text to or video model you can just Google open as

9:23 s you'll find the demo you can give a text prompt and it will generate an entire video out of it so all of this has become possible because of Transformer architecture and now that you have good understanding of variety of models in geni space let's look at an analogy based understanding of llm what I'm trying to do here is clarify all these Concepts such as llm Vector DB Etc because these concepts are used heavily in the field of gen okay so next I'm going to play an analogy based animated video video of [Music] llm Peter pande has a curious parrot called buddy buddy has a great mimicking ability and a sharp memory Shar memory

10:10 buddy listens to all the conversations in Peter's home and can mimic them very accurately now when he hears feeling hungry I would like to have some Biryani for this case the probability of him saying Biryani cherries or food is much higher than the words such as bicycle or book Budd he doesn't understand the meaning of Biryani or food or cherries the way humans do all he's doing is using statistical probability along with some Randomness to predict the next word or set of words purely based on the past conversations he has listened to we can call Buddy is stochastic parot stochastic Means A system that is characterized by Randomness or probability a language model is somewhat like a stochastic parrot there are

10:55 computer programs that use a technology called neural networks to predict the next set of words for a sentence for a simple explanation of a neural network please watch this particular video just like how Budd is trained on Peter's home conversations data set you can have a language model that is trained on for example all movie related articles from Wikipedia and it will be able to predict the next set of words for a movie related sentence Gmail autocomplete is one of the many applications that uses a language model underneath now that we have some understanding of a language model let's understand what the heck is a large language model let's go back to our bir example allbody got some Divine superpower and now he can listen to

11:42 Peter's neighbor conversations conversations that are happening in schools and universities in the town in fact not only in his town but all the towns across the world with this extra power and knowledge nowbody can complete the next set of work words on a history subject give your nutrition advice or even write a poem like our powerful parrot buddy large language models are trained on a huge volume of data such as Wikipedia articles Google news articles online books and so on if you look inside the llm you will find a neural network containing trillions of parameters that can capture more complex patterns and nuances in a language chat GPT is an application that uses llm

12:28 called gpt3 or gp4 behind the scenes other examples of llms are palm2 by Google and Lama by meta on top of statistical predictions llm uses another approach called reinforcement learning with human feedback rlf let's understand this once again with Buddy one day Peter was having a conversation with his cute little 2-year-old son son don't eat too much bananas else I will punish you with an iron Rod hearing this Peter realized that buddy has been listening to the conversation from abusive parents in his town what he said was the effect of that

13:14 Peter then starts keeping a close eye on what buddy is saying for the same question buddy can produce multiple answers and all Peter has to do is tell him which one is toxic and which one is not after this training budy doesn't use any toxic language while training CAD GPT open a used a similar approach of human intervention rlf open I used a huge Workforce of humans to make Chad GPT less toxic while llms are very powerful they don't have any subjective experience emotions or Consciousness that we as humans have llms work purely based on the data that they have been trained on now that you have some understanding of llm let let's cover two other important topics called

14:01 embeddings and Vector database embedding is nothing but a numeric representation of text in form of a vector such that you can capture the meaning of that text once you create embeddings for a given text you can do math with the words and sentences such as Paris minus France plus India equal to Delhi or apple minus Tim Cook plus sat Nela equal to Microsoft this sounds crazy right but it is actually possible due to embeddings and Vector database allows to store these embeddings and perform efficient search on these embeddings so let's try to understand these Concepts in a bit more detail we'll start first with the

14:47 startup boom that is going on in the field of vector databases there are some AI startups that have raised millions of dollars of funding and they have one product in common which is Vector database let's try to understand what exactly is Vector database today when you search in Google calories in apple versus employees in apple Google figures out that the first Apple means fruit and the second one is a company have you ever wondered how does Google does this it uses a technique called semantic search semantic search means not searching using the exact keyword matching but understanding the intent of a user query and using the context to perform the search for doing semantic search internally it uses the concept of

15:33 embedding word embedding or sentence embedding is nothing but a numerical representation of text let's first understand how exactly embedding Works let's figure out how you can represent this word Apple into a numeric presentation given this particular context one way is to think about different features of properties of words here you can have related to phones e location has talk Etc as properties and then you assign value for each of these properties Revenue here means $82 billion you get a sequence of numbers as a result and that is nothing but a vector so this Vector is a word embedding for the word Apple for this particular context if you're talking about apple the fruit then the embedding might look something different because

16:20 the value of these properties is different and when you have the embeddings for different words looking at the embedding you can say that the second apple and the word orange they are similar because look at their values they're matching of course there are some values which are not matching but compared to this vector and the first Vector second and third Vector are kind of similar same way if you have let's say Samsung as a word you can represent that into a numeric uh presentation is it related to phones yes is it a location no and when you look at again all the vectors you can figure out that the first and the fourth vectors are kind of similar so using these vectors

17:06 you can figure out the similarity not just similarity you can actually do a complex arithmetics such as this this is a famous example in the NLP domain where you can perform this mathematics using a technique called word to word to is a technique to represent word into a numeric representation I have made a separate video so if you want to know more about it you can go and look at this video in that video I have explained how you can generate handcrafted features for each of these and you can do this particular math now just for intuition I explain everything using handcrafted features but in reality you use some complex statistical techniques to generate these word

17:51 embeddings again if you have curiosity you can watch those two videos or this particular video on bir so far let's say you have this understanding that there are variety of these techniques that you can use to represent a word or a sentence or even a document into an embedding and here are just different techniques which are being used uh in chat GPT era obviously Transformer based embedding techniques are getting popular so when you're using open AI API for embedding uh you know what technique it is using underneath when you're are building any text based AI application you might have thousands or even millions of embedding vectors and you need to store them somewhere when you think about storing them the first option that comes to mind is a

18:37 traditional relational database so let's say for our use case we have these four articles the first two red are related to apple the fruit the remaining are Apple the company you will first generate the embedding let's say using open AI API and then you will save that into let's say your SQL database now when you have a search query you will also generate embedding for that and you will try to compare this embedding with the stored embedding and try to retrieve the relevant documents here you will use a concept of cosign similarity to retrieve the matching vectors and you can display it in your Google search result this in theory works okay but in reality you will have millions of Records or even millions of Records in

19:22 your database and that's when things starts getting interesting because just think about matching this Vector for a query Vector if you want to match with these stored vectors then one of the approach you can use is linear search where you go one by one and if cosine similarity is close to one then you will put that Vector into your result data set and then you can keep on going and store your result vectors now you already realize the problem if there are millions of stored Vector embeddings your computation is going to be too much you know your head will be raised because you can't handle delay and computational uh requirements for such a use case you need to do something smart

20:09 how do we do this in a traditional database well we use a thing called index database index helps you search things faster similarly in this particular use case we can use one hashing function we don't need to go into detail what that hashing function is but let's say this hashing function is creating buckets of s similar looking embeddings okay and then when you have a search query you can let that go through the same hashing function which will bucket it into one of these three buckets and then within that bucket you can do individual linear search this way you are only matching with those vectors which are in bucket one you don't have to match it with bucket two and bucket three this will speed things up and this

20:54 technique is called locality sensitive hashing this is one of the Techni techniques that Vector databases is using there are many such techniques and those techniques are outlined in this beautifully written article I'll provide a link to this article you can uh read through it so far you have realized that Vector databases help you do faster search they also help you store things in an optimal way so these are the two big benefits why Vector databases are gaining popularity after databases we need to understand the concept of retrieval augmented generation also known as rag in my company at lck Technologies where we work on multiple AI client projects one of the common

21:40 requirements is we have this private organizational data or we have this public custom data set can we fine-tune chat GPT on this or can we build chat GPT like Solution on this specific data set and the way you can do that is by using rag let me explain this by giving you an analogy let's say you have a college student called mea mea is a very smart individual she's uh generally good with things she's studying in computer science now we want her to appear in some competitive biology exam which will be based on this book called How microbes rule the world the idea here is

22:25 to not make Mera a biologist but she should just go and appear in that competitive exam and win the prize now you can do this thing in two ways option one is full-fledged training where she goes to a college attends biology classes for one entire year and becomes expert in biology option number two is if the exam committee is allowing open book exam mea can take this book with her you might be aware about open book exam concept where you can take a book in the examination and you can refer to the book to write the answer which option will you go for obviously option number two because you will save time and money both mea doesn't have to wait

23:12 for one entire year to uh finish her biology studies instead she will take this book and since she's very good in terms of Reading Writing and generally she's very smart so she can quickly figure out the answers and she can uh write those answers during the exam similar to this approach when you're building rag based jna application what you do is you take question uh from a user and the llm or the large language model will refer to our database now this database could be Excel files PDF documents SQL database anything this could be either your private internal organizational data or it could be a

23:58 public data the idea is you want llm to pull the answer from this particular data sources and llm can do that using this concept called rag okay we will go over technical understanding of rag when we do our end to end project later on in this video but let's first look at the tools that we can use for building gen AI applications Chad GPT is a gen AI application okay which uses gp4 as a large language model now this AI model is similar to human brain which is train see if I look at my brain it has been trained through my experiences through the college studies through the books

24:45 that I have read Etc gp4 is a similar model that is Stained on your Wikipedia articles books and so much of that data now having brain alone is not enough you need a body correct so I have this brain but uh if you want to uh get benefit of the knowledge in this brain you need this body like this eyes this voice which can interact and solve variety of problems similar to that gp4 model is just a just like a human brain you need body around it and that body is nothing but a backend server if you have done software engineering you might be aware about this web servers which are based on nodejs Jango fast API Etc so you

25:30 build this kind of a server now when you're using chat GPT application uh the server runs in open AI Cloud but let's say tomorrow you want to build your own application let's say this kind of uh database Q&A system for retail store where you ask a question and it will pull the answer from your internal private database we are going to build this tool by the way later on okay so stay tuned but here this UI or the front end will be making a call to a backend server which can be using this gp4 model and it will use retrieval augmented generation rag to pull the answer from my SQL database we will see in when we

26:16 do that project how exactly this is done but I'm just giving you an overview right now so the answer that you looking for that how much total price of inventory for small size t-shirt this answer is is available in this database and gp4 is converting your uh question into SQL query and it is pulling the answer and you can deploy this backend server into Azure Cloud uh Azure has this service called aszure open AI service where you can host your private gp4 model so that your data is protected it doesn't go out if you don't want to use Azure you can use things like Amazon badrock now Amazon badrock doesn't have GPT model but it has other foundational models such as uh Cloe and I think it

27:04 has this model called llama see llama 2 mistal stable diffusion it has so many models so to summarize there are commercial models such as GPT jini Etc but there are open- Source models such as MRA Lama Etc now let's say you build your application using GPT model and tomorrow you are not able to afford the gp4 bill because they charge per token okay and let's say you want to save your cost what do you do well you use open source model such as Lama 2 so in this case you might have build time to uh develop this code let's say you have 20,000 lines of code and now you have to redo all these things wouldn't it be nice if we have some way where you write

27:51 an application you write some code and you plug your gp4 model and tomorrow if you can't afford the bill you plug a different model okay so that way is Lang chain Lang chain is a python framework that is used to build J applications and it's a very popular framework nowadays so if you are building J application you'll have to learn this framework because that makes building llm apps easier to summarize the tooling for Gen first thing you need is some model some llm model it can be a commercial model such as gp4 or or open source model such as llama or mistal it can be image models as well then you need cloud service so you can use aure open AI

28:38 Amazon badrock Google Cloud there are so many Cloud options that you have and then you need framework like Lang chain you can also use hugging face Transformer library to use variety of Open Source model and then in terms of deep learning libraries you can use py torch tensor flow Etc now is the time that we learn about Lang chain uh framework so I had this other Lang chain crash course on my YouTube channel uh in which I went through the Lang chain framework so I'm just going to uh play that now it is a crash course on Lang chain all right so let's get started with Lang chain now Lang chain is a framework that allows you to build applications on top of llm or large language model in this crash course

29:23 video we are going to go over all the basics of Lang chain and then we will build a restaurant idea generator application using streamlet where you can input any cuisine Indian Mexican Etc and it will generate a fancy restaurant name along with the manual items first let us understand uh what is Lang chain and what kind of problem does it addressed when you are using Chad GPD as an application internally it is making call to open AI API which internally uses uh any llm such as GPD 3.5 or 4 in this case chat GPD itself is not an l l m it is an application whereas GPD 3.54 these are large language models now let's say you want to build an application for restaurant idea generator where you give a Cuisine and

30:11 then it will generate a fancy name such as sto Temptation for Mexican and manual items as well let's say you give Indian Cuisine it will say Okay Curry K Palace or Sahara Palace for Arabic along with this manual items so this is a sample application which we are going to build but this is an llm based application and for this we can use the same architecture as jet GPT where we can directly call open AI API and here I have provided a screenshot of their main API uh so you can call it and you can get a behavior similar to chat GPT internally it will use GPT 3.5 or GPT 4 model and this case once again Resturant idea generator is an application similar to chat GPT but internally you are using

30:58 open AI API and the llms now there are couple of limitations of following this approach and by the way uh the reason I'm telling you this is nowadays there is a big boom in the industry where every business wants to build their own llm you would think why they can't use chat GPD because chat GPD has no access to your internal organization data so people want to build applications which are based on llm okay so there is a clear demand and clear boom in the industry for this and why do business not use this kind of architecture well there are couple of things to consider first of all calling open AI API has a cost associated with it for every th000 token they will charge2 or something you can check open

31:44 AI pricing page but there is a cost associated with it and if you're a startup who is having funding issues and you know your budget is limited this is going to be a bottleneck for you another thing is you might have noticed Chad GP doesn't answer latest question its knowledge is limited to September 2021 as of this video recording so if you want to incorporate some latest information let's say from Google Wikipedia or somewhere else you can't get that here the other issue is attick is my own software development and data science company if I want to know how many employees joined last month chat GPT can't answer because it doesn't have access to my own internal organization data so if you use this kind of architecture for building your

32:30 application uh you will hit some roadblocks or you will rather have some limitation and look open AI guys are pretty smart actually if they want they can address all of this but their stance is very clear we will provide foundational apis and building framework is something that other people should do and that's what happens see if you have just open a API it is not enough to build llm therefore you need need some kind of framework where you can call open AI gpt3 gp4 or maybe if you want to save the cost you call some open-source models such as hugging face Bloom there are so many models out there let's say you want to use them you don't want to uh spend money on openi gpt3 model uh

33:17 then this framework should provide that plug-and play uh support you know where you can integrate to one of these models and your code kind of Remains the Same this framework should also Pro provide integration with Google search Wikipedia or even integration with your own organizational databases so that the application can pull information from these various sources as well and this framework is Lang chain that is what it does it's a framework that allows you to build applications using llm okay let's install Lang chain now and uh do some initial setup let us first create an account on open AI you can go to open a website click on login and create a login using Google or individual email

34:03 credentials and once your login is created you will come to a dashboard so let me just show you so you'll go to open AI say login you're logged in click on API and then from your account you can go to your manage account and API Keys you will find a key here which will look something like SK hyphen something that is like a password so you need to use that key in our code for Lang chain you can also create a separate keys for separate projects so I have some client projects going on a YouTube tutorials so for each of them I have a separate key in your case you can just use the one key by the way you can generate a new key here as well uh so let's say you

34:49 copy that key to some secure place after that you won't be able to access it here so you have to delete and create a new key okay so let's say you have that key ready with you uh here then you can just import OS module and then in OS module you can create a environment variable with that particular key your key will be SK something okay uh in my case I have stored that key in one python file okay I don't want to share that key with all of you that is a reason and that python file look something like this you know secretor key. Pi uh it will have my own internal key I can have n number of keys

35:36 here and I'm just importing that variable here and just setting it here contrl enter so that thing is set now let's go to the terminal and install some modules so you're going to install Lang chain module that's number one and the second module you're going to install is called pip install open AI once you have installed those modules let's now import few important things from Lang chain uh we are going to import the llm called open AI now we are using open a because open AI I know it cost some money but it is the best one uh if you want some other ones then just just hit Tab and it will just show you hugging phas whatever the

36:22 the other type of whatever other llms that it has available it will show you all of that we are right now happy with open Ai and then I will create my openi model it has a variable called temperature now what temperature means is how creative you want your model to be so if the temperature is set to let's say zero it means it is very safe it is not taking any bets but if it is one it will take risk it might generate wrong output but it is very creative at the same time I tend to set it to 6.7 things like that and now in that llm

37:07 you can pass any question so let's say I want to open a restaurant for Indian food and I want some fancy name for it I'm not able to come up with that product name idea or restaurant name idea and let's see what this guy does and I typed in the same question in uh here also see I want to open a restaurant for Mexican food it told me this uh if you say Indian food it will tell you something else so we are using essentially the same concept here okay so here uh it says okay Maharaja Palace Cuisine uh if you say Italian food see the name sounds real as if it's an Italian restaurant so we have imported that open class which

37:55 created an llm and in the llm we are just passing a simple text now I don't want to keep on changing this same string so I will now go ahead and create something called a prompt template so from Lang chain. prompts you can import a prompt template and in that prompt template you can pass some variables such as input variable what will be the input variable by the way variables it will be a Cuisine and then the template that you want looks something like this so I'll just copy paste here and what we're

38:40 doing is just changing that Italian Etc with that kuin variable and this template is called prompt template and let's say this is for a restaurant name that's why I'm saying name here uh and once that template is created you can just say prompt template name. format and in that format you can pass cuisine as let's say Mexican and see I want to open a restaurant for Mexican food if you say Italian it will say Italian food this is more like a python string formatting you would be wondering why you don't use Python string formatting well let me just show you that using something

39:26 called chain so we going to use this concept of chain in Lang chain and it is one of the most important objects in in in Lang chain framework you can figure out from the name of the framework itself uh and we are importing llm chain and llm chain is essentially a very simple object where you are saying my llm is this whatever you created here okay that is my llm and my prompt is this prompt template and this is my chain and in the chain you can say chain. run let's say I want to open a American

40:13 restaurant see the All American Grill and bar so now here I don't have to pass the whole sentence I want to open a restaurant for is that I just pass the cuisine the variable and it will just work every time Mexican see so internally it is calling openi API uh and we made that connection via this module here so if you are using hugging phas then you'll have to do the hugging facee setup and it will call hugging phas okay here we are kind of paying that cost but by the way uh when you created that open API account you got $5 free credit so you should be okay $5 is more than enough for initial learning and exploration and after that if you like it you can go ahead and pay money

41:00 so that is the simple chain that we got here now let's look at something called a sequential chain uh so let me just explain the concept first so far what we did is we had this chain for generating restaurant name and it was generating it but let's say for that restaurant you want to generate a manual item food manual items so you can have this second component or a second chain where you pass restaurant name as an input and it should give you the menu items that you should include in that restaurant so if it is Indian restaurant it will say p Mangus Etc if it is Mexican it will say quadia burrito things like that this thing is called Simple sequential chain and let's uh code that up but just to

41:47 clarify the idea here you have one input and one output and you can have intermediate steps where the input of the second step is the output of the first step it is as easy as that so here uh once again I'm generating everything from scratch I have generated the name chain the same way that we did before this is the exit copy paste of the previous code so nothing fancy here and then we are going to create another chain and I'm just copy pasting just to save your time where the input is restaurant name and we are saying suggest me some food menu items for restaurant that so it is like saying this see uh you you are saying I want to open a restaurant for Indian forood

42:32 suggest a fancy name for this only one name please so let's say you talk to CH GPT and CH GP generated this now you are saying that generate food menu items for seon spice and then return it as a comma separated list see this this is what you want you you want to generate all this list and you have now two chains which we are going to do control enter and execute this code and now from Lang chain from Lang chain dot chains it will show you all

43:19 kind of chains you're going to import a simple sequential chain okay and that simple sequential chain will contain these individual chains that we created and by the way the order matters here so this is a restaurant name chain and then you have a food item chain and that's it and now you will say chain. run let's say you want to generate it for Indian food you're getting a response here and you print a response sometimes it takes time so you have to wait for a few seconds but it will generate the manual items see Lamb kma

44:05 vegetable CH FR Sak paneer yummy huh you're probably getting a water in your mouth uh let's do Mexican for all those Mexican food lovers so while this chain looks good it is generating those food menu items uh I'm not getting the restaurant name as such because if Mexican food it is saying all these item but what is the restaurant name that was that intermediate step here that was the intermediate step but in the output in the simple sequential chain it gives you just one output but I want the rest name and the menual items both for that we have to use a different chain called sequential chain and this sequential chain can have multiple input multiple

44:51 outputs so I can just say okay give me a name for Indian restaurant which is wagan and then in the output I can say give me restaurant name and items both as an output all right so let's try something like this here I think this whole code kind of Remains the Same I'm just going to add one or two extra things here so see this is my first chain and the extra thing that I have added is the output key so the output of the first uh chain is the restaurant name and the second chain looks something like this where the output key is manual items okay and now let's create the simple sequence and chain so I'm going to say

45:39 from Lang chain. chains import sequential chain we already used simple sequential chain this is a sequential chain which is little kind of generic so sequential chain will take what kind of parameters will it take well first of all it will say chains and these are the two chains I have and then my input variables you can specify all the input variables so input variables I'm not including that veon Etc let let's keep things simple all I want is two output remaining things are same okay so in the output variables I should say that I want a restaurant name

46:27 and the manual items as my output variable and let's call this a chain and by the way when you run this chain you can't just say Mexican because you might have multiple input variables that's why you need to give a dictionary you will say Cuisine is let's say Arabic uh run not supported okay run is not yes so run is not supported you have to just call it just like that no function just chain and then bracket just give the argument see Hummers with P bread falafal and the name of the restaurant is the Arabian Bistro so it's giving in

47:12 fact it's giving input as well so it's giving input and both the outputs whatever code we have written so far we will use that code and create a streamlit based application for restaurant name generator I'm in my C code directory I have created this empty folder called restorant name generator you see there are no files here and I'm going to Now launch Pam uh which is a free uh python code editor and in this you can select open and you can open that particular folder so I will go to my C code directory and in that I will locate Resturant name generator hit okay and it will create a I think

47:57 empty main.py file and I can just remove the content here and I'm going to just import of streamlet Library first now if you don't know about streamlit it is a library that allows data scientist to build POC application proof of concept application simple applications very very quickly you don't have to use frontend Frameworks such as reactjs Etc this Library will allow you to do all of this things very very fast so let me just show you so you can um just create a simple application with with a title restaurant name generator and by the way you have to do pip install pip install

48:42 streamlit before you start using it otherwise you'll get an error so make sure you have run that uh and this is the simple app with one title now I can go to terminal and I can just say streamlet run Main pi and it is going to open up an application in my browser see simple application with this particular title now I can create uh the Picker where you can pick the cuisine and for that I'll use the sidebar so in stream LD there is something called sidebar where you can create a select box and give a name to that box so you will say pick a Cuisine and give all the options that you want in that particular drop

49:29 down so I will just put bunch of Cuisines indan American Mexican and so on and then here you will say if or let me just show you how this looks so just say hit contr s save and click on rerun you can click go here and rerun or just press R key and it will show you see you get this kind of nice picker uh and if someone picks any entry let's say someone picks Mexican what's going to happen is that call is going to return that value in a variable which we will store in this Cuisine okay so I I will just store it in this particular variable and you can say if Cuisine then

50:16 do something okay what do I want to do I want to generate the restaurant fancy restaurant name and list of manual items here for that let me just write a a dummy code here so I will just call that function let's say get restaurant name and items where you supply the cuisine as input and it returns let's say restorant name it is always a good idea to write this kind of stub function or empty function so that you can check your wiring and then you can write the actual code in that function so let's say my restorant name is Cur delight and my menu items is whatever items you

51:05 like okay so if if Cuisine then get those things as a response and then from the response let's say the restaurant name I can show it here on the right hand side see here somewhere below this header I want to show it and I will use um maybe let's say st. header I will use st. header as a UI control so let's do this s3. [Music] header and when I get the menual items

51:52 so let me get the menual items here so the menu items are going to be this uh Manu items here and whenever you have comma separator string uh you can call this split function and specify the separator which is comma here and this should return you a list and once you get that list you can iterate over that list and you can write those items here uh um I can just say item and maybe I can put some character just to indicate this is an item you can also uh write like kind of like a header

52:38 where you'll say okay these are manual items menu items okay hit save this code is ready you go back to your UI rerun and see you're getting the restaurant name in the menual items now when you change this selection it's not going to change because obviously we are returning the hardcoded response and the next step is to write that code which we wrote in our Jupiter notebook and put that code here okay and since I like to modularize my code I'm going to create a new python file let's call it Lang chain Helper and in this file I will copy paste this

53:23 function and here you can import that module and you just simply call that here see now let's focus on Lang chain helper so what do we need to do here well the same thing that we did in our notebook so I'm just going to copy paste some code from my notebook here okay we don't need to go over it because we have already uh return that code I will also create create a file for my secret key so I will call it secret key and in that secret key file I will place my open a secret key now I'm

54:09 not going to show you my key because of course it's private thing but you will type whatever key you got uh remember you got $5 credit so you can do a lot of things with $5 is more than enough uh so put that uh that thing here and then you are using that key see you importing that variable from that python file here directly okay so my key is ready what else do I need to do well again copy paste business folks copy paste is a boon for any programmer or a data scientist so we just copy pasted the code for the sequential chain that we wrote in our notebook see here we create a restaurant name chain here we create manual items chain and we just return

54:55 this response folks this is this is so straightforward okay and I have this habit of creating this function main function just so that I can taste it so I will say if name isore meain uh then print generate Resturant name let's say Italian okay and now what I'll do is I'll pause the video and put my real secret key here all right my secret key I have placed it uh and now I can run it and see what happens so we are generating Italian food restaurant name in the manual items perfect so the restaurant name is La do V whatever and

55:43 manual items are margara Piza alfredo lasagna and all that one thing I'm noticing here is I see some extra slen characters here so maybe we need to remove them so the way to do that would be let's go back to our streamlit code and instead of just saying respons restaurant name we can call this St function that will remove the leading and trailing wi spaces including those slash and characters and you can use that same thing here as well before calling split so hit contrl s save let's go back rerun see Italian food this is my restaurant name manual item you can

56:28 change it to Mexican change it to whatever just play with it and folks The Art of Getting skillful at coding is to practice just by watching this video you're not going to learn it so make sure you're practicing while watching this video all right our streamlit application is ready as a next step we are going to look into something called agents which is a very powerful Concept in Lang chain and by the way all the code that we are writing check video description we're going to give you all of that code agents is a very powerful Concept in Lang chain what happens when you type this in jat GPD when you say give me two flights options from New York to Delhi on a given date obviously it won't be able to answer because it has knowledge till September

57:13 2021 but if you have chat GPD Plus subscription there is this thing called plugins and I have installed those plugins especially the xedia plugin xedia is a website which helps you find tickets and when I give the same question now with the plug-in enable magically it will start working so it will go to xedia plug-in try to pull the information on the flights for a given date and given source and destination and then it will start typing those two options see number one option is only I think $500 ticket $518 ticket which is a pretty good deal by the way I should I think I should book it for my next India and there is the second option and it

57:59 will give you a link where you can go and book those tickets on xedia so what exactly happened when we enable uh this plugin let's try to understand that so when you think about llm many people think that it is just a knowledge engine it has knowledge and it just Tred to give answer based on that knowledge but the knowledge is only limited till September 2021 the thing that we miss out is it has a reasoning component so it is a reasoning engine too and using that reasoning engine it can figure out that when someone types this kind of question see when as a human when we look at this questions what do we think let's say if we have to go to Wikipedia or not

58:44 Wikipedia xedia and if you have to type uh convert this question let's say if your friend asks you this question and let's say you are that reasoning engine you go to xedia and in the source you will put New York destination you will put Delhi date you will put 1st August how can you do that because you have that reasoning engine in your brain similarly llm has a reasoning engine using which from that sentence it will figure out source is this destination is this that and it will call the xedia plug-in and that will return the response back let's look at some other question when was Elon mus born what is his age now in 2023 now maybe this can be answered by the llm knowledge but let's say you are

59:31 asking some question which is related to an event which happened in 2022 now this guy doesn't have knowledge after September 2021 but once again it has a reasoning capability so it will say okay in order to answer that question first I need to find out when was Elon Musk born for that it can use things like Wikipedia so agents essentially do this thing agents will have tools and using that tool it will try to fetch the answer silon mus was born in 1971 and then there could be another tool which will tell you 2023 minus 1971 how much is that so there there is a math tool uh that it can use to compute that and it will in the end say Elon Musk is

1:00:16 52y old so this is what agents are agents will connect with external tools it will use llm reasoning capabilities to perform a given task let's look at a different question how much was US GDP in 2022 plus five I'm doing just like a it's a it's a silly operation no one cares uh but US GDP in 2022 llm doesn't know because it knowledge is still 2021 so it will go to Google it will find that answer and then it will use math tool to do plus five all these tools like Google Search tool math tool and Wikipedia tool are available as part of Lang chain and you can configure your agent so your agent is nothing but using

1:01:03 all these tools and lm's reasoning capability to to perform a given task that is your agent and this agent can be used in our jupyter notebook so that's what I'm going to show you next so let's first import couple of uh important modules and classes and once I have imported them I will will create tools so I will say load tools and I will give list of tools now if you do Google search On Tools here so let's say if you do Google search L chain agent load tools you'll come here you will see list of tools see I have Wikipedia as a tool I have twio I have

1:01:50 all these tools that I can use so we're going to use Wikipedia tool here is called Wikipedia Wikipedia and the math tool is called llm math and here you need to provide the llm variable is the one which we created above somewhere here see this is the variable okay so this thing is called tools and then you can create an agent using this initialize agent method okay so initialization method will take two tools it will take llm and it will take agent and in the agent I will give this zero now see hold on zero short

1:02:38 react uh description react means uh thought and action so when we are reasoning we first have a thought then we figure out where to go and we take an action so it mimics that particular concept here I will call this an agent and and then I will ask the question agent. run when was Elon mus born and what is his AG in 2023 so let's see what this gives us see perfect it says 52 year old in 2023 if you want to go uh step by step in the reasoning process you can say verbos is equal to True uh and it will tell you by the way verbos is equal to True is the

1:03:24 variable that you can use here in any function to kind of figure out the internal steps that it is taking so here the first step when it Encounters this question it knows actually that it has to go to Wikipedia to get the birth date of Elon Musk so it went to Elon Musk Wikipedia page and which will have this particular date here and then um I think it uses the Matt tool sometimes I don't know it should have used the mat tool it doesn't show but if you rerun it again okay I don't know why this is not working but previously I was seeing that let me show you a previous snapshot that

1:04:10 I have um here it say okay it went to Wikipedia for Elon mus birth dat and then it use C action as a calculator so there it is using the llm math tool and it is just calculating the final answer it is saying it is 52 year old let's try a different option so this time we are going to use U Sur API so if you don't know about Sur API it is Google search API whatever you do uh in Google and what results uh it gives you if you want to access those results programmatically you can use this particular API you can log in using your Gmail account I have already logged in and when you go to dashboard

1:04:56 it will give you this API key so this is similar to our open AI API key it will be a big big string I have stored that API key into my private file that secret key file that I have and I'm going to initialize some environment variable so for Ser API you need to initialize this variable and this is the key which I got from there okay so you can just copy paste this key if you want to keep things simple I don't want to show that key publicly here that's why I I have this thing here and once I have this thing the next steps are kind of similar so I can just copy paste pretty much everything here and I will just say Okay initialize the agent sir PPA and llm

1:05:42 math are the two tools I'm going to use and in my agent I will say agent. run and what was the US GDP in 2022 and plus + 5 okay and while it is executing it let's do Google here so when you do Google US GDP 2022 it will tell you 25.46 so this Sur API this API will do Google search and it will tell you the answer that it is this so check this so first it is searching US GDP this and then it is adding F number to this and it is giving you uh this particular result we'll talk more about agents in

1:06:27 our future videos uh one thing I noticed is agents are not perfect sometimes they give stupid answer this whole thing is evolving so in the future it will get better but for agent this is what we have and now we will talk about memory when you look at any chatboard application such as chat GPT you will notice that it remembers the past conversation exchange here I asked who won the first Cricket World Cup then a it's totally irrelevant conversation what is 5 + 5 then I'm asking who was the captain of the winning team now see here I did not say which match which game cricket football Etc but it remembers that I'm talking about cricket and it is giving me a relevant answer same thing happens with human conversation we start a topic then we keep on saying things but we remember

1:07:13 what the topic is about if you look at the llm Chain by default These Chains do not have memory they are stateless and if you look at the available methods that this chain has you'll find that it has an element called memory here see memory uh and if you check the memory so here if you try to print the memory see you don't get anything uh because the object is set to none now if you want you can attach memory to it so for memory you have to create an additional object and attach it so that it remembers all these conversations this is useful especially

1:08:00 if you're building a chat boat let's say for your customer uh care Department uh in that many times they need to save the transcripts of those conversations for legal and compliance reasons so here uh I'm going to import uh an object called conversational buffer memory which is a very common type of memory in Lang CH module and create an object of that class so I will just say this is a conversation buffer memory and then the same chain uh I will print here but I will just pass memory as an additional

1:08:45 argument okay and then I'll run the same chain one more time uh for a different question now when you look at chain. memory see there is a memory attached to it we explicitly attach this conversion memory to it and if you look at the buffer and if you just print it for nice alignment Etc see human then AI this human and AI this now this looks good you can save this into your database as a saved transcript of your customer service center conversation uh but one problem with this particular object which is conversation buffer memories that it will keep on growing endlessly so let's

1:09:31 say you have 100 conversational exchanges and by that what I mean is one question answer pair so one question answer pair is one conversational exchange this is second so in total this is two conversional exchange so here if you have 100 conversational exchange what's going to happen is next time when you ask a question to open AI when you say chain run it is going to send all this past history to open Ai and open AI charges you per token so this is one token second token third four and so on for th000 token they charge like2 something based on on the model so your cost is going to go up so if you want to save the cost and kind of do things in

1:10:16 an optimized way uh you need to restrict this buffer size you can say just remember last five conversational exchanges okay and this thing can be done using something called converation chain so open AI Lang chain provides this conversation chain which is just very simple object so let me just create that conversation chain where you can just pass llm is equal to open AI temperature is equal to 0.7 and I'll just call it convo and let's check the default prompt that is associated with it see default

1:11:03 promp is this uh let me just check the template let me just print the template associated with this this is the default template that comes and it says that the following is a friendly conversation between human and Ai and there is history and there is input so if you look at this uh conversation window here there is a history this is a history and the next question you're going to type is the input so input history same way input history okay now when you ask bunch of questions through this so I'm just going to copy paste uh those questions here uh what is 5 + 5 and then who was the captain of

1:11:48 the winning team now when you do con. memory it won't be empty because by default conversation chain has this memory associated with it so by default this conversation chain object comes with inbuilt conversation buffer memory and if you print the buffer you will see the entire transcript of our conversation now while this looks good once again think about the open AI token cost if this keeps on building the buffer endlessly then you might have 5,000 tokens in one conversation and when you make a next call like this it's going to actually send entire history entire conversation

1:12:34 to open a and that will increase your bill on the API call to tackle that problem maybe what you can do is you can say just send only last 10 or 20 conversational exchanges because that's what I care about that might be enough based on the use case that you're dealing with uh and for that there is an object call from Lang chain. memory import there is a conversational bu for window memory you are restricting the window you are saying let's say my window and K is the parameter you're saying just remember only last one conversational exchange which is one question answer pair okay so let's try

1:13:21 this out and let's see uh how this goes so I'm going to copy paste some code here created a same conversational chain object asking my first question asking my second question now when I ask this second question here what is 5 + 5 it remembers the previous exchange but when I I asked the third question here it remembers only what is 5 + 5 it doesn't remember this it's like a shortterm memory lost like momento or gz movie it just forgot what happened here so when I ask this question it will say I'm sorry I don't know because it doesn't know which game you're talking about which particular match you're talking about okay so here I know it's probably not the best example uh but the idea is I

1:14:08 wanted to demonstrate this K parameter here and Bas on this use case uh you might see benefit in using conversational buffer window memory that is all we had in terms of Lang chain fundamentals Now using these fundamentals which you just learned now we are going to build build two end to end projects first project is in finance domain second project is in retail domain I previously published these two projects on YouTube and I'm going to use the same two videos because these are very high quality end to endend projects Below in the video description the link for the code is also available so let's start with the first project now today we will build an end to end

1:14:55 llm project that covers a real life industry use case of equity research analysis we will build a news research tool where you can give a bunch of news article URLs and then when you ask a question it will retrieve the answer based on those news articles in terms of Technology we have used Lang chain open Ai and streamlet to make this project more interesting we have added some fun storytelling as well so let's take a

…

Share

YouTube Video Summaries