-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
132 lines (105 loc) · 4.14 KB
/
app.py
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
import streamlit as st
import os
from src.rag import RAG
from typing import List
from config import DEFAULT_GITHUB_REPO
def init_rag(repo_path_or_url: str):
# from adalflow.utils import setup_env
os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"]
rag = RAG()
print(f"Loading repository from: {repo_path_or_url}")
rag.prepare_retriever(repo_url_or_path=repo_path_or_url)
return rag
st.title("GithubChat")
st.caption("Learn a repo with RAG assistant")
repo_path = st.text_input(
"Repository Path",
value=DEFAULT_GITHUB_REPO,
help="Github repo URL",
)
if "messages" not in st.session_state:
st.session_state.messages = []
if "rag" not in st.session_state:
st.session_state.rag = None
if st.button("Initialize local RAG"):
try:
st.session_state.rag = init_rag(repo_path)
if st.session_state.rag:
st.toast("Repository loaded successfully!")
except Exception as e:
st.toast(f"Load failed for repository at: {repo_path}")
# TODO: Better reset the conversation
if st.button("Clear Chat"):
st.session_state.messages = []
if st.session_state.rag:
st.session_state.rag.memory.current_conversation.dialog_turns.clear()
def display_messages():
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.write("Assistant:")
if "rationale" in message:
st.write(message["rationale"])
st.write(message["content"])
if "context" in message:
with st.expander("View context"):
for doc in message["context"]:
st.write(
f"file_path: {doc.meta_data.get('file_path', 'unknown')}"
)
st.write(f"language: {doc.meta_data.get('type', 'unknown')}")
language = doc.meta_data.get("type", "python")
if language == "py":
st.code(doc.text, language="python")
else:
st.write(doc.text)
from adalflow.core.types import Document
def form_context(context: List[Document]):
formatted_context = ""
for doc in context:
formatted_context += ""
f"file_path: {doc.meta_data.get('file_path', 'unknown')} \n"
f"language: {doc.meta_data.get('type', 'python')} \n"
f"content: {doc.text} \n"
return formatted_context
if st.session_state.rag and (
query := st.chat_input(
"Ask about the code (e.g., 'Show me the implementation of the RAG class', 'How is memory handled?')"
)
):
st.session_state.messages.append({"role": "user", "content": query})
with st.chat_message("user"):
st.write(query)
with st.chat_message("assistant"):
with st.spinner("Analyzing code..."):
st.write(f"memory: {st.session_state.rag.memory()}")
response, docs = st.session_state.rag(query)
# Show relevant context first, then the explanation
if docs and docs[0].documents:
context = docs[0].documents
# Add to chat history
st.write(f"add to history")
st.session_state.messages.append(
{
"role": "assistant",
"rationale": (
response.rationale
if hasattr(response, "rationale")
else None
),
"content": (
response.answer
if hasattr(response, "answer")
else response.raw_response
),
"context": context,
}
)
else:
st.write(response)
st.session_state.messages.append(
{"role": "assistant", "content": response}
)
elif not st.session_state.rag:
st.info("Please load a repository first!")
# Finally, call display_messages *after* everything is appended
display_messages()