1 line
2.5 KiB
JSON
1 line
2.5 KiB
JSON
{"repo": "aadityas18/ekaimpact-ngo", "files": {"rag.py": {"51a3a043f454c4bf55550c30c6b5e97b5529e64b": {"t": 1785674529.5458667, "n": 2275, "h": "b8764000b4d46051", "c": "# rag.py\nimport os\nimport requests\nfrom typing import Optional\nfrom langchain.prompts import PromptTemplate\nfrom langchain_community.vectorstores import FAISS\nfrom langchain_huggingface import HuggingFaceEmbeddings\nfrom dotenv import load_dotenv\nfrom pathlib import Path\n\nRAG_PROMPT = \"\"\"You are an FAQ BOT with access to EkaImpact NGO information. \nUse the provided context to answer the user’s question.\n\nIf the answer is not in the context, say: \"I don’t know based on EkaImpact’s data.\"\n\nContext:\n{context}\n\nQuestion: {question}\n\nAnswer:\"\"\"\n\n# --- Load FAISS index ---\nembedding = HuggingFaceEmbeddings(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")\nvectorstore = FAISS.load_local(\"data/faiss_index\", embedding, allow_dangerous_deserialization=True)\nretriever = vectorstore.as_retriever(search_kwargs={\"k\": 4})\n\n# --- Custom Prompt ---\nprompt_tmpl = PromptTemplate(\n input_variables=[\"context\", \"question\"],\n template=RAG_PROMPT\n)\n\ndef answer_question(question: str, mistral_api_key: str, model: str = \"mistral-small-latest\") -> str:\n \"\"\"\n Ask a question using Mistral API directly (no client lib).\n \"\"\"\n # 1. Retrieve relevant docs\n docs = retriever.invoke(question)\n context = \"\\n\\n\".join([doc.page_content for doc in docs])\n\n # 2. Format prompt\n prompt = RAG_PROMPT.format(context=context, question=question)\n \n\n # 3. Call mistral API directly\n url = \"https://api.mistral.ai/v1/chat/completions\"\n headers = {\"Authorization\": f\"Bearer {mistral_api_key}\", \"Content-Type\": \"application/json\"}\n payload = {\n \"model\": model,\n \"messages\": [\n {\"role\": \"system\", \"content\": \"You are an assistant answering FAQs.\"},\n {\"role\": \"user\", \"content\": prompt}\n ],\n \"max_tokens\": 400\n }\n\n resp = requests.post(url, headers=headers, json=payload)\n if resp.status_code != 200:\n raise RuntimeError(f\"API Error {resp.status_code}: {resp.text}\")\n\n result = resp.json()\n return result[\"choices\"][0][\"message\"][\"content\"]\n\n# --- Script entry ---\nif __name__ == \"__main__\":\n load_dotenv(dotenv_path=Path(__file__).with_name(\".env\"))\n key= os.getenv(\"MISTRAL_API_KEY\") \n \n q = input(\"Question: \")\n print(\"Answer:\", answer_question(q, mistral_api_key=key))\n"}}}} |