LangChain 101: The Complete Beginner's Guide

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
langjin probably one of the hottest things in AI right now after Vector databases it's a framework used to develop applications on top of large language models like GPT llama hugging face models and much more it first started as a python package but there is also a typescript port catching up in feature parity and a ruby one that just got started but why is it needed in the first place could we simply send a request an API or model and call it a day and you're right that would work for simple applications but as soon as you start increasing the complexity like connecting the language model to your own data from Google analytics stripe SQL PDF csvs or making the language models take actions such as sending emails searching the web or running code in your terminal things start to get messy and repetitive Lane chain offers to fix this issue with components we can use document loaders to load data from sources like PDF stripe and then optionally use a text splitter to chunk it down before storing it in a vector database at runtime the data can be injected in a prompt template to be then sent as input to the models we might also use tools to take actions for example sending an email with the output's content in practice these abstractions mean you can easily swap to another llm because of cost or features test another Vector database capabilities or ingest another data source in a few lines of code chains are how the magic happens we chain together components in a way to accomplish a specific tasks agents are more abstract the first think about what they need to do using a language model before doing it using things like tools if you're curious about research papers showing the power of connecting language models to your own data and the outside world consider looking at self ask with search and react which were published around the same time Lang chain was launched now let's take a look at what's really going on behind the scenes there are currently seven modules LinkedIn offers that newcomers to the space should know about models prompts indexes memory chains and agents models at a high level there are two different types of models language models and text embedding models the embedding models turn text into an array of numbers we can then think of text in a vector space in this image we can see how in a 2d space man is the king what woman is the queen they represent different things but we can see a pattern this allows for semantic search where we look for pieces of text that are most similar in meaning for a given thesis text within the vector space for example open ai's text embedding model can embed large pieces of text precisely 8 100 and 91 tokens which given their word to token ratio of 0.75 should roughly work for up to 6143 words it outputs vectors of 1536 Dimensions we can use LINE chain to interface with multiple embedding providers some apis like open Ai and cohere are paid but we can do it for free and enter privacy on our data by using open source models run locally with hugging faces open source embedding you can now create your own embeddings on your own machine using only four lines of code however the number of Dimensions might be different and the quality of embeddings might be lower which could make retrieval less accurate moving on to language models there are two different subtypes llms and chat models llms wrap apis which take text in and return text chat models wrap models which take chat messages in and return a chat message it's a subtle difference but the interface to use them is the same we can import both classes instantiate them and then use the predict function on both of them and see the difference however you probably won't be passing text directly to the models and instead use prompts a prompt refers to the input of the model we often want more flexibility than in hard-coded string link chain provides the prompt template class to constructive prompt using multiple values the important Concepts to know of prompts are prompt templates output parsers example selectors and chat prompt templates prompt templates here's an example first you'll want to create a prompt template object there are two ways to do this import prompt template then instant sheet 1 using the Constructor specify your input variables in an array and in your template string by putting them inside of curly braces if you're feeling lazy you can also use the helper method from template so you don't have to specify the input variables explicitly in both cases you can then format The Prompt by telling it what values to replace the placeholders with under the hood it uses F strings to format your prompt by default but you could also use ginger 2 but then why not just use f strings directly the prompts help with readability play well with the rest of the ecosystem and support common use cases like Q shot learning or output parsing few shot learning means we give our prompts a few examples to guide its output let's see how this is done first create a list with a few examples next we specify the template to format the examples we have provided every example will be formatted using this prompt template finally we create the few shot prompt template object pass in the examples the example formatter a prefix given the command and suffix that aims to guide the output of the llm let's not forget the input variables and a separator that will be used to separate the examples from the prefix and the suffix we can now generate a prompt and this is what it looks like this is an extremely useful Paradigm to control the output of the llm and guide its response similarly we might want to use output parsers we will be automatically parsing the output of the language model into an object it's a little more complex but very useful in structuring the random outputs from llms let's say we want to use openai to create joke objects we can Define our joke class to be more specific we'll need the jokes setup and punchline We'll add descriptions to help the language model understand what those mean then we can set up a parser and tell it to parse using our joke class we use the pedantic output parser which is the strongest and most recommended now we create our prompt template let's pass the template string input variable and we can use the partial variables field to inject or parsing instructions into the prompt template we can then ask the llm to tell us a joke now that we have our prompt ready this is what we need to do to send it to openai we'll first load our openai API key from our DOT and file then instantiate the model run its called under method and parse the model's output using the parser that we've instantiated it and there we go we now have our joke object with its setup and punchline defined the generated prompt is pretty complex consider checking out the GitHub repo to have a better look at it and to really understand what's going on we've previously covered few shot learning where we pass along examples to show the model expected answers for a certain type of query it might be possible that we have many such examples and we can't possibly fit all of them also that could become pretty expensive pretty fast that's where example selectors come into play to keep the cost of a prompt relatively constant we'll use the length-based example selector just like before we specify an example prompt this defines how each example will be formatted we curator selector pass in the examples and then the max length by default the length refers to the number of words and new lines used by the formatter examples section of the prompt and we can take the previous View shot template we defined and simply add the selector we just created replacing the example keyword argument but what about interacting with chat models this brings us to chat prompt template as we've previously mentioned chat models take a list of chat messages as input the list is referred to as a prompt what's different about them is that each message is prepended by a role either AI human or system the model is supposed to follow instructions from the system message closely there's only one system message at the beginning and it can at times sound pretty hypnotic you are a kind customer service agent responds allegedly to the customer's question dot dot dot something like that to tell the chatbot how to behave AI messages are messages that come from the model human message is what we've typed in the roles give the LM better context on the ongoing conversation models and prompts are cool and standardized but how can we use our own data this is where the indexes module comes in handy data is the new oil and you can definitely dig anywhere and find tons of it Lange delivers the rigs by providing document loaders document is their fancy way of saying text there are a ton of supported formats and services like CSV email SQL Discord AWS S3 PDF the list goes on and on it can take only three lines of code to import your here's how easy it is first import the loader then specify the file path and then call the load method this will load the PDF as text in memory as an array where each index represents a page that's nice but when we want to construct a prompt and include text from these Pages they might be too big to fit within the input token size we've talked about earlier which is why we want to use text Splitters to chop them into chunks after reading the text we can instantiate a recursive character text splitter and specify a chunk size and a chunk overlap we call the create documents method and place our text as argument we then get an array of documents now that we have text chunks we'll want to embed and store them to eventually retrieve them using semantic search which is why we have Vector stores this part of the indexes module offers multiple Integrations with Vector databases like pine cone redis Super Bass chroma DB and much more once you have your documents ready you'll want to choose your embedding provider and store the documents using a vector database helper method now we can write a question and do a similarity search over the vector space moving on to the retriever part of the module the retriever Lane chain has the most support for is the vector store retriever which you can get by calling as retriever on a vector store note that the retriever interface is generic and doesn't have to represent a vector store it could also be querying services like Wikipedia a chat GPT Plugin or a couple other Integrations it only has to implement the get relevant documents which takes in a string and return documents as an illustrative example one thing you could do is self querying with chroma say you have films stored in chroma DB the movies have as metadata score and genre then you could run the following line of code and get the movies that you wanted now that we have models prompts and indexes we can use chains to abstract a model call to an llm we can instantiate our model writer prompt and then pass our model and prompt to the llm change we can then run the chain on the input variables of our prompt the chain takes care of formatting The Prompt sending it to the model and returning the results this is a stateless operation meaning the chain does not remember previous interactions we could use memory to make it stateful let's instantiate a chat model and now use a conversation chain we'll pass the chat model along with a memory object now we'll ask it to respond partially to a question and then to complete its answer you could also create your own custom chains or use high level chains to do things like chaining chains together solve math problems or even things like letting the llm write code and then execute it let's say we want to create a company name and write a good catchphrase for it we can chain both LM calls and a chain after instantiating our model we create our first prompt and our first chain then we write our second prompt asking for the catchphrase and create our second chain now we can chain them using the simple sequential chain and specifying the chains in order we run the chain on the input variables of the first prompt and its output will be used as input variable for the second prompt there are also a couple of pre-made chains that you could use like the LM math chain moving on to agents as this video is already getting pretty long we will cover agents more in depth with real life examples Concepts and working code in a following video this is a hot space and has tons of practical applications if you want to be notified hit the Subscribe button and as always thank you for watching
Info
Channel: Edrick
Views: 2,501
Rating: undefined out of 5
Keywords: OpenAI, ChatGPT, LangChain, ChromaDB, Tutorial, Beginner, EasyTutorial, AI, ArtificialIntelligence, MachineLearning, LargeLanguageModel, LLM, Course, PDF, Concepts, Guide, Education, Programming, Software, Engineering
Id: P3MAbZ2eMUI
Channel Id: undefined
Length: 11min 1sec (661 seconds)
Published: Mon Jun 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.