|
| 1 | +""" |
| 2 | +Git Second Brain - Streamlit Chat UI |
| 3 | +Ask natural-language questions about FastAPI's commit history, |
| 4 | +powered by Oracle AI Database 26ai Vector Search + LangChain + OpenAI. |
| 5 | +
|
| 6 | +Run: |
| 7 | + streamlit run app.py |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | + |
| 12 | +import streamlit as st |
| 13 | +from langchain_core.output_parsers import StrOutputParser |
| 14 | +from langchain_core.prompts import ChatPromptTemplate |
| 15 | +from langchain_openai import ChatOpenAI |
| 16 | +from retriever import OracleCommitRetriever |
| 17 | + |
| 18 | +# ========================= Page config ========================= |
| 19 | +st.set_page_config( |
| 20 | + page_title="Git Second Brain", |
| 21 | + page_icon="🧠", |
| 22 | + layout="wide", |
| 23 | +) |
| 24 | + |
| 25 | +# ========================= Sidebar ============================= |
| 26 | +with st.sidebar: |
| 27 | + st.title("Git Second Brain") |
| 28 | + st.caption("Oracle AI Database 26ai + LangChain + OpenAI") |
| 29 | + |
| 30 | + openai_key = st.text_input( |
| 31 | + "OpenAI API Key", |
| 32 | + type="password", |
| 33 | + value=os.getenv("OPENAI_API_KEY", ""), |
| 34 | + help="Stored only in this session, never persisted.", |
| 35 | + ) |
| 36 | + |
| 37 | + model_name = st.selectbox( |
| 38 | + "Model", |
| 39 | + ["gpt-4o-mini", "gpt-4o", "gpt-4.1-mini", "gpt-4.1-nano"], |
| 40 | + index=0, |
| 41 | + ) |
| 42 | + |
| 43 | + top_k = st.slider("Commits to retrieve", min_value=3, max_value=15, value=8) |
| 44 | + |
| 45 | + temperature = st.slider("Temperature", min_value=0.0, max_value=1.0, value=0.2, step=0.05) |
| 46 | + |
| 47 | + st.divider() |
| 48 | + st.markdown( |
| 49 | + "**How it works**\n\n" |
| 50 | + "1. Your question is embedded with sentence-transformers\n" |
| 51 | + "2. Oracle 26ai runs `VECTOR_DISTANCE` to find the most relevant commits\n" |
| 52 | + "3. LangChain passes those commits as context to OpenAI\n" |
| 53 | + "4. You get a grounded answer with commit citations" |
| 54 | + ) |
| 55 | + |
| 56 | + st.divider() |
| 57 | + st.markdown("**Sample questions**") |
| 58 | + sample_questions = [ |
| 59 | + "Why did FastAPI switch to Pydantic v2?", |
| 60 | + "How has dependency injection evolved?", |
| 61 | + "What were the biggest breaking changes in the last 2 years?", |
| 62 | + "When did lifespan replace startup/shutdown events?", |
| 63 | + "What security fixes were applied recently?", |
| 64 | + ] |
| 65 | + for q in sample_questions: |
| 66 | + if st.button(q, use_container_width=True): |
| 67 | + st.session_state["prefill"] = q |
| 68 | + |
| 69 | +# ========================= System prompt ======================= |
| 70 | +SYSTEM_PROMPT = """\ |
| 71 | +You are Git Second Brain, an AI assistant that answers questions about the |
| 72 | +FastAPI open-source project by analyzing its Git commit history. |
| 73 | +
|
| 74 | +You will receive a set of relevant commits retrieved from Oracle AI Database 26ai |
| 75 | +via vector similarity search. Use ONLY these commits to answer the question. |
| 76 | +If the commits do not contain enough information, say so honestly. |
| 77 | +
|
| 78 | +Rules: |
| 79 | +- Cite specific commits by their short SHA and date when supporting a claim. |
| 80 | +- Summarize the narrative arc when multiple commits tell a story. |
| 81 | +- Keep answers concise but thorough (3-6 paragraphs max). |
| 82 | +- If you are unsure, say "Based on the commits I found..." to hedge. |
| 83 | +- Never invent commit SHAs or dates. |
| 84 | +""" |
| 85 | + |
| 86 | +RAG_TEMPLATE = ChatPromptTemplate.from_messages( |
| 87 | + [ |
| 88 | + ("system", SYSTEM_PROMPT), |
| 89 | + ("human", "Retrieved commits:\n\n{context}\n\n---\nQuestion: {question}"), |
| 90 | + ] |
| 91 | +) |
| 92 | + |
| 93 | +# ========================= Init state ========================== |
| 94 | +if "messages" not in st.session_state: |
| 95 | + st.session_state.messages = [] |
| 96 | + |
| 97 | +if "retriever" not in st.session_state: |
| 98 | + with st.spinner("Connecting to Oracle AI Database 26ai ..."): |
| 99 | + st.session_state.retriever = OracleCommitRetriever(top_k=top_k) |
| 100 | + |
| 101 | +# ========================= Chat display ======================== |
| 102 | +st.header("Ask your repo anything") |
| 103 | + |
| 104 | +for msg in st.session_state.messages: |
| 105 | + with st.chat_message(msg["role"]): |
| 106 | + st.markdown(msg["content"]) |
| 107 | + if msg.get("sources"): |
| 108 | + with st.expander(f"Retrieved commits ({len(msg['sources'])})"): |
| 109 | + for doc in msg["sources"]: |
| 110 | + meta = doc.metadata |
| 111 | + st.markdown( |
| 112 | + f"**`{meta['sha'][:10]}`** | {meta['date']} | " |
| 113 | + f"*{meta['author']}*\n\n" |
| 114 | + f"> {meta['subject']}" |
| 115 | + ) |
| 116 | + st.divider() |
| 117 | + |
| 118 | +# ========================= Chat input ========================== |
| 119 | +prefill = st.session_state.pop("prefill", None) |
| 120 | +user_input = st.chat_input("Ask about FastAPI's history ...") or prefill |
| 121 | + |
| 122 | +if user_input: |
| 123 | + if not openai_key: |
| 124 | + st.error("Please enter your OpenAI API key in the sidebar.") |
| 125 | + st.stop() |
| 126 | + |
| 127 | + # Show user message |
| 128 | + st.session_state.messages.append({"role": "user", "content": user_input}) |
| 129 | + with st.chat_message("user"): |
| 130 | + st.markdown(user_input) |
| 131 | + |
| 132 | + # Retrieve from Oracle 26ai |
| 133 | + with st.chat_message("assistant"): |
| 134 | + with st.spinner("Searching Oracle 26ai Vector Search ..."): |
| 135 | + retriever = st.session_state.retriever |
| 136 | + retriever.top_k = top_k |
| 137 | + docs = retriever.invoke(user_input) |
| 138 | + |
| 139 | + context = "\n\n---\n\n".join(doc.page_content for doc in docs) |
| 140 | + |
| 141 | + # LangChain RAG chain |
| 142 | + llm = ChatOpenAI( |
| 143 | + model=model_name, |
| 144 | + temperature=temperature, |
| 145 | + api_key=openai_key, |
| 146 | + ) |
| 147 | + chain = RAG_TEMPLATE | llm | StrOutputParser() |
| 148 | + |
| 149 | + with st.spinner("Generating answer ..."): |
| 150 | + answer = chain.invoke({"context": context, "question": user_input}) |
| 151 | + |
| 152 | + st.markdown(answer) |
| 153 | + |
| 154 | + # Show retrieved commits |
| 155 | + with st.expander(f"Retrieved commits ({len(docs)})"): |
| 156 | + for doc in docs: |
| 157 | + meta = doc.metadata |
| 158 | + st.markdown( |
| 159 | + f"**`{meta['sha'][:10]}`** | {meta['date']} | " |
| 160 | + f"*{meta['author']}*\n\n" |
| 161 | + f"> {meta['subject']}" |
| 162 | + ) |
| 163 | + st.divider() |
| 164 | + |
| 165 | + st.session_state.messages.append( |
| 166 | + { |
| 167 | + "role": "assistant", |
| 168 | + "content": answer, |
| 169 | + "sources": docs, |
| 170 | + } |
| 171 | + ) |
0 commit comments