dbsense-ai
v1.2.3
Published
A quick and lightweight library to chat with databases.
Downloads
40
Readme
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 ?
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
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<boolean>
-deleteFile(filePath: string): Promise<void>
}
class PromptService {
-vectorStore: HNSWLib | undefined
-model: OpenAI
-vectorStorePath: string
-openAIEmbeddings: OpenAIEmbeddings
-rawData: string
-jsonData: any
+createSqlQuery(question: string): void
+checkFileExists(filePath: string): Promise<boolean>
+summarizeResponse(question: string, answer: any): void
+parseMessage(unformatedPrompt: string, ...args: string[]): string | undefined
}
class DBService {
-connection: dbconfig
-client: Client
+queryDatabase(inputQuery: string): Promise<QueryResult>
+connect(): Promise<void>
}
class DbSenseAi {
-dbService: DBService
-promptService: PromptService
-llmService: LLMService
+createTable(createQuery: string): Promise<boolean>
+updateTable(updateQuery: string): Promise<boolean>
+ask(question: string): Promise<QuestionResponse>
}
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
sudo apt install make
Check version
make -version
G++
Install G++ on Linux
sudo apt install g++
Check version
g++ --version
Installation
npm i dbsense-ai
Setting-up .env
file
Your .env
file should include
export OPENAI_API_KEY=<YOUR_OPENAI_KEY>
DB_DATABASE=<YOUR_DATABASE_NAME>
DB_HOST=<YOUR_DATABASE_HOST>
DB_PORT=<YOUR_DATABASE_PORT>
DB_USER=<YOUR_DATABASE_USER>
DB_PASSWORD=<YOUR_DATABASE_PASSWORD>
Once the package is installed, you can import the library using import or require approach:
var DbSenseAi = require("dbsense-ai");
Instanciate the DbSenseAi class
const dbsenseai = new DbSenseAi();
Usage
Add your create table query inside the createTable() function
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
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
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.
make
$ sudo apt install make
Check version
$ make -version
G++
$ sudo apt install g++
Check version
$ g++ --version
Fork this repository
Clone the repository
Open a terminal and run the following git command:
git clone "COPIED_URL"
e.g : git clone https://github.com/vedanti-u/db.ai.git
Install dependencies
npm install
Create a branch
$ cd dbsense-ai
Now create a branch using the git checkout
command:
$ 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
export OPENAI_API_KEY=<YOUR_OPENAI_KEY>
DB_DATABASE=<YOUR_DATABASE_NAME>
DB_HOST=<YOUR_DATABASE_HOST>
DB_PORT=<YOUR_DATABASE_PORT>
DB_USER=<YOUR_DATABASE_USER>
DB_PASSWORD=<YOUR_DATABASE_PASSWORD>
Linking the library locally
rm -rf dist
tsc
npm link
npm link dbsense-ai
Testing the library locally
node test/localLibrary.test.ts --env=.env
Create a pull request
$ 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.
e.g : $ git push --set-upstream origin optimise-binding
your branch will be merged on code review