5
5
extract_class_definition ,
6
6
extract_class_name_from_query ,
7
7
)
8
+ from typing import List
8
9
9
10
from config import DEFAULT_GITHUB_REPO
10
11
@@ -51,9 +52,22 @@ def init_rag(repo_path_or_url: str):
51
52
for message in st .session_state .messages :
52
53
with st .chat_message (message ["role" ]):
53
54
st .write (message ["content" ])
54
- if "context" in message :
55
- with st .expander (f"View source from { message .get ('file_path' , 'unknown' )} " ):
56
- st .code (message ["context" ], language = message .get ("language" , "python" ))
55
+ # if "context" in message:
56
+ # with st.expander(f"View source from {message.get('file_path', 'unknown')}"):
57
+ # st.code(message["context"], language=message.get("language", "python"))
58
+
59
+ from adalflow .core .types import Document
60
+
61
+
62
+ def form_context (context : List [Document ]):
63
+ formatted_context = ""
64
+ for doc in context :
65
+ formatted_context += ""
66
+ f"file_path: { doc .meta_data .get ('file_path' , 'unknown' )} \n "
67
+ f"language: { doc .meta_data .get ('type' , 'python' )} \n "
68
+ f"content: { doc .text } \n "
69
+ return formatted_context
70
+
57
71
58
72
if st .session_state .rag and (
59
73
prompt := st .chat_input (
@@ -65,51 +79,29 @@ def init_rag(repo_path_or_url: str):
65
79
with st .chat_message ("user" ):
66
80
st .write (prompt )
67
81
68
- class_name = extract_class_name_from_query (prompt )
82
+ # class_name = extract_class_name_from_query(prompt)
83
+ query = prompt
69
84
70
85
with st .chat_message ("assistant" ):
71
86
with st .spinner ("Analyzing code..." ):
72
87
response , docs = st .session_state .rag (prompt )
73
88
74
89
# Show relevant context first, then the explanation
75
90
if docs and docs [0 ].documents :
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
-
99
- with st .expander (f"View source from { file_path } " ):
100
- st .code (context , language = file_type )
91
+ context = docs [0 ].documents
101
92
102
93
# Now show the explanation
103
- st .write (response )
94
+ st .write (f"Rationale: { response .rationale } " )
95
+ st .write (f"Answer: { response .answer } " )
96
+
97
+ st .write (f"context: { form_context (context )} " )
104
98
105
99
# Add to chat history
106
100
st .session_state .messages .append (
107
101
{
108
102
"role" : "assistant" ,
109
103
"content" : response ,
110
104
"context" : context ,
111
- "file_path" : file_path ,
112
- "language" : file_type ,
113
105
}
114
106
)
115
107
else :
0 commit comments