Skip to content

Commit 6d0de36

Browse files
authored
Merge pull request #2 from eosho/feature/cleanup
Release 12-30-2025
2 parents 9b76c91 + a51bfcf commit 6d0de36

17 files changed

Lines changed: 285 additions & 312 deletions

‎README.md‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
<div align="center">
2+
<div align="left">
33

44
```diff
55
+ ╔╦╗╔═╗╔╦╗╔═╗ ╔═╗╔═╗╔═╗╔╗╔╔╦╗
66
+ ║║╠═╣ ║ ╠═╣ ╠═╣║ ╦║╣ ║║║ ║
77
+ ═╩╝╩ ╩ ╩ ╩ ╩ ╩ ╩╚═╝╚═╝╝╚╝ ╩
8-
+
9-
+ Natural Language → SQL Query Agent
8+
9+
[ Natural Language → SQL Query Agent ]
1010
```
1111

1212
</div>
@@ -64,11 +64,11 @@ Generates, validates, and executes SQL queries with retry logic.
6464
### Installation
6565

6666
```bash
67-
git clone <repository-url>
67+
git clone https://github.com/eosho/langchain_data_agent
6868
cd langchain_data_agent
69-
uv sync
69+
uv sync --all-extras
7070
cp .env.example .env
71-
# Edit .env with your Azure OpenAI credentials
71+
# Edit .env with your values
7272
```
7373

7474
### CLI Usage
@@ -238,7 +238,7 @@ The platform includes built-in configuration for these databases:
238238
| PostgreSQL | `postgres` | postgres |
239239
| Azure SQL | `azure_sql` | tsql |
240240
| Azure Synapse | `synapse` | tsql |
241-
| Azure Cosmos DB | `cosmos` | tsql |
241+
| Azure Cosmos DB | `cosmos` | cosmosdb |
242242
| Databricks SQL | `databricks` | databricks |
243243
| Google BigQuery | `bigquery` | bigquery |
244244
| MySQL | `mysql` | mysql |
@@ -250,13 +250,10 @@ The platform includes built-in configuration for these databases:
250250

251251
```bash
252252
# Format and lint
253-
uv run poe format
253+
uv run pre-commit run --all-files
254254

255255
# Run tests
256256
uv run pytest
257-
258-
# Type check
259-
uv run basedpyright
260257
```
261258

262259
## License

‎docs/CONFIGURATION.md‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ data_agents:
3535
blocked_functions:
3636
- pg_sleep
3737
- pg_read_file
38-
code_interpreter:
39-
enabled: true
4038
system_prompt: |
4139
You are an SQL assistant...
4240
{schema_context}
@@ -58,22 +56,21 @@ data_agents:
5856
5957
## Code Interpreter (Data Visualization)
6058
61-
Enable the code interpreter to generate charts and visualizations from query results. When enabled, the LLM can detect visualization intent (e.g., "show me a chart", "visualize", "plot") and generate matplotlib code to create charts.
59+
The data agent can generate charts and visualizations from query results. When the LLM detects visualization intent (e.g., "show me a chart", "visualize", "plot"), it generates matplotlib code to create charts.
6260
63-
```yaml
64-
code_interpreter:
65-
enabled: true
66-
azure_sessions_endpoint: ${AZURE_SESSIONS_POOL_ENDPOINT}
67-
```
61+
Visualization is **automatically enabled** - no YAML configuration needed. The executor is selected based on environment:
6862
69-
| Setting | Description | Default |
70-
|---------|-------------|---------|
71-
| `enabled` | Enable/disable visualization generation | `false` |
72-
| `azure_sessions_endpoint` | Azure Container Apps session pool management endpoint URL | - |
63+
| Environment | Executor | Use Case |
64+
|-------------|----------|----------|
65+
| `AZURE_SESSIONS_POOL_ENDPOINT` set | Azure Sessions | Production (secure, Hyper-V isolation) |
66+
| Not set | Local executor | Development (no sandboxing) |
7367

74-
**Note:** Visualization requires Azure Container Apps Dynamic Sessions for secure, isolated code execution.
68+
```bash
69+
# Production: Set the Azure Sessions endpoint
70+
export AZURE_SESSIONS_POOL_ENDPOINT="https://eastus.dynamicsessions.io/subscriptions/.../sessionPools/..."
71+
```
7572

76-
See [VISUALIZATION.md](VISUALIZATION.md) for complete setup instructions, architecture details, and troubleshooting.
73+
See [VISUALIZATION.md](VISUALIZATION.md) for Azure setup instructions and troubleshooting.
7774

7875
## SQL Validation
7976

‎docs/VISUALIZATION.md‎

Lines changed: 29 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -27,84 +27,17 @@ Visualization requires Azure Container Apps Dynamic Sessions. This provides:
2727

2828
## Azure Setup
2929

30-
### 1. Create a Container Apps Environment
30+
Follow the [Azure Container Apps Dynamic Sessions with LangChain tutorial](https://learn.microsoft.com/en-us/azure/container-apps/sessions-tutorial-langchain) to:
3131

32-
If you don't already have one:
32+
1. Create a Container Apps session pool
33+
2. Get the pool management endpoint
34+
3. Assign the `Azure ContainerApps Session Executor` role to your identity
3335

34-
```bash
35-
az containerapp env create \
36-
--name aca-env \
37-
--resource-group rg-data-agent \
38-
--location eastus
39-
```
40-
41-
### 2. Create the Session Pool
42-
43-
```bash
44-
az containerapp sessionpool create \
45-
--name session-pool-viz \
46-
--resource-group rg-data-agent \
47-
--container-type PythonLTS \
48-
--max-sessions 100 \
49-
--cooldown-period 300 \
50-
--location eastus
51-
```
52-
53-
**Parameters:**
54-
- `--container-type PythonLTS`: Python runtime with common data science packages
55-
- `--max-sessions`: Maximum concurrent sessions
56-
- `--cooldown-period`: Seconds before idle session is terminated
57-
58-
### 3. Get the Pool Management Endpoint
59-
60-
```bash
61-
az containerapp sessionpool show \
62-
--name session-pool-viz \
63-
--resource-group rg-data-agent \
64-
--query "properties.poolManagementEndpoint" -o tsv
65-
```
66-
67-
This returns a URL like:
36+
Once complete, you'll have an endpoint URL like:
6837
```
6938
https://eastus.dynamicsessions.io/subscriptions/<sub>/resourceGroups/<rg>/sessionPools/<pool>
7039
```
7140

72-
### 4. Assign the Executor Role
73-
74-
Grant your identity permission to execute code in the session pool:
75-
76-
```bash
77-
# Get your user ID
78-
USER_ID=$(az ad signed-in-user show --query id -o tsv)
79-
80-
# Get the session pool resource ID
81-
POOL_ID=$(az containerapp sessionpool show \
82-
--name session-pool-viz \
83-
--resource-group rg-data-agent \
84-
--query id -o tsv)
85-
86-
# Assign the role
87-
az role assignment create \
88-
--role "Azure ContainerApps Session Executor" \
89-
--assignee $USER_ID \
90-
--scope $POOL_ID
91-
```
92-
93-
**Note:** For service principals or managed identities, replace `$USER_ID` with the appropriate object ID.
94-
95-
### 5. Install the SDK
96-
97-
```bash
98-
pip install langchain-azure-dynamic-sessions
99-
```
100-
101-
Or add to your `pyproject.toml`:
102-
```toml
103-
dependencies = [
104-
"langchain-azure-dynamic-sessions>=0.1.0",
105-
]
106-
```
107-
10841
## Configuration
10942

11043
### Environment Variable
@@ -120,23 +53,16 @@ Or in `.env`:
12053
AZURE_SESSIONS_POOL_ENDPOINT=https://eastus.dynamicsessions.io/subscriptions/.../sessionPools/...
12154
```
12255

123-
### YAML Configuration
56+
### Executor Selection
12457

125-
Enable visualization in your agent config:
58+
The system automatically selects the executor based on environment:
12659

127-
```yaml
128-
data_agents:
129-
- name: "sales_agent"
130-
# ... other config ...
131-
code_interpreter:
132-
enabled: true
133-
azure_sessions_endpoint: ${AZURE_SESSIONS_POOL_ENDPOINT}
134-
```
60+
| `AZURE_SESSIONS_POOL_ENDPOINT` | Executor | Use Case |
61+
|-------------------------------|----------|----------|
62+
| Set | Azure Sessions | Production (secure, Hyper-V isolation) |
63+
| Not set | Local Python REPL | Development (fast, no sandboxing) |
13564

136-
| Setting | Description | Default |
137-
|---------|-------------|---------|
138-
| `enabled` | Enable/disable visualization | `false` |
139-
| `azure_sessions_endpoint` | Session pool management endpoint URL | - |
65+
**No YAML configuration needed** - visualization is always enabled, with the executor determined by environment.
14066

14167
### System Prompt
14268

@@ -159,43 +85,23 @@ system_prompt: |
15985
16086
## How It Works
16187
162-
```
163-
┌─────────────────────────────────────────────────────────────────┐
164-
│ User Query │
165-
│ "Show me a bar chart of sales by region" │
166-
└─────────────────────────────────────────┬───────────────────────┘
167-
│
168-
▼
169-
┌─────────────────────────────────────────────────────────────────┐
170-
│ SQL Generation LLM │
171-
│ Generates SQL + sets visualization_requested: true │
172-
└─────────────────────────────────────────┬───────────────────────┘
173-
│
174-
▼
175-
┌─────────────────────────────────────────────────────────────────┐
176-
│ Database Query │
177-
│ Execute SQL, return result rows │
178-
└─────────────────────────────────────────┬───────────────────────┘
179-
│
180-
▼
181-
┌─────────────────────────────────────────────────────────────────┐
182-
│ Visualization LLM │
183-
│ Generates matplotlib code based on data + user question │
184-
└─────────────────────────────────────────┬───────────────────────┘
185-
│
186-
▼
187-
┌─────────────────────────────────────────────────────────────────┐
188-
│ Azure Container Apps Dynamic Sessions │
189-
│ • Code executed in Hyper-V isolated container │
190-
│ • plt.show() output captured automatically │
191-
│ • Image returned as base64 PNG │
192-
└─────────────────────────────────────────┬───────────────────────┘
193-
│
194-
▼
195-
┌─────────────────────────────────────────────────────────────────┐
196-
│ Response │
197-
│ Text explanation + embedded chart image │
198-
└─────────────────────────────────────────────────────────────────┘
88+
```mermaid
89+
sequenceDiagram
90+
participant User
91+
participant SQL LLM
92+
participant Database
93+
participant Viz LLM
94+
participant Executor
95+
96+
User->>SQL LLM: "Show me a bar chart of sales by region"
97+
SQL LLM->>SQL LLM: Generate SQL + set visualization_requested: true
98+
SQL LLM->>Database: Execute SQL query
99+
Database-->>SQL LLM: Result rows
100+
SQL LLM->>Viz LLM: Data + user question
101+
Viz LLM->>Viz LLM: Generate matplotlib code
102+
Viz LLM->>Executor: Execute code
103+
Executor-->>Viz LLM: PNG image (base64)
104+
Viz LLM-->>User: Text response + chart image
199105
```
200106
201107
### Execution Flow
@@ -218,9 +124,3 @@ These prompts trigger visualization:
218124
| "Create a pie chart of transaction types" | Pie chart |
219125
| "Graph the distribution of order values" | Histogram |
220126
| "Compare Q1 vs Q2 performance" | Grouped bar |
221-
222-
## Further Reading
223-
224-
- [Azure Container Apps Dynamic Sessions](https://learn.microsoft.com/azure/container-apps/sessions)
225-
- [Session Pool Management](https://learn.microsoft.com/azure/container-apps/sessions-code-interpreter)
226-
- [LangChain Azure Dynamic Sessions](https://python.langchain.com/docs/integrations/tools/azure_dynamic_sessions)

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ dependencies = [
4646
"psycopg2>=2.9.11",
4747
"langchain-azure-dynamic-sessions>=0.2.0",
4848
"matplotlib>=3.10.8",
49+
"tabulate>=0.9.0",
4950
]
5051

5152
[project.scripts]

‎scripts/generate_diagrams.py‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""Generate flow diagrams for documentation using LangGraph visualization.
2+
3+
This script generates PNG images for the data agent and intent detection flows
4+
using LangGraph's built-in visualization.
5+
6+
Usage:
7+
uv run python scripts/generate_diagrams.py
8+
"""
9+
10+
import os
11+
from pathlib import Path
12+
13+
from dotenv import load_dotenv
14+
15+
load_dotenv()
16+
17+
18+
def main():
19+
"""Generate diagrams from LangGraph and save to docs folder."""
20+
from unittest.mock import MagicMock
21+
22+
from langchain_openai import AzureChatOpenAI
23+
24+
from data_agent.config import CONFIG_DIR
25+
from data_agent.config_loader import ConfigLoader
26+
from data_agent.graph import DataAgentGraph
27+
28+
docs_dir = Path(__file__).parent.parent / "docs"
29+
docs_dir.mkdir(exist_ok=True)
30+
31+
# Load a config to get a data agent graph
32+
config = ConfigLoader.load(CONFIG_DIR / "amex.yaml")
33+
34+
if config.data_agents:
35+
agent_config = config.data_agents[0]
36+
37+
# Create LLM
38+
llm = AzureChatOpenAI(
39+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT"),
40+
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
41+
azure_deployment=os.getenv("AZURE_OPENAI_DEPLOYMENT", "gpt-4o"),
42+
api_version="2024-08-01-preview",
43+
temperature=0,
44+
)
45+
46+
# Create a mock datasource for diagram generation (we won't execute queries)
47+
mock_datasource = MagicMock()
48+
49+
# Build the graph and compile to get visualization
50+
graph_builder = DataAgentGraph(llm, mock_datasource, agent_config)
51+
compiled_graph = graph_builder.compile()
52+
53+
# Generate PNG using LangGraph's visualization
54+
png_data = compiled_graph.get_graph().draw_mermaid_png()
55+
output_path = docs_dir / "data_agent_graph.png"
56+
output_path.write_bytes(png_data)
57+
print(f"Generated: {output_path}")
58+
59+
print("Done!")
60+
61+
62+
if __name__ == "__main__":
63+
main()

0 commit comments

Comments
 (0)