1 line
12 KiB
JSON
1 line
12 KiB
JSON
{"repo": "atlaslattice/sheldonbrain-rag-api", "files": {"rag_api.py": {"a57bbbf97a321981cc0bff5338d2e3d25c1dd448": {"t": 1785674550.7583, "n": 5741, "h": "995e47f64b794708", "c": "\"\"\"\nFlask API for RAG operations.\nProvides HTTP endpoints for querying and storing memories.\n\"\"\"\n\nfrom flask import Flask, request, jsonify\nfrom flask_cors import CORS\nfrom pinecone_client import query_similar, upsert_document, get_index_stats\nfrom rag_wrapper import recall_memory, store_memory\nfrom grok_rag import grok_with_rag\nfrom notion_sync import full_sync\nimport os\n\napp = Flask(__name__)\nCORS(app)\n\n\n@app.route('/health', methods=['GET'])\ndef health():\n \"\"\"\n Health check endpoint.\n Returns index statistics.\n \"\"\"\n try:\n stats = get_index_stats()\n return jsonify({\n \"status\": \"healthy\",\n \"pinecone_stats\": stats,\n \"api_keys_configured\": {\n \"pinecone\": bool(os.getenv(\"PINECONE_API_KEY\")),\n \"openai\": bool(os.getenv(\"OPENAI_API_KEY\")),\n \"xai\": bool(os.getenv(\"XAI_API_KEY\")),\n \"notion\": bool(os.getenv(\"NOTION_API_KEY\"))\n }\n })\n except Exception as e:\n return jsonify({\n \"status\": \"error\",\n \"error\": str(e)\n }), 500\n\n\n@app.route('/query', methods=['POST'])\ndef query():\n \"\"\"\n Query for similar memories.\n \n Body:\n {\n \"query\": \"search query\",\n \"top_k\": 5,\n \"sphere_filter\": \"S069\" // optional\n }\n \"\"\"\n try:\n data = request.json\n query_text = data.get(\"query\")\n top_k = data.get(\"top_k\", 5)\n sphere_filter = data.get(\"sphere_filter\")\n \n if not query_text:\n return jsonify({\"error\": \"query is required\"}), 400\n \n results = query_similar(\n query=query_text,\n top_k=top_k,\n sphere_filter=sphere_filter\n )\n \n return jsonify({\n \"success\": True,\n \"query\": query_text,\n \"results\": results,\n \"count\": len(results)\n })\n \n except Exception as e:\n return jsonify({\n \"success\": False,\n \"error\": str(e)\n }), 500\n\n\n@app.route('/recall', methods=['POST'])\ndef recall():\n \"\"\"\n Recall memories in formatted text (for prompt injection).\n \n Body:\n {\n \"query\": \"search query\",\n \"top_k\": 5,\n \"sphere_filter\": \"S069\" // optional\n }\n \"\"\"\n try:\n data = request.json\n query_text = data.get(\"query\")\n top_k = data.get(\"top_k\", 5)\n sphere_filter = data.get(\"sphere_filter\")\n \n if not query_text:\n return jsonify({\"error\": \"query is required\"}), 400\n \n memories = recall_memory(\n query=query_text,\n top_k=top_k,\n sphere_filter=sphere_filter\n )\n \n return jsonify({\n \"success\": True,\n \"query\": query_text,\n \"memories\": memories\n })\n \n except Exception as e:\n return jsonify({\n \"success\": False,\n \"error\": str(e)\n }), 500\n\n\n@app.route('/store', methods=['POST'])\ndef store():\n \"\"\"\n Store new memory.\n \n Body:\n {\n \"content\": \"text to store\",\n \"sphere\": \"S016\",\n \"source\": \"grok_session\",\n \"metadata\": {} // optional additional metadata\n }\n \"\"\"\n try:\n data = request.json\n content = data.get(\"content\")\n sphere = data.get(\"sphere\", \"S016\")\n source = data.get(\"source\", \"api\")\n additional_metadata = data.get(\"metadata\", {})\n \n if not content:\n return jsonify({\"error\": \"content is required\"}), 400\n \n doc_id = store_memory(\n content=content,\n sphere=sphere,\n source=source,\n additional_metadata=additional_metadata\n )\n \n return jsonify({\n \"success\": True,\n \"document_id\": doc_id\n })\n \n except Exception as e:\n return jsonify({\n \"success\": False,\n \"error\": str(e)\n }), 500\n\n\n@app.route('/grok', methods=['POST'])\ndef grok():\n \"\"\"\n Query Grok with RAG enhancement.\n \n Body:\n {\n \"message\": \"user message\",\n \"model\": \"grok-beta\", // optional\n \"auto_recall\": true, // optional\n \"auto_store\": false, // optional\n \"conversation_history\": [] // optional\n }\n \"\"\"\n try:\n data = request.json\n message = data.get(\"message\")\n model = data.get(\"model\", \"grok-beta\")\n auto_recall = data.get(\"auto_recall\", True)\n auto_store = data.get(\"auto_store\", False)\n conversation_history = data.get(\"conversation_history\", [])\n \n if not message:\n return jsonify({\"error\": \"message is required\"}), 400\n \n result = grok_with_rag(\n user_message=message,\n model=model,\n auto_recall=auto_recall,\n auto_store=auto_store,\n conversation_history=conversation_history\n )\n \n return jsonify(result)\n \n except Exception as e:\n return jsonify({\n \"success\": False,\n \"error\": str(e)\n }), 500\n\n\n@app.route('/sync', methods=['POST'])\ndef sync():\n \"\"\"\n Trigger full sync from Notion to Pinecone.\n \"\"\"\n try:\n synced_count = full_sync()\n return jsonify({\n \"success\": True,\n \"synced_count\": synced_count\n })\n except Exception as e:\n return jsonify({\n \"success\": False,\n \"error\": str(e)\n }), 500\n\n\nif __name__ == '__main__':\n port = int(os.getenv('PORT', 5050))\n app.run(host='0.0.0.0', port=port, debug=True)\n"}}, "grok_rag.py": {"a57bbbf97a321981cc0bff5338d2e3d25c1dd448": {"t": 1785674551.3898227, "n": 5781, "h": "a54d7daca5043db4", "c": "\"\"\"\nGrok API integration with RAG memory.\nThis enables Grok to access full Notion workspace context.\n\"\"\"\n\nfrom xai_sdk import Client\nfrom config import XAI_API_KEY\nfrom rag_wrapper import recall_memory, store_memory, enrich_prompt_with_memory\nfrom typing import Optional\n\n# Initialize Grok client\ngrok_client = Client(api_key=XAI_API_KEY)\n\n\ndef grok_with_rag(\n user_message: str,\n model: str = \"grok-beta\",\n system_prompt: Optional[str] = None,\n auto_recall: bool = True,\n auto_store: bool = False,\n conversation_history: list = None\n) -> dict:\n \"\"\"\n Call Grok API with RAG memory enhancement.\n \n Args:\n user_message: The user's message/query\n model: Grok model to use (grok-beta, grok-2-1212, etc.)\n system_prompt: Optional system prompt\n auto_recall: Automatically recall relevant memories before responding\n auto_store: Automatically store the response as new memory\n conversation_history: Previous messages in the conversation\n \n Returns:\n Dictionary with response and metadata\n \"\"\"\n # Build messages array\n messages = []\n \n # Add conversation history if provided\n if conversation_history:\n messages.extend(conversation_history)\n \n # Recall relevant memories if enabled\n recalled_context = \"\"\n if auto_recall:\n recalled_context = recall_memory(user_message, top_k=5)\n print(f\"📚 Recalled {recalled_context.count('Memory')} relevant memories\")\n \n # Build enhanced user message\n if recalled_context and recalled_context != \"[No relevant memories found]\":\n enhanced_message = f\"{recalled_context}\\n\\n{user_message}\"\n else:\n enhanced_message = user_message\n \n messages.append({\n \"role\": \"user\",\n \"content\": enhanced_message\n })\n \n # Call Grok API using the correct SDK method\n try:\n response = grok_client.chat.create(\n model=model,\n messages=messages\n )\n \n assistant_message = response.choices[0].message.content\n \n # Store response as new memory if enabled\n stored_id = None\n if auto_store:\n memory_content = f\"Query: {user_message}\\n\\nResponse: {assistant_message}\"\n stored_id = store_memory(\n content=memory_content,\n sphere=\"S016\", # Computer Science/AI\n source=\"grok_session\",\n additional_metadata={\n \"model\": model,\n \"query\": user_message[:200]\n }\n )\n print(f\"💾 Stored response as memory: {stored_id}\")\n \n return {\n \"success\": True,\n \"response\": assistant_message,\n \"recalled_memories\": recalled_context if auto_recall else None,\n \"stored_memory_id\": stored_id,\n \"model\": model,\n \"usage\": {\n \"prompt_tokens\": response.usage.prompt_tokens if hasattr(response, 'usage') else 0,\n \"completion_tokens\": response.usage.completion_tokens if hasattr(response, 'usage') else 0,\n \"total_tokens\": response.usage.total_tokens if hasattr(response, 'usage') else 0\n }\n }\n \n except Exception as e:\n return {\n \"success\": False,\n \"error\": str(e),\n \"response\": None\n }\n\n\ndef grok_query_with_context(query: str, sphere_filter: Optional[str] = None) -> str:\n \"\"\"\n Simple helper: Query Grok with automatic RAG context injection.\n \n Args:\n query: The question to ask Grok\n sphere_filter: Optional sphere to filter memories by\n \n Returns:\n Grok's response string\n \"\"\"\n # Recall memories with optional sphere filter\n context = recall_memory(query, top_k=5, sphere_filter=sphere_filter)\n \n # Build enhanced prompt\n enhanced_query = f\"{context}\\n\\n{query}\"\n \n # Call Grok\n response = grok_client.chat.create(\n model=\"grok-beta\",\n messages=[{\"role\": \"user\", \"content\": enhanced_query}]\n )\n \n return response.choices[0].message.content\n\n\ndef interactive_grok_session():\n \"\"\"\n Start an interactive Grok session with RAG memory.\n \"\"\"\n print(\"🤖 Grok RAG Interactive Session\")\n print(\"=\" * 50)\n print(\"Type 'exit' to quit, 'recall <query>' to search memories\")\n print(\"=\" * 50)\n \n conversation_history = []\n \n while True:\n user_input = input(\"\\n👤 You: \").strip()\n \n if user_input.lower() == \"exit\":\n print(\"👋 Goodbye!\")\n break\n \n if user_input.lower().startswith(\"recall \"):\n # Manual memory recall\n recall_query = user_input[7:]\n memories = recall_memory(recall_query, top_k=5)\n print(f\"\\n📚 Memories:\\n{memories}\")\n continue\n \n # Call Grok with RAG\n result = grok_with_rag(\n user_message=user_input,\n auto_recall=True,\n auto_store=False,\n conversation_history=conversation_history\n )\n \n if result[\"success\"]:\n print(f\"\\n🤖 Grok: {result['response']}\")\n \n # Update conversation history\n conversation_history.append({\"role\": \"user\", \"content\": user_input})\n conversation_history.append({\"role\": \"assistant\", \"content\": result['response']})\n else:\n print(f\"\\n❌ Error: {result['error']}\")\n\n\nif __name__ == \"__main__\":\n # Example usage\n import sys\n \n if len(sys.argv) > 1:\n query = \" \".join(sys.argv[1:])\n print(f\"Query: {query}\\n\")\n response = grok_query_with_context(query)\n print(f\"Response: {response}\")\n else:\n interactive_grok_session()\n"}}}} |