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