1- import streamlit as st
21import logging
3- from typing import Dict , List
2+
3+ import streamlit as st
44from openai import OpenAI
5- from coderag .config import OPENAI_API_KEY , OPENAI_CHAT_MODEL
5+
6+ from coderag .config import OPENAI_API_KEY
67from prompt_flow import execute_rag_flow
78
89# Configure logging for Streamlit
2324
2425# Set page config
2526st .set_page_config (
26- page_title = "CodeRAG: Your Coding Assistant" ,
27- page_icon = "🤖" ,
28- layout = "wide"
27+ page_title = "CodeRAG: Your Coding Assistant" , page_icon = "🤖" , layout = "wide"
2928)
3029
3130st .title ("🤖 CodeRAG: Your Coding Assistant" )
4039# Sidebar with controls
4140with st .sidebar :
4241 st .header ("Controls" )
43-
42+
4443 if st .button ("🗑️ Clear Conversation" , type = "secondary" ):
4544 st .session_state .messages = []
4645 st .session_state .conversation_context = []
4746 st .rerun ()
48-
47+
4948 # Status indicators
5049 st .header ("Status" )
5150 if client :
5251 st .success ("✅ OpenAI Connected" )
5352 else :
5453 st .error ("❌ OpenAI Not Connected" )
5554 st .error ("Please check your API key in .env file" )
56-
55+
5756 # Conversation stats
5857 if st .session_state .messages :
5958 st .info (f"💬 { len (st .session_state .messages )} messages in conversation" )
6867
6968# Chat input with validation
7069if not client :
71- st .warning ("⚠️ OpenAI client not available. Please configure your API key to use the assistant." )
70+ st .warning (
71+ "⚠️ OpenAI client not available. Please configure your API key to use "
72+ "the assistant."
73+ )
7274 st .stop ()
7375
7476if prompt := st .chat_input ("What is your coding question?" , disabled = not client ):
7577 # Validate input
7678 if not prompt .strip ():
7779 st .warning ("Please enter a valid question." )
7880 st .stop ()
79-
81+
8082 # Add user message
8183 st .session_state .messages .append ({"role" : "user" , "content" : prompt })
8284 # Add to conversation context for better continuity
8385 st .session_state .conversation_context .append (f"User: { prompt } " )
84-
86+
8587 with st .chat_message ("user" ):
8688 st .markdown (prompt )
8789
8890 with st .chat_message ("assistant" ):
8991 message_placeholder = st .empty ()
90-
92+
9193 # Show loading indicator
9294 with st .spinner ("🔍 Searching codebase and generating response..." ):
9395 try :
9496 # Execute RAG flow with error handling
9597 response = execute_rag_flow (prompt )
96-
98+
9799 # Check if response indicates an error
98- if response .startswith ("Error:" ) or "error occurred" in response .lower ():
100+ if (
101+ response .startswith ("Error:" )
102+ or "error occurred" in response .lower ()
103+ ):
99104 message_placeholder .error (response )
100105 else :
101106 message_placeholder .markdown (response )
102-
107+
103108 full_response = response
104-
109+
105110 except Exception as e :
106111 error_message = f"Unexpected error: { str (e )} "
107112 logger .error (f"Streamlit error: { error_message } " )
108113 message_placeholder .error (error_message )
109114 full_response = error_message
110115
111116 # Add assistant response to session
112- st .session_state .messages .append ({"role" : "assistant" , "content" : full_response })
117+ st .session_state .messages .append (
118+ {"role" : "assistant" , "content" : full_response }
119+ )
113120 # Add to conversation context
114- st .session_state .conversation_context .append (f"Assistant: { full_response [:200 ]} ..." ) # Truncate for context
115-
121+ st .session_state .conversation_context .append (
122+ f"Assistant: { full_response [:200 ]} ..."
123+ ) # Truncate for context
124+
116125 # Keep conversation context manageable (last 10 exchanges)
117126 if len (st .session_state .conversation_context ) > 20 :
118- st .session_state .conversation_context = st .session_state .conversation_context [- 20 :]
127+ st .session_state .conversation_context = (
128+ st .session_state .conversation_context [- 20 :]
129+ )
119130
120131# Footer with helpful information
121132if not st .session_state .messages :
122133 st .markdown ("---" )
123134 st .markdown ("### 💡 Tips for better results:" )
124- st .markdown ("""
135+ st .markdown (
136+ """
125137 - Ask specific questions about your code
126138 - Mention file names or functions you're interested in
127139 - Request explanations, improvements, or debugging help
128140 - Ask about code patterns or best practices
129- """ )
130-
141+ """
142+ )
143+
131144 st .markdown ("### 🚀 Example queries:" )
132145 col1 , col2 = st .columns (2 )
133146 with col1 :
134147 if st .button ("📝 Explain the indexing process" ):
135- st .session_state .messages .append ({"role" : "user" , "content" : "Explain how the FAISS indexing works in this codebase" })
148+ st .session_state .messages .append (
149+ {
150+ "role" : "user" ,
151+ "content" : "Explain how the FAISS indexing works in this codebase" ,
152+ }
153+ )
136154 st .rerun ()
137155 with col2 :
138156 if st .button ("🐛 Help debug search issues" ):
139- st .session_state .messages .append ({"role" : "user" , "content" : "How can I debug issues with code search not returning results?" })
140- st .rerun ()
157+ st .session_state .messages .append (
158+ {
159+ "role" : "user" ,
160+ "content" : (
161+ "How can I debug issues with code search not returning "
162+ "results?"
163+ ),
164+ }
165+ )
166+ st .rerun ()
0 commit comments