This repository was archived by the owner on Jul 4, 2025. It is now read-only.
forked from langchain-ai/langconnect
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathMain.py
More file actions
405 lines (337 loc) · 13.7 KB
/
Main.py
File metadata and controls
405 lines (337 loc) · 13.7 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import json
import os
import pickle
import time
from pathlib import Path
from typing import Any, Optional
import requests
import streamlit as st
from dotenv import load_dotenv
load_dotenv()
API_BASE_URL = os.getenv("API_BASE_URL", "http://localhost:8080")
IS_TESTING = os.getenv("IS_TESTING", "").lower() == "true"
# Check for saved auth credentials in environment variables
SAVED_TOKEN = os.getenv("LANGCONNECT_TOKEN", "")
SAVED_EMAIL = os.getenv("LANGCONNECT_EMAIL", "")
st.set_page_config(page_title="LangConnect Client", page_icon="🔗", layout="wide")
# Define auth cache file path
AUTH_CACHE_FILE = Path.home() / ".langconnect_auth_cache"
# Function to save auth data to file
def save_auth_to_file(token: str, email: str):
"""Save authentication data to a local file."""
try:
auth_data = {"token": token, "email": email, "timestamp": time.time()}
with open(AUTH_CACHE_FILE, "wb") as f:
pickle.dump(auth_data, f)
except Exception as e:
print(f"Failed to save auth data: {e}")
# Function to load auth data from file
def load_auth_from_file():
"""Load authentication data from a local file."""
try:
if AUTH_CACHE_FILE.exists():
with open(AUTH_CACHE_FILE, "rb") as f:
auth_data = pickle.load(f)
# Check if auth data is not too old (e.g., 7 days)
if time.time() - auth_data.get("timestamp", 0) < 7 * 24 * 3600:
return auth_data.get("token"), auth_data.get("email")
except Exception as e:
print(f"Failed to load auth data: {e}")
return None, None
# Function to clear auth file
def clear_auth_file():
"""Clear the authentication cache file."""
try:
if AUTH_CACHE_FILE.exists():
AUTH_CACHE_FILE.unlink()
except Exception as e:
print(f"Failed to clear auth file: {e}")
# Initialize session state for authentication
if "authenticated" not in st.session_state:
st.session_state.authenticated = False
if "access_token" not in st.session_state:
st.session_state.access_token = None
if "user_email" not in st.session_state:
st.session_state.user_email = None
if "auth_loaded" not in st.session_state:
st.session_state.auth_loaded = False
# Try to load auth from file or environment on first load
if not st.session_state.auth_loaded:
# First try to load from file
token, email = load_auth_from_file()
# If not found in file, try environment variables
if not token and SAVED_TOKEN and SAVED_EMAIL:
token = SAVED_TOKEN
email = SAVED_EMAIL
# If we have valid credentials, set them
if token and email:
st.session_state.authenticated = True
st.session_state.access_token = token
st.session_state.user_email = email
st.session_state.auth_loaded = True
def get_headers(include_content_type=True):
headers = {
"Accept": "application/json",
}
# Use access_token from SUPABASE
token = st.session_state.get("access_token")
if token:
headers["Authorization"] = f"Bearer {token}"
if include_content_type:
headers["Content-Type"] = "application/json"
return headers
def make_request(
method: str,
endpoint: str,
data: Optional[dict] = None,
files: Optional[dict] = None,
json_data: Optional[dict] = None,
) -> tuple[bool, Any]:
url = f"{API_BASE_URL}{endpoint}"
try:
if method == "GET":
headers = get_headers()
response = requests.get(url, headers=headers, params=data)
elif method == "POST":
if files:
headers = get_headers(include_content_type=False)
response = requests.post(url, headers=headers, data=data, files=files)
else:
headers = get_headers()
response = requests.post(url, headers=headers, json=json_data)
elif method == "DELETE":
headers = get_headers()
response = requests.delete(url, headers=headers)
elif method == "PATCH":
headers = get_headers()
response = requests.patch(url, headers=headers, json=json_data)
else:
return False, f"Unsupported method: {method}"
if response.status_code in [200, 201, 204]:
if response.status_code == 204:
return True, "Success (No content)"
try:
return True, response.json()
except:
return True, response.text
else:
try:
error_detail = response.json()
return (
False,
f"Error {response.status_code}: {json.dumps(error_detail, indent=2)}",
)
except:
return False, f"Error {response.status_code}: {response.text}"
except requests.exceptions.ConnectionError:
return (
False,
f"Connection failed. Please check if the API is running at {API_BASE_URL}",
)
except Exception as e:
return False, f"Request failed: {e!s}"
# Collections tab has been moved to pages/1_Collections.py
# API tester tab has been moved to pages/4_API_Tester.py
# Document upload tab has been moved to pages/2_Documents.py
# Vector search tab has been moved to pages/3_Search.py
# Document management tab has been moved to pages/2_Documents.py
def auth_page():
"""Display authentication page."""
st.title("🔗 LangConnect Client")
st.subheader("Authentication")
# Testing mode - simple authentication
if IS_TESTING:
st.info("🧪 Testing Mode - Use simple credentials")
with st.form("testing_signin_form"):
st.write("**Testing Users:**")
st.write("- user1")
st.write("- user2")
username = st.text_input("Username", placeholder="user1 or user2")
submitted = st.form_submit_button("Sign In (Testing)", type="primary")
if submitted:
if username in ["user1", "user2"]:
st.session_state.authenticated = True
st.session_state.access_token = (
username # Use username as token in testing
)
st.session_state.user_email = f"{username}@test.com"
# Save to file
save_auth_to_file(username, f"{username}@test.com")
st.success(f"Successfully signed in as {username}!")
st.rerun()
else:
st.error("Please use 'user1' or 'user2' for testing")
st.divider()
st.subheader("Production Authentication")
st.write(
"*For production use, configure SUPABASE_URL and SUPABASE_KEY, then set IS_TESTING=false*"
)
return
tab1, tab2 = st.tabs(["Sign In", "Sign Up"])
with tab1:
with st.form("signin_form"):
email = st.text_input("Email", placeholder="user@example.com")
password = st.text_input("Password", type="password")
submitted = st.form_submit_button("Sign In", type="primary")
if submitted:
if not email or not password:
st.error("Please enter both email and password")
else:
with st.spinner("Signing in..."):
success, result = make_request(
"POST",
"/auth/signin",
json_data={"email": email, "password": password},
)
if success:
st.session_state.authenticated = True
st.session_state.access_token = result["access_token"]
st.session_state.user_email = result["email"]
# Save to file
save_auth_to_file(result["access_token"], result["email"])
st.success("Successfully signed in!")
st.rerun()
else:
st.error(f"Sign in failed: {result}")
with tab2:
with st.form("signup_form"):
new_email = st.text_input("Email", placeholder="user@example.com")
new_password = st.text_input("Password", type="password")
confirm_password = st.text_input("Confirm Password", type="password")
submitted = st.form_submit_button("Sign Up", type="primary")
if submitted:
if not new_email or not new_password:
st.error("Please enter both email and password")
elif new_password != confirm_password:
st.error("Passwords do not match")
elif len(new_password) < 6:
st.error("Password must be at least 6 characters")
else:
with st.spinner("Creating account..."):
success, result = make_request(
"POST",
"/auth/signup",
json_data={"email": new_email, "password": new_password},
)
if success:
st.session_state.authenticated = True
st.session_state.access_token = result["access_token"]
st.session_state.user_email = result["email"]
# Save to file
save_auth_to_file(result["access_token"], result["email"])
st.success("Account created successfully!")
st.rerun()
else:
st.error(f"Sign up failed: {result}")
def main():
# Check if user is authenticated
if not st.session_state.authenticated:
auth_page()
return
st.title("🔗 LangConnect Client")
st.markdown(
"""
Welcome to **LangConnect** - A powerful document management and search system powered by LangChain and PostgreSQL.
## 🚀 Features
This application provides a comprehensive interface for managing documents with advanced search capabilities:
"""
)
# Page navigation
col1, col2 = st.columns(2)
with col1:
st.markdown("### 📚 Collections Management")
st.markdown(
"""
- Create and manage document collections
- View collection statistics
- Bulk delete collections
"""
)
st.page_link("pages/1_Collections.py", label="Go to Collections", icon="📚")
st.markdown("### 📄 Document Management")
st.markdown(
"""
- Upload multiple documents (PDF, TXT, MD, DOCX)
- View and manage document chunks
- Delete individual chunks or entire documents
"""
)
st.page_link("pages/2_Documents.py", label="Go to Documents", icon="📄")
with col2:
st.markdown("### 🔍 Search")
st.markdown(
"""
- **Semantic Search**: AI-powered similarity search
- **Keyword Search**: Traditional full-text search
- **Hybrid Search**: Best of both approaches
- Advanced metadata filtering
"""
)
st.page_link("pages/3_Search.py", label="Go to Search", icon="🔍")
st.markdown("### 🧪 API Tester")
st.markdown(
"""
- Test all API endpoints directly
- Explore the API functionality
- Debug and develop integrations
"""
)
st.page_link("pages/4_API_Tester.py", label="Go to API Tester", icon="🧪")
st.divider()
# Project information
st.markdown("## 📌 About This Project")
col1, col2 = st.columns([2, 1])
with col1:
st.markdown(
"""
**LangConnect** is an open-source project that combines the power of:
- 🦜 **LangChain** for document processing and embeddings
- 🐘 **PostgreSQL** with pgvector extension for vector storage
- ⚡ **FastAPI** for high-performance API backend
- 🎨 **Streamlit** for interactive user interface
Perfect for building RAG (Retrieval-Augmented Generation) applications!
"""
)
with col2:
st.markdown("### 🔗 Links")
st.markdown(
"""
- 📦 [GitHub Repository](https://github.com/teddynote-lab/LangConnect-Client)
- 👨💻 [TeddyNote LAB](https://github.com/teddynote-lab)
- 📚 [Documentation](https://github.com/teddynote-lab/LangConnect-Client#readme)
"""
)
st.divider()
# Footer
st.markdown(
"""
<div style='text-align: center; color: #666; padding: 20px;'>
Made with ❤️ by <a href='https://github.com/teddynote-lab' target='_blank'>TeddyNote LAB</a>
</div>
""",
unsafe_allow_html=True,
)
with st.sidebar:
st.header("Configuration")
if st.session_state.authenticated:
st.write(f"**User:** {st.session_state.user_email}")
if st.button("Sign Out", key="signout_btn"):
st.session_state.authenticated = False
st.session_state.access_token = None
st.session_state.user_email = None
# Clear auth file
clear_auth_file()
st.rerun()
st.divider()
st.subheader("Connection Status")
if st.button("Test Connection"):
with st.spinner("Testing connection..."):
success, result = make_request("GET", "/health")
if success:
st.success("✅ API is healthy")
st.json(result)
else:
st.error("❌ Connection failed")
st.error(result)
if __name__ == "__main__":
main()