Build a LangChain App [Tutorial]

Lucidate · Beginner ·🛠️ AI Tools & Apps ·3y ago

About this lesson

"Unleash the power of AI in equity research with Lucidate! In this tutorial, Lucidate guides you through the process of building a powerful application with LangChain. From automating data analysis to generating high-quality reports, discover how LangChain can transform your work. In this example we look at how to apply ai in an investment bank. We use LangChain to automate Equity Analysts workflow. Get started now - download the application from our GitHub repository - https://github.com/mrspiggot/FinGPT . Dive deeper into LangChain's capabilities by visiting our website: www.lucidate.co.uk. Stay tuned for more! Like, subscribe, and don't forget to hit the 'Super Thanks' button to support our free content. Connect with us on social media: https://www.linkedin.com/in/richard-walker-a18528/ https://www.linkedin.com/company/86355714 And enjoy the rest of Lucidate's content on YouTube: https://www.youtube.com/channel/UClqbtKdqcleUqpH-jcNnc8g

Full Transcript

hi there this is Richard Walker from lucidate and today we will be putting Lang chain through its Paces as we design and build an investment Bank equity research application the role of a sell side Equity analyst involves multiple tasks we're going to show how Lang chain is able to automate and assist in these areas making the job more productive and most importantly allowing the professional analysts to focus on more value-added roles those that require human judgment and creativity such as relationship building and networking with clients here's an example of the front page of an equity research report from the Investment Bank JP Morgan this is covering the company 20th Century Fox a media company on this page you'll see the name of the firm a recommendation along with a price Target a stock performance chart and some pros justifying the recommendation of the stock this particular research note is 31 pages long full of value-added content for JP Morgan's clients in this video and subsequent videos we're going to show you how Lang chain can chain together business applications internet search and large language models to be able to produce a high quality document like this one in minutes perhaps even seconds depending on where your information is stored what llms you use and how fast your internet connection is here's a sample front page of elucidate built research report on Apple the code to build this application is in GitHub and the URL on your screen the URL is also in the description of this video if you have an open AI key you can download the application now and have it running by the time this video finishes I hope you will download the app and use it as a base for your own experimentation as well as being a building block for your own apps you're free to use and modify lucidates code in any way you wish consider hitting the super thanks button which is YouTube's equivalent of a tip jar if you find the code or this video useful both the code and the video are free but voluntary contributions to show your thanks are always always welcome we can build this app using just a fraction of Lang Chain's vast capabilities here's a review of The Lang chain component architecture again all we need to use to build this app are Lang change chain components as well as its models and indexes in future videos we'll enhance this equity research application and introduce other components like prompts and agents to build in even more functionality let's break down the problem and see how we can solve it there's some boilerplate information always that we don't need AI for in fact we shouldn't use AI for it things like the closing price charts the table of yearly revenue and earnings and the key financial data are all pieces of information we'd sourced from a market data provider like Bloomberg or refinitive we don't want our AI hallucinating and making these things up now as most viewers won't have access to things like Bloomberg and refinitive we'll simulate these services with calls to the free y Finance for this video while why Finance is not robust enough for professional applications it's perfectly fine to illustrate how Lang chain apps can access real-time data from Information Services and integrate them with llms then there are areas where we might want AI to help informed decisions but the ultimate decision would rest with a professional human in the loop AI the stock recommendation is an excellent example of this here the AI can make a proposal for the recommendation but the ultimate decision would rest with the analyst or more likely a committee and then there are areas where we would love the AI to do most of the heavy lifting the opportunities for investors text certainly fits that description what Lang chain allows you to do is chain together specialist and generalist large language models work with tabular numeric information and written text as well as your own applications to create a library of information on this company and then just like a library ask a series of questions in natural language prompts to be able to get the text that you'd like to put in this section now this is essentially mimicking the task that the am list will perform in acquiring The Source data analyzing and synthesizing that data and then writing a report for this video we won't build a whole library of information that would make for a very long video we'll just use a single document but it will be a very relevant document it will be the company's most recent 10K filing a 10K form is the annual report that publicly traded companies are required to file with the U.S Securities and Exchange Commission it provides a comprehensive summary of a company's financial performance and includes details about its operations Financial condition and Management's plans for future growth you can find company filings at the SEC website as shown on your screen but we can do a little bit better than that and use the Edgar module in Python that will allow us to download any company's 10K with just a few lines of code now once we've done this we quickly run into a very big problem these 10K documents can run to 200 Pages contain up to sixty thousand words approximately one hundred thousand tokens if we want any currently sized large language model to analyze this then it would choke even the largest general purpose llms from firms like open AI only accept thousands of tokens at a time as inputs enter Lang chain specifically the index components these components allow us to be able to take large amounts of large documents and split them into chunks that are small enough to be fed into llms they do this in a really efficient way they don't just Chunk Up the text although that is valuable in itself but they'll vectorize the chunks that's to say turn the text into tokens turn the tokens into an embedding vector and the series of embedding vectors into an embedding Matrix and store that in a vector database like pineco and it doesn't stop there when prompted with a question the index components of Lang chain are smart enough to find the most similar chunk of text for the question and load that chunk into the llm useful things indexes let's look at an animated schematic of what's going on with these indexes and then let's look at the Lang chain code to accomplish that so firstly we'll have a general question to ask and any particular company to ask it off we'll create a lang chain function to provide the answer the first thing this function will do is visit the Edgar website to get the firm that we've specified 10K then chunks and vectorizes these chunks and stores them in a vector database with those chunks safely stored and logged it can then create an instance of GPT 3.5 to do the natural language question answering and summarization it builds a chain of the vector database and the llm it can then pose the question submitted to the function chain takes care of the One-Shot learning and text creation and produces an answer here's the Lang chain code to do exactly that this function takes in a company's stock ID and a question it does the Edgar magic to get the 10K from the SEC it instantiates a version of GPT 3.5 splits the dock into 3 000 token chunks with overlap now this is important if the answer to the question spans more than a couple of chunks it builds a simple chain to set up a summarization of the text creates the embeddings stores the embeddings in a vector database and then uses the chain to get the answer to the question input at the top of the function great now let's see this in action to make it a little more interesting let's pose three questions to create a reasonable sized opportunities for investors section now in reality an analyst like Lucy would have dozens or hundreds of questions to pose the app if you look at the source code is written to support that and I'll do a brief Code walkthrough of the lucidate code at the end of this video after the lucidate sculpture animation for those who are interested here are the three questions you might say there are five what other firm's key products and services what are the new products and growth opportunities for this firm what are its unique strengths who are the firm's key competitors what are the principal threats with that in mind let's take a look at the app in action here's the front page of the research report for Apple you can see the pro forma stuff we don't need an llm for but are nevertheless vital components of our application the stock chart the revenue and earnings table and the key financials are all down the left hand side of the document you can see at the very bottom the stock is recommended as a buy but where it gets a little more interesting for our purposes is in the text in the second column you'll see here that the text is generated in response to our three questions firstly what are this firms products and services and it's pulled out iPhones iPads Mac computers and the range of other Hardware offered by Apple the next questions are on growth opportunities and key strengths here it talks about Apple's ability to stimulate a buzz and demand for its products and its solid financial position then we conclude with a section on competitors and threats which seems to focus on Force major events now the source of all this information remember is the chunked up vectorized text from the latest Apple 10K this is fed to our GPT model and Lang chain handles all of the coordination to produce our answers let's now take a look at Amazon as expected this is pulled out both Amazon's retail business as well as the AWS compute Suite of services growth opportunities are all centered around customer experiences while strengths revolve around cash management including collecting from customers before paying suppliers threats seem to revolve around Financial volatility destabilizing the cash management of Amazon better get some of those fancy derivatives then as before this stock is recommended as a buy and to conclude we see that Netflix is in the content creation business with a strength being its ability to dynamically optimize its pricing strategies with Prime and Disney plus highlighted as key competitors in conclusion Lang chain is a powerful tool that can greatly enhance the efficiency of tasks that previously relied heavily on human effort it can automate and assist in areas such as equity research helping professionals focus on more strategic and creative aspects of their work this groundbreaking technology allows us to swiftly analyze vast amounts of data providing insights and producing high quality reports in a fraction of the time it would take a human analyst but it's important to remember that Lang chain is not designed to replace humans but to augment their capabilities helping them make better informed decisions and ultimately add more value to their work the ability to involve a human in the loop ensures that while we leverage ai's speed and efficiency we still maintain the much needed human touch understanding and discretion in our analysis as we've seen the potential application of Lang chain are vast and we've only scratched the surface in this video we encourage you to download the app experiment with the code and explore the endless possibilities that this technology offers so thank you for watching and stay tuned until after the lucid8 sculpture if you'd like a brief walkthrough of the app code and don't forget to like And subscribe for more videos where we will delve deeper into the capabilities of land chain and continue to revolutionize the way we work this is Richard Walker from lucidite until next time thank you [Applause] okay for those still here here's a very quick walkthrough of the code to give you some pointers as to what's going on as ever I always think the best way to learn something is to use it as is and then play around with it break it see what happens so the first section look it's essentially loading in the python modules I've tried to group them up well as I can into sensible associations the first modules handle all of the user interface code these next modules help us get our source data from the internet from Edgar and from Yahoo if you're using different sources you'll want to replace these import libraries with the ones you're going to use the next group just handles my openai credentials I'm going to be using open AI models later in the code but again if you want to use your own large language model or something you've downloaded from hugging face that will be a great alteration for this code I definitely encourage you to do that the next libraries are all of the Lang chain modules we'll be using there are a ton of these and amongst the many good things that I will say about land chain it has great documentation you will not get lost so here in the code I load my open8 I key pretty straightforward and then we have a simple utility function we're going to get big numbers back from uh from Yahoo and I want to convert them to millions billions and trillions so that we can format them nicely in the table uh here's something for a live app you definitely swap out this is a dictionary for the stock tickers and the cik keys to get the data from Yahoo and Edgar but you'd use a database for this in a live app but again this is easy to customize if you want to add some new firms to this dictionary knock yourself out just put the right names tickers and CI K keys and they'll pop up right in the streamer app when you run it so this is definitely an area that you'll probably want to look at customizing now we get into the Lang chain stuff uh the first piece of code I took straight from the Edgar documentation online here we get back the text of the latest 10K and it's using the dictionary that we defined earlier to get the name and the cik that you pass into the Edgar company class we then grab a model I'm using the default which at the time of video production is GPT 3.5 DaVinci zero zero three but if you want to use another llm maybe one you've built or downloaded from hugging face uh here's where you'd put the code in and again I definitely encourage you to switch out different llms to see what happens this next bit is embarrassing this is just a shameful hack and you can get rid of it if you want I'm basically doing some chunking of the documents myself but really simple stuff I'm splitting it into thirds that's the reason or the reason is because you know taking a look at these 10ks most of the initial text that you get back is all formatting and indexing and the last third is references so the stuff you want in the middle of the document so this is kind of like buying a pastrami sandwich and throwing away the rye bread and keeping the meat it's pretty messy you can change it if you want but I'm trying to save myself a ton of open AI fees sorry Sam then we formally and properly chunk the data bear in mind you'll need to cater for the prompt and the completion so I always think it's best to be cautious and ER on the lower side of the amount of tokens that you send in uh for a demo you might get away with a smaller overlap than I've got but overlap is important as I said in the main video if your answer is split across several different chunks adjacent chunks you want to capture it and overlap is a great way to be able to do that so this is the embarrassing bit where I throw away the bread and keep the pastrami I'm terribly sorry about that what a hack we then get the word embeddings and store them in a vector database I'm using face but here you can switch this out to another Vector database like pine cone it's entirely up to you we then build a chain this is where things really start to happen from a lang train perspective uh from our model and uh vector embeddings and Lang chain is doing all the hooking up behind the scenes and this is what makes Lang Chang so nice to work with I'm being overly explicit in this part of the code just for demo purposes uh we're taking the question that we input into this function seemingly hours ago and then running our chain uh then I'm doing a little bit of formatting of the reply for the return from this function now the rest from here on is is just a streamlit code to format the web app and if you're not familiar with streamlit then I'd suggest at least at first you leave it as it is but maybe you want to Tink around with it later the first bit is just simple page setup uh we create the title and we create a two column page in the left hand side we're going to have the charts and the tables and all the financials in the right hand side we're going to have some text I load the lucidate logo change that out put your own logo in there I don't care and set up a drop down list based on the data in dictionary that we defined at the top of the script the next section uh it's going to the internet and loading the closing prices from Yahoo into a pandas data frame sending the data frame to a line chart within streamlit and that's a really cool little widget that they've got and displays it in the First Column again in Anger you'd be using refinitive you'll be using Bloomberg but Yahoo works fine the code then grabs the company description and sticks it in the top right hand column the next piece of the code that builds the revenue and the earnings table we load the raw data into a panda state of rain and use the utility function we defined at the top of the script to format the numbers and streamlit can render a table uh straight out of the panda State frame which is nice now if this wasn't a demo I'd probably use the same data frame approach for this additional data here but I wanted to give you some other code you can use for tabular information so here we're just building a python dictionary out of the financial data and then we Loop through the dictionary to write the financials in that first column on the left now and this is where things start to happen right at the end we call the function we defined earlier that built and ran our Lang chain and I definitely encourage you to add more of these lines with different questions to flesh out the analyst report and hey that's it please leave any questions in the comments this is Richard Walker until next time

Original Description

"Unleash the power of AI in equity research with Lucidate! In this tutorial, Lucidate guides you through the process of building a powerful application with LangChain. From automating data analysis to generating high-quality reports, discover how LangChain can transform your work. In this example we look at how to apply ai in an investment bank. We use LangChain to automate Equity Analysts workflow. Get started now - download the application from our GitHub repository - https://github.com/mrspiggot/FinGPT . Dive deeper into LangChain's capabilities by visiting our website: www.lucidate.co.uk. Stay tuned for more! Like, subscribe, and don't forget to hit the 'Super Thanks' button to support our free content. Connect with us on social media: https://www.linkedin.com/in/richard-walker-a18528/ https://www.linkedin.com/company/86355714 And enjoy the rest of Lucidate's content on YouTube: https://www.youtube.com/channel/UClqbtKdqcleUqpH-jcNnc8g
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Related Reads

Up next
Advanced Tutorial NotebookLM Slides For Powerpoint
Russell Stannard (TTVideos)
Watch →