baderanas commited on
Commit
bc957c6
·
verified ·
1 Parent(s): 52ba610

Update rag.py

Browse files
Files changed (1) hide show
  1. rag.py +12 -45
rag.py CHANGED
@@ -1,32 +1,22 @@
1
- from fastapi import FastAPI, HTTPException
2
- from pydantic import BaseModel
3
  from typing import List, Optional
4
  from llms import initialize_llms, answer_query_with_chunks, query_to_retrieve
5
  from chroma_operations.retrieve import search_similar_chunks
6
-
7
- # Initialize FastAPI
8
- app = FastAPI()
9
-
10
-
11
- # Define request model
12
- class RAGRequest(BaseModel):
13
- query_text: str
14
- file_names: List[str]
15
- collection_name: str = "rag_collection"
16
-
17
 
18
  # Load LLM once at startup
19
  llms = initialize_llms()
20
  llm = llms["llm"]
21
 
22
-
23
- @app.post("/ask")
24
- async def ask_question(
25
- query_text: str, file_names: List[str], collection_name: str = "rag_collection"
26
- ):
27
  try:
 
28
  query_search = query_to_retrieve(query_text, llm)
29
-
 
30
  retrieved_docs = search_similar_chunks(
31
  query_search,
32
  document_names=file_names,
@@ -34,34 +24,11 @@ async def ask_question(
34
  )
35
 
36
  if not retrieved_docs:
37
- raise HTTPException(status_code=404, detail="No matching documents found.")
38
-
39
- answer = answer_query_with_chunks(request.query_text, retrieved_docs, llm)
40
- return {"answer": answer, "chunks": retrieved_docs}
41
-
42
- except Exception as e:
43
- raise HTTPException(status_code=500, detail=str(e))
44
-
45
-
46
- # For backward compatibility with older clients
47
- @app.post("/ask-single")
48
- async def ask_question_single(
49
- query_text: str, file_name: str, collection_name: str = "rag_collection"
50
- ):
51
- try:
52
- query_search = query_to_retrieve(query_text, llm)
53
-
54
- retrieved_docs = search_similar_chunks(
55
- query_search,
56
- document_names=[file_name], # Convert single filename to a list
57
- collection_name=collection_name,
58
- )
59
-
60
- if not retrieved_docs:
61
- raise HTTPException(status_code=404, detail="No matching documents found.")
62
 
 
63
  answer = answer_query_with_chunks(query_text, retrieved_docs, llm)
64
  return {"answer": answer, "chunks": retrieved_docs}
65
 
66
  except Exception as e:
67
- raise HTTPException(status_code=500, detail=str(e))
 
 
 
1
  from typing import List, Optional
2
  from llms import initialize_llms, answer_query_with_chunks, query_to_retrieve
3
  from chroma_operations.retrieve import search_similar_chunks
4
+ import asyncio
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Load LLM once at startup
7
  llms = initialize_llms()
8
  llm = llms["llm"]
9
 
10
+ def ask_question(
11
+ query_text: str,
12
+ file_names: List[str],
13
+ collection_name: str = "rag_collection"
14
+ ) -> dict:
15
  try:
16
+ # Await async LLM calls
17
  query_search = query_to_retrieve(query_text, llm)
18
+
19
+ # Await ChromaDB search
20
  retrieved_docs = search_similar_chunks(
21
  query_search,
22
  document_names=file_names,
 
24
  )
25
 
26
  if not retrieved_docs:
27
+ return {"answer": "No matching documents found.", "chunks": []}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # Await answer generation
30
  answer = answer_query_with_chunks(query_text, retrieved_docs, llm)
31
  return {"answer": answer, "chunks": retrieved_docs}
32
 
33
  except Exception as e:
34
+ return {"answer": f"Error processing request: {str(e)}", "chunks": []}