It was only a matter of time before attempts at combining artificial intelligence (AI) and the blockchain appeared. The immutability of data, security of the trustless environment, and decentralized power provided by blockchain technology can be perfectly united with the power of AI to process huge datasets, optimize processes, and make predictions upon request.
AI and blockchain applications are widely used in the Internet of Things (IoT). Massive amounts of data — produced by IoT devices and saved on-chain — can be processed by artificial intelligence. Also, a lot of AI marketplaces are developed and tokenized on the blockchain. There are even some blockchain and AI projects that combine decentralized neural network learning with a blockchain consensus protocol.
One of the most popular ways to combine AI and blockchain is AI as a Service. It combines the predictive power of AI with the decentralized security of the blockchain. And AI as a Service has found its rightful place in the financial sphere.
In this article, we analyze the advantages of AI and the blockchain for banking and how these technologies can complement each other. We also provide a use case and the architecture of our own solution for ensuring data security in lending.
This article will be useful for developers who want to know more about the possibilities of combining AI and the blockchain.
The financial sphere has always used the most innovative technologies, as it needs to ensure process security and data protection. The blockchain has already found its rightful place in financial institutions. As evidenced by an impressive number of cryptocurrencies, blockchains allow financial operations to be conducted safely, consistently, and verifiably.
AI can effectively deal with massive amounts of data. AI helps us to analyze, filter, and structure massive streams of data and then make predictions and conclusions based on it.
One of the most popular use cases for combining AI and the blockchain into a single application is related to loan services. Thousands of loans are accepted or declined every day. A bank should keep records of all the requests to form a customer’s banking history. Thus, a loaning service requires:
- Reliable data storage
- Security of personal data
- An unbreakable data–client connection
- The ability to process large numbers of requests
This scenario looks like a perfect candidate for blockchain and AI collaboration.
Let’s see how this collaboration might work in practice. Clients will be able to check their ability to get a loan, the amount they can borrow, available loan repayment periods, access to particular financial services, etc. Such requests can be saved on-chain, serving as request proof for banks and other financial organizations, and can be used for processing loan requests. Hashing and cryptography techniques provide the necessary level of privacy and security.
Using a system built on AI and the blockchain, a bank can deterministically regulate the service parameters, set validity periods for client requests, and treat those requests as official since they’re saved on-chain.
AI will allow banks to process all loan requests, simplify loan acceptance algorithms, and make their loan approval systems more flexible.
A possible use case for loaning service looks like this:
- A client accesses an application (for example, a web form or mobile app), gets an ID, chooses a banking service, and inputs data.
- The application sends this request to the blockchain (a smart contract or its analog). This request is stored on-chain, so the client and bank can easily track it. Due to security and privacy requirements, the blockchain stores a hash of the information, not the information itself. This hash creates a connection between the client’s information and the client’s ID, but the information itself stays private.
- Simultaneously, the application sends data to the backend, where it’s processed in the AI module. This module contains pretrained neural networks for each banking service. The backend waits for confirmation from the AI and analyzes information to predict the result.
- Information about the request or part of the data from the application can be backed up in the database. This provides the bank with additional instruments for processing the request.
- The AI module returns the result to the blockchain, where it’s saved on-chain and becomes unbreakable evidence. Both the client and the bank can see the loan decision. For security reasons, only the client ID is saved on-chain.
- A client application stores the request, request ID, and official response so it can easily be sent to the banking application for confirmation.
After the official response is saved on-chain, it can be used by a bank employee to get confirmation and provide the requested service — in our case, give a loan to the customer. The process looks like this:
- Since the client application keeps IDs and requests, it can be used to redirect the service response to the banking application for further actions.
- A bank employee can easily check the validity of a client’s data by accessing it on the blockchain. Saving data on-chain protects both sides from redundant checks, simplifies the process of loan approval, and prevents fraud and misbehavior (on both sides).
As you can see, combining the blockchain and AI can make loan acceptance simpler, faster, and more convenient for clients while keeping it secure and easily manageable for banks.
So far, we’ve discussed how blockchain and AI convergence improves lending procedures. But in fact, both technologies bring many more advantages. Let’s take a look at other benefits of AI and blockchain.
The blockchain and artificial intelligence are completely different technologies that used to act in different areas. The blockchain is a decentralized technology that works with open data that’s immutable and unbreakably linked. On the contrary, AI is a highly centralized service that works with private data that can’t be accessed or recreated.
Their collaboration brings a lot of advantages, especially for lending services. In our case, we’ll use a set of pretrained neural networks and an Ethereum-based platform with support for smart contracts.
Let’s take a look at the benefits of these technologies.
Advantages of AI for financial services
Flexibility. A neural network model is more flexible than a statically coded algorithm. A model can be easily trained on a specific dataset. You need to spend a lot more time creating the architecture for a static algorithm than you do training a neural network. While a neural network needs just another dataset to change the model, a static algorithm may need a lot of corrections. Also, you can easily reuse the same neural network model with different data for different services.
Customization. There can be a lot of AI models for each kind of loan service. With AI, we aren’t limited to providing a service for a single type of loan. We can create models (based on the same dataset) to check the maximum number of loans for a client, the loan period, the services available for the client, and so on.
Modularity. The training of a neural network is separated from its usage. This means we can train our model in one program and use it in another. We can easily change models in our AI module without making any changes to the code. So if a bank changes the conditions for their loan service, e can just train a new model.
Upgradability. We can upgrade our model with new data received from client requests or provided by clients from other services. This keeps the model up-to-date and may give additional depth to its algorithm.
Independence. Since a neural network model can be kept separately from the code, it’s easy to track changes to the model (through hashing or checksums), make backups, and migrate to other services.
Benefits of blockchains with smart contract support
Unbreakable evidence. Once saved on-chain, loan requests and decisions can’t be changed. This way, blockchain data can serve as unbreakable proof for banking services in order to process clients’ loan requests.
Unified data. The blockchain guarantees that both clients and banks use the same information in a similar way. Since all information saved on-chain is publically accessible and unchangeable, it can be easily checked.
Verification. Every request, response, and service parameter change (e.g. duration and amount of a loan) can be found on-chain and checked for authenticity and validity. Since data on the blockchain can’t be lost, transaction history is always available and can be verified.
Security and privacy. Every ID and piece of data saved on-chain can be reconstructed from its hash. This creates a strong connection between pieces of data but keeps the information itself private. Only clients and authorized bank employees have access to the information behind the hashes and IDs.
Control. Basic information such as the loan terms, access rights, and number of loan applications are saved in a smart contract. They can be changed only by authorized persons, and every change is tracked and verified. This allows a bank to control the system in a safe manner.
Applications. The blockchain allows for developing decentralized applications (dApps). You can build dApps for different services, loan programs, and credit history. dApps can be both client- and bank-oriented. There’s even the potential for interoperability between blockchains and different external services — and even between different banks. All these features share the security and reliability of the blockchain.
The best features of the blockchain and AI technologies can be combined to create products that offer the advantages of both. Now we can take a look at the architecture for our loan application.
Let’s take a look at a possible implementation of a loan system based on the blockchain and AI. Its architecture should cover our use case for a loaning service and provide the required level of security. Here’s what we came up with:
Now let’s take a closer look at the main components of the applications that make up this system.
This application gives all the functionality a client might need:
- Forms for entering credentials
- Ability to enter personal information
- Ability to choose a service type/loan program
- Ability to choose conditions for the loan or service
- And more
A client application can be developed as a website, mobile application, or desktop application. It should allow a client to access the blockchain and get their transaction history. The client application is connected with the backend, so these two applications can share processed data. But the main purpose of the client application is to provide banking services in the fastest way possible and to get a response that will simplify the loan process.
Let’s see how a client interacts with our application:
Inputs credentials to get an ID. This can be easily achieved by hashing — for example, by hashing the client’s full name (or other identifying information) and a random seed. This hash can then be used as a client ID. The hash is secure enough so that personal information can’t be revealed while allowing that information to easily be recreated by clients themselves.
Chooses a loan program and loan conditions. Service ID and service data are formed out of it. Since loan information must can treated as private, the application hashes this data. Hashes of both the service ID and service data are sent to a smart contract to form an official request that’s saved on-chain. Confirmation of the request creation is sent to the backend. By that time, our client application has sent the client ID and service data to the backend, where it’s redirected to the AI module.
In general, a client’s request is processed in four steps:
- The client enters their credentials, personal information, the type of loan they would like to apply for, and their desired loan conditions.
- The application generates a client ID, loan service ID, service data, and service data hash.
- The application sends:
- the client ID, loan service ID, and service data hash to the smart contract
- the loan service ID and service data to the backend
- the client ID and request ID to the banking application
- The application gets:
- a request ID from the smart contract
- a confirmation and response from the AI module (through the smart contract event)
If necessary, client information can be securely saved in the bank database to make it easier to identify clients.
Sensitive information is saved only in the client’s application, and the public blockchain contains only IDs and hashes. When the blockchain needs to connect with a banking application, the information stored in the blockchain can be re-sent from the client application.
The banking application helps authorized banking personnel interact with the blockchain. It has two main purposes:
- Verify clients’ requests
- Control smart contract parameters
In order to get a loan, a client sends the banking application their credentials, request, and loan confirmation from the AI module. All this information is saved on-chain, so it can easily be found and verified. This can even be done automatically by getting the IDs and hashes and checking transactions containing this data in the blockchain.
A smart contract contains service parameters such as the term of request validity, number of allowed requests, etc. A banking application should be able to check these parameters against the client’s request. At the same time, authorized employees should be able to change smart contract parameters if necessary.
One of the most important functions of banking applications is to keep track of the integrity of AI models. As we mentioned, each model is saved as a separate file. We can get its hash and save it on-chain, so we can check if a model is valid. The hash of a model can also be used as a change log identifier: if the model changes, its hash also changes, and we should update its on-chain identifier.
Let’s see how our banking application works:
- The application gets information from the client to be checked in the blockchain.
- The application:
- makes changes to the smart contract parameters
- updates and checks the IDs of AI models saved in the smart contract.
The backend is what connects the blockchain and the AI module. This application accomplishes three main tasks:
- Receives requests from the client application
- Replicates them to the AI module
- Translates responses from the AI to the blockchain
When a client sends a request, the backend application collects data from the client application. After that, the backend application waits for confirmation from the event generated by the smart contract while at the same time checking the integrity of the requested AI service.
The AI model is saved separately, and its hash is used as a model ID and proof of integrity. Since the model hash is saved on-chain and the service’s ID is replicated through the request, the backend can check if the model matches its hash. This procedure guarantees the integrity of the system and protects clients from unauthorized changes to the AI model.
When all checks are passed, the backend calls the AI module, which makes a prediction based on the client data and the chosen neural network. This prediction is sent to the blockchain, and the smart contract creates an event directed to the client application.
Some bank services require saving client requests and data. In order to do so, we can connect our application with a database and copy the required information to it.
To sum up, a backend application performs these actions:
- Receives an AI service ID, data for processing, and confirmation from the blockchain
- Checks the integrity of AI models communicating with the smart contract
- Communicates with the AI module and forwards its response to the blockchain
Though the backend application seems to be the center of the system, it only facilitates communication between the smart contract and AI models and passes along received data. All of the main system logic is concentrated in the AI module and smart contract. Let’s take a look at these components.
The AI module contains functionality for:
- Loading neural network models (or other AI models)
- Training networks
- Updating networks
- Making predictions and conclusions
As we mentioned, every model can be saved separately, substituted with a new model, and loaded when needed. Such functionality gives a lot of flexibility, though it creates a security issue because an unauthorized person can change models. We solved this issue by saving the hash of the model in the smart contract so we can access it on-chain and compare it with the currently used model.
We can even have a specific pretrained model for each supported service.
Modules work in a simple way: they get new data to analyze and make a prediction. The main part of the module consists of training data. Financial organizations accumulate a lot of information about approved and denied loans, which can be used to train a neural network. New data received by the service can be used for additional training.
To review, an AI module:
- Contains neural network models, each connected to a particular service
- Gets loan service data and service IDs
- Provides decisions from the neural network regarding the chosen service
- Saves the model hash on-chain to ensure system integrity
The blockchain and smart contracts
We’ll create an Ethereum-based platform for our loaning service. An Ethereum smart contract is the true heart of the system. It accomplishes three key tasks:
- Ensures all requests and AI responses are saved on-chain
- Keeps IDs and hashes of all AI models
- Stores all loan service parameters
Clients can access their transaction history through a smart contract. Authorized bank employees can change loan conditions (such as the amount and term of a loan or the interest rate) in the smart contract.
For our use case, we’ll use a permissioned Ethereum-based blockchain within a separate network. It gives us the functionality of smart contracts and access to the block history, though trusted entities can validate the chain.
A blockchain may serve as the foundation for dApps developed upon the described service. For example, you can create a separate application for checking clients’ loan histories. Moreover, a simple token system can make a platform more attractive for clients and banking services. But the main purpose of the blockchain is to keep evidence of clients’ requests and AI decisions.
In our use case, a smart contract:
- Creates request IDs from the client data
- Keeps client IDs, request IDs, service data hashes, and AI responses
- Sends events to confirm received requests and loan decisions
- Keeps loan service IDs, hashes of models, and parameters of services
- Allows parameters to be changed only by authorized employees
We’ve discussed key elements of our architecture. Now we can start working on our AI model and smart contract.
The foundation of any AI model is data. In our case, we use a free dataset that contains a massive amount of data on accepted and declined loan requests. It allows banks to check if a loan is risky or has a high probability of being fully paid.
The first step in creating a model is preparing data. Our dataset contains records for 36- and 60-month loans. Since we can create separate models for different banking services, we may choose a single type of loan — for example, loans with a 60-month repayment period. Currently, the dataset contains over 100 parameters for each loan record. To develop a deep and accurate AI model, you'll need as many parameters as possible, nevertheless, we’ll narrow our choice to the most valuable:
loan_amnt— The loan amount requested by the client
int_rate— The interest rate (monthly or annual), which сan be provided in the client application by a bank as part of the service settings
installment— The monthly payment owed by the borrower; also a part of the service settings
grade— The loan grade calculated by the bank based on the client’s loan history and other data
emp_length— Employment length in years, as provided by the client
home_ownership— Home ownership information, as provided by the client
annual_inc— Annual income of the client
purpose— The purpose of the loan, as stated by the client in their application
inq_last_12m— The number of open credit lines, which can be calculated by the bank based on the client’s credit history
delinq_2yrs— The number of credit inquiries in the past 2 years, which can be calculated based on the number of requests already saved on-chain
loan_status— The loan status (approved or declined), as decided by the AI
The original dataset contains almost 1 million records. We’ll narrow our data to 20,000 records that meet specific criteria, without empty or undetermined columns. Also, we have to normalize the data so it’s ready for use in the neural network.
We’ll use the Python pandas (Panel Dataframe) library to handle this data:
After that procedure, we get a normalized dataset prepared for training. We have 20,000 records, of which we’ll use 80% for training (16,000 records) and 20% for testing (4,000 records).
After a bit of experimenting, we built a model that consists of five layers:
- The input layer contains 10 neurons, one per parameter.
- The first and second hidden layers have “relu” activation functions and contain 10 neurons each.
- The second hidden layer also provides a regularization for the input data as a form of model optimization.
- The third hidden layer has a hyperbolic tangent activation function and contains 5 neurons.
- The output layer consists of a single output neuron that provides the loan decision: 0 (loan declined) or 1 (loan accepted).
Here’s what our training model looks like:
Our model has been trained and saved to separate files: model.json and model.h5. We had 100 training epochs, and the neural network model showed 94% successful predictions on the test dataset. Now it’s ready to work with data from the client application:
The calculated result is sent to the appropriate request of the smart-contract.
At this point, we’ve described the complete process of creating the AI model: preparing the dataset, training the neural network, saving the model to a file, using the saved model on the received data. Now let’s move to the process of creating the smart contract.
A smart contract is the true center of our system. It contains all the connecting data, system control parameters, and evidence of requests and responses. Since we’re building an Ethereum-based system, we’ll write our smart contract in the Solidity language.
The smart contract should correspond with the architecture of our solution. It will have the following functionality:
- Allow the client to create a request. A request contains the ID of the AI service to be called and the data hash that service will analyze. We also need to include a request ID for request identification and integrity checks. For simplicity, we’ll use a request number as request ID. Requests will be represented as data structures. The smart contract will keep these structures in a mapping (clientRequests), so every user ID will be connected with an array of structures.
- Allow the AI service to pass a response. We’ll create a separate structure for making loan decisions. It will store AI responses and valuable parameters such as the term of validity and timestamp. All predictions are connected to requests and clients by their IDs in the second mapping (clientPredictions).
- Generate an appropriate event for the backend service and client application after a request is created and a prediction is made.
- Keep the connection between the model ID and its hash in the third mapping (aiModels) for data integrity.
- Provide security so only authorized employees can change global parameters and update AI model hashes.
5 Security Tips for Writing Smart Contracts
Our full smart contract looks like this:
The blockchain and AI are the two extremes of the technology spectrum, but their collaboration brings us the advantages of both technologies. Flexible AI solutions allow for different approaches to analyzing data and architecting banking services. A blockchain ecosystem with smart contracts provides security and reliability, especially when storing data.
In this article, we described a use case of blockchain and AI convergence for lending services. We used the blockchain for storing data and confirming requests, and used an AI module for making lending predictions.
Our team always stays up-to-date with technological innovations. Our developers are experts in both artificial intelligence and blockchain solutions. If your project requires AI and blockchain skills, challenge us with it!