File size: 1,837 Bytes
ff754bc
c71e7c0
 
c33079c
 
 
 
c71e7c0
 
c33079c
c71e7c0
 
 
 
 
 
 
 
c33079c
 
 
 
 
 
 
 
 
 
 
c71e7c0
 
 
 
 
 
 
c33079c
 
 
 
 
 
 
c71e7c0
 
 
c33079c
c71e7c0
 
 
c33079c
 
 
 
 
c71e7c0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import requests
import os
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Get Lambda URL from environment variable
LAMBDA_URL = os.getenv("LAMBDA_URL") or "http://localhost:8000/chats/messages"
if not LAMBDA_URL:
    st.error("LAMBDA_URL environment variable is not set. Please check your .env file.")
    st.stop()

st.set_page_config(page_title="Ask Redshift", page_icon="🤖")

st.title("🧠 Ask your Redshift Database Anything...")

# Initialize conversation_id in session state if not exists
if "conversation_id" not in st.session_state:
    st.session_state.conversation_id = None

# Add system prompt input
system_prompt = st.text_area(
    "System Prompt (Optional)",
    help="Customize the AI's behavior and instructions. Leave empty to use default behavior.",
    height=100
)

question = st.text_input(
    "Enter your question about your data (e.g., 'What is the average cost per lead for March?')"
)

if st.button("Run Query"):
    if question:
        with st.spinner("Querying..."):
            # Prepare payload
            payload = {"question": question}
            if st.session_state.conversation_id:
                payload["conversation_id"] = st.session_state.conversation_id
            if system_prompt:
                payload["system_prompt"] = system_prompt

            # Call the API
            response = requests.post(
                LAMBDA_URL,
                json=payload,
            )
            response.raise_for_status()
            response = response.json()

            # Store conversation_id for future requests
            if "conversation_id" in response:
                st.session_state.conversation_id = response["conversation_id"]

        st.success(response["answer"])
    else:
        st.error("Please enter a question.")