import streamlit as st import json import torch import numpy as np import pandas as pd from sentence_transformers import SentenceTransformer import lancedb import os import requests from groq import Groq from dotenv import load_dotenv # Load environment variables load_dotenv() # =============================== # PAGE CONFIG & PREMIUM TECH UI # =============================== st.set_page_config(page_title="Git Intelligence System", page_icon="đģ", layout="wide") st.markdown(""" """, unsafe_allow_html=True) # =============================== # RESOURCE LOADING # =============================== @st.cache_resource def load_resources(): device = 'cuda' if torch.cuda.is_available() else 'cpu' model = SentenceTransformer("BAAI/bge-m3", device=device) # Add FlashRank reranker as in Ramayana app from flashrank import Ranker, RerankRequest ranker = Ranker(model_name="ms-marco-MiniLM-L-12-v2") db = lancedb.connect("./.lancedb") tbl = db.open_table("git_knowledge") with open('alias_map.json', 'r', encoding='utf-8') as f: alias_map = json.load(f) return model, ranker, tbl, alias_map try: model, ranker, tbl, alias_map = load_resources() except Exception as e: st.error(f"Failed to load resources: {e}") st.stop() # =============================== # LLM LOGIC # =============================== def get_answer_from_llm(query, context): system_prompt = """You are a STERN and HIGHLY PRECISE Technical Document Analyst. Your objective is to answer questions using ONLY the provided Context Chunks. STRICT ANTI-HALLUCINATION PROTOCOLS: 1. ABSOLUTE GROUNDING: Every claim you make MUST be directly supported by the provided text. 2. NO EXTERNAL KNOWLEDGE: Do NOT use your internal training data to explain commands, syntax, or concepts. If the information is not in the context, you MUST say: "The provided documentation does not contain information regarding [Topic]." 3. LIST vs. DESCRIPTION: If the context merely lists a command (e.g., in a 'git --help' style table) but does not provide details or syntax, you MUST state: "The documentation mentions [Command] but provides no instructions or explanations." 4. CITE EVERYTHING: When providing an answer, explicitly mention which 'Source' provided the data. 5. NEGATIVE CONSTRAINT: Do NOT be 'helpful' by providing external tutorials. Your helpfulness is measured by how accurately you report ONLY what is in the document.""" messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": f"Context Chunks:\n{context}\n\nQuestion: {query}"} ] input_tokens = len(str(messages)) // 4 g_key = os.getenv("GROQ_API_KEY") if g_key: try: client = Groq(api_key=g_key) # Try different models for m in ["llama-3.3-70b-versatile", "mixtral-8x7b-32768"]: try: res = client.chat.completions.create(model=m, messages=messages, temperature=0.1) ans = res.choices[0].message.content return ans, m, input_tokens, len(ans) // 4 except Exception: continue except Exception: pass s_key = os.getenv("SAMBANOVA_API_KEY") if s_key and s_key != "your_key_here": try: url = "https://api.sambanova.ai/v1/chat/completions" headers = {"Authorization": f"Bearer {s_key}", "Content-Type": "application/json"} data = { "model": "Meta-Llama-3.3-70B-Instruct", "messages": messages, "temperature": 0.1, "stream": False } res = requests.post(url, headers=headers, json=data, timeout=12) if res.status_code == 200: ans = res.json()['choices'][0]['message']['content'] return ans, "SambaNova (Llama-3.3)", input_tokens, len(ans) // 4 except Exception: pass return "LLM integration failed. Please check API keys.", "None", 0, 0 # =============================== # MAIN UI # =============================== st.markdown('
> git query --intelligent-search --ultra-premium
', unsafe_allow_html=True) if "messages" not in st.session_state: st.session_state.messages = [] # =============================== # SIDEBAR # =============================== with st.sidebar: st.markdown('', unsafe_allow_html=True) # Display Messages for msg in st.session_state.messages: if msg["role"] == "user": st.markdown(f'Expert RAG system for official Git Documentation with FlashRank Optimization.
v2.0.4-stable
RAG-ACTIVE