Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/arjunthilak05_multimodal-rag.json
T

1 line
5.1 KiB
JSON

{"repo": "arjunthilak05/multimodal-rag", "files": {"rag.py": {"584e92d88f2fa5676c9b08cd3762687675ac4046": {"t": 1785674360.3067925, "n": 4753, "h": "c62912239742d3a6", "c": "import os\nfrom openai import OpenAI\nfrom retrival import HybridRAGRetriever\n\nclass RAGSystem:\n def __init__(self, api_key, model=\"x-ai/grok-4.1-fast\"):\n \"\"\"Initialize RAG system with Hybrid Retrieval.\"\"\"\n print(\"Initializing RAG System...\")\n self.retriever = HybridRAGRetriever()\n \n self.client = OpenAI(\n base_url=\"https://openrouter.ai/api/v1\",\n api_key=api_key\n )\n self.model = model\n print(f\"✅ RAG System ready! Using model: {model}\")\n \n def generate_answer(self, query, context_items, max_tokens=500):\n \"\"\"Generate answer using LLM with retrieved context.\"\"\"\n context_text = \"\\n\\n\".join([\n f\"[Source: {item['source']} | Type: {item['type']}]\\n{item['content']}\"\n for item in context_items\n ])\n \n prompt = f\"\"\"You are a helpful assistant answering questions about NeoTech Solutions company policies.\n\nUse the following context to answer the question. If the answer is not in the context, say \"I don't have enough information to answer that question.\"\n\nCONTEXT:\n{context_text}\n\nQUESTION: {query}\n\nANSWER:\"\"\"\n \n response = self.client.chat.completions.create(\n model=self.model,\n messages=[\n {\"role\": \"user\", \"content\": prompt}\n ],\n max_tokens=max_tokens,\n temperature=0.7\n )\n \n return response.choices[0].message.content\n \n def query(self, question, top_k=5, alpha=0.6, show_sources=True):\n \"\"\"Main RAG pipeline with hybrid retrieval.\"\"\"\n print(f\"\\n{'='*80}\")\n print(f\"🔍 Question: {question}\")\n print(f\"{'='*80}\")\n \n print(\"\\n📚 Retrieving relevant context (Hybrid: CLIP + BM25)...\")\n results = self.retriever.retrieve(query_text=question, top_k=top_k, alpha=alpha)\n context_items = results['results']\n \n if not context_items:\n return {\n 'question': question,\n 'answer': \"I couldn't find any relevant information to answer your question.\",\n 'sources': []\n }\n \n print(f\"💭 Generating answer using {self.model}...\")\n answer = self.generate_answer(question, context_items)\n \n result = {\n 'question': question,\n 'answer': answer,\n 'sources': [\n {\n 'rank': item['rank'],\n 'source': item['source'],\n 'type': item['type'],\n 'hybrid_score': item['hybrid_score'],\n 'clip_score': item['clip_score'],\n 'bm25_score': item['bm25_score'],\n 'content_preview': item['content'][:200] + \"...\"\n }\n for item in context_items\n ]\n }\n \n self.display_result(result, show_sources)\n return result\n \n def display_result(self, result, show_sources=True):\n \"\"\"Pretty print the RAG result.\"\"\"\n print(f\"\\n{'='*80}\")\n print(\"✨ ANSWER\")\n print(f\"{'='*80}\")\n print(result['answer'])\n \n if show_sources and result['sources']:\n print(f\"\\n{'='*80}\")\n print(\"📖 SOURCES (Hybrid Retrieval)\")\n print(f\"{'='*80}\")\n for source in result['sources']:\n print(f\"\\n[{source['rank']}] {source['source']} ({source['type']})\")\n print(f\"Scores - Hybrid: {source['hybrid_score']:.4f} | CLIP: {source['clip_score']:.4f} | BM25: {source['bm25_score']:.4f}\")\n print(f\"Preview: {source['content_preview']}\")\n\ndef main():\n API_KEY = \"sk-or-v1-###\"\n \n rag = RAGSystem(\n api_key=API_KEY,\n model=\"x-ai/grok-4.1-fast\"\n )\n \n print(\"\\n\" + \"=\"*80)\n print(\"🤖 NeoTech Solutions RAG System - Hybrid Retrieval Mode\")\n print(\"=\"*80)\n print(\"Features: CLIP Semantic Search + BM25 Keyword Search + Priority Re-ranking\")\n print(\"Ask questions about company policies, or type 'quit' to exit.\\n\")\n \n while True:\n try:\n question = input(\"Your question: \").strip()\n \n if not question:\n continue\n \n if question.lower() in ['quit', 'exit', 'q']:\n print(\"\\n👋 Goodbye!\")\n break\n \n rag.query(question, top_k=5, alpha=0.6, show_sources=True)\n print(\"\\n\" + \"=\"*80 + \"\\n\")\n \n except KeyboardInterrupt:\n print(\"\\n\\n👋 Goodbye!\")\n break\n except Exception as e:\n print(f\"\\n❌ Error: {e}\")\n print(\"Please try again.\\n\")\n\nif __name__ == \"__main__\":\n main()\n"}}}}