Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import pandas as pd
|
| 3 |
import os
|
| 4 |
-
import requests
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
# --- Page config
|
| 8 |
-
st.set_page_config(page_title="
|
| 9 |
|
| 10 |
-
|
| 11 |
-
st.title("CSV-Backed AI Chat Agent")
|
| 12 |
|
| 13 |
# --- Load API key
|
| 14 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
@@ -21,102 +19,140 @@ HEADERS = {
|
|
| 21 |
"Content-Type": "application/json",
|
| 22 |
}
|
| 23 |
|
| 24 |
-
# --- Sidebar:
|
| 25 |
-
st.sidebar.header("Upload
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
# ---
|
|
|
|
|
|
|
| 29 |
if "messages" not in st.session_state:
|
| 30 |
st.session_state.messages = []
|
| 31 |
if "temp_input" not in st.session_state:
|
| 32 |
st.session_state.temp_input = ""
|
| 33 |
|
| 34 |
-
# ---
|
| 35 |
-
if
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
else:
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
-
return {"error":
|
| 82 |
|
| 83 |
-
def
|
|
|
|
| 84 |
try:
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
-
return {"error":
|
| 89 |
|
| 90 |
# --- Function schemas for OpenAI
|
| 91 |
function_schema = [
|
| 92 |
{
|
| 93 |
-
"name": "
|
| 94 |
-
"description": "
|
| 95 |
"parameters": {
|
| 96 |
"type": "object",
|
| 97 |
"properties": {
|
| 98 |
-
"
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
},
|
| 102 |
},
|
| 103 |
-
"required": ["
|
| 104 |
},
|
| 105 |
},
|
| 106 |
{
|
| 107 |
-
"name": "
|
| 108 |
-
"description": "
|
| 109 |
"parameters": {
|
| 110 |
"type": "object",
|
| 111 |
"properties": {
|
| 112 |
-
"
|
| 113 |
-
"type": "string",
|
| 114 |
-
"description": "The column name to count unique values."
|
| 115 |
-
},
|
| 116 |
},
|
| 117 |
-
"required": ["
|
| 118 |
},
|
| 119 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
]
|
| 121 |
|
| 122 |
# --- Chat interface
|
|
@@ -138,18 +174,18 @@ def send_message():
|
|
| 138 |
user_input = st.session_state.temp_input
|
| 139 |
if user_input and user_input.strip():
|
| 140 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 141 |
-
# Limit history for context size
|
| 142 |
chat_messages = st.session_state.messages
|
| 143 |
if len(chat_messages) > 10:
|
| 144 |
chat_messages = [chat_messages[0]] + chat_messages[-9:]
|
| 145 |
else:
|
| 146 |
chat_messages = chat_messages.copy()
|
| 147 |
-
#
|
| 148 |
chat_resp = requests.post(
|
| 149 |
"https://api.openai.com/v1/chat/completions",
|
| 150 |
headers=HEADERS,
|
| 151 |
json={
|
| 152 |
-
"model": "gpt-4.1",
|
| 153 |
"messages": chat_messages,
|
| 154 |
"functions": function_schema,
|
| 155 |
"function_call": "auto",
|
|
@@ -167,11 +203,12 @@ def send_message():
|
|
| 167 |
func_name = msg["function_call"]["name"]
|
| 168 |
args_json = msg["function_call"]["arguments"]
|
| 169 |
args = json.loads(args_json)
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
else:
|
| 176 |
function_result = {"error": f"Unknown function: {func_name}"}
|
| 177 |
st.session_state.messages.append({
|
|
@@ -179,7 +216,7 @@ def send_message():
|
|
| 179 |
"name": func_name,
|
| 180 |
"content": json.dumps(function_result),
|
| 181 |
})
|
| 182 |
-
#
|
| 183 |
followup_messages = st.session_state.messages
|
| 184 |
if len(followup_messages) > 12:
|
| 185 |
followup_messages = [followup_messages[0]] + followup_messages[-11:]
|
|
@@ -204,5 +241,8 @@ def send_message():
|
|
| 204 |
|
| 205 |
st.session_state.temp_input = ""
|
| 206 |
|
| 207 |
-
if
|
| 208 |
st.text_input("Your message:", key="temp_input", on_change=send_message)
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
import os
|
|
|
|
| 3 |
import json
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
# --- Page config
|
| 7 |
+
st.set_page_config(page_title="JSON-Backed AI Chat Agent", layout="wide")
|
| 8 |
|
| 9 |
+
st.title("JSON-Backed AI Chat Agent")
|
|
|
|
| 10 |
|
| 11 |
# --- Load API key
|
| 12 |
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
|
|
|
| 19 |
"Content-Type": "application/json",
|
| 20 |
}
|
| 21 |
|
| 22 |
+
# --- Sidebar: Multiple JSON upload & preview
|
| 23 |
+
st.sidebar.header("Upload Multiple JSON Files")
|
| 24 |
+
uploaded_files = st.sidebar.file_uploader(
|
| 25 |
+
"Choose one or more JSON files", type="json", accept_multiple_files=True
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# --- Session State for data and chat
|
| 29 |
+
if "json_data" not in st.session_state:
|
| 30 |
+
st.session_state.json_data = {}
|
| 31 |
if "messages" not in st.session_state:
|
| 32 |
st.session_state.messages = []
|
| 33 |
if "temp_input" not in st.session_state:
|
| 34 |
st.session_state.temp_input = ""
|
| 35 |
|
| 36 |
+
# --- Load all JSON files
|
| 37 |
+
if uploaded_files:
|
| 38 |
+
st.session_state.json_data.clear()
|
| 39 |
+
file_summaries = []
|
| 40 |
+
for f in uploaded_files:
|
| 41 |
+
try:
|
| 42 |
+
content = json.load(f)
|
| 43 |
+
st.session_state.json_data[f.name] = content
|
| 44 |
+
# For summary in system prompt
|
| 45 |
+
if isinstance(content, dict):
|
| 46 |
+
keys = list(content.keys())
|
| 47 |
+
elif isinstance(content, list) and content and isinstance(content[0], dict):
|
| 48 |
+
keys = list(content[0].keys())
|
| 49 |
+
else:
|
| 50 |
+
keys = []
|
| 51 |
+
file_summaries.append(f"{f.name}: keys={keys[:10]}{'...' if len(keys)>10 else ''}")
|
| 52 |
+
st.sidebar.success(f"Loaded: {f.name}")
|
| 53 |
+
st.sidebar.write(f"Keys: {keys[:10]}{'...' if len(keys)>10 else ''}")
|
| 54 |
+
except Exception as e:
|
| 55 |
+
st.sidebar.error(f"Error reading {f.name}: {e}")
|
| 56 |
+
|
| 57 |
+
# Compose system prompt for the LLM
|
| 58 |
+
system_message = {
|
| 59 |
+
"role": "system",
|
| 60 |
+
"content": (
|
| 61 |
+
"You are an AI data analyst for the following JSON files:\n" +
|
| 62 |
+
"\n".join(file_summaries) +
|
| 63 |
+
"\nEach file may have a different structure and set of keys. "
|
| 64 |
+
"When the user asks a question, identify which file(s) it applies to, "
|
| 65 |
+
"then use the most relevant function to extract the answer. "
|
| 66 |
+
"If the user does not specify a file, make your best guess based on keys/fields mentioned."
|
| 67 |
+
),
|
| 68 |
+
}
|
| 69 |
+
# Reset chat if new files loaded
|
| 70 |
+
st.session_state.messages = [system_message]
|
| 71 |
else:
|
| 72 |
+
st.session_state.json_data.clear()
|
| 73 |
|
| 74 |
+
# --- Functions for querying JSON files
|
| 75 |
+
def search_json(file_name, key, value):
|
| 76 |
+
"""Return all records in the given JSON file (list of dicts) where key == value."""
|
| 77 |
+
try:
|
| 78 |
+
data = st.session_state.json_data[file_name]
|
| 79 |
+
if isinstance(data, list):
|
| 80 |
+
results = [item for item in data if isinstance(item, dict) and item.get(key) == value]
|
| 81 |
+
return results[:10]
|
| 82 |
+
elif isinstance(data, dict):
|
| 83 |
+
if key in data and data[key] == value:
|
| 84 |
+
return [{key: value}]
|
| 85 |
+
else:
|
| 86 |
+
return []
|
| 87 |
+
else:
|
| 88 |
+
return []
|
| 89 |
+
except Exception as e:
|
| 90 |
+
return {"error": str(e)}
|
| 91 |
|
| 92 |
+
def list_keys(file_name):
|
| 93 |
+
"""Return all top-level keys of the JSON file."""
|
| 94 |
try:
|
| 95 |
+
data = st.session_state.json_data[file_name]
|
| 96 |
+
if isinstance(data, dict):
|
| 97 |
+
return list(data.keys())
|
| 98 |
+
elif isinstance(data, list) and data and isinstance(data[0], dict):
|
| 99 |
+
return list(data[0].keys())
|
| 100 |
+
else:
|
| 101 |
+
return []
|
| 102 |
except Exception as e:
|
| 103 |
+
return {"error": str(e)}
|
| 104 |
|
| 105 |
+
def count_key_occurrences(file_name, key):
|
| 106 |
+
"""Count number of occurrences of a given key in the JSON file."""
|
| 107 |
try:
|
| 108 |
+
data = st.session_state.json_data[file_name]
|
| 109 |
+
if isinstance(data, dict):
|
| 110 |
+
return 1 if key in data else 0
|
| 111 |
+
elif isinstance(data, list):
|
| 112 |
+
return sum(1 for item in data if isinstance(item, dict) and key in item)
|
| 113 |
+
else:
|
| 114 |
+
return 0
|
| 115 |
except Exception as e:
|
| 116 |
+
return {"error": str(e)}
|
| 117 |
|
| 118 |
# --- Function schemas for OpenAI
|
| 119 |
function_schema = [
|
| 120 |
{
|
| 121 |
+
"name": "search_json",
|
| 122 |
+
"description": "Find records in the specified JSON file where key matches a given value.",
|
| 123 |
"parameters": {
|
| 124 |
"type": "object",
|
| 125 |
"properties": {
|
| 126 |
+
"file_name": {"type": "string", "description": "The uploaded JSON file to search."},
|
| 127 |
+
"key": {"type": "string", "description": "The key/field to filter by."},
|
| 128 |
+
"value": {"type": "string", "description": "The value to match."}
|
|
|
|
| 129 |
},
|
| 130 |
+
"required": ["file_name", "key", "value"],
|
| 131 |
},
|
| 132 |
},
|
| 133 |
{
|
| 134 |
+
"name": "list_keys",
|
| 135 |
+
"description": "List all top-level keys in a given JSON file.",
|
| 136 |
"parameters": {
|
| 137 |
"type": "object",
|
| 138 |
"properties": {
|
| 139 |
+
"file_name": {"type": "string", "description": "The uploaded JSON file."},
|
|
|
|
|
|
|
|
|
|
| 140 |
},
|
| 141 |
+
"required": ["file_name"],
|
| 142 |
},
|
| 143 |
+
},
|
| 144 |
+
{
|
| 145 |
+
"name": "count_key_occurrences",
|
| 146 |
+
"description": "Count the number of times a given key appears in a JSON file.",
|
| 147 |
+
"parameters": {
|
| 148 |
+
"type": "object",
|
| 149 |
+
"properties": {
|
| 150 |
+
"file_name": {"type": "string", "description": "The uploaded JSON file."},
|
| 151 |
+
"key": {"type": "string", "description": "The key to count."},
|
| 152 |
+
},
|
| 153 |
+
"required": ["file_name", "key"],
|
| 154 |
+
},
|
| 155 |
+
},
|
| 156 |
]
|
| 157 |
|
| 158 |
# --- Chat interface
|
|
|
|
| 174 |
user_input = st.session_state.temp_input
|
| 175 |
if user_input and user_input.strip():
|
| 176 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 177 |
+
# Limit history for context size
|
| 178 |
chat_messages = st.session_state.messages
|
| 179 |
if len(chat_messages) > 10:
|
| 180 |
chat_messages = [chat_messages[0]] + chat_messages[-9:]
|
| 181 |
else:
|
| 182 |
chat_messages = chat_messages.copy()
|
| 183 |
+
# OpenAI call
|
| 184 |
chat_resp = requests.post(
|
| 185 |
"https://api.openai.com/v1/chat/completions",
|
| 186 |
headers=HEADERS,
|
| 187 |
json={
|
| 188 |
+
"model": "gpt-4.1", # Use latest available model for this purpose
|
| 189 |
"messages": chat_messages,
|
| 190 |
"functions": function_schema,
|
| 191 |
"function_call": "auto",
|
|
|
|
| 203 |
func_name = msg["function_call"]["name"]
|
| 204 |
args_json = msg["function_call"]["arguments"]
|
| 205 |
args = json.loads(args_json)
|
| 206 |
+
if func_name == "search_json":
|
| 207 |
+
function_result = search_json(args.get("file_name"), args.get("key"), args.get("value"))
|
| 208 |
+
elif func_name == "list_keys":
|
| 209 |
+
function_result = list_keys(args.get("file_name"))
|
| 210 |
+
elif func_name == "count_key_occurrences":
|
| 211 |
+
function_result = count_key_occurrences(args.get("file_name"), args.get("key"))
|
| 212 |
else:
|
| 213 |
function_result = {"error": f"Unknown function: {func_name}"}
|
| 214 |
st.session_state.messages.append({
|
|
|
|
| 216 |
"name": func_name,
|
| 217 |
"content": json.dumps(function_result),
|
| 218 |
})
|
| 219 |
+
# Second call to OpenAI for the final answer
|
| 220 |
followup_messages = st.session_state.messages
|
| 221 |
if len(followup_messages) > 12:
|
| 222 |
followup_messages = [followup_messages[0]] + followup_messages[-11:]
|
|
|
|
| 241 |
|
| 242 |
st.session_state.temp_input = ""
|
| 243 |
|
| 244 |
+
if st.session_state.json_data:
|
| 245 |
st.text_input("Your message:", key="temp_input", on_change=send_message)
|
| 246 |
+
else:
|
| 247 |
+
st.info("Please upload at least one JSON file to start chatting.")
|
| 248 |
+
|