MukeshKapoor25 commited on
Commit
5606ca5
·
1 Parent(s): d79a15b

Add initial project structure with environment configuration, README, and core functionality

Browse files

- Created .env file for database and Hugging Face API configuration
- Added README.md with project description, features, setup, and usage instructions
- Implemented agent_core.py for SQL agent logic and database connection
- Developed app.py for FastAPI integration with SQL agent
- Included sql_agent.py for command-line interaction with the agent
- Established requirements.txt for project dependencies
- Added .gitignore to exclude unnecessary files

.env ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Database configuration
3
+ PG_DATABASE=logbert_rca
4
+ PG_USER=trans_owner
5
+ PG_PASSWORD=BookMyService7
6
+ PG_HOST=ep-sweet-surf-a1qeduoy.ap-southeast-1.aws.neon.tech
7
+ PG_PORT=5432
8
+ # Optionally, you can use the full SQLAlchemy URI if needed:
9
+ # DATABASE_URI=postgresql+asyncpg://trans_owner:BookMyService7@ep-sweet-surf-a1qeduoy.ap-southeast-1.aws.neon.tech/logbert_rca?options=-csearch_path%3Dtrans
10
+
11
+ HUGGINGFACEHUB_API_TOKEN=
12
+
13
+ # Hugging Face/Model configuration
14
+ HUGGINGFACE_MODEL=distilgpt2
15
+ HF_CACHE_DIR=.cache
.gitignore ADDED
File without changes
README.md CHANGED
@@ -1,3 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
  title: Rca Sqlagent
3
  emoji: 👀
 
1
+ # RCA SQL Agent
2
+
3
+ This project is an intelligent SQL agent that uses LangChain and Hugging Face SLM to generate and execute SQL queries on a PostgreSQL database.
4
+
5
+ ## Features
6
+ - Natural language to SQL query generation using Hugging Face SLM via LangChain
7
+ - Executes generated queries on a PostgreSQL database
8
+ - Returns results to the user
9
+
10
+ ## Setup
11
+ 1. Install dependencies:
12
+ ```bash
13
+ pip install langchain[all] psycopg2-binary transformers
14
+ ```
15
+ 2. Set your PostgreSQL connection details in environment variables or update the script:
16
+ - `PG_HOST`, `PG_PORT`, `PG_USER`, `PG_PASSWORD`, `PG_DATABASE`
17
+ 3. Set your Hugging Face API key in the environment variable `HUGGINGFACEHUB_API_TOKEN`.
18
+
19
+ ## Usage
20
+ Run the agent script and enter your question in natural language. The agent will generate and execute the SQL query, then display the results.
21
+
22
+ ```bash
23
+ python sql_agent.py
24
+ ```
25
+
26
+ Type your question (e.g., "How many users signed up last month?") and get the result from your Postgres database.
27
+
28
+ ---
29
  ---
30
  title: Rca Sqlagent
31
  emoji: 👀
__pycache__/agent_core.cpython-313.pyc ADDED
Binary file (1.81 kB). View file
 
__pycache__/app.cpython-313.pyc ADDED
Binary file (1.46 kB). View file
 
agent_core.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ from dotenv import load_dotenv
4
+ load_dotenv()
5
+ from langchain_huggingface import HuggingFaceEndpoint
6
+ from langchain_community.utilities.sql_database import SQLDatabase
7
+ from langchain_community.agent_toolkits.sql.base import SQLDatabaseToolkit, create_sql_agent
8
+ from langchain.agents.agent_types import AgentType
9
+
10
+ # Set your Hugging Face API key and model
11
+ HUGGINGFACEHUB_API_TOKEN = os.getenv("HUGGINGFACEHUB_API_TOKEN")
12
+ HUGGINGFACE_MODEL = os.getenv("HUGGINGFACE_MODEL", "microsoft/phi-2")
13
+
14
+ # Set your Postgres connection details
15
+ PG_HOST = os.getenv("PG_HOST", "localhost")
16
+ PG_PORT = os.getenv("PG_PORT", "5432")
17
+ PG_USER = os.getenv("PG_USER", "postgres")
18
+ PG_PASSWORD = os.getenv("PG_PASSWORD", "password")
19
+ PG_DATABASE = os.getenv("PG_DATABASE", "postgres")
20
+
21
+
22
+ # Create the SQLAlchemy connection string
23
+ connection_string = f"postgresql+psycopg2://{PG_USER}:{PG_PASSWORD}@{PG_HOST}:{PG_PORT}/{PG_DATABASE}"
24
+
25
+ def get_sql_agent(verbose=False):
26
+ db = SQLDatabase.from_uri(connection_string)
27
+ llm = HuggingFaceEndpoint(
28
+ repo_id=HUGGINGFACE_MODEL,
29
+ huggingfacehub_api_token=HUGGINGFACEHUB_API_TOKEN,
30
+ temperature=0.2,
31
+ max_new_tokens=256
32
+ )
33
+ agent_executor = create_sql_agent(
34
+ llm=llm,
35
+ db=db,
36
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
37
+ verbose=verbose
38
+ )
39
+ return agent_executor
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ import os
4
+
5
+ from agent_core import get_sql_agent
6
+
7
+ _db_chain = get_sql_agent(verbose=True)
8
+
9
+ app = FastAPI()
10
+
11
+ class QueryRequest(BaseModel):
12
+ question: str
13
+
14
+ @app.post("/query")
15
+ def query_sql_agent(request: QueryRequest):
16
+ try:
17
+ result = _db_chain.invoke(request.question)
18
+ return {"result": result}
19
+ except Exception as e:
20
+ raise HTTPException(status_code=500, detail=str(e))
21
+
22
+ @app.get("/")
23
+ def root():
24
+ return {"message": "RCA SQL Agent API. POST to /query with {'question': ...}"}
requirements.txt ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ fastapi
3
+ uvicorn
4
+ langchain[all]
5
+ transformers
6
+ psycopg2-binary
7
+ asyncpg
8
+ pydantic
9
+ python-dotenv
10
+ redis
11
+ langchain_community
sql_agent.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from agent_core import get_sql_agent
3
+
4
+ def main():
5
+ db_chain = get_sql_agent(verbose=True)
6
+ print("Welcome to the RCA SQL Agent! Type your question (or 'exit' to quit):")
7
+ while True:
8
+ user_input = input("\n> ")
9
+ if user_input.lower() in ("exit", "quit"):
10
+ break
11
+ try:
12
+ result = db_chain.invoke(user_input)
13
+ print("\nResult:\n", result)
14
+ except Exception as e:
15
+ print("Error:", e)
16
+
17
+ if __name__ == "__main__":
18
+ main()