Skip to content

Commit 56a94bc

Browse files
Hotfixes done
1 parent 5d7c24d commit 56a94bc

6 files changed

Lines changed: 59 additions & 33 deletions

File tree

.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
DEBUG=True
2-
OPENAI_API_KEY=your-openai-api-key-here
2+
GROQ_API_KEY=your-groq-api-key-here
33
SENTENCE_TRANSFORMER_MODEL=all-MiniLM-L6-v2
44

55
PGVECTOR_DB_NAME=llm_analytics

README.md

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,25 @@
2929
### Layout Structure
3030

3131
**Header**
32+
3233
- Application title: "LLM SQL Analytics"
3334
- Navigation: Dashboard, Query Interface, Schema Docs, Settings
3435

3536
**Main Content Area**
37+
3638
- Dashboard: Overview of connected databases and recent queries
3739
- Query Interface: Natural language input + SQL preview + results table
3840
- Schema Docs: List of uploaded documents with semantic search
3941
- Settings: Database connection configuration
4042

4143
**Footer**
44+
4245
- Version info and copyright
4346

4447
### Visual Design
4548

4649
**Color Palette**
50+
4751
- Primary: `#1E3A5F` (Deep Navy)
4852
- Secondary: `#3D5A80` (Slate Blue)
4953
- Accent: `#48CAE4` (Cyan)
@@ -56,11 +60,13 @@
5660
- Warning: `#FFC107` (Yellow)
5761

5862
**Typography**
63+
5964
- Headings: "Inter", sans-serif, 600 weight
6065
- Body: "Inter", sans-serif, 400 weight
6166
- Monospace (SQL): "JetBrains Mono", monospace
6267

6368
**Spacing**
69+
6470
- Base unit: 8px
6571
- Container max-width: 1200px
6672
- Card padding: 24px
@@ -69,23 +75,27 @@
6975
### Components
7076

7177
**Query Input Card**
78+
7279
- Textarea for natural language input
7380
- "Generate SQL" button (primary style)
7481
- "Execute" button (accent style)
7582
- SQL preview panel with syntax highlighting
7683

7784
**Results Table**
85+
7886
- Sortable columns
7987
- Pagination for large results
8088
- Export to CSV option
8189

8290
**Schema Doc Card**
91+
8392
- Document name
8493
- Upload date
8594
- Preview snippet
8695
- Semantic search input
8796

8897
**Database Connection Card**
98+
8999
- Connection name
90100
- Database type indicator
91101
- Status indicator (connected/disconnected)
@@ -98,33 +108,39 @@
98108
### Core Features
99109

100110
#### 4.1 Database Connection Management
111+
101112
- Add PostgreSQL database connections with connection details
102113
- Test database connectivity
103114
- Store connection configurations securely
104115
- Support multiple database connections
105116

106117
#### 4.2 Schema Introspection
118+
107119
- Automatically fetch table names and column information
108120
- Store schema in PostgreSQL with pgvector for semantic search
109121
- Support uploading schema documentation (PDF)
110122

111123
#### 4.3 Natural Language to SQL
124+
112125
- Accept natural language queries
113126
- Use LLM to convert natural language to SQL
114127
- Support SELECT, INSERT, UPDATE, DELETE operations
115128
- Include validation and error handling
116129

117130
#### 4.4 Semantic Schema Search
131+
118132
- Embed schema documentation using sentence-transformers
119133
- Store embeddings in pgvector
120134
- Search schema semantically to find relevant tables/columns
121135

122136
#### 4.5 Query Execution
137+
123138
- Execute generated SQL against target database
124139
- Return formatted results
125140
- Handle errors gracefully with user-friendly messages
126141

127142
#### 4.6 Query History
143+
128144
- Store all queries with timestamps
129145
- View past queries and their results
130146
- Re-run previous queries
@@ -192,20 +208,20 @@ QueryHistory
192208

193209
## 6. API Endpoints
194210

195-
| Method | Endpoint | Description |
196-
|--------|----------|-------------|
197-
| GET | /api/connections/ | List all database connections |
198-
| POST | /api/connections/ | Add new database connection |
199-
| GET | /api/connections/{id}/ | Get connection details |
200-
| PUT | /api/connections/{id}/ | Update connection |
201-
| DELETE | /api/connections/{id}/ | Delete connection |
202-
| POST | /api/connections/{id}/test/ | Test connection |
203-
| GET | /api/connections/{id}/schema/ | Get database schema |
204-
| POST | /api/query/ | Execute natural language query |
205-
| GET | /api/history/ | Get query history |
206-
| POST | /api/docs/ | Upload schema document |
207-
| GET | /api/docs/ | List schema documents |
208-
| POST | /api/docs/search/ | Semantic search in documents |
211+
| Method | Endpoint | Description |
212+
| ------ | ----------------------------- | ------------------------------ |
213+
| GET | /api/connections/ | List all database connections |
214+
| POST | /api/connections/ | Add new database connection |
215+
| GET | /api/connections/{id}/ | Get connection details |
216+
| PUT | /api/connections/{id}/ | Update connection |
217+
| DELETE | /api/connections/{id}/ | Delete connection |
218+
| POST | /api/connections/{id}/test/ | Test connection |
219+
| GET | /api/connections/{id}/schema/ | Get database schema |
220+
| POST | /api/query/ | Execute natural language query |
221+
| GET | /api/history/ | Get query history |
222+
| POST | /api/docs/ | Upload schema document |
223+
| GET | /api/docs/ | List schema documents |
224+
| POST | /api/docs/search/ | Semantic search in documents |
209225

210226
---
211227

@@ -220,4 +236,4 @@ QueryHistory
220236
7. ✓ System performs semantic search over schema docs
221237
8. ✓ Query history is maintained
222238
9. ✓ Web interface is responsive and functional
223-
10. ✓ Error handling provides helpful feedback
239+
10. ✓ Error handling provides helpful feedback

core/services.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import psycopg2
55
from psycopg2.extras import RealDictCursor
66
from django.conf import settings
7-
from langchain_openai import ChatOpenAI
7+
from langchain_groq import ChatGroq
88
from langchain.prompts import PromptTemplate
99
from langchain.chains import LLMChain
1010
from sentence_transformers import SentenceTransformer
@@ -122,13 +122,13 @@ class SQLGenerationService:
122122
@classmethod
123123
def get_llm(cls):
124124
if cls._llm is None:
125-
api_key = os.environ.get('OPENAI_API_KEY', '')
125+
api_key = os.environ.get('GROQ_API_KEY', '')
126126
if not api_key:
127-
raise ValueError("OPENAI_API_KEY not set")
128-
cls._llm = ChatOpenAI(
129-
model='gpt-4',
127+
raise ValueError("GROQ_API_KEY not set")
128+
cls._llm = ChatGroq(
129+
model='llama-3.3-70b-versatile',
130130
temperature=0,
131-
openai_api_key=api_key
131+
api_key=api_key
132132
)
133133
return cls._llm
134134

@@ -139,21 +139,28 @@ def generate_sql(cls, natural_language: str, schema_info: str) -> str:
139139
prompt = PromptTemplate(
140140
input_variables=["question", "schema"],
141141
template="""
142-
You are a SQL expert. Given a natural language question and a database schema, generate a valid PostgreSQL query.
142+
You are a SQL expert. Given a natural language question and a database schema, generate a valid PostgreSQL query.
143143
144-
Schema:
145-
{schema}
144+
Schema:
145+
{schema}
146146
147-
Question: {question}
147+
Question: {question}
148148
149-
Generate only the SQL query, no explanation. If the question cannot be answered with the given schema, generate a query that returns an appropriate empty result.
150-
"""
151-
)
149+
Generate only the SQL query and format it as string, no explanation. If the question cannot be answered with the given schema, generate a query that returns an appropriate empty result.
150+
"""
151+
)
152152

153153
chain = LLMChain(llm=llm, prompt=prompt)
154154
result = chain.run(question=natural_language, schema=schema_info)
155-
156-
return result.strip()
155+
print('LLM Output:', result)
156+
sql = result.strip()
157+
if sql.lower().startswith('sql'):
158+
sql = sql[3:].strip()
159+
if sql.lower().startswith('```sql'):
160+
sql = sql[6:].strip()
161+
if sql.endswith('```'):
162+
sql = sql[:-3].strip()
163+
return sql
157164

158165
@classmethod
159166
def format_schema_for_prompt(cls, schema: Dict[str, Any]) -> str:

llm_sql_analytics/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22
from pathlib import Path
3+
from dotenv import load_dotenv
4+
5+
load_dotenv()
36

47
BASE_DIR = Path(__file__).resolve().parent.parent
58

@@ -84,7 +87,6 @@
8487
],
8588
}
8689

87-
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')
8890
SENTENCE_TRANSFORMER_MODEL = os.environ.get('SENTENCE_TRANSFORMER_MODEL', 'all-MiniLM-L6-v2')
8991

9092
PGVECTOR_CONNECTION = {

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ langchain>=0.1.0
55
langchain-core>=0.1.0
66
langchain-text-splitters>=0.3.0
77
langchain-openai>=0.1.0
8+
langchain-groq>=0.1.0
89
sentence-transformers>=2.2.2
910
python-dotenv>=1.0.0
1011
PyPDF2>=3.0.0

templates/core/query.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ <h3 class="card-title">Query Result</h3>
5050

5151
{% if query_result.success %}
5252
<div class="alert alert-success">
53-
Query executed successfully. {{ query_result.row_count|default:query_result.rows_affected|default:0 }} row(s) returned.
53+
Query executed successfully. {% if query_result.row_count %}{{ query_result.row_count }}{% elif query_result.rows_affected %}{{ query_result.rows_affected }}{% else %}0{% endif %} row(s) returned.
5454
</div>
5555

5656
{% if query_result.rows %}

0 commit comments

Comments
 (0)