Spaces:
Sleeping
Sleeping
Refactor get_summaries and chat endpoints to improve logging and remove unnecessary whitespace
4b89ce5
| 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=["*"], | |
| ) | |
| def greet_json(): | |
| return {"Hello": "World!"} | |
| 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}) | |
| 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"} | |
| 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 | |
| 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"] | |
| } | |