Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
157 lines (117 loc) · 3.81 KB
/
Copy pathapp.py
File metadata and controls
157 lines (117 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
from pathlib import Path
import requests
import streamlit as st
from search import search, ask_llm
LANGUAGES = {
".py": "python",
".js": "javascript",
".ts": "typescript",
".tsx": "tsx",
".jsx": "jsx",
".java": "java",
".go": "go",
".rs": "rust",
".c": "c",
".cpp": "cpp",
".h": "c",
".cs": "csharp",
".rb": "ruby",
".php": "php",
".sh": "bash",
".sql": "sql",
".html": "html",
".css": "css",
".json": "json",
".yaml": "yaml",
".yml": "yaml",
".toml": "toml",
".md": "markdown",
}
EXAMPLE_QUESTIONS = [
"What does this codebase do?",
"How is the search index built?",
"Where is the LLM called?",
]
st.set_page_config(
page_title="Codebase RAG Assistant",
page_icon="🔎",
layout="centered",
)
def language_for(filename):
return LANGUAGES.get(Path(filename).suffix.lower(), "text")
def render_sources(results):
with st.expander(f"📚 {len(results)} retrieved sources"):
for rank, result in enumerate(results, start=1):
st.markdown(
f"**{rank}. `{result['filename']}`** "
f"· distance `{result['distance']:.3f}`"
)
st.code(
result["content"],
language=language_for(result["filename"]),
line_numbers=True,
)
def answer_question(question, k):
results = search(question, k=k)
context = "\n\n".join(
f"FILE: {result['filename']}\n"
f"{result['content']}"
for result in results
)
try:
answer = ask_llm(question, context)
except requests.exceptions.ConnectionError:
answer = (
"⚠️ Could not reach the LLM at `localhost:11434`. "
"Make sure Ollama is running (`ollama serve`)."
)
except Exception as error:
answer = f"⚠️ Something went wrong while generating an answer: `{error}`"
return answer, results
if "messages" not in st.session_state:
st.session_state.messages = []
with st.sidebar:
st.header("⚙️ Settings")
k = st.slider(
"Sources to retrieve",
min_value=1,
max_value=10,
value=3,
help="Number of code chunks passed to the LLM as context.",
)
show_sources = st.toggle("Show retrieved sources", value=True)
st.divider()
if st.button("🗑️ Clear conversation", width="stretch"):
st.session_state.messages = []
st.rerun()
st.caption("Embeddings: `all-MiniLM-L6-v2` · LLM: `llama3.2` via Ollama")
st.title("🔎 Codebase RAG Assistant")
st.caption("Ask questions about your code. Answers are grounded in retrieved source files.")
pending_question = None
if not st.session_state.messages:
st.markdown("##### Try asking")
columns = st.columns(len(EXAMPLE_QUESTIONS))
for column, example in zip(columns, EXAMPLE_QUESTIONS):
if column.button(example, width="stretch"):
pending_question = example
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if show_sources and message.get("sources"):
render_sources(message["sources"])
question = st.chat_input("Ask a question about the codebase") or pending_question
if question:
st.session_state.messages.append({"role": "user", "content": question})
with st.chat_message("user"):
st.markdown(question)
with st.chat_message("assistant"):
with st.spinner("Searching the codebase and thinking..."):
answer, results = answer_question(question, k)
st.markdown(answer)
if show_sources:
render_sources(results)
st.session_state.messages.append(
{"role": "assistant", "content": answer, "sources": results}
)
if pending_question:
st.rerun()