-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
347 lines (289 loc) · 12.3 KB
/
main.py
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
from fastapi import FastAPI, HTTPException, Request, Query
from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from chromastore import ChromaStore
from openai import OpenAI
from helper import extract_article_details, get_chat_prompt
import os
from dotenv import load_dotenv
import httpx
from typing import Optional, List
import json
# Load environment variables from .env file
load_dotenv()
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
# Get API key from environment variable
openai = OpenAI(
api_key=os.getenv("DEEPINFRA_API_KEY"),
base_url="https://api.deepinfra.com/v1/openai",
)
# Store Google Maps API key for use in templates
GOOGLE_MAPS_API_KEY = os.getenv("GOOGLE_MAPS_API_KEY")
# Constants for Unsplash API
UNSPLASH_API_URL = "https://api.unsplash.com/photos/random"
UNSPLASH_ACCESS_KEY = "axfyxtGBmNUo_ufK-tMD1qQ1kZi7CwUG3qwd-5tHL0M"
class UserInput(BaseModel):
message: str
history: list = []
class ArticleInput(BaseModel):
url: str
class LoginInput(BaseModel):
email: str
password: str
class SignupInput(BaseModel):
name: str
email: str
password: str
chroma_store = ChromaStore()
@app.get("/", response_class=HTMLResponse)
async def root():
# Serve login page as the landing page
with open("static/auth.html", encoding="utf-8") as f:
return f.read()
@app.get("/app", response_class=HTMLResponse)
async def app_page():
# Serve the main app page
with open("static/index.html", encoding="utf-8") as f:
content = f.read()
# Replace placeholder with actual API key
content = content.replace("YOUR_GOOGLE_MAPS_API_KEY", GOOGLE_MAPS_API_KEY)
return content
@app.post("/api/login")
async def login(login_input: LoginInput):
# In a real application, you would validate credentials against a database
# For demo purposes, we'll accept a hardcoded user
if login_input.email == "demo@example.com" and login_input.password == "password":
return {"success": True, "name": "Demo User", "email": login_input.email}
else:
raise HTTPException(status_code=401, detail="Invalid credentials")
@app.post("/api/signup")
async def signup(signup_input: SignupInput):
# In a real application, you would store the user in a database
# For demo purposes, we'll just return success
return {
"success": True,
"name": signup_input.name,
"email": signup_input.email
}
@app.post("/chat")
async def chat(user_input: UserInput):
user_message = user_input.message
chat_history = user_input.history
try:
# Minimal fallback response in case of API issues
fallback_response = {"response": "I'm sorry, I'm having trouble processing your request right now. Please try again later."}
# Log incoming message for debugging
print(f"Received message: {user_message}")
# Add some basic error handling for empty messages
if not user_message or user_message.strip() == "":
return {"response": "I didn't receive a message. Can you please try again?"}
# Query for relevant documents
try:
results = chroma_store.query_documents(
query_text=user_message, user_id="embedding_collection"
)
print(f"Document results: {len(results['documents'] if 'documents' in results else [])}")
except Exception as doc_error:
print(f"Error querying documents: {str(doc_error)}")
results = {"documents": []}
# Get system prompt with context
system_message = get_chat_prompt(results.get("documents", []))
# Prepare messages for the API
messages = [{"role": "system", "content": system_message}]
# Add chat history to messages (limited to last 10 messages for context window considerations)
if chat_history:
for msg in chat_history[-10:]:
messages.append({"role": msg["role"], "content": msg["content"]})
# If the most recent message is not the current user message, add it
if not chat_history or chat_history[-1]["content"] != user_message:
messages.append({"role": "user", "content": user_message})
# Call the LLM with the full conversation context
try:
# Set a timeout for the API call
chat_completion = openai.chat.completions.create(
model="meta-llama/Llama-3.2-3B-Instruct",
messages=messages,
stream=False,
timeout=15 # 15 second timeout
)
# Extract the response content from the completion
if chat_completion and hasattr(chat_completion, 'choices') and len(chat_completion.choices) > 0:
response_content = chat_completion.choices[0].message.content
print(f"Response generated: {response_content[:50]}...") # Log first 50 chars
return {"response": response_content}
else:
print("Empty or invalid completion response")
return fallback_response
except Exception as api_error:
# Handle specific API errors
print(f"API error: {str(api_error)}")
# Provide more specific error messages based on common API issues
if "timeout" in str(api_error).lower():
return {"response": "I'm taking too long to respond. Let me try with a simpler answer. Can you ask me again?"}
elif "rate limit" in str(api_error).lower():
return {"response": "I'm currently handling too many requests. Please try again in a moment."}
else:
return fallback_response
except Exception as e:
print(f"Error in chat endpoint: {str(e)}")
return {"response": "Something went wrong with our conversation. Let's start again."}
@app.post("/extract")
async def extract(article_input: ArticleInput):
url = article_input.url
if not url:
raise HTTPException(status_code=400, detail="URL is required")
extracted_content = extract_article_details(url)
try:
chroma_store.embed_documents(
texts=extracted_content, user_id="embedding_collection"
)
return {"url": url, "content": str(extracted_content)}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/unsplash/random")
async def get_unsplash_random(
collections: Optional[str] = None,
topics: Optional[str] = None,
username: Optional[str] = None,
query: Optional[str] = None,
orientation: Optional[str] = None,
content_filter: Optional[str] = "low",
count: Optional[int] = 1
):
"""
Fetch random images from Unsplash API.
Parameters:
- collections: Public collection ID(s), comma-separated
- topics: Public topic ID(s), comma-separated
- username: Limit selection to a single user's photos
- query: Search term for filtering photos
- orientation: landscape, portrait, or squarish
- content_filter: low or high (default: low)
- count: Number of photos to return (default: 1, max: 30)
"""
# Validate parameters
if orientation and orientation not in ["landscape", "portrait", "squarish"]:
raise HTTPException(
status_code=400,
detail="Invalid orientation. Must be 'landscape', 'portrait', or 'squarish'."
)
if content_filter and content_filter not in ["low", "high"]:
raise HTTPException(
status_code=400,
detail="Invalid content_filter. Must be 'low' or 'high'."
)
if count and (count < 1 or count > 30):
raise HTTPException(
status_code=400,
detail="Invalid count. Must be between 1 and 30."
)
# Prepare query parameters
params = {}
if collections:
params["collections"] = collections
if topics:
params["topics"] = topics
if username:
params["username"] = username
if query:
params["query"] = query
if orientation:
params["orientation"] = orientation
if content_filter:
params["content_filter"] = content_filter
if count:
params["count"] = count
# Prepare headers with authentication
headers = {
"Authorization": f"Client-ID {UNSPLASH_ACCESS_KEY}"
}
try:
async with httpx.AsyncClient() as client:
response = await client.get(
UNSPLASH_API_URL,
params=params,
headers=headers,
timeout=10.0 # 10 seconds timeout
)
# Check for errors
response.raise_for_status()
# Parse response
data = response.json()
# Extract relevant information
if count == 1 and not isinstance(data, list):
# Single image response
result = {
"success": True,
"images": [{
"id": data.get("id"),
"url": data.get("urls", {}).get("regular"),
"thumb": data.get("urls", {}).get("thumb"),
"full": data.get("urls", {}).get("full"),
"download": data.get("links", {}).get("download"),
"description": data.get("description"),
"alt_description": data.get("alt_description"),
"user": {
"name": data.get("user", {}).get("name"),
"username": data.get("user", {}).get("username"),
"portfolio_url": data.get("user", {}).get("portfolio_url"),
},
"location": data.get("location"),
}]
}
else:
# Multiple images response
result = {
"success": True,
"images": [
{
"id": img.get("id"),
"url": img.get("urls", {}).get("regular"),
"thumb": img.get("urls", {}).get("thumb"),
"full": img.get("urls", {}).get("full"),
"download": img.get("links", {}).get("download"),
"description": img.get("description"),
"alt_description": img.get("alt_description"),
"user": {
"name": img.get("user", {}).get("name"),
"username": img.get("user", {}).get("username"),
"portfolio_url": img.get("user", {}).get("portfolio_url"),
},
"location": img.get("location"),
}
for img in data
]
}
# Include rate limit information if available
if "X-Ratelimit-Remaining" in response.headers:
result["rate_limit"] = {
"remaining": response.headers.get("X-Ratelimit-Remaining"),
"total": response.headers.get("X-Ratelimit-Limit"),
}
return result
except httpx.TimeoutException:
raise HTTPException(
status_code=504,
detail="Request to Unsplash API timed out"
)
except httpx.HTTPStatusError as e:
# Handle specific API errors
status_code = e.response.status_code
try:
error_data = e.response.json()
error_message = error_data.get("errors", ["Unknown error"])[0]
except:
error_message = str(e)
raise HTTPException(
status_code=status_code,
detail=f"Unsplash API error: {error_message}"
)
except Exception as e:
raise HTTPException(
status_code=500,
detail=f"An unexpected error occurred: {str(e)}"
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)