|
1 | 1 | import streamlit as st
|
2 | 2 | import os
|
3 |
| -import tempfile |
4 | 3 | from src.rag import RAG
|
5 |
| -from tests.test_rag import initialize_test_database |
| 4 | +from src.data_pipeline import ( |
| 5 | + extract_class_definition, |
| 6 | + extract_class_name_from_query, |
| 7 | +) |
6 | 8 |
|
| 9 | +from config import DEFAULT_GITHUB_REPO |
7 | 10 |
|
8 |
| -# Initialize RAG system with test data |
9 |
| -@st.cache_resource |
10 |
| -def init_rag(): |
11 |
| - # Set your OpenAI API key |
12 |
| - os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"] |
13 | 11 |
|
14 |
| - # Create a temporary directory for the database |
15 |
| - temp_dir = tempfile.mkdtemp() |
16 |
| - db_path = os.path.join(temp_dir, "test_db") |
| 12 | +def init_rag(repo_path_or_url: str): |
| 13 | + |
| 14 | + # from adalflow.utils import setup_env |
| 15 | + |
| 16 | + os.environ["OPENAI_API_KEY"] = st.secrets["OPENAI_API_KEY"] |
17 | 17 |
|
18 |
| - # Initialize test database with example data |
19 |
| - initialize_test_database(db_path) |
| 18 | + rag = RAG() |
| 19 | + print(f"Loading repository from: {repo_path_or_url}") |
| 20 | + rag.prepare_retriever(repo_url_or_path=repo_path_or_url) |
| 21 | + return rag |
20 | 22 |
|
21 |
| - # Create RAG instance with test database using general QA prompt |
22 |
| - return RAG(index_path=db_path, prompt_type="general_qa") |
23 | 23 |
|
| 24 | +st.title("GithubChat") |
| 25 | +st.caption("Learn a repo with RAG assistant") |
24 | 26 |
|
25 |
| -st.title("RAG Chat Interface") |
26 |
| -st.caption( |
27 |
| - "Test data includes information about Alice (software engineer), Bob (data scientist), and the company cafeteria." |
| 27 | +repo_path = st.text_input( |
| 28 | + "Repository Path", |
| 29 | + value=DEFAULT_GITHUB_REPO, |
| 30 | + help="Github repo URL", |
28 | 31 | )
|
29 | 32 |
|
30 |
| -# Initialize session state for chat history |
31 | 33 | if "messages" not in st.session_state:
|
32 | 34 | st.session_state.messages = []
|
| 35 | +if "rag" not in st.session_state: |
| 36 | + st.session_state.rag = None |
33 | 37 |
|
34 |
| -# Initialize RAG |
35 |
| -rag = init_rag() |
| 38 | +if st.button("Initialize local RAG"): |
| 39 | + try: |
| 40 | + st.session_state.rag = init_rag(repo_path) |
| 41 | + if st.session_state.rag: |
| 42 | + st.toast("Repository loaded successfully!") |
| 43 | + except Exception as e: |
| 44 | + st.toast(f"Load failed for repository at: {repo_path}") |
36 | 45 |
|
37 |
| -# Clear chat button |
38 | 46 | if st.button("Clear Chat"):
|
39 | 47 | st.session_state.messages = []
|
40 |
| - # Reset RAG memory |
41 |
| - rag.memory.current_conversation.dialog_turns.clear() |
| 48 | + if st.session_state.rag: |
| 49 | + st.session_state.rag.memory.current_conversation.dialog_turns.clear() |
42 | 50 |
|
43 |
| -# Display chat messages |
44 | 51 | for message in st.session_state.messages:
|
45 | 52 | with st.chat_message(message["role"]):
|
46 | 53 | st.write(message["content"])
|
47 | 54 | if "context" in message:
|
48 |
| - with st.expander(f"View source from {message.get('file_path', 'sample')}"): |
49 |
| - st.code(message["context"], language=message.get("language", "text")) |
| 55 | + with st.expander(f"View source from {message.get('file_path', 'unknown')}"): |
| 56 | + st.code(message["context"], language=message.get("language", "python")) |
50 | 57 |
|
51 |
| -# Chat input |
52 |
| -if prompt := st.chat_input( |
53 |
| - "What would you like to know about Alice, Bob, or the cafeteria?" |
| 58 | +if st.session_state.rag and ( |
| 59 | + prompt := st.chat_input( |
| 60 | + "Ask about the code (e.g., 'Show me the implementation of the RAG class', 'How is memory handled?')" |
| 61 | + ) |
54 | 62 | ):
|
55 |
| - # Add user message to chat history |
56 | 63 | st.session_state.messages.append({"role": "user", "content": prompt})
|
57 | 64 |
|
58 |
| - # Display user message |
59 | 65 | with st.chat_message("user"):
|
60 | 66 | st.write(prompt)
|
61 | 67 |
|
62 |
| - # Get RAG response |
| 68 | + class_name = extract_class_name_from_query(prompt) |
| 69 | + |
63 | 70 | with st.chat_message("assistant"):
|
64 |
| - with st.spinner("Thinking..."): |
65 |
| - response, docs = rag(prompt) |
66 |
| - st.write(response) |
| 71 | + with st.spinner("Analyzing code..."): |
| 72 | + response, docs = st.session_state.rag(prompt) |
67 | 73 |
|
68 |
| - # Show relevant context |
| 74 | + # Show relevant context first, then the explanation |
69 | 75 | if docs and docs[0].documents:
|
70 |
| - context = docs[0].documents[0].text |
71 |
| - file_path = docs[0].documents[0].meta_data.get("title", "sample") |
| 76 | + # Try to find implementation code first |
| 77 | + implementation_docs = [ |
| 78 | + doc |
| 79 | + for doc in docs[0].documents |
| 80 | + if doc.meta_data.get("is_implementation", False) |
| 81 | + ] |
| 82 | + |
| 83 | + # Use implementation if found, otherwise use first document |
| 84 | + doc = ( |
| 85 | + implementation_docs[0] |
| 86 | + if implementation_docs |
| 87 | + else docs[0].documents[0] |
| 88 | + ) |
| 89 | + context = doc.text |
| 90 | + file_path = doc.meta_data.get("file_path", "unknown") |
| 91 | + file_type = doc.meta_data.get("type", "python") |
| 92 | + |
| 93 | + # If asking about a specific class, try to extract just that class definition |
| 94 | + if class_name and file_type == "python": |
| 95 | + class_context = extract_class_definition(context, class_name) |
| 96 | + if class_context != context: # Only use if we found the class |
| 97 | + context = class_context |
| 98 | + |
72 | 99 | with st.expander(f"View source from {file_path}"):
|
73 |
| - st.code(context, language="text") |
| 100 | + st.code(context, language=file_type) |
| 101 | + |
| 102 | + # Now show the explanation |
| 103 | + st.write(response) |
74 | 104 |
|
75 |
| - # Add assistant message with context to chat history |
| 105 | + # Add to chat history |
76 | 106 | st.session_state.messages.append(
|
77 | 107 | {
|
78 | 108 | "role": "assistant",
|
79 | 109 | "content": response,
|
80 | 110 | "context": context,
|
81 | 111 | "file_path": file_path,
|
82 |
| - "language": "text", |
| 112 | + "language": file_type, |
83 | 113 | }
|
84 | 114 | )
|
85 | 115 | else:
|
86 |
| - # Add assistant message without context to chat history |
| 116 | + st.write(response) |
87 | 117 | st.session_state.messages.append(
|
88 | 118 | {"role": "assistant", "content": response}
|
89 | 119 | )
|
| 120 | +elif not st.session_state.rag: |
| 121 | + st.info("Please load a repository first!") |
0 commit comments