Creating a Chatbot for Your Firm — that Uses Your Own Docs

Creating a Chatbot for Your Firm — that Uses Your Own Docs

Start at your n8n URL.

Phase 1

Creating a new workflow

Workflows are the basic canvas for building out a given automation task in n8n.

Зображення

Tab Name: Workflow Automation - n8n

Click on Add first step

Зображення

Click “On chat message”

Зображення

Go back to the main canvas

Зображення

Click on the plus sign to add the next step in the flow

Зображення

Click on Advanced AI

Зображення

Select AI Agent

Зображення

Go back to the main canvas

Зображення

Click on the plus sign by “Chat Model”

Зображення

Select your chat model (I’m using OpenAI in this guide)

People get hung up on which model is "best." While there are some objectively measured outcomes that try to answer these questions, if you are here, you don't need to worry about that yet.

Зображення

Click on the Credential Area to select or create your API credentials for OpenAI

If you don't know how to set up credentials, there is a little "Docs" button when you create your credential. I'll make an OpenAI-specific walkthrough and put it here when it's ready, but here is an example of setting up credentials for a Google API.

Зображення

Go back to the main canvas

Зображення

Click on the plus sign next to “Memory”

Зображення

Select Simple Memory, or the memory option of your choice

Memory is what allows the chatbot to maintain the context of the conversation. You can have multiple conversations going in their own silos, or you can have one big conversation that encompasses all of your conversations with your bots. For now, don't worry too much about it. Let's get your bot up and running first.

Зображення

Back to canvas without changing anything

Зображення

Click the plus sign next to “Tool”

Зображення

Select Simple Vector Store, or the vector store of your choice

A vector store is like a filing cabinet where the AI knows to look for specific types of documents. If you want to know more about memory and vector stores, see this post or get CLE credit for watching the video here.

Зображення

Setting the name of your first vector store. Best practice is to use all lowercase letters and no punctuation or spaces. Let’s do “general_information_tool” for simplicity.

Зображення

Let’s rename the node itself to the same name as the vector store for simplicity.

Зображення

Let’s update the description to “Use this vector store to retrieve general information about my firm.”

Зображення

Go back to canvas

Зображення

Every so often you can click the “Tidy Up” icon to keep things organized

Зображення

Let’s add another tool by clicking on the plus button next to “Tool”

Зображення

Again select a Simple Vector Store or the vector store of your choice

Зображення

Let’s change the name of this vector store to texas_penal_code_information_tool (or you can change it to something else if you want to use a different data set)

Зображення

Let’s rename the Node to the name of the tool, texas_penal_code_information_tool, for simplicity

Зображення
Зображення

Click on the plus button next to “Embedding” on either of your tool nodes.

Зображення

Select your embeddings provider (I’m using OpenAI but other providers are fine).

Зображення

Make sure to select or create your credential then go back to the canvas.

Зображення

Click to drag the plus sign by the word “Embedding” on the other tool node and drag it to the “Embeddings OpenAI” node.

Зображення

I’ll tidy up again

Зображення

I’ll save here as well to avoid any lost progress

Зображення

Phase 2

Click on the button "Open nodes panel Tab"

Зображення

Click on "Simple Vector Store" or whichever vector store you selected above

Зображення

Click on "Add documents to vector store"

Зображення

Click on "Simple Vector Store" and let’s rename it to something connected to your other vector stores, like “general_information_tool Vector Store”

Зображення

Let’s rename the memory key to something relevant, like “general_information_tool_store,” then go back to the canvas.

Зображення

Click on the "Embedding" plus button next to the vector store node.

Зображення

Click on "Embeddings OpenAI" or the provider you used above.

Зображення

Click on the button "Embedding Document general_information_tool Vector Store"

Зображення

Click the plus sign next to “Document Loader” on the vector store node.

Зображення

Change the Type of Data from JSON to Binary

Зображення

Select “Add Option”

Зображення

Click on "Split Pages in PDF", then go back to the canvas.

Зображення

Click on the plus button "Document Text Splitter" next to Default Data Loader.

Зображення

Click on "Recursive Character Text Splitter"

Зображення

Change the Chunk Size to 4000 and the Chunk Overlap to 400.

Chunk size is the length in characters that is snipped from the provided content, and chunk overlap is how many characters back from the end of the previous snippet to begin the next snippet. This is useful for maintaining the context of the returned results for the AI so things aren't cut in strange ways.

Зображення

Go back to canvas

Зображення

Click on the button "Open nodes panel Tab"

Зображення

Click on "Google Drive," or the document manager of your choice.

Зображення

Click on "Triggers" near the bottom

Зображення

Click on "On changes involving a specific folder"

Зображення

Select or create your credential for the Drive Node.

Зображення

Select the folder you will be using to store the documents you want the bot to rely on for the law firm general knowledge.

Зображення

Click on “Watch For”

Зображення

Select "File Created”

Зображення

Click on "Back to canvas"

Зображення

Drag the plus sign from "Google Drive Trigger" to the vector store.

Зображення

Tidy up

Зображення

Hover over the line between the Google Drive trigger and the vector store, then click on the plus button to add a new step.

Зображення

Select  "Google Drive," or whichever document manager you selected previously.

Зображення

Click on "Download file"

Зображення

Change “From list” to By ID

Зображення

Click on "Expression"

Зображення

Type in “ {{ $json.id }} ”, it will be gray and won’t look like anything happened. That’s normal. Return to canvas.

Зображення

Select your Google Drive Trigger and all accompanying nodes.

Зображення

Right-click and select "Duplicate 6 nodes"

Зображення

Click on the button "Tidy Up ⇧ Alt T"

Зображення

Double click on the new Google Drive Trigger

Зображення

Select the second folder where you hold the other set of documents. For my demo purposes, I'm just using a generic folder for the Texas Penal Code.

Зображення

Double click on the new Embedding Document node

Зображення

Change the node name and Memory Key to “texas_penal_code_information_tool_store” or something that is relevant for you.

Зображення

Click on "Back to canvas"

Зображення

Now let’s go back up to the top and make sure our Memory Key in the tool matches the Memory Key in the store

Зображення
Зображення

Go back to the canvas

Зображення

Check the other tool

Зображення
Зображення

Click on "Back to canvas"

Зображення

Click here to activate the flow

Зображення

Click on the button "Got it"

Зображення

Go to your Google Drive Folder for your law firm information knowledge

Click on the button "New"

Зображення
Зображення

Go back to n8n. Double-click on the button "When chat message received Open chat"

Зображення

Turn on publicly available chat and set the Authentication to “n8n User Auth.” Click on the chat URL to copy it to your clipboard

Зображення
Зображення
Зображення

Back on n8n, go back to canvas

Зображення

Double-click on your AI Agent node

Зображення

Click on the input to add options

Зображення

Select "System Message"

The system message can be thought of as the personality or directive of the bot.

Зображення

Add a message in the input box along these lines: 

You are an AI assistant who answers questions for users. You look for answers in the documents.
You have access to one vector store with the Texas Penal Code, and another vector store with information about my law firm.
Always access the documents before answering questions. If the answer isn't present in the vector stores, say "I don't know."
Зображення

Click on "Back to canvas"

Зображення

Go back to the chat and try again.

Зображення
Зображення
Зображення
Зображення

Back in n8n, click on the "Executions" tab.

Зображення
Зображення

Double-click on the texas_penal_code_information_tool (or whichever tool had a response in your most recent interaction with the bot).

Зображення

Click on "Logs"

Зображення

Click on "Output"

Зображення

You’ll see a bunch of meaningless (to us) numbers — that’s how the AI sees your information and prompts.

Зображення

Go back to "Output"

Зображення

This is the human-readable version of what the AI saw. We can read it because the “Embeddings” node is translating the numbers for us.

Зображення

You’ll see the basis of the AI’s answers here.

Зображення
Зображення

Let’s go back to the canvas and make another tweak.

Зображення

Click on "Editor"

Зображення

Re-open the AI Agent node by double-clicking on the node.

Зображення

Let’s adjust the system message just a little bit.

Зображення

Let’s add a final paragraph:

If you retrieve an answer from the Penal Code vector store, include any citations to the specific statute, if present.
Зображення

Try the chat again to see if anything changed.

Зображення

Click here

Зображення

Verifying the information

Зображення

I believe that learning the tech is the way law will be done in the next five to ten years, so I encourage you to try the walkthrough. Paid membership required for the actual code snippets. Click to jump back up.