https://github.com/vedanti-u/dbsense-ai
A quick and lightweight library to chat with databases. It uses schema over bulky data embedding for efficient and speedy interaction, ensuring data security.
https://github.com/vedanti-u/dbsense-ai
ai database db langchain ml npm npm-package open-ai openai oss postgresql sql typescript
Last synced: about 1 month ago
JSON representation
A quick and lightweight library to chat with databases. It uses schema over bulky data embedding for efficient and speedy interaction, ensuring data security.
- Host: GitHub
- URL: https://github.com/vedanti-u/dbsense-ai
- Owner: vedanti-u
- License: mit
- Created: 2024-02-02T18:21:50.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-20T19:24:17.000Z (almost 2 years ago)
- Last Synced: 2025-02-11T15:42:19.558Z (about 1 year ago)
- Topics: ai, database, db, langchain, ml, npm, npm-package, open-ai, openai, oss, postgresql, sql, typescript
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/dbsense-ai
- Size: 7.56 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README


   
  
[](https://github.com/Naereen/badges/)
[](https://npmjs.com/package/dbsense-ai)
[](https://www.npmjs.com/package/dbsense-ai)
[](https://www.npmjs.com/package/dbsense-ai)
[](https://GitHub.com/vedanti-u/DbSense-AI/graphs/contributors/)
https://github.com/user-attachments/assets/66115d65-1015-4875-a8a3-131095df8a80
### 📦 NPM Downloads
> **[`dbsense-ai`](https://www.npmjs.com/package/dbsense-ai)** has crossed **1,000+ downloads** on NPM! 🎉
> Thank you to the amazing developer community for the love and support! 🙌
## What is DbSenseAi
**DbSenseAI** is a fast and lightweight library that simplifies chatting with your database. Unlike traditional methods, it doesn't need to embed all your database data. Instead, it focuses only on the schema, making it efficient and quick.
**Features**:
- _**Efficient Schema Embedding**: Only embeds schema, skipping the need to embed all database data._
- _**Fast Database Interaction**: Enables quick communication with your database._
- _**Resource Optimization**: Saves resources by avoiding unnecessary data embedding._
- _**Versatile Integration**: Works well with various database systems._
## How DbSenseAi works ?
```mermaid
sequenceDiagram
participant User
participant App
participant LLM_Model
participant Database
User->>App: Provides data (Schema of tables)
App->>LLM_Model: Sends schema
LLM_Model->>App: Creates Vector Embedding
App->>App: Stores Vector Embedding in Local File
App->>User: Acknowledgement
User->>App: Asks query: "All students passed with above 80 marks"
App->>LLM_Model: Sends query with Vector Embedding
LLM_Model->>LLM_Model: Converts to SQL
LLM_Model->>App: Sends SQL
App->>Database: Sends SQL
Database->>Database: Processes SQL
Database-->>App: Returns response
App-->>User: Sends response
```
The sequence diagram illustrates the process flow of a system where a user provides data to DBSenseAi, which includes schema information of tables. DBSenseAi forwards this schema to the Language Model (LLM_Model), which generates Vector Embeddings. These embeddings are stored locally by DBSenseAi. When the user queries for students who passed with above 80 marks, DBSenseAi sends this query along with the embeddings to the LLM_Model, which converts it into SQL. The SQL is then forwarded to the Database, processed, and the response is sent back to DBSenseAi, which in turn delivers it to the user.
## Class Diagram
```mermaid
classDiagram
class User {
+ProvidesData()
+AsksQuery()
}
class LLMService {
-vectorStore: HNSWLib | undefined
-model: OpenAI
-vectorStorePath: string
-openAIEmbeddings: OpenAIEmbeddings
+createTable(sqlQueryForTable: string): void
+updateTable(sqlQueryForTable: string): void
-extractTableNameFromCreateQuery(sqlQueryForTable: string): string | null
-extractTableNameFromUpdateQuery(sqlQueryForTable: string): string | null
+createVectorEmbeddings(tableString: string): void
-checkFileExists(filePath: string): Promise
-deleteFile(filePath: string): Promise
}
class PromptService {
-vectorStore: HNSWLib | undefined
-model: OpenAI
-vectorStorePath: string
-openAIEmbeddings: OpenAIEmbeddings
-rawData: string
-jsonData: any
+createSqlQuery(question: string): void
+checkFileExists(filePath: string): Promise
+summarizeResponse(question: string, answer: any): void
+parseMessage(unformatedPrompt: string, ...args: string[]): string | undefined
}
class DBService {
-connection: dbconfig
-client: Client
+queryDatabase(inputQuery: string): Promise
+connect(): Promise
}
class DbSenseAi {
-dbService: DBService
-promptService: PromptService
-llmService: LLMService
+createTable(createQuery: string): Promise
+updateTable(updateQuery: string): Promise
+ask(question: string): Promise
}
class OpenAIEmbeddings {
// properties and methods
}
class OpenAI {
// properties and methods
}
class HNSWLib {
// properties and methods
}
class dbconfig {
// properties
}
class Client {
// properties and methods
}
class QueryResult {
// properties and methods
}
class QuestionResponse {
// properties and methods
}
User --> DbSenseAi : Uses
DbSenseAi --> LLMService : Uses
DbSenseAi --> PromptService : Uses
DbSenseAi --> DBService : Uses
PromptService --> OpenAIEmbeddings : Uses
PromptService --> OpenAI : Uses
LLMService --> OpenAIEmbeddings : Uses
LLMService --> OpenAI : Uses
LLMService --> HNSWLib : Uses
DBService --> dbconfig : Contains
DBService --> Client : Contains
DBService --> QueryResult : Returns
DbSenseAi --> QuestionResponse : Returns
```
## ⚡ Try DbSenseAi
## Prerequisites
- **Make**
> Install make on Linux
```bash
sudo apt install make
```
_Check version_
```bash
make -version
```
- ### **G++**
> Install G++ on Linux
```bash
sudo apt install g++
```
_Check version_
```bash
g++ --version
```
## Installation
```bash
npm i dbsense-ai
```
## Setting-up `.env` file
Your `.env` file should include
```bash
export OPENAI_API_KEY=
DB_DATABASE=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
```
_Once the package is installed, you can import the library using import or require approach:_
```javascript
var DbSenseAi = require("dbsense-ai");
```
##### Instanciate the DbSenseAi class
```javascript
const dbsenseai = new DbSenseAi();
```
## Usage
Add your _create table query_ inside the createTable() function
```javascript
await dbsenseai.createTable(
"CREATE TABLE cosmetics (brand VARCHAR(100) NOT NULL,product_type VARCHAR(100) NOT NULL,product_price NUMERIC(10, 2));"
);
```
Add _your prompt_ inside the ask() function
```javascript
let response = await dbsenseai.ask(
"Give me name of all brands sorted in ascending order of price"
);
```
_You can get the response as table and summary_
```javascript
console.table(response.table);
console.log(response.summary);
```
# 🤝 Contributing to Library
> [!NOTE]
> Contributing Guidelines
### Dependencies

 
 
### Prerequisites
> If you don't have git on your machine, [install it](https://docs.github.com/en/get-started/quickstart/set-up-git).
- #### **make**
Install make on Linux
```bash
$ sudo apt install make
```
_Check version_
```bash
$ make -version
```
- #### **G++**
Install G++ on Linux
```bash
$ sudo apt install g++
```
_Check version_
```bash
$ g++ --version
```
### Fork this repository
Forking
Fork this repository by clicking on the fork button on the top of this page. This will create a copy of this repository in your account.
### Clone the repository
Cloning

Now clone the forked repository to your machine. Go to your GitHub account, open the forked repository, click on the code button and then click the _copy to clipboard_ icon, this is the COPIED_URL.
> _Open a terminal and run the following git command:_
```git
git clone "COPIED_URL"
```
e.g : `git clone https://github.com/vedanti-u/db.ai.git`
### Install dependencies
```bash
npm install
```
---
### Create a branch
Branch naming conviction
Change to the repository directory on your computer (if you are not already there):
```bash
$ cd dbsense-ai
```
Now create a branch using the `git checkout` command:
```bash
$ git checkout -b new-branch-name
```
e.g : `git checkout -b llm-prompt-support`
**Name your branch according to the feature you are working on :**
e.g : you want to work on creating more llm prompt support, name your branch like `llm-prompt-support`
_(follow this naming convention i.e using "-" in between)_
### Make contribution to _Code_
#### Create a `.env` File with format
```bash
export OPENAI_API_KEY=
DB_DATABASE=
DB_HOST=
DB_PORT=
DB_USER=
DB_PASSWORD=
```
### Linking the library locally
```bash
rm -rf dist
tsc
npm link
npm link dbsense-ai
```
---
## Testing the library locally
```bash
node test/localLibrary.test.ts --env=.env
```
### Create a pull request
Creating pull requests
Once you have modified an existing file or added a new file to the project of your choice, you can stage it to your local repository, which we can do with the `git add` command. In our example, `filename.md`, we will type the following command.
$ git add filename.md
where filename is the file you have modified or created
If you are looking to add all the files you have modified in a particular directory, you can stage them all with the following command:
`git add .`
Or, alternatively, you can type `git add -all` for all new files to be staged.
Commiting the changes
git commit -m "Added a new prompt in prompts.json file"
To PUSH your branch to your remote main
$ git push --set-upstream origin your-branch-name
e.g : `$ git push --set-upstream origin optimise-binding`
Open Github
click on compare & pull request
write a description for your pull request specifing the changes you have made, title it and then, Click on create pull request
_your branch will be merged on code review_
### :octocat: Statistics
[](https://github.com/ellerbrock/open-source-badges/)



[](https://github.com/Naereen/badges/)
[](https://npmjs.com/)
[](https://npmjs.com/package/dbsense-ai)
[](https://www.npmjs.com/package/dbsense-ai)
[](https://www.npmjs.com/package/dbsense-ai)
[](https://GitHub.com/vedanti-u/DbSense-AI/graphs/contributors/)
[](https://GitHub.com/Naereen/StrapDown.js/commit/)
[](https://GitHub.com/vedanti-u/DbSense-AI/issues/)
[](https://GitHub.com/vedanti-u/DbSense-AI/issues?q=is%3Aissue+is%3Aclosed)
[](https://GitHub.com/vedanti-u/DbSense-AI/pulls?q=is%3Aclosed)
[](https://GitHub.com/vedanti-u/DbSense-AI/pulls?q=is%3Aclosed)
[](https://GitHub.com/vedanti-u/DbSense-AI/pulls?q=is%3Amerged)
[](https://github.com/vedanti-u/DbSense-AI/blob/master/LICENSE)
