Spaces:
Sleeping
Sleeping
vhr1007
commited on
Commit
·
bcd2179
1
Parent(s):
d343a87
x_api_key new end point
Browse files- app.py +84 -1
- utils/auth_x.py +7 -0
app.py
CHANGED
@@ -9,6 +9,7 @@ from utils.auth import token_required
|
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
import torch
|
|
|
12 |
|
13 |
# Load environment variables from .env file
|
14 |
load_dotenv()
|
@@ -78,7 +79,11 @@ class SearchDocumentsRequest(BaseModel):
|
|
78 |
|
79 |
class GenerateRAGRequest(BaseModel):
|
80 |
search_query: str
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
|
83 |
|
84 |
@app.get("/")
|
@@ -160,6 +165,84 @@ async def generate_rag_response_api(
|
|
160 |
logging.error(f"Unexpected error: {e}")
|
161 |
raise HTTPException(status_code=500, detail=str(e))
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
if __name__ == '__main__':
|
164 |
import uvicorn
|
165 |
uvicorn.run(app, host='0.0.0.0', port=8000)
|
|
|
9 |
from dotenv import load_dotenv
|
10 |
import os
|
11 |
import torch
|
12 |
+
from utils.auth_x import x_api_key_auth
|
13 |
|
14 |
# Load environment variables from .env file
|
15 |
load_dotenv()
|
|
|
79 |
|
80 |
class GenerateRAGRequest(BaseModel):
|
81 |
search_query: str
|
82 |
+
|
83 |
+
class XApiKeyRequest(BaseModel):
|
84 |
+
organization_id: str
|
85 |
+
user_id: str
|
86 |
+
search_query: str
|
87 |
|
88 |
|
89 |
@app.get("/")
|
|
|
165 |
logging.error(f"Unexpected error: {e}")
|
166 |
raise HTTPException(status_code=500, detail=str(e))
|
167 |
|
168 |
+
@app.post("/api/search-documents/v1")
|
169 |
+
async def search_documents_x_api_key(
|
170 |
+
body: XApiKeyRequest,
|
171 |
+
authorized: bool = Depends(x_api_key_auth)
|
172 |
+
):
|
173 |
+
if not authorized:
|
174 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
175 |
+
|
176 |
+
organization_id = body.organization_id
|
177 |
+
user_id = body.user_id
|
178 |
+
logging.info(f'search query {body.search_query}')
|
179 |
+
logging.info(f"organization_id: {organization_id}, user_id: {user_id}")
|
180 |
+
logging.info("Received request to search documents with x-api-key auth")
|
181 |
+
try:
|
182 |
+
logging.info("Starting document search")
|
183 |
+
|
184 |
+
# Encode the query using the custom embedding function
|
185 |
+
query_embedding = embed_text(body.search_query)
|
186 |
+
collection_name = "embed" # Use the collection name where the embeddings are stored
|
187 |
+
|
188 |
+
# Perform search using the precomputed embeddings
|
189 |
+
hits, error = searcher.search_documents(collection_name, query_embedding, user_id, limit=3)
|
190 |
+
|
191 |
+
if error:
|
192 |
+
logging.error(f"Search documents error: {error}")
|
193 |
+
raise HTTPException(status_code=500, detail=error)
|
194 |
+
|
195 |
+
logging.info(f"Document search completed with {len(hits)} hits")
|
196 |
+
return hits
|
197 |
+
except Exception as e:
|
198 |
+
logging.error(f"Unexpected error: {e}")
|
199 |
+
raise HTTPException(status_code=500, detail=str(e))
|
200 |
+
|
201 |
+
@app.post("/api/generate-rag-response/v1")
|
202 |
+
async def generate_rag_response_x_api_key(
|
203 |
+
body: XApiKeyRequest,
|
204 |
+
authorized: bool = Depends(x_api_key_auth)
|
205 |
+
):
|
206 |
+
# Assuming x_api_key_auth validates the key
|
207 |
+
if not authorized:
|
208 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
209 |
+
|
210 |
+
organization_id = body.organization_id
|
211 |
+
user_id = body.user_id
|
212 |
+
|
213 |
+
logging.info(f'search query {body.search_query}')
|
214 |
+
logging.info(f"organization_id: {organization_id}, user_id: {user_id}")
|
215 |
+
logging.info("Received request to generate RAG response with x-api-key auth")
|
216 |
+
try:
|
217 |
+
logging.info("Starting document search")
|
218 |
+
|
219 |
+
# Encode the query using the custom embedding function
|
220 |
+
query_embedding = embed_text(body.search_query)
|
221 |
+
collection_name = "embed" # Use the collection name where the embeddings are stored
|
222 |
+
|
223 |
+
# Perform search using the precomputed embeddings
|
224 |
+
hits, error = searcher.search_documents(collection_name, query_embedding, user_id)
|
225 |
+
|
226 |
+
if error:
|
227 |
+
logging.error(f"Search documents error: {error}")
|
228 |
+
raise HTTPException(status_code=500, detail=error)
|
229 |
+
|
230 |
+
logging.info("Generating RAG response")
|
231 |
+
|
232 |
+
# Generate the RAG response using the retrieved documents
|
233 |
+
response, error = generate_rag_response(hits, body.search_query)
|
234 |
+
|
235 |
+
if error:
|
236 |
+
logging.error(f"Generate RAG response error: {error}")
|
237 |
+
raise HTTPException(status_code=500, detail=error)
|
238 |
+
|
239 |
+
return {"response": response}
|
240 |
+
except Exception as e:
|
241 |
+
logging.error(f"Unexpected error: {e}")
|
242 |
+
raise HTTPException(status_code=500, detail=str(e))
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
if __name__ == '__main__':
|
247 |
import uvicorn
|
248 |
uvicorn.run(app, host='0.0.0.0', port=8000)
|
utils/auth_x.py
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import Header, HTTPException
|
2 |
+
|
3 |
+
def x_api_key_auth(x_api_key: str = Header(...)):
|
4 |
+
expected_api_key = "your_expected_api_key" # Replace with your actual API key
|
5 |
+
if x_api_key != expected_api_key:
|
6 |
+
raise HTTPException(status_code=401, detail="Invalid API Key")
|
7 |
+
return True
|