Skip to content

Commit 8611ff8

Browse files
sathnindukgitbook-bot
authored andcommitted
GITBOOK-1: No subject
1 parent 041aadf commit 8611ff8

File tree

2 files changed

+38
-42
lines changed

2 files changed

+38
-42
lines changed

README.md

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1+
---
2+
icon: hand-wave
3+
---
4+
15
# ChatWithSQL
26

37
**ChatWithSQL** is a Python library that bridges the gap between natural language queries and SQL databases. Designed for reliability, security, and performance, ChatWithSQL allows developers to leverage advanced Language Learning Models (LLMs) like OpenAI, Gemini, and more to retrieve database data using simple, intuitive natural language prompts.
48

5-
69
## 🎯 Why ChatWithSQL?
710

811
A major risk of Text-to-SQL systems is the potential execution of arbitrary SQL queries, which can result in **unauthorized data access, security vulnerabilities, inefficient query performance, or incorrect query results**. Common mitigations include using restricted roles, read-only databases, and sandboxed environments. However, ChatWithSQL takes this one step further.
912

10-
1113
ChatWithSQL has implemented a **schema-based validation approach** to ensure that only SQL queries adhering to a predefined schema are generated and executed. This mechanism restricts the scope of data retrieval strictly within the defined parameters, effectively mitigating the risks of arbitrary or malicious queries. Each query is validated against the schema before execution, **guaranteeing compliance and eliminating unauthorized access**.
1214

13-
1415
This unique approach positions ChatWithSQL as a leader in secure and reliable, natural language-driven SQL data retrieval.
1516

16-
1717
## 🚀 Key Features
1818

19-
- **Natural Language to SQL**: Translate human-readable prompts into actionable SQL queries.
20-
- **Schema-Validated Queries**: Ensures only schema-defined queries are executed, mitigating arbitrary query risks.
21-
- **LLM Flexibility**: Seamless integration with multiple LLMs (Gemini, OpenAI, Ollama, LlamaAPI).
22-
- **Secure Execution**: Parameter validation and query sanitization to prevent SQL injection and unauthorized access.
23-
- **Dynamic Query Parameter Handling**: Automatically extracts, validates, and maps parameters to SQL placeholders.
24-
- **Database-Agnostic**: Compatible with any database supported by the URI connection format.
25-
- **Comprehensive Logging**: Provides detailed logs for easier debugging and monitoring.
26-
19+
* **Natural Language to SQL**: Translate human-readable prompts into actionable SQL queries.
20+
* **Schema-Validated Queries**: Ensures only schema-defined queries are executed, mitigating arbitrary query risks.
21+
* **LLM Flexibility**: Seamless integration with multiple LLMs (Gemini, OpenAI, Ollama, LlamaAPI).
22+
* **Secure Execution**: Parameter validation and query sanitization to prevent SQL injection and unauthorized access.
23+
* **Dynamic Query Parameter Handling**: Automatically extracts, validates, and maps parameters to SQL placeholders.
24+
* **Database-Agnostic**: Compatible with any database supported by the URI connection format.
25+
* **Comprehensive Logging**: Provides detailed logs for easier debugging and monitoring.
2726

2827
## 📦 Installation
2928

@@ -33,7 +32,6 @@ You can install ChatWithSQL using pip:
3332
pip install chatwithsql
3433
```
3534

36-
3735
## 🛠️ Setup and Usage
3836

3937
### 1. **Initialization**
@@ -75,7 +73,6 @@ response = chat_with_sql.execute_query(prompt)
7573
print(response)
7674
```
7775

78-
7976
## 🛡️ Security by Design
8077

8178
ChatWithSQL mitigates one of the largest risks of Text-to-SQL systems: **arbitrary query execution**. It employs schema-based validation to restrict query generation within pre-defined parameters. SQL queries are dynamically constructed and validated, ensuring:
@@ -84,20 +81,19 @@ ChatWithSQL mitigates one of the largest risks of Text-to-SQL systems: **arbitra
8481
2. Parameters are sanitized and validated against expected types.
8582
3. Arbitrary query execution by LLMs is entirely eliminated.
8683

87-
8884
## 🌐 Supported LLMs
8985

90-
- **OpenAI** (e.g., GPT-4, GPT-3.5)
91-
- **Gemini**
92-
- **LlamaAPI**
93-
- **Ollama**
94-
86+
* **OpenAI** (e.g., GPT-4, GPT-3.5)
87+
* **Gemini**
88+
* **LlamaAPI**
89+
* **Ollama**
9590

9691
## 🧰 API Reference
9792

9893
### **ChatWithSQL**
9994

10095
#### Constructor
96+
10197
```python
10298
ChatWithSQL(
10399
database_url: str,
@@ -107,29 +103,33 @@ ChatWithSQL(
107103
query_schema: Optional[List[Dict[str, Any]]] = None,
108104
)
109105
```
110-
- **`database_url`**: Connection URI for the database.
111-
- **`llm`**: Type of LLM to use (`gemini`, `openai`, `llama_api`, `ollama`).
112-
- **`model`**: LLM model name.
113-
- **`llm_api_key`**: API key for accessing the LLM.
114-
- **`query_schema`**: List of schema definitions, each with `description`, `name`, `sql`, and `params`.
106+
107+
* **`database_url`**: Connection URI for the database.
108+
* **`llm`**: Type of LLM to use (`gemini`, `openai`, `llama_api`, `ollama`).
109+
* **`model`**: LLM model name.
110+
* **`llm_api_key`**: API key for accessing the LLM.
111+
* **`query_schema`**: List of schema definitions, each with `description`, `name`, `sql`, and `params`.
115112

116113
#### Method: `execute_query(prompt: str) -> Dict[str, Any]`
114+
117115
Executes a natural language query and retrieves data.
118-
- **`prompt`**: Natural language request.
119-
- **Returns**: Query results as a dictionary.
120116

117+
* **`prompt`**: Natural language request.
118+
* **Returns**: Query results as a dictionary.
121119

122120
## 📝 Query Schema Format
123121

124122
The `query_schema` parameter ensures secure and structured interactions. Each schema item includes:
125-
- **`description`**: Human-readable description of the query.
126-
- **`name`**: Unique name of the query.
127-
- **`sql`**: SQL query template with placeholders (`?`) for parameters.
128-
- **`params`**: Dictionary defining parameters with:
129-
- `type`: Data type (`str`, `int`, `float`, `date`, `datetime`).
130-
- `default`: Specify the default value for the parameter or if it can be any value, indicate it as null.
123+
124+
* **`description`**: Human-readable description of the query.
125+
* **`name`**: Unique name of the query.
126+
* **`sql`**: SQL query template with placeholders (`?`) for parameters.
127+
* **`params`**: Dictionary defining parameters with:
128+
* `type`: Data type (`str`, `int`, `float`, `date`, `datetime`).
129+
* `default`: Specify the default value for the parameter or if it can be any value, indicate it as null.
131130

132131
Example:
132+
133133
```json
134134
[
135135
{
@@ -147,28 +147,24 @@ Example:
147147
]
148148
```
149149

150-
151150
## 🐛 Logging and Debugging
152151

153152
ChatWithSQL includes extensive logging for better observability:
154-
- Logs parameter validation errors.
155-
- Logs malformed prompts or unexpected results from the LLM.
156-
- Tracks query construction and database execution.
157153

158-
Enable logging by configuring Python’s `logging` module.
154+
* Logs parameter validation errors.
155+
* Logs malformed prompts or unexpected results from the LLM.
156+
* Tracks query construction and database execution.
159157

158+
Enable logging by configuring Python’s `logging` module.
160159

161160
## 🏗️ Contributing
162161

163162
Contributions are welcome! Please submit a pull request or open an issue on our [GitHub Repository](https://github.com/sathninduk/ChatWithSQL).
164163

165-
166164
## 📜 License
167165

168166
ChatWithSQL is open-source software licensed under the MIT License.
169167

170-
171168
## 🤝 Support
172169

173170
If you have any questions or issues, feel free to contact us at [hello@bysatha.com](mailto:hello@bysatha.com) or open a GitHub issue.
174-

SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Table of contents
22

3-
* [Page](README.md)
3+
* [ChatWithSQL](README.md)

0 commit comments

Comments
 (0)