<b>In this video, we will create a powerful</b> <b>AI assistant without</b> <b>writing a single line of code. </b> <b>These assistants have many applications</b> <b>for businesses, and using</b> <b>the skills that you will</b> <b>learn in this video, you will be able to</b> <b>create an assistant that</b> <b>will not just be able to answer</b> <b>questions from your own knowledge base,</b> <b>but also perform</b> <b>complex tasks using tools. </b> <b>But first, let's discuss the differences</b> <b>between chains and agents.
</b> <b>In the previous videos, we saw that</b> <b>chains follow pretty</b> <b>much a simple sequence. </b> <b>They respond to our</b> <b>questions in a predefined manner. </b> <b>So we would send them some prompt, and</b> <b>they will then return a</b> <b>response based on a prompt template</b> <b>that we provided.
Agents, on the other</b> <b>hand, are able to use</b> <b>reasoning to figure out the actions</b> <b>that it needs to take to complete a task. </b> <b>So basically, the</b> <b>major difference between</b> <b>agents and chains is that on agents, we</b> <b>are able to provide a</b> <b>list of available tools. </b> <b>And this example might look a little bit</b> <b>intimidating, but effectively, all we did</b> <b>was provide a list of available tools</b> <b>that the agent can use.
So</b> <b>as an example, if we ask this</b> <b>chatbot to perform some calculation, it</b> <b>will reach out to the</b> <b>calculator tool in order to</b> <b>get the result. So this actually provides</b> <b>some complex</b> <b>implementations. For example,</b> <b>we could attach a tool that will allow</b> <b>the agent to capture</b> <b>information from the user,</b> <b>and then store that information in some</b> <b>sort of booking or a lead system.
</b> <b>So let's go ahead and create our</b> <b>assistant. I'm going to add a new chat</b> <b>flow. I'm going to save</b> <b>it.
I'll simply call it Agent Demo. And</b> <b>then let's start off by</b> <b>adding a node to the canvas. </b> <b>Up until now, we used chains.
But in this</b> <b>video, we will now create</b> <b>an agent. So under agents,</b> <b>let's add the conversational agent. And</b> <b>in the previous videos, we</b> <b>spend a lot of time looking</b> <b>at the conversational chain.
And let's</b> <b>add that chain so that</b> <b>we can see the difference. </b> <b>So under chains, let me add this</b> <b>conversation chain. And you</b> <b>will see a few similarities.
</b> <b>In both instances, we need to attach a</b> <b>chat model. And we can</b> <b>also provide memory. And this</b> <b>is simply so that the chain can keep</b> <b>track of our conversation history.
We are</b> <b>able to set the system</b> <b>message by clicking on additional</b> <b>parameters. The big difference is the</b> <b>agent allows us to provide</b> <b>a list of tools. And to give you an</b> <b>analogy is let's say I ask you a super</b> <b>complex math problem,</b> <b>you might try to calculate the math</b> <b>problem by yourself in your head and</b> <b>provide some sort of</b> <b>answer.
But it's very possible that you</b> <b>are not confident in your</b> <b>answer. But what if I provided</b> <b>you with a calculator, and I then ask you</b> <b>to perform the calculation</b> <b>again, I'm sure that your</b> <b>answer will be greatly improved, or at</b> <b>least I would hope so. So</b> <b>that is what an agent will do.
</b> <b>When we ask our agent the question, it</b> <b>will first try to see if</b> <b>it's able to answer the question</b> <b>from its training data. And if it can't,</b> <b>it will have a look at</b> <b>the list of available tools,</b> <b>and then use that tool to find a</b> <b>solution. Let's delete this conversation</b> <b>chain.
And let's set up</b> <b>the basics. We know we need a chat model</b> <b>and memory. So let's start</b> <b>with those first.
So under</b> <b>chat models, let's add the chat open AI</b> <b>model. And let's add this</b> <b>to our agent, let's select</b> <b>our credentials. And I'm actually going</b> <b>to select GPT-4 in this</b> <b>example.
But if you don't yet have</b> <b>access to GPT-4, then feel free to use</b> <b>GPT 3. 5 Turbo instead. </b> <b>I'm actually going to select</b> <b>GPT-4 Turbo preview just to keep things</b> <b>interesting.
For the</b> <b>temperature, let's select a low value,</b> <b>because we want the agent to rely on its</b> <b>tools and not get too</b> <b>creative with its answers. Then let's</b> <b>also add memory. So within add nodes,</b> <b>let's go to memory, and let's</b> <b>simply add buffer memory.
And</b> <b>let's add this to our agent, like so. </b> <b>Great, all we have to do now</b> <b>is add tools to this agent. And</b> <b>sticking with the example of a</b> <b>calculator, let's add a calculator to</b> <b>this agent.
So within tools,</b> <b>let's go ahead and add our calculator to</b> <b>this canvas, and let's</b> <b>attach our calculator to our</b> <b>allowed tools. Let's save this chat flow,</b> <b>and let's test this out. </b> <b>Let's ask a question like,</b> <b>what is 5000 divided by 20?
And let's see</b> <b>what we get back. Right, so we are</b> <b>getting our response</b> <b>back. And it's very hard to tell just by</b> <b>looking at this, whether a</b> <b>tool was used, or whether this</b> <b>was simply answered by the model.
And in</b> <b>order to see what happened behind the</b> <b>scenes, we can use a</b> <b>tool like Langsmith. And this is</b> <b>completely optional. You do</b> <b>not have to include Langsmith</b> <b>in your project to follow along.
But I</b> <b>think for the sake of</b> <b>teaching you, you might find this</b> <b>interesting. So I'm going to enable</b> <b>Langsmith by going to analyze chat flow. </b> <b>And under Langsmith,</b> <b>I'll provide my Langsmith credentials.
</b> <b>And for the project name,</b> <b>I'll call it agent demo. And I'm</b> <b>going to switch on tracing. And I'm going</b> <b>to save this.
And let's run</b> <b>this again. I'm just going to</b> <b>copy this, it's clear the chat. And let's</b> <b>run this again.
And if we go over to</b> <b>Langsmith, we can view</b> <b>the strace. And within the sequence, we</b> <b>can see this calculator</b> <b>tool, we can see the input into</b> <b>the tool as well as the output. If you're</b> <b>unfamiliar with Langsmith,</b> <b>then I highly recommend checking</b> <b>out my previous video in the series,</b> <b>where we went over the steps for</b> <b>integrating Langsmith</b> <b>into Flow-wise in detail.
So let's have a</b> <b>look at a few more</b> <b>examples. Let's move this calculator</b> <b>tool somewhere else. And let's add a tool</b> <b>that will allow our agent</b> <b>to search Google for answers.
</b> <b>So let's go to add nodes, let's go to</b> <b>tools. Then there are a few options for</b> <b>adding Google search</b> <b>functionality. But in this example, I'll</b> <b>just add this SERP API</b> <b>tool.
And let's attach this</b> <b>to our agents. And you will notice, we</b> <b>can add more than one tool into this</b> <b>agent input. In order</b> <b>to use SERP API, we do have to set its</b> <b>credentials.
So just click on Create New,</b> <b>give your credentials</b> <b>a name like SERP API. And then in order</b> <b>to get the API key, go over to SERP</b> <b>API. com and then click on</b> <b>Sign In or register your account.
After</b> <b>signing in, you should see this</b> <b>dashboard. And what we are</b> <b>interested in is this API key. So simply</b> <b>copy this key and then add</b> <b>it to this field and press</b> <b>Add.
We can now start using this tool. So</b> <b>let's test this out by</b> <b>going to the chat. And let's</b> <b>ask a question about something that the</b> <b>agent will have no idea how to answer</b> <b>from its training data.
</b> <b>Like let's say, what is the current price</b> <b>of an RTX 4090 on</b> <b>Amazon. com? And let's run this.
</b> <b>And in the response, we are getting these</b> <b>values back. And if we go over to</b> <b>Langsmith, we can open</b> <b>that trace. And in this trace, we can see</b> <b>that the SERP API tool was</b> <b>indeed used.
And this is the</b> <b>response we got back. And with this</b> <b>response, we can actually see the link</b> <b>where this information</b> <b>was scraped from. So let's actually copy</b> <b>this URL.
And if we open</b> <b>this in the browser, we can</b> <b>indeed see that price that was returned. </b> <b>So our agent is starting to</b> <b>get a little bit more useful,</b> <b>as it can assist us with math questions</b> <b>and browse the internet and</b> <b>do research on our behalf. </b> <b>But one thing it's missing is the ability</b> <b>to generate unique</b> <b>recipes and tell me what's for</b> <b>dinner.
And if you've been following</b> <b>along with the series, the solution for</b> <b>this might be obvious</b> <b>to you. In one of the very early videos,</b> <b>we created an LLM chain for</b> <b>generating a unique recipe. And</b> <b>we will do that in this solution as well.
</b> <b>We will create an LLM chain</b> <b>that will produce the recipe,</b> <b>and that chain will be added to the agent</b> <b>as a tool. So whenever we</b> <b>ask the agent to generate</b> <b>a recipe for us, that tool will be</b> <b>called. Now to add a chain as a tool, we</b> <b>can simply go to tools.
</b> <b>And from here, let's select the chain</b> <b>tool. Let's just move this</b> <b>OpenAI node out of the way. And</b> <b>let's attach this chain tool to our</b> <b>agent, like so.
Now we can</b> <b>give our chain a name. I'll just</b> <b>call mine Chef chain. Now in the</b> <b>description, we can tell the agent when</b> <b>it should use this specific</b> <b>tool.
So this description is extremely</b> <b>important. Let's type</b> <b>something like, "Use this tool when</b> <b>asked to create a food recipe. " Let's</b> <b>save this.
And what we can do now is</b> <b>simply create our LLM</b> <b>chain. So let's do that real quick. </b> <b>Within chains, let's grab our LLM chain,</b> <b>and let's attach this</b> <b>to the chain tool.
Then let's assign our</b> <b>LLM. Then let's assign a</b> <b>model as well. And I'll simply</b> <b>select an LLM, and let's add the OpenAI</b> <b>node to the canvas.
And this</b> <b>is identical to what we did</b> <b>earlier in this tutorial series. So if</b> <b>you are a bit rusty on</b> <b>what all of these nodes mean,</b> <b>then just go and check out that video</b> <b>again. I'll leave this</b> <b>on GPT 3.
5 Turbo instruct,</b> <b>and let's also add our prompt template. </b> <b>So under prompts, let's add</b> <b>a prompt template. And let's</b> <b>attach this prompt template to our LLM</b> <b>chain.
For the template value, let's</b> <b>enter something like,</b> <b>"Based on the below user input, create a</b> <b>unique recipe, including</b> <b>cooking instructions. " And</b> <b>let's also add the input from the chat</b> <b>box, and we'll simply call this</b> <b>placeholder input. Let's</b> <b>save this.
Then let's click on format</b> <b>prompt values, and let's</b> <b>assign a value to input. And</b> <b>this will be the question from the chat</b> <b>box. And let's go ahead and</b> <b>save this.
Let's test this out</b> <b>by running chat, and let's clear the chat</b> <b>history. And let's enter</b> <b>something like, "What's for dinner? </b> <b>I'm in the mood for chicken.
" Now, I do</b> <b>want to point this out,</b> <b>because this is very interesting. </b> <b>The agent is saying that based on the</b> <b>recipe provided, you're</b> <b>making garlic parmesan,</b> <b>and etc. This is not actually the recipe</b> <b>that was provided by the</b> <b>chain, but that recipe was</b> <b>simply passed onto the agent, and then</b> <b>the agent is giving its</b> <b>interpretation of that recipe.
And</b> <b>in some cases, that might be fine. But if</b> <b>you simply want to return</b> <b>the response from this chain,</b> <b>you can simply toggle this switch over</b> <b>here. On the chain tool,</b> <b>simply enable return direct,</b> <b>and now see what happens in the chat</b> <b>window.
So I'll clear the</b> <b>chat, let's enter the same</b> <b>prompt, and now we're actually receiving</b> <b>the entire recipe as it was</b> <b>generated by the add-all-in</b> <b>chain. And if we have a look at</b> <b>Langsmith, the chef chain was indeed</b> <b>called, and we can see the</b> <b>output from that chain. Next, I'm going</b> <b>to show you how to get</b> <b>your agent to answer questions</b> <b>from a knowledge base.
And this is useful</b> <b>for building customer</b> <b>support chatbots that are</b> <b>trained on your business data. This is</b> <b>useful for building customer support</b> <b>chatbots that are able</b> <b>to answer questions based on your</b> <b>business data. But first, please like</b> <b>this video, hit subscribe,</b> <b>and comment below which tools you would</b> <b>like me to cover in a future</b> <b>video.
So we can add custom</b> <b>knowledge bases to our agent using a</b> <b>retrieval tool. Retrieval is</b> <b>something we had a look at in</b> <b>one of the previous videos in detail. And</b> <b>I'm sure that you will</b> <b>agree that adding retrieval</b> <b>to an agent that is also able to perform</b> <b>so many other functions is</b> <b>extremely powerful.
To add</b> <b>retrieval, click on add nodes, then under</b> <b>tools, grab the retrieval</b> <b>tool and add it to your canvas. </b> <b>Let's attach this tool to our agent as</b> <b>well. Let's give our retrieval tool a</b> <b>name, and we will look at</b> <b>two examples of using retrieval.
First,</b> <b>we'll just use a simple</b> <b>example where we will upload a PDF</b> <b>document and then ask questions about</b> <b>that PDF. Secondly, we'll attach this</b> <b>agent to an existing</b> <b>pinecone database and ask questions from</b> <b>an existing knowledge</b> <b>base. So in our first example,</b> <b>we've got this financial statement about</b> <b>Tesla.
And let's say that</b> <b>whenever the user asks questions</b> <b>about Tesla, this retrieval tool will be</b> <b>used to fetch all this</b> <b>information from the PDF document,</b> <b>and we can then answer questions based on</b> <b>this information. So let's</b> <b>call this retriever a Tesla</b> <b>retriever. The name is not all that</b> <b>important, but the description is.
This</b> <b>description will tell</b> <b>the agent when to use this tool. So let's</b> <b>enter something like use</b> <b>this tool when ask questions</b> <b>about Tesla and their financial</b> <b>statements. Now if you're doing this for</b> <b>a business, you can very well</b> <b>enter something like use this tool when</b> <b>ask questions about</b> <b>business XYZ and then upload a</b> <b>document containing all the questions and</b> <b>answers about that business.
</b> <b>Right, so we can now attach</b> <b>a vector store to this retriever. So</b> <b>under vector stores, let's add the in</b> <b>memory vector store. </b> <b>We will have a look at adding pinecone in</b> <b>a second and let's add</b> <b>embeddings as well.
So under</b> <b>embeddings, let's grab the open AI</b> <b>embeddings and let's attach this to the</b> <b>vector store. I do have a</b> <b>video going into a lot of depth on</b> <b>creating a rag chat bots. So if you're</b> <b>unsure about what I'm doing</b> <b>here, please check out that video.
Let's</b> <b>add our credentials to this</b> <b>embeddings node and let's add</b> <b>a document uploader for this PDF</b> <b>document. So under document uploaders,</b> <b>let's grab the PDF document</b> <b>loader and add it to the canvas. Let's</b> <b>attach this to our vector store.
Let's</b> <b>upload that PDF file</b> <b>or whatever data you prefer to use. I'm</b> <b>also going to attach a text</b> <b>splitter just to chunk this data. </b> <b>So under text splitters, I'm going to</b> <b>grab the recursive character text</b> <b>splitter and let's add</b> <b>this to our PDF document uploader or</b> <b>whichever document loader</b> <b>you prefer to use.
I'll leave</b> <b>the chunk size on 1000 and for the chunk</b> <b>overlap, I'll just enter 100. </b> <b>Now let's test this out. I'm</b> <b>going to save this.
Let's up the data</b> <b>into the vector store. After this is</b> <b>done, we can now test</b> <b>this by opening the chat. It's clear the</b> <b>chat history.
And now</b> <b>let's ask a question contained</b> <b>within a document like what were Tesla's</b> <b>total assets in 2023. </b> <b>And that should give us the</b> <b>information contained in this column,</b> <b>which is 93, which is this value</b> <b>contained in this column. </b> <b>But I actually received this message.
And</b> <b>this was intentional</b> <b>because I've had so many students</b> <b>fall into this silly little trap. If you</b> <b>get this message, it</b> <b>simply means we need to replace</b> <b>this conversation agent with an agent</b> <b>that looks pretty much identical, but</b> <b>which is optimized for</b> <b>performing retrieval. So let's remove</b> <b>this node is go to add nodes under</b> <b>agents, it simply add</b> <b>the conversational retrieval agent.
And</b> <b>all we have to do is attach</b> <b>all of these tools into this</b> <b>agent. We can also assign the memory. </b> <b>It's also attached to chat</b> <b>model.
And let's assign this</b> <b>retrieval tool as well. Right, let's save</b> <b>this. We don't have to</b> <b>perform the upsert again,</b> <b>because we didn't delete the database.
</b> <b>Let's test this again by</b> <b>asking what were Tesla's assets for</b> <b>2023. And we can see this retrieval chain</b> <b>returning quite a bit of</b> <b>information from that PDF. And I</b> <b>guess we could go and ask it what the</b> <b>totals were.
But this answer is</b> <b>sufficient as well. Now, let's</b> <b>have a look at adding an existing vector</b> <b>store to this flow. So let's</b> <b>delete this in memory vector</b> <b>store, as well as the PDF node, let's</b> <b>delete the text split there is</b> <b>delete the embeddings.
And I'm</b> <b>actually going to use the vector store</b> <b>that I created in the</b> <b>previous video, where we scraped</b> <b>the Lang chain documentation. And we were</b> <b>then able to answer</b> <b>questions related to LCEL. So under</b> <b>vector stores, I'm going to add my</b> <b>pinecone vector store, let's</b> <b>attach this to this retriever,</b> <b>I'll select my pinecone credentials.
And</b> <b>I remember we call this</b> <b>index flow wise, we also</b> <b>need to attach this embeddings node. So</b> <b>I'll just add this open AI embeddings</b> <b>node like so. And let's</b> <b>attach it to this pinecone node.
Now I'm</b> <b>not interested in loading</b> <b>any new documents, I simply</b> <b>want to fetch the existing documents from</b> <b>this vector store. Now,</b> <b>the data in this vector store</b> <b>relates to Lang chain. So I'm just going</b> <b>to rename this retrieval tool</b> <b>to, let's say the Lang chain</b> <b>retriever.
And let's change the</b> <b>description as well to use</b> <b>this tool when ask questions about</b> <b>Lang chain, like so, let's save this. And</b> <b>just to keep things</b> <b>interesting, let's enable return</b> <b>source documents, let's save this chat</b> <b>flow. And in the chat, let's ask a</b> <b>question about Lang chain,</b> <b>like what is Lang chain LCEL and we are</b> <b>getting the correct response</b> <b>back.
And if we have a look</b> <b>at this in Langsmith, we can see that the</b> <b>Lang chain retrieval tool</b> <b>was indeed used as part of</b> <b>this process. If you enjoyed this video,</b> <b>then you might be interested</b> <b>in some of these other videos,</b> <b>click on the screen right now to see how</b> <b>you can create an</b> <b>appointment booking agent using</b> <b>Flow Wise or watch the next video in this</b> <b>Flow Wise tutorial series.