Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,40 +11,6 @@ DEFAULT_PIPELINE = os.environ.get("PIPELINE_ID", "")
|
|
| 11 |
st.set_page_config(page_title="LlamaIndex Cloud Chat", page_icon="💬")
|
| 12 |
st.title("💬 Chat with Your LlamaIndex Cloud Dataset")
|
| 13 |
|
| 14 |
-
# ------------ Sidebar ------------
|
| 15 |
-
with st.sidebar:
|
| 16 |
-
st.subheader("Settings")
|
| 17 |
-
pipeline_id = st.text_input(
|
| 18 |
-
"Pipeline ID",
|
| 19 |
-
value=DEFAULT_PIPELINE,
|
| 20 |
-
help="Copy the UUID from cloud.llamaindex.ai → your pipeline URL",
|
| 21 |
-
)
|
| 22 |
-
|
| 23 |
-
st.caption(
|
| 24 |
-
f"Key present: {'✅' if bool(LLAMA_KEY) else '❌'} • "
|
| 25 |
-
f"Default Pipeline ID: {'✅' if bool(DEFAULT_PIPELINE) else '❌'}"
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
def health_check():
|
| 29 |
-
if not pipeline_id:
|
| 30 |
-
st.error("Set a Pipeline ID first.")
|
| 31 |
-
return
|
| 32 |
-
try:
|
| 33 |
-
data = llama_chat(
|
| 34 |
-
pipeline=pipeline_id,
|
| 35 |
-
messages=[{"role": "user", "content": "ping"}],
|
| 36 |
-
)
|
| 37 |
-
st.success(f"Chat OK (got keys: {', '.join(list(data.keys())[:5])})")
|
| 38 |
-
except Exception as e:
|
| 39 |
-
st.error(str(e))
|
| 40 |
-
|
| 41 |
-
# Placeholders; actual function defined below
|
| 42 |
-
st.button("Health check", on_click=lambda: None)
|
| 43 |
-
st.markdown("---")
|
| 44 |
-
if st.button("Clear chat"):
|
| 45 |
-
st.session_state.messages = []
|
| 46 |
-
st.rerun()
|
| 47 |
-
|
| 48 |
# ------------ Helpers ------------
|
| 49 |
def llama_chat(pipeline: str, messages: list[dict]):
|
| 50 |
"""
|
|
@@ -98,9 +64,40 @@ def render_sources(sources):
|
|
| 98 |
if meta:
|
| 99 |
st.code(meta, language="json")
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
with st.sidebar:
|
| 103 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
# ------------ Chat State ------------
|
| 106 |
if "messages" not in st.session_state:
|
|
@@ -123,7 +120,10 @@ if q:
|
|
| 123 |
with st.chat_message("assistant"):
|
| 124 |
with st.spinner("Thinking…"):
|
| 125 |
try:
|
| 126 |
-
data = llama_chat(
|
|
|
|
|
|
|
|
|
|
| 127 |
# Common response shapes: { message, sources, ... } or { answer, source_nodes, ... }
|
| 128 |
answer = data.get("message") or data.get("answer") or "No answer."
|
| 129 |
st.markdown(answer)
|
|
|
|
| 11 |
st.set_page_config(page_title="LlamaIndex Cloud Chat", page_icon="💬")
|
| 12 |
st.title("💬 Chat with Your LlamaIndex Cloud Dataset")
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
# ------------ Helpers ------------
|
| 15 |
def llama_chat(pipeline: str, messages: list[dict]):
|
| 16 |
"""
|
|
|
|
| 64 |
if meta:
|
| 65 |
st.code(meta, language="json")
|
| 66 |
|
| 67 |
+
def do_health_check(pipeline_id: str):
|
| 68 |
+
if not pipeline_id:
|
| 69 |
+
st.error("Set a Pipeline ID first (sidebar).")
|
| 70 |
+
return
|
| 71 |
+
try:
|
| 72 |
+
data = llama_chat(
|
| 73 |
+
pipeline=pipeline_id,
|
| 74 |
+
messages=[{"role": "user", "content": "ping"}],
|
| 75 |
+
)
|
| 76 |
+
st.success(f"Chat OK · keys: {', '.join(list(data.keys())[:5])}")
|
| 77 |
+
except Exception as e:
|
| 78 |
+
st.error(str(e))
|
| 79 |
+
|
| 80 |
+
# ------------ Sidebar ------------
|
| 81 |
with st.sidebar:
|
| 82 |
+
st.subheader("Settings")
|
| 83 |
+
pipeline_id = st.text_input(
|
| 84 |
+
"Pipeline ID",
|
| 85 |
+
value=DEFAULT_PIPELINE,
|
| 86 |
+
help="Copy the UUID from cloud.llamaindex.ai → your pipeline URL",
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
st.caption(
|
| 90 |
+
f"Key present: {'✅' if bool(LLAMA_KEY) else '❌'} • "
|
| 91 |
+
f"Default Pipeline ID: {'✅' if bool(DEFAULT_PIPELINE) else '❌'}"
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if st.button("Health check", key="health_check_btn"):
|
| 95 |
+
do_health_check(pipeline_id or DEFAULT_PIPELINE)
|
| 96 |
+
|
| 97 |
+
st.markdown("---")
|
| 98 |
+
if st.button("Clear chat", key="clear_chat_btn"):
|
| 99 |
+
st.session_state.messages = []
|
| 100 |
+
st.rerun()
|
| 101 |
|
| 102 |
# ------------ Chat State ------------
|
| 103 |
if "messages" not in st.session_state:
|
|
|
|
| 120 |
with st.chat_message("assistant"):
|
| 121 |
with st.spinner("Thinking…"):
|
| 122 |
try:
|
| 123 |
+
data = llama_chat(
|
| 124 |
+
pipeline=pipeline_id or DEFAULT_PIPELINE,
|
| 125 |
+
messages=st.session_state.messages,
|
| 126 |
+
)
|
| 127 |
# Common response shapes: { message, sources, ... } or { answer, source_nodes, ... }
|
| 128 |
answer = data.get("message") or data.get("answer") or "No answer."
|
| 129 |
st.markdown(answer)
|