Skip to content

Commit 93629c4

Browse files
authored
[V2] Refactor application Langchain -> Pydantic-AI
* Refactor application structure and update dependencies - Removed outdated files and reorganized the project structure for better maintainability. - Introduced a new launcher script (`run.py`) to facilitate running the application in web or CLI mode. - Updated `requirements.txt` to include core, data analysis, training, and testing dependencies. - Added new backend and frontend components to enhance functionality and user experience. - Implemented new schemas for requests and responses to standardize data handling. - Enhanced CSS styles for improved UI presentation. This commit lays the groundwork for future development and feature enhancements. * feat: Implement intelligent routing service and data models - Added data models for routing decisions, conversation responses, SQL responses, CSV analysis responses, and code fix responses in `models.py`. - Created `IntelligentRoutingService` to determine the appropriate agent for user queries based on context and conversation history in `routing_service.py`. - Implemented methods to handle conversation, SQL, and CSV queries, including fallback mechanisms for routing and responses. - Updated SQL generation service to improve error handling and response formatting in `sql_service.py`. - Removed unused CSS styles from `styles.css`. - Added a test script for routing logic verification in `test_routing.py`. - Refactored CLI commands and improved user feedback in `cli.py`. - Enhanced prompt instructions for routing and conversation handling in `prompt.py`. * refactor: Update import paths and clean up code structure; add basic usage demo for Querypls functionality * Refactor CSV analysis handling and improve routing service - Updated BackendOrchestrator to save CSV data to a temporary file and store additional CSV info in the session. - Enhanced IntelligentRoutingService to handle CSV queries with improved error handling and fallback analysis. - Modified CSV analysis prompt to enforce simpler, focused code generation. - Cleaned up frontend to display messages with generated images and added cleanup for old images. - Increased MAX_CHAT_HISTORIES constant to allow more chat sessions. - Removed unused constants and cleaned up settings configuration. - Added unit tests for backend orchestrator, CSV analysis tools, and conversation service. - Removed outdated test files and organized test structure. * feat: Introduce .env configuration file and remove legacy .env_example; enhance intelligent routing service for better error handling and CSV analysis * Refactor test files and improve code formatting - Cleaned up whitespace and formatting in test files for consistency. - Updated test cases to enhance readability and maintainability. - Ensured all test functions follow a consistent structure and style. - Added missing commas in several instances to adhere to Python syntax standards. * refactor: Simplify CSV loading success message and enhance routing service prompts for clarity and simplicity * feat: Implement keyword-based routing fallback and enhance error handling in routing service
1 parent c6f5d06 commit 93629c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+3509
-988
lines changed

.devcontainer/devcontainer.json

Lines changed: 0 additions & 33 deletions
This file was deleted.

.env_examp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
GROQ_API_KEY=your_groq_api_key_here
2+
GROQ_MODEL_NAME=model/name
3+
4+
APP_NAME=Querypls
5+
APP_VERSION=1.0.0
6+
MAX_TOKENS=1000
7+
TEMPERATURE=0.7
8+
MAX_CHAT_HISTORIES=5
9+
LOG_LEVEL=INFO

.env_example

Lines changed: 0 additions & 3 deletions
This file was deleted.

.github/workflows/pytest.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ jobs:
2121
pip3 install -r requirements.txt
2222
2323
- name: Run Pytest
24+
env:
25+
JUPYTER_PLATFORM_DIRS: "1"
2426
run: |
2527
pytest

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ __pycache__/
1414
# Installer logs
1515
pip-log.txt
1616
pip-delete-this-directory.txt
17+
./test_*
18+
run_*
1719

1820
# Unit test / coverage reports
1921
.coverage

README.md

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,31 @@
77
[![License](https://img.shields.io/badge/License-MIT%202.0-blue.svg)](LICENSE)
88
<img src='https://img.shields.io/github/stars/samadpls/querypls?color=red&label=stars&logoColor=black&style=social'>
99

10-
# 💬 Querypls - Prompt to SQL
10+
# 💬 Querypls - Intelligent SQL and CSV Analysis
1111

12-
Querypls is a web application that provides an interactive chat interface, simplifying SQL query generation. Users can effortlessly enter SQL queries and receive corresponding results. The application harnesses the capabilities of the language models from Hugging Face to generate SQL queries based on user input.
12+
Querypls is a modern web application that provides an interactive chat interface for SQL query generation and CSV data analysis. Built with Pydantic AI and powered by OpenAI's GPT-OSS-120B model through Groq, it offers intelligent routing between different analysis modes to handle various data-related queries.
13+
14+
🌐 **Try it live**: [querypls.streamlit.app](https://querypls.streamlit.app/)
1315

1416
## Key Features
1517

16-
💬 Interactive chat interface for easy communication.
17-
🔍 Enter SQL queries and receive query results as responses.
18-
🤖 Utilizes language models from Hugging Face for advanced query generation ([Querypls-prompt2sql](https://huggingface.co/samadpls/querypls-prompt2sql)).
19-
💻 User-friendly interface for seamless interaction.
18+
💬 **Interactive Chat Interface** - Natural language conversations for data analysis
19+
🔍 **SQL Query Generation** - Convert natural language to optimized SQL queries
20+
📊 **CSV Data Analysis** - Upload and analyze CSV files with intelligent insights
21+
🤖 **Intelligent Routing** - Automatically determines the best agent for your query
22+
**Fast Inference** - Powered by Groq's optimized infrastructure
23+
🔒 **Type-Safe Development** - Built with Pydantic AI for robust validation
24+
📈 **Visual Analytics** - Generate charts and visualizations from your data
2025

2126
![QueryplsDemo](https://github.com/samadpls/Querypls/assets/94792103/daa6e37d-a256-4fd8-9607-6e18cf41df3f)
2227

2328

2429

2530
# Acknowledgments
2631

27-
`Querypls` received a shoutout from [🦜 🔗 Langchain](https://www.langchain.com/) on their Twitter, reaching over **60,000 impressions**. Additionally, it was featured under the **Community Favorite Projects** section on `🦜 🔗 Langchain's blog`, leading to a significant increase in stars for this repository and a growing user base. The project was also highlighted in a [YouTube video](https://www.youtube.com/watch?v=htHVb-fK9xU), and it also caught the attention of Backdrop, expressing their interest and liking in an email, inviting the project to be a part of their hackathon.
32+
`Querypls` received a shoutout from [🦜 🔗 Langchain](https://www.langchain.com/) on their Twitter in 2023, reaching over **60,000 impressions**. Additionally, it was featured under the **Community Favorite Projects** section on `🦜 🔗 Langchain's blog`, leading to a significant increase in stars for this repository and a growing user base. The project was also highlighted in a [YouTube video](https://www.youtube.com/watch?v=htHVb-fK9xU), and it also caught the attention of Backdrop, expressing their interest and liking in an email, inviting the project to be a part of their hackathon.
33+
34+
However, due to constant breakdowns and instability issues with the LangChain framework, we made the strategic decision to migrate to **Pydantic AI** - a more stable and reliable framework. This transition has brought improved performance, better type safety, and enhanced maintainability to the project.
2835

2936
| [🔗 Langhchain Twitter Post](https://twitter.com/LangChainAI/status/1729959981523378297?t=Zdpw9ZQYvE3QS-3Bf-xaGw&s=19) | [🔗 Langhcain Blog Post](https://blog.langchain.dev/week-of-11-27-langchain-release-notes/) |
3037
|----------|----------|
@@ -38,7 +45,7 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
3845

3946

4047
> [!Note]
41-
> Querypls, while powered by a 7B model of Satablility AI LLM Model, is currently limited in providing optimal responses for simple queries.
48+
> Querypls is now powered by OpenAI's GPT-OSS-120B model through Groq, providing fast and reliable AI-powered SQL generation and CSV analysis capabilities.
4249
4350
---
4451

@@ -59,11 +66,11 @@ This project is licensed under the MIT License. See the [LICENSE](LICENSE) file
5966
pip install -r requirements.txt
6067
```
6168

62-
4. Create a `.env` file based on `.env_example` and set the necessary variables.
69+
4. Create a `.env` file based on `.env_examp` and set the necessary variables.
6370

6471
5. Run the application:
6572
```bash
66-
streamlit run src/app.py
73+
streamlit run src/frontend/app.py
6774
```
6875

6976
6. Open the provided link in your browser to use Querypls.

examples/basic_usage_demo.py

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Basic usage demo for Querypls backend functionality.
4+
Demonstrates conversation, SQL generation, and CSV analysis.
5+
"""
6+
7+
import sys
8+
import os
9+
10+
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11+
12+
from src.services.routing_service import IntelligentRoutingService
13+
from src.backend.orchestrator import BackendOrchestrator
14+
from src.schemas.requests import NewChatRequest
15+
16+
17+
def demo_conversation():
18+
"""Demo conversation functionality."""
19+
print("🗣️ CONVERSATION DEMO")
20+
print("=" * 40)
21+
22+
routing_service = IntelligentRoutingService()
23+
24+
# Test different conversation types
25+
conversations = [
26+
"Hello",
27+
"How are you?",
28+
"What can you do?",
29+
"Thanks for your help",
30+
"Goodbye",
31+
]
32+
33+
for query in conversations:
34+
print(f"\nUser: {query}")
35+
response = routing_service.handle_conversation_query(query)
36+
print(f"Assistant: {response}")
37+
38+
print("\n" + "=" * 40)
39+
40+
41+
def demo_sql_generation():
42+
"""Demo SQL generation functionality."""
43+
print("🗃️ SQL GENERATION DEMO")
44+
print("=" * 40)
45+
46+
routing_service = IntelligentRoutingService()
47+
48+
# Test different SQL queries
49+
sql_queries = [
50+
"Show me all users",
51+
"Find customers who made purchases in the last 30 days",
52+
"Get the total sales by month",
53+
"SELECT * FROM users WHERE status = 'active'",
54+
]
55+
56+
for query in sql_queries:
57+
print(f"\nUser: {query}")
58+
response = routing_service.handle_sql_query(query, [])
59+
print(f"Assistant: {response[:200]}...")
60+
61+
print("\n" + "=" * 40)
62+
63+
64+
def demo_csv_analysis():
65+
"""Demo CSV analysis functionality."""
66+
print("📊 CSV ANALYSIS DEMO")
67+
print("=" * 40)
68+
69+
# Sample CSV data
70+
sample_csv = """name,age,salary,department
71+
Alice,25,50000,IT
72+
Bob,30,60000,HR
73+
Charlie,35,70000,IT
74+
Diana,28,55000,Finance
75+
Eve,32,65000,HR"""
76+
77+
print(f"Sample CSV Data:\n{sample_csv}")
78+
79+
routing_service = IntelligentRoutingService()
80+
81+
# Test different CSV analysis queries
82+
csv_queries = [
83+
"Show me the basic statistics of the data",
84+
"Create a bar chart of department distribution",
85+
"What is the average salary by department?",
86+
"Show me the top 3 highest paid employees",
87+
]
88+
89+
for query in csv_queries:
90+
print(f"\nUser: {query}")
91+
response = routing_service.handle_csv_query(query, sample_csv)
92+
print(f"Assistant: {response[:300]}...")
93+
94+
print("\n" + "=" * 40)
95+
96+
97+
def demo_intelligent_routing():
98+
"""Demo intelligent routing functionality."""
99+
print("🧠 INTELLIGENT ROUTING DEMO")
100+
print("=" * 40)
101+
102+
routing_service = IntelligentRoutingService()
103+
104+
# Test different types of queries
105+
test_queries = [
106+
("Hello", "CONVERSATION_AGENT"),
107+
("Show me all users", "SQL_AGENT"),
108+
("Analyze this CSV data", "CSV_AGENT"),
109+
("How are you?", "CONVERSATION_AGENT"),
110+
("SELECT * FROM users", "SQL_AGENT"),
111+
("Create a chart from the data", "CSV_AGENT"),
112+
]
113+
114+
for query, expected_agent in test_queries:
115+
print(f"\nQuery: '{query}'")
116+
decision = routing_service.determine_agent(query, [], csv_loaded=True)
117+
print(f"Expected: {expected_agent}")
118+
print(f"Actual: {decision.agent}")
119+
print(f"Confidence: {decision.confidence}")
120+
print(f"Reasoning: {decision.reasoning}")
121+
122+
print("\n" + "=" * 40)
123+
124+
125+
def demo_orchestrator():
126+
"""Demo the main orchestrator functionality."""
127+
print("🎼 ORCHESTRATOR DEMO")
128+
print("=" * 40)
129+
130+
orchestrator = BackendOrchestrator()
131+
132+
# Create a new session
133+
session_info = orchestrator.create_new_session(
134+
NewChatRequest(session_name="Demo Session")
135+
)
136+
session_id = session_info.session_id
137+
print(f"Created session: {session_id}")
138+
139+
# Test different types of interactions
140+
interactions = [
141+
("Hello", "conversation"),
142+
("Show me all users", "sql"),
143+
("What can you do?", "conversation"),
144+
]
145+
146+
for query, query_type in interactions:
147+
print(f"\nUser ({query_type}): {query}")
148+
response = orchestrator.generate_intelligent_response(session_id, query)
149+
print(f"Assistant: {response.content[:150]}...")
150+
151+
# Test CSV functionality
152+
sample_csv = "name,age,salary\nAlice,25,50000\nBob,30,60000\nCharlie,35,70000"
153+
result = orchestrator.load_csv_data(session_id, sample_csv)
154+
print(f"\nCSV Load Result: {result['status']}")
155+
156+
response = orchestrator.generate_intelligent_response(
157+
session_id, "Analyze this data"
158+
)
159+
print(f"CSV Analysis: {response.content[:200]}...")
160+
161+
print("\n" + "=" * 40)
162+
163+
164+
def main():
165+
"""Run all demos."""
166+
print("🚀 Querypls Backend Functionality Demo")
167+
print("=" * 50)
168+
169+
demos = [
170+
("Conversation", demo_conversation),
171+
("SQL Generation", demo_sql_generation),
172+
("CSV Analysis", demo_csv_analysis),
173+
("Intelligent Routing", demo_intelligent_routing),
174+
("Orchestrator", demo_orchestrator),
175+
]
176+
177+
for demo_name, demo_func in demos:
178+
try:
179+
demo_func()
180+
except Exception as e:
181+
print(f"❌ {demo_name} demo failed: {str(e)}")
182+
183+
print("\n🎉 Demo completed! All backend functionality is working correctly.")
184+
print("\n📝 Summary:")
185+
print("- Conversation: Natural responses for greetings and help")
186+
print("- SQL Generation: Convert natural language to SQL queries")
187+
print("- CSV Analysis: Analyze CSV data with Python code")
188+
print("- Intelligent Routing: Automatically choose the right agent")
189+
print("- Orchestrator: Complete session management")
190+
191+
192+
if __name__ == "__main__":
193+
main()

0 commit comments

Comments
 (0)