-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
73 lines (59 loc) · 2.18 KB
/
Copy pathexample.py
File metadata and controls
73 lines (59 loc) · 2.18 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
from fastapi import FastAPI
from pydantic import BaseModel
import uvicorn
from langchain_core.messages import HumanMessage
# Import UltimateRAG components
from ultimaterag.core.container import rag_engine
from ultimaterag.utils.Response_Helper import make_response
from ultimaterag.utils.Response_Helper_Model import HTTPStatusCode, APICode
# Create a new FastAPI app
app = FastAPI(title="UltimateRAG Custom Example")
class QueryRequest(BaseModel):
query: str
@app.post("/ask")
async def ask_agent(request: QueryRequest):
"""
Custom endpoint using the UltimateRAG engine to answer a query.
1. Retrieves relevant documents using vector_manager.
2. Generates an answer using the LLM.
"""
try:
# 1. Retrieval
print(f"Searching for: {request.query}")
docs = rag_engine.vector_manager.similarity_search(
query=request.query,
k=3,
filter={"access_level": "common"} # Basic filtering
)
context = "\n\n".join([doc.page_content for doc in docs])
# 2. Generation
prompt = f"""You are a helpful assistant. Use the context below to answer the user's question.
Context:
{context}
Question:
{request.query}
Answer:"""
response = rag_engine.llm.invoke([HumanMessage(content=prompt)])
return make_response(
status=HTTPStatusCode.OK,
code=APICode.OK,
message="Query processed successfully",
data={
"answer": response.content,
"retrieved_context": [doc.page_content for doc in docs]
}
)
except Exception as e:
return make_response(
status=HTTPStatusCode.INTERNAL_SERVER_ERROR,
code=APICode.INTERNAL_SERVER_ERROR,
message="Failed to process query",
error=str(e)
)
@app.get("/health")
async def health_check():
return {"status": "ok", "rag_engine_initialized": rag_engine is not None}
if __name__ == "__main__":
# Run on a different port (8001) to avoid conflict with main app
print("Starting Custom UltimateRAG Example on port 8001...")
uvicorn.run(app, host="0.0.0.0", port=8001)