Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/api/prompts/create_prompts_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import os
import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../')))
from app.utils.db import get_db_connection_direct

def run_create_prompts_table_sql():
sql_path = os.path.join(os.path.dirname(__file__), "create_prompts_table.sql")
with open(sql_path, "r") as f:
sql = f.read()
conn = get_db_connection_direct()
try:
with conn.cursor() as cur:
cur.execute(sql)
conn.commit()
print("prompts table created or already exists.")
finally:
conn.close()

if __name__ == "__main__":
run_create_prompts_table_sql()
9 changes: 9 additions & 0 deletions app/api/prompts/create_prompts_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Migration: Create prompts table
CREATE TABLE IF NOT EXISTS prompts (
id SERIAL PRIMARY KEY,
prompt TEXT NOT NULL,
response TEXT NOT NULL,
duration_ms INTEGER NOT NULL,
timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(),
llm VARCHAR(128) NOT NULL
);
42 changes: 36 additions & 6 deletions app/api/prompts/prompts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

from app import __version__

import os
from app.utils.make_meta import make_meta
from fastapi import APIRouter, Query, Path
Expand All @@ -10,9 +11,38 @@

@router.get("/prompts")
def root() -> dict:
"""GET /prospects endpoint."""
meta = make_meta("success", "Prompts endpoint")
data = [
{"init": f"{base_url}/prompts"},
]
return {"meta": meta, "data": data}
"""GET /prompts endpoint."""
from fastapi import status
meta = None
data = []
# Check if 'prompts' table exists
from app.utils.db import get_db_connection_direct
conn = get_db_connection_direct()
try:
with conn.cursor() as cur:
cur.execute("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'prompts'
);
""")
result = cur.fetchone()
exists = result[0] if result else False
finally:
conn.close()
if not exists:
meta = make_meta("warning", "Table 'prompts' does not exist.")
# Do not include 'kata' key or any other keys in data
response = {"meta": meta}
else:
meta = make_meta("success", "Prompts endpoint")
# Example: include 'kata' key only if table exists (add as needed)
data = [
{"init": f"{base_url}/prompts", "kata": "example"},
]
response = {"meta": meta, "data": data}
# Remove 'kata' key from all items in data if table does not exist
if not exists:
for item in data:
item.pop('kata', None)
return response
9 changes: 9 additions & 0 deletions app/utils/db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
def get_db_connection_direct():
"""Create and return a PostgreSQL connection (not a generator)."""
return psycopg2.connect(
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT', '5432'),
dbname=os.getenv('DB_NAME'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
)
import os
import psycopg2
from dotenv import load_dotenv
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pytest>=8.1.0
python-dotenv>=1.0.0
psycopg2-binary>=2.9.0
python-multipart>=0.0.20
Faker>=25.2.0
Loading