File size: 2,535 Bytes
27ccfa6
e0ec272
942b420
e0ec272
948e715
 
51a3d33
 
 
 
 
e0ec272
 
27ccfa6
 
 
948e715
 
 
 
 
 
 
 
 
27ccfa6
 
e0ec272
 
 
 
 
 
 
948e715
 
 
 
e0ec272
51a3d33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
942b420
 
 
4b89ce5
 
 
942b420
 
 
4b89ce5
 
 
942b420
 
 
 
 
 
 
 
 
 
 
e0ec272
 
 
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
from fastapi import FastAPI
from langgraph.agents.summarize_agent.graph import graph   
from langgraph.agents.rag_agent.graph import graph as rag_graph
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware

from langchain_core.documents import Document

from utils.create_vectordb import create_chroma_db_and_document,query_chroma_db





app = FastAPI()

# cors 
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.get("/")
def greet_json():
    return {"Hello": "World!"}



@app.post("/summarize")
async def summarize(request: Request):
    data = await request.json()
    urls = data.get("urls")
    codes = data.get("codes")
    notes = data.get("notes")
    return  graph.invoke({"urls": urls, "codes": codes, "notes": notes})


@app.post("/save_summary")
async def save_summary(request: Request):
    data = await request.json()
    summary = data.get("summary", "")
    post_id = data.get("post_id", None)
    title = data.get("title", "")
    category = data.get("category", "") 
    tags = data.get("tags", [])
    references = data.get("references", [])
    
    page_content = f"""
    Title: {title}
    Category: {category}
    Tags: {', '.join(tags)}
    Summary: {summary}
    """
    
    document = Document(
        page_content=page_content,
        id = str(post_id)
        
    )
    
    is_added = create_chroma_db_and_document(document)
    
    if not is_added:
        return {"error": "Failed to save summary to the database." , "status": "error"}
    
    return {"message": "Summary saved successfully." , "status": "success"}

@app.post("/summaries")
async def get_summaries(request: Request):
  
    data = await request.json()
    print(data)
    query = data.get("query" , "")
    
    print(f"Query received: {query}")
    results = query_chroma_db(query=query)
    return results
    
    
    
    
@app.post("/chat")
async def chat(request: Request):
    data = await request.json()
    
    print(f"Chat request data: {data}")
  
    user_input = data.get("message", "")
    chat_history = data.get("chat_history", [])
    
    print(f"User input: {user_input}")
    print(f"Chat history: {chat_history}")
    
    # Invoke the RAG chatbot graph
    result = rag_graph.invoke({
        "user_input": user_input,
        "chat_history": chat_history
    })
    
    return {
        "response": result["response"],
        "chat_history": result["chat_history"]
    }