Spaces:
Sleeping
Sleeping
| # import os | |
| # from fastapi import FastAPI, HTTPException | |
| # from pydantic import BaseModel | |
| # from fastapi.middleware.cors import CORSMiddleware | |
| # from dotenv import load_dotenv | |
| # # --- Modern LangChain Imports (LCEL) --- | |
| # from langchain_groq import ChatGroq | |
| # from langchain_huggingface import HuggingFaceEmbeddings | |
| # from langchain_chroma import Chroma | |
| # from langchain_core.prompts import ChatPromptTemplate | |
| # from langchain.chains import create_retrieval_chain | |
| # from langchain.chains.combine_documents import create_stuff_documents_chain | |
| # # 1. Load Environment | |
| # load_dotenv() | |
| # # 2. Setup App | |
| # app = FastAPI() | |
| # app.add_middleware( | |
| # CORSMiddleware, | |
| # allow_origins=["*"], | |
| # allow_credentials=True, | |
| # allow_methods=["*"], | |
| # allow_headers=["*"], | |
| # ) | |
| # print("Initializing AI Backend...") | |
| # # 3. Setup The Brain | |
| # try: | |
| # # A. Embeddings | |
| # embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") | |
| # # B. Database | |
| # db = Chroma( | |
| # persist_directory="chroma_db", | |
| # embedding_function=embeddings | |
| # ) | |
| # # C. LLM | |
| # llm = ChatGroq( | |
| # model="llama-3.1-8b-instant", | |
| # temperature=0, | |
| # api_key=os.getenv("GROQ_API_KEY") | |
| # ) | |
| # # D. The Modern RAG Chain (LCEL Style) | |
| # # 1. Create the Prompt (UPDATED with Strict Rules & Persona) | |
| # custom_prompt_template = """ | |
| # You are the Official AI Agent of Ganga Bhumi Club at VIT Bhopal. | |
| # Your Persona: Warm, friendly, and cultural. You belong to UP/Bihar/Jharkhand culture. | |
| # Language: Mix 70% English and 30% Hinglish (e.g., use words like "Ji", "Namaskar", "Bhaiya", "Dost"). | |
| # STRICT RULES (DO NOT IGNORE): | |
| # 1. USE ONLY the Context below. Do not use outside knowledge. | |
| # 2. If the user asks about something NOT in the context (like "Who is Modi?" or "Python code"), politely redirect them: "Arre dost, main bas Ganga Bhumi Club ki baatein karta hoon. Club ke baare mein kuch puchiye na!" | |
| # 3. Do not assume or generalize. If specific data (like a phone number) is missing, say: "Maaf kijiye, ye jaankari abhi database mein nahi hai." | |
| # Context Data: | |
| # <context> | |
| # {context} | |
| # </context> | |
| # User Question: {input} | |
| # Your Answer:""" | |
| # prompt = ChatPromptTemplate.from_template(custom_prompt_template) | |
| # # 2. Create the Document Chain (Combines retrieved docs into the prompt) | |
| # document_chain = create_stuff_documents_chain(llm, prompt) | |
| # # 3. Create the Retrieval Chain (Connects database -> docs -> LLM) | |
| # retriever = db.as_retriever(search_kwargs={"k": 3}) | |
| # retrieval_chain = create_retrieval_chain(retriever, document_chain) | |
| # print("✅ AI Backend Ready!") | |
| # except Exception as e: | |
| # print(f"❌ Error during initialization: {e}") | |
| # retrieval_chain = None | |
| # # 4. API Endpoint | |
| # class QueryRequest(BaseModel): | |
| # query: str | |
| # @app.post("/ask") | |
| # async def ask_question(request: QueryRequest): | |
| # if not retrieval_chain: | |
| # raise HTTPException(status_code=500, detail="Server failed to initialize AI.") | |
| # try: | |
| # # Note: In LCEL, the input key is often 'input' instead of 'query' | |
| # response = retrieval_chain.invoke({"input": request.query}) | |
| # # The answer is in the 'answer' key | |
| # return {"answer": response["answer"]} | |
| # except Exception as e: | |
| # raise HTTPException(status_code=500, detail=str(e)) | |
| # @app.get("/") | |
| # def read_root(): | |
| # return {"status": "Club Bot Server is Running"} | |
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from dotenv import load_dotenv | |
| # --- Modern LangChain Imports (LCEL) --- | |
| from langchain_groq import ChatGroq | |
| from langchain_huggingface import HuggingFaceEmbeddings | |
| from langchain_chroma import Chroma | |
| from langchain_core.prompts import ChatPromptTemplate | |
| from langchain.chains import create_retrieval_chain | |
| from langchain.chains.combine_documents import create_stuff_documents_chain | |
| # 1. Load Environment | |
| load_dotenv() | |
| # 2. Setup App | |
| app = FastAPI() | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| print("Initializing AI Backend...") | |
| # 3. Setup The Brain | |
| try: | |
| # A. Embeddings | |
| embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-mpnet-base-v2") | |
| # B. Database | |
| db = Chroma( | |
| persist_directory="chroma_db", | |
| embedding_function=embeddings | |
| ) | |
| # C. LLM | |
| llm = ChatGroq( | |
| model="llama-3.1-8b-instant", | |
| temperature=0, | |
| api_key=os.getenv("GROQ_API_KEY") | |
| ) | |
| # D. The Modern RAG Chain (LCEL Style) | |
| # 1. Create the Prompt (UPDATED with Your Professional Persona) | |
| custom_prompt_template = """ | |
| You are the Official AI Agent of Ganga Bhumi Club (GBC), VIT Bhopal. | |
| PERSONA: | |
| You are warm, polite, culturally rooted, and respectful. | |
| You represent the culture of Uttar Pradesh, Bihar, and Jharkhand. | |
| Your communication must reflect humility, respect, and dignity. | |
| LANGUAGE STYLE: | |
| • 70% English + 30% Hinglish | |
| • Use respectful words such as: “Ji”, “Namaskar”, “Aap”, “Unka” | |
| • Avoid casual or slang words like: “tu”, “tum”, “dost”, “arre bhai” | |
| • Maintain an official yet friendly tone suitable for an academic institution | |
| STRICT ENFORCEMENT RULES (DO NOT IGNORE): | |
| 1. CONTEXT BOUNDARY RULE | |
| You MUST use ONLY the information provided in the Context. | |
| Do NOT use outside knowledge, assumptions, or general explanations. | |
| 2. EXACT MATCH PRIORITY | |
| If the user asks about a person, role, event, or detail that exists in the Context, | |
| you MUST answer directly and accurately using respectful language. | |
| 3. NO ASSUMPTIONS RULE | |
| If any specific information is missing from the Context, respond politely: | |
| “Maaf kijiye, yeh jaankari abhi hamare database mein uplabdh nahi hai.” | |
| 4. REDIRECTION RULE (ONLY WHEN NECESSARY) | |
| If the user asks something completely unrelated to Ganga Bhumi Club | |
| (for example: politics, coding, general knowledge), | |
| DO NOT answer the question. | |
| Politely redirect using this tone: | |
| “Namaskar Ji, main sirf Ganga Bhumi Club se judi jaankari hi pradan karta hoon. | |
| Agar aap club, events, ya team ke baare mein kuch puchna chahte hain, | |
| toh kripya batayein.” | |
| 5. RESPONSE FORMAT RULE | |
| • Use short, clear paragraphs or bullet points | |
| • Be factual, calm, and welcoming | |
| • Never exaggerate roles or achievements | |
| Context Data: | |
| <context> | |
| {context} | |
| </context> | |
| User Question: {input} | |
| Your Answer:""" | |
| prompt = ChatPromptTemplate.from_template(custom_prompt_template) | |
| # 2. Create the Document Chain (Combines retrieved docs into the prompt) | |
| document_chain = create_stuff_documents_chain(llm, prompt) | |
| # 3. Create the Retrieval Chain (Connects database -> docs -> LLM) | |
| retriever = db.as_retriever(search_kwargs={"k": 3}) | |
| retrieval_chain = create_retrieval_chain(retriever, document_chain) | |
| print("✅ AI Backend Ready!") | |
| except Exception as e: | |
| print(f"❌ Error during initialization: {e}") | |
| retrieval_chain = None | |
| # 4. API Endpoint | |
| class QueryRequest(BaseModel): | |
| query: str | |
| async def ask_question(request: QueryRequest): | |
| if not retrieval_chain: | |
| raise HTTPException(status_code=500, detail="Server failed to initialize AI.") | |
| try: | |
| # Note: In LCEL, the input key is often 'input' instead of 'query' | |
| response = retrieval_chain.invoke({"input": request.query}) | |
| # The answer is in the 'answer' key | |
| return {"answer": response["answer"]} | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def read_root(): | |
| return {"status": "Club Bot Server is Running"} |