Learning - 2023-09-14
Build a Financial Chatbot with Metal
by Pablo Rios

Oil painting of a witch trading stocks
In the previous article, we saw some of the challenges faced by Large Language Models (LLMs) when working with spreadsheets or tabular data, and presented attribute extraction as a key method to address them.
This time, we are going to build a chatbot to "talk with our spreadsheet" and answer questions about an earnings report. This chatbot will be a financial expert, proficient in analyzing and interpreting complex financial statements, performing basic calculations, offering relevant metrics, and providing in-depth insights.Â
In a matter of minutes, we're certain to have something impressive like this:

Chatbot experience
Ready? Let's get started
Prerequisites
To follow along with this tutorial, you should have:
-Â Basic knowledge of Python or JavaScript
-Â Node.js v18+ (we recommend using nvm)
-Â An OpenAI Api key - get one here
Step 1: Set up your Metal account
If you haven't already, sign up to Metal and instantiate the Metal class in your preferred programming environment:
from metal_sdk.metal import Metalapi_key= 'METAL_API_KEY'client_id= 'METAL_CLIENT_ID'metal = Metal(api_key, client_id)
Step 2: Create a Data Source
First, we will create a new Data Source within Metal. This is where we’ll be uploading our files as Data Entities and setting up Metadata Fields as required.
payload = {"name": "Financial Data Sources","sourcetype": "File","autoExtract": True,"metadataFields": [{"name": "Revenue","type": "number","description": "What is the company's revenue for the most recent period?"}]}metal.create_datasource(payload)
Step 3: Create an Index
Navigate to your Dashboard and create a new Index. Then, link this Index to the Data Source you just set up. This step ensures that any financial report we upload can be easily accessed, streamlining data retrieval.

Create Index
Step 4: Upload the File
It's time to add the PDF containing our tabular data. We will use Apple Inc.'s latest financial statement as an example.

Upload File
Once the PDF is uploaded, give Metal a few moments to process and convert the tables from the report into embeddings. This transformation ensures the data is ready and accessible for our chat application. While this is underway, we can proceed with setting up our chatbot.
Step 5: Deploy the Metal AI Chatbot
Clone the Metal Chatbot repo and go to the directory.
5.1: Install dependencies
npm i
5.2: Add environment variables
cp .env.example .env.local
Now, set up the necessary environment variables for your project. Locate the METAL_INDEX_ID in the 'Settings' section of the Index linked to your Data Source.
Note you can also set up a password to protect your data by adding DEMO_PW to your environment variables.
5.3: Run the development server
npm run dev
5.4: Experience Your Chatbot Live!
Great job! Now, visit http://localhost:3000 to interact with your live chatbot powered by your data. To get a feel for the experience, try logging a few sample queries relevant to the dataset.
Step 6: Customize the Chat Experience
Metal’s Chatbot comes with a few handy configurations out of the box. You can configure the temperature, max number of tokens per response, and chunk count. You can also modify the prompt and give your chat a specific personality.
To demonstrate this, let's update the prompt of our chatbot to evaluate the financial health of a company by drawing analogies to physical health and referring to financial metrics as their "fitness" analogies, such as Liquidity Ratios as "Hydration Levels" or Profitability Ratios as "Muscle Mass".Â

Finance Fitness
Impressive, isn't it? The chatbot was able to interpret financial data by identifying details from a spreadsheet across two distinct periods. Not only did it conduct precise calculations, but it also used its financial acumen to address a challenging query. Moreover, it did it while breaking down complex financial ideas using familiar fitness comparisons, making the information clear and relatable.
Final Step
Now that you've seen the chatbot in action and experienced Metal's capabilities, it's your turn to deploy your chat application with your data in minutes. Let Metal take care of data processing, retrieval augmented generation, attribute extraction, and memory handling for you. Book a call or sign up for Metal today to get started!