Notebooks
L
LanceDB
Fintech Ai Agent

Fintech Ai Agent

fintech-ai-agentagentsllmsvector-databaselancedbgptopenaiAImultimodal-aimachine-learningembeddingsfine-tuningexamplesdeep-learninggpt-4-visionllama-indexragmultimodallangchainlancedb-recipes

Fintech AI Agent

Fintech-AI-Agent.excalidraw.png

These days, there are tons of frameworks out there to help you whip up AI agents fast. Honestly, it feels like a new “low-code” tool shows up every other day, promising to make building agents a breeze. They’re all over the place—simple drag-and-drop setups or text boxes where you plug in a few prompts and some data, and just like that, your agent’s ready to go.

This time, we will go all in to create a fintech AI agent with no framework, everything is in raw python. This notebook carries an imporant appication of the fintech industry where people face enormus amount of time in processsing loan queries and insurance claims.

[36]

Setting up the data needs and libraries

[ ]
--2025-02-26 14:08:46--  https://drive.google.com/uc?export=download&id=1huZAkbQzTwiELbKbNpghbC2hEBLOqW9V
Resolving drive.google.com (drive.google.com)... 173.194.217.139, 173.194.217.113, 173.194.217.102, ...
Connecting to drive.google.com (drive.google.com)|173.194.217.139|:443... connected.
HTTP request sent, awaiting response... 303 See Other
Location: https://drive.usercontent.google.com/download?id=1huZAkbQzTwiELbKbNpghbC2hEBLOqW9V&export=download [following]
--2025-02-26 14:08:46--  https://drive.usercontent.google.com/download?id=1huZAkbQzTwiELbKbNpghbC2hEBLOqW9V&export=download
Resolving drive.usercontent.google.com (drive.usercontent.google.com)... 173.194.217.132, 2607:f8b0:400c:c13::84
Connecting to drive.usercontent.google.com (drive.usercontent.google.com)|173.194.217.132|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1804682 (1.7M) [application/octet-stream]
Saving to: ‘credit_risk_dataset.csv’

credit_risk_dataset 100%[===================>]   1.72M  --.-KB/s    in 0.02s   

2025-02-26 14:08:49 (102 MB/s) - ‘credit_risk_dataset.csv’ saved [1804682/1804682]

LLM and Embedding Model Customization

For the language model operations, I’ve implemented Mistral as the foundation, delivering reliable and efficient performance tailored to various tasks. However, the setup is highly flexible—you can seamlessly switch to any other provider or model that better suits your specific needs, ensuring adaptability for your unique use case. The same level of customization applies to the embedding model, allowing you to choose an alternative that aligns perfectly with your project requirements. If you decide to stick with this configuration, just remember to include your Hugging Face token and Mistral API key to authenticate and unlock the full potential of the system without any hiccups.

[40]

Predictive ML Modelling

I’m using the Credit Risk Dataset as a demo for loans. In reality, companies could use something like a CIBIL score to see if someone’s eligible. But for this POC, I grabbed that dataset, trained a random forest model, and used it for predictive analysis.

[41]

ML Modelling

[42]
[43]
Model accuracy: 0.84
[44]
['loan_approval_model.pkl']

Setting up the Kernel Agent

The Kernel Agent serves as the intelligent core of the system, designed to comprehend user queries and determine the appropriate actions to direct them to the correct agent—be it the loan agent or the insurance agent—ensuring a smooth and efficient process. It leverages key details provided by the user, such as parameters, along with the detected intent behind the query, to make informed decisions and route requests accurately to the most relevant agent for resolution.

[45]
[46]
[51]
Probability for loan approval: 0.960
Query: 'I need a loan for home renovation' | Params: {'age': 30, 'income': 50000, 'loan_amount': 10000} -> Result: Eligible

Probability for loan approval: 0.440
Query: 'I need a loan for medical expenses' | Params: {'age': 25, 'income': 20000, 'loan_amount': 25000} -> Result: Not Eligible

Generating the Demo Insurance Claim dataset

Before diving in, I want to walk you through my thought process for creating the Insurance Agent. Picture this: you walk into an insurance company, sit down with someone, and start explaining why you need to file a claim—why it’s legit and why you deserve it. That conversation is key because it’s the starting point for deciding whether the claim gets processed or not.

I tried hunting down a dataset with those kinds of conversations and clear yes or no labels for claims, but I came up empty. So, to keep things moving, I built a simple synthetic dataset pipeline. It generates a fake dataset with text queries on one side and a target on the other—showing if the person filing the claim is eligible or not.

[48]
Generating 10 Auto Insurance Claim Queries:
--------------------------------------------------
1. Query: Hi, I'm reaching out about an auto insurance claim I need to file. Last night, I was involved in a single-car accident in Massachusetts. I was driving home from work around 8 PM, and it was raining quite heavily. I hydroplaned and lost control of my vehicle, crashing into a guardrail. I was alone in the car, and thankfully, I only have some minor bruises and a slight concussion, but my car is pretty badly damaged. The front end is smashed, and the airbags deployed, so I'm sure the repairs will be costly.

I have comprehensive coverage with a $500 deductible. I'm worried, though, because I had a glass of wine at a work event before driving home. I didn't think I was impaired, but I'm concerned that this could affect my claim. I know I should have called a cab, but I
   Target: 0 (Not Approved)
--------------------------------------------------
2. Query: I was involved in a rear-end collision last week in California. I was stopped at a red light when another driver hit me from behind. The impact caused significant damage to the rear bumper and trunk of my car, and I also have some neck pain that I'm getting checked out by a doctor. The other driver admitted they were distracted by their phone and didn't see me stopped in time. I have their insurance information, but I'm worried because I had a lapse in my insurance coverage for a few days last month. I forgot to pay the bill on time, but I did reinstate it before the accident. Could this lapse affect my claim? I'm looking to get the damage to my car repaired and potential medical expenses covered. What are the next steps I should take, and is there any chance my claim could be denied due to the lapse in coverage?
   Target: 0 (Not Approved)
--------------------------------------------------
3. Query: Hi, I'm calling about an auto insurance claim I need to file. I was involved in a car accident yesterday in California. I was driving on the highway when another driver merged into my lane without signaling and hit the front passenger side of my car. The airbags deployed, and my car is pretty badly damaged—the bumper is dented, the headlight is broken, and there are scratches all along the side. I took photos of the damage and have the other driver's insurance information. Fortunately, I only have some minor bruises, but I did go to the emergency room to get checked out, and they recommended I follow up with my primary care doctor. I'm looking to claim for the repairs to my vehicle and potential medical expenses, as I'm not sure how much the follow-up appointments will cost. Could you guide me through the process of filing this claim and let me know what other information you might need from me?
   Target: 1 (Approved)
--------------------------------------------------
4. Query: Dear Insurance Agent,

I hope this message finds you well. I am writing to inquire about filing an auto insurance claim following a recent accident. On October 15, 2023, at approximately 7:30 PM, I was involved in a collision in the state of California. The accident occurred on Interstate 5, near Exit 33.

The incident was a rear-end collision. I was traveling at the speed limit when another vehicle hit me from behind. The cause of the accident appears to be the other driver's inattention; they were distracted by their phone. The damages to my vehicle include significant rear bumper damage, a broken taillight, and possible frame issues. Fortunately, I sustained only minor injuries—a few bruises and a sore neck. The other driver also suffered minor injuries.

I would like to claim for the damages to my vehicle and any necessary medical expenses. My policy
   Target: 0 (Not Approved)
--------------------------------------------------
5. Query: Hi, I hope you're doing well. I need to file an auto insurance claim and I'm looking for some guidance on the process. Last weekend, I was involved in a rear-end collision in California. I was stopped at a red light in downtown Los Angeles when the driver behind me didn't brake in time and hit my car from the back. The impact was quite significant; my car has considerable damage to the rear bumper and trunk, and the airbags were deployed. I also sustained some injuries—mainly whiplash and a few bruises. I went to the emergency room right after the accident and have the medical reports and bills. The other driver admitted fault and we exchanged insurance information. I have their details as well as the police report from the scene.

My car is a 2018 Toyota Corolla, and I'm not sure if it's drivable in its current condition. I
   Target: 1 (Approved)
--------------------------------------------------
6. Query: I was involved in a car accident last week in California and need to file a claim. I was driving my 2018 Honda Civic on the I-5 freeway when another driver merged into my lane without signaling and hit the passenger side of my car. The impact caused me to lose control and hit the guardrail. The other driver's insurance company has already accepted liability.

My car has significant damage to the passenger side, including dents, scratches, and a broken side mirror. The front bumper was also damaged from the impact with the guardrail. I took my car to a local auto body shop, and they estimated the repairs to be around $4,500. I also had to rent a car while mine is in the shop, which is costing me $35 a day.

I sustained some minor injuries—mainly bruises and whiplash. I visited the ER immediately
   Target: 1 (Approved)
--------------------------------------------------
7. Query: Hi, I need to file an auto insurance claim. Last night, I was involved in a rear-end collision in California. I was driving home from work on the freeway when traffic suddenly slowed down. Unfortunately, the driver behind me didn't brake in time and hit the back of my car. The impact was significant; my car's rear bumper is severely damaged, and the trunk is crumpled. I also have some whiplash and bruising from the impact.

I've been to the hospital and have the medical reports and bills ready. I also have photos of the damage to my vehicle and a copy of the police report. The other driver admitted fault and gave me their insurance information, but I'm worried because they mentioned they might have let their policy lapse. I need to claim for the damages to my car and the medical expenses. However, I'm concerned my claim might be affected because I had a glass of wine
   Target: 0 (Not Approved)
--------------------------------------------------
8. Query: I was involved in a car accident last week in California, and I need to file a claim. I was driving my 2018 Honda Civic on the freeway when another driver merged into my lane without signaling and hit the side of my car. The accident caused significant damage to the driver's side door and rear panel, and I also have some minor injuries—bruises and a slight sprain in my neck. The other driver was cited for an unsafe lane change, but I'm worried because I was driving while slightly over the legal alcohol limit, which I know was a terrible mistake.

I want to know if my comprehensive coverage will pay for the damages to my car and if my medical payments coverage will cover my hospital bills. I'm also concerned about whether my claim might be denied due to the fact that I was driving under the influence. Can you guide me through the process and let me know what to expect?
   Target: 0 (Not Approved)
--------------------------------------------------
9. Query: Hi, I hope you're doing well. I need to file an auto insurance claim for an accident that occurred yesterday in California. I was driving my 2018 Honda Civic on the I-5 freeway when another driver merged into my lane without signaling and hit the rear passenger side of my car. The impact caused me to lose control and crash into the median. Fortunately, there were no injuries, but my car has significant damage. The rear bumper is severely dented, the passenger side door is scraped, and the rear taillight is broken. The other driver admitted fault, and we exchanged insurance information. I have his name, contact details, and insurance policy number. I also took photos of the scene and the damages to both vehicles.

I need to know the next steps to file a claim for the damages to my car. Specifically, I want to understand:

1. What documents or information do I need
   Target: 1 (Approved)
--------------------------------------------------
10. Query: Hi, I'd like to inquire about filing an auto insurance claim. Last night, I was involved in a rear-end collision in California. I was driving home from work on the freeway when traffic suddenly slowed down. Unfortunately, the driver behind me didn't brake in time and hit my car from the rear. The impact deployed my airbags and caused significant damage to my bumper, trunk, and rear windshield. I also sustained some minor injuries—whiplash and bruises from the airbag. I went to the emergency room and was treated and released.

My car is a 2018 Honda Civic, and I have comprehensive coverage with a $500 deductible. The other driver's insurance company has already accepted liability. However, I'm worried because I had a glass of wine at a work event before the accident. Although I don't feel I was impaired, I
   Target: 0 (Not Approved)
--------------------------------------------------
Total queries generated: 10
Dataset saved to 'auto_insurance_claims.csv'

Inserting Demo Insurance Queries into LanceDB

The demo insurance queries are added to LanceDB to serve as a foundation for future operations, enabling semantic search capabilities across the existing query dataset. When a new query is introduced, it will be embedded once more, allowing the system to leverage hybrid search techniques to effectively explore and match it against the pre-existing database, ensuring accurate and efficient retrieval of relevant information.

This can become a go-to for the fintech comapaines where they can plug in their own Insurance Conversations logs and do the semantic search on the new embeded queries to retrive the eligibility for the claim.

[49]

Puttting it all together!

[53]
Testing Integrated System with Mistral:
--------------------------------------------------
Probability for loan approval: 0.960
Query: 'I need a loan for home renovation'
Params: {'age': 30, 'income': 50000, 'loan_amount': 10000}
Result: Eligible
--------------------------------------------------
Approval rate among similar claims: 20.0%
Query: 'I had a bad accident yesterday, but I think I was a bit drunk, need some insurance'
Decision: Not Approved
Similar Queries:
- Hi, I'm reaching out about an auto insurance claim I need to file. Last night, I was involved in a single-car accident in Massachusetts. I was driving home from work around 8 PM, and it was raining quite heavily. I hydroplaned and lost control of my vehicle, crashing into a guardrail. I was alone in the car, and thankfully, I only have some minor bruises and a slight concussion, but my car is pretty badly damaged. The front end is smashed, and the airbags deployed, so I'm sure the repairs will be costly.

I have comprehensive coverage with a $500 deductible. I'm worried, though, because I had a glass of wine at a work event before driving home. I didn't think I was impaired, but I'm concerned that this could affect my claim. I know I should have called a cab, but I
- Hi, I'd like to inquire about filing an auto insurance claim. Last night, I was involved in a rear-end collision in California. I was driving home from work on the freeway when traffic suddenly slowed down. Unfortunately, the driver behind me didn't brake in time and hit my car from the rear. The impact deployed my airbags and caused significant damage to my bumper, trunk, and rear windshield. I also sustained some minor injuries—whiplash and bruises from the airbag. I went to the emergency room and was treated and released.

My car is a 2018 Honda Civic, and I have comprehensive coverage with a $500 deductible. The other driver's insurance company has already accepted liability. However, I'm worried because I had a glass of wine at a work event before the accident. Although I don't feel I was impaired, I
- Hi, I need to file an auto insurance claim. Last night, I was involved in a rear-end collision in California. I was driving home from work on the freeway when traffic suddenly slowed down. Unfortunately, the driver behind me didn't brake in time and hit the back of my car. The impact was significant; my car's rear bumper is severely damaged, and the trunk is crumpled. I also have some whiplash and bruising from the impact.

I've been to the hospital and have the medical reports and bills ready. I also have photos of the damage to my vehicle and a copy of the police report. The other driver admitted fault and gave me their insurance information, but I'm worried because they mentioned they might have let their policy lapse. I need to claim for the damages to my car and the medical expenses. However, I'm concerned my claim might be affected because I had a glass of wine
- Dear Insurance Agent,

I hope this message finds you well. I am writing to inquire about filing an auto insurance claim following a recent accident. On October 15, 2023, at approximately 7:30 PM, I was involved in a collision in the state of California. The accident occurred on Interstate 5, near Exit 33.

The incident was a rear-end collision. I was traveling at the speed limit when another vehicle hit me from behind. The cause of the accident appears to be the other driver's inattention; they were distracted by their phone. The damages to my vehicle include significant rear bumper damage, a broken taillight, and possible frame issues. Fortunately, I sustained only minor injuries—a few bruises and a sore neck. The other driver also suffered minor injuries.

I would like to claim for the damages to my vehicle and any necessary medical expenses. My policy
- Hi, I hope you're doing well. I need to file an auto insurance claim and I'm looking for some guidance on the process. Last weekend, I was involved in a rear-end collision in California. I was stopped at a red light in downtown Los Angeles when the driver behind me didn't brake in time and hit my car from the back. The impact was quite significant; my car has considerable damage to the rear bumper and trunk, and the airbags were deployed. I also sustained some injuries—mainly whiplash and a few bruises. I went to the emergency room right after the accident and have the medical reports and bills. The other driver admitted fault and we exchanged insurance information. I have their details as well as the police report from the scene.

My car is a 2018 Toyota Corolla, and I'm not sure if it's drivable in its current condition. I
--------------------------------------------------