@@ -628,6 +628,196 @@ def show_about_page():
628
628
""" )
629
629
630
630
631
+ def show_ai_chat_page ():
632
+ """Display AI Chat Assistant page"""
633
+ st .markdown ('<h1 class="main-header">🤖 AI Chat Assistant</h1>' , unsafe_allow_html = True )
634
+ st .markdown ("**Conversational AI for Invoice Processing & Analytics**" )
635
+
636
+ # Check agent status
637
+ AGENT_URL = "http://localhost:8001" # Our test agent
638
+
639
+ col1 , col2 = st .columns ([4 , 1 ])
640
+
641
+ with col2 :
642
+ try :
643
+ response = requests .get (f"{ AGENT_URL } /agent/status" , timeout = 5 )
644
+ if response .status_code == 200 :
645
+ status_data = response .json ()
646
+ st .success ("✅ AI Agent Ready" )
647
+
648
+ with st .expander ("🛠️ Agent Info" ):
649
+ st .write (f"**Status:** { status_data .get ('status' , 'unknown' ).title ()} " )
650
+ st .write (f"**Uptime:** { status_data .get ('uptime' , 'unknown' )} " )
651
+ st .write (f"**Conversations:** { status_data .get ('conversation_length' , 0 )} " )
652
+
653
+ tools = status_data .get ('available_tools' , [])
654
+ st .write ("**Available Tools:**" )
655
+ for tool in tools :
656
+ st .write (f"• { tool .replace ('_' , ' ' ).title ()} " )
657
+ else :
658
+ st .error ("❌ Agent Offline" )
659
+ st .info ("Start the AI agent: `python tests/test_agent.py` in port 8001" )
660
+ return
661
+ except :
662
+ st .error ("❌ Cannot connect to AI Agent" )
663
+ st .info ("💡 Make sure test_agent.py is running on port 8001" )
664
+ return
665
+
666
+ # Initialize chat history
667
+ if "chat_messages" not in st .session_state :
668
+ st .session_state .chat_messages = []
669
+
670
+ # Chat container
671
+ with col1 :
672
+ st .markdown ("### 💬 Chat with AI Assistant" )
673
+
674
+ # Display chat messages
675
+ for message in st .session_state .chat_messages :
676
+ with st .chat_message (message ["role" ]):
677
+ st .write (message ["content" ])
678
+
679
+ # Chat input
680
+ if prompt := st .chat_input ("Ask me about invoice processing, search, or analytics..." ):
681
+ # Add user message
682
+ st .session_state .chat_messages .append ({"role" : "user" , "content" : prompt })
683
+
684
+ # Display user message
685
+ with st .chat_message ("user" ):
686
+ st .write (prompt )
687
+
688
+ # Get agent response
689
+ with st .spinner ("🤖 Processing your request..." ):
690
+ try :
691
+ response = requests .post (
692
+ f"{ AGENT_URL } /agent/chat" ,
693
+ json = {"message" : prompt },
694
+ timeout = 30
695
+ )
696
+ if response .status_code == 200 :
697
+ agent_response = response .json ()["response" ]
698
+ st .session_state .chat_messages .append ({
699
+ "role" : "assistant" ,
700
+ "content" : agent_response
701
+ })
702
+
703
+ # Display agent response
704
+ with st .chat_message ("assistant" ):
705
+ st .write (agent_response )
706
+ else :
707
+ st .error ("Failed to get response from AI assistant" )
708
+ except Exception as e :
709
+ st .error (f"Error: { str (e )} " )
710
+
711
+ st .rerun ()
712
+
713
+ # Quick action buttons
714
+ st .markdown ("---" )
715
+ st .subheader ("⚡ Quick Actions" )
716
+
717
+ col1 , col2 , col3 , col4 = st .columns (4 )
718
+
719
+ def send_quick_message (message ):
720
+ """Helper function to send quick messages"""
721
+ st .session_state .chat_messages .append ({"role" : "user" , "content" : message })
722
+ try :
723
+ response = requests .post (
724
+ f"{ AGENT_URL } /agent/chat" ,
725
+ json = {"message" : message },
726
+ timeout = 30
727
+ )
728
+ if response .status_code == 200 :
729
+ agent_response = response .json ()["response" ]
730
+ st .session_state .chat_messages .append ({
731
+ "role" : "assistant" ,
732
+ "content" : agent_response
733
+ })
734
+ except :
735
+ pass
736
+ st .rerun ()
737
+
738
+ with col1 :
739
+ if st .button ("💡 What can you help with?" ):
740
+ send_quick_message ("What can you help me with?" )
741
+
742
+ with col2 :
743
+ if st .button ("📄 How to process invoices?" ):
744
+ send_quick_message ("How do I process an invoice?" )
745
+
746
+ with col3 :
747
+ if st .button ("🔍 Search examples" ):
748
+ send_quick_message ("Show me search examples" )
749
+
750
+ with col4 :
751
+ if st .button ("📊 Analytics insights" ):
752
+ send_quick_message ("What analytics can you provide?" )
753
+
754
+ # Chat controls
755
+ st .markdown ("---" )
756
+ col1 , col2 , col3 = st .columns ([1 , 1 , 2 ])
757
+
758
+ with col1 :
759
+ if st .button ("🗑️ Clear Chat" ):
760
+ st .session_state .chat_messages = []
761
+ st .rerun ()
762
+
763
+ with col2 :
764
+ if st .button ("💾 Export Chat" ):
765
+ if st .session_state .chat_messages :
766
+ chat_export = {
767
+ "timestamp" : datetime .now ().isoformat (),
768
+ "messages" : st .session_state .chat_messages
769
+ }
770
+ json_data = json .dumps (chat_export , indent = 2 )
771
+ st .download_button (
772
+ label = "📥 Download Chat History" ,
773
+ data = json_data ,
774
+ file_name = f"chat_history_{ datetime .now ().strftime ('%Y%m%d_%H%M%S' )} .json" ,
775
+ mime = "application/json"
776
+ )
777
+
778
+ with col3 :
779
+ if st .session_state .chat_messages :
780
+ st .info (f"💬 { len (st .session_state .chat_messages )} messages in this conversation" )
781
+
782
+ # Example questions
783
+ with st .expander ("💡 Example Questions to Try" ):
784
+ col1 , col2 = st .columns (2 )
785
+
786
+ with col1 :
787
+ st .markdown ("""
788
+ **Processing & Upload:**
789
+ - "How do I process an invoice?"
790
+ - "What file formats do you support?"
791
+ - "How accurate is the extraction?"
792
+
793
+ **Search & Query:**
794
+ - "Find invoices from Microsoft"
795
+ - "Show invoices over $5000"
796
+ - "List recent vendor invoices"
797
+ """ )
798
+
799
+ with col2 :
800
+ st .markdown ("""
801
+ **Analytics & Insights:**
802
+ - "What spending insights can you provide?"
803
+ - "Analyze my vendor relationships"
804
+ - "Flag any unusual patterns"
805
+
806
+ **Help & Examples:**
807
+ - "What can you help me with?"
808
+ - "Show me demo examples"
809
+ - "How does this system work?"
810
+ """ )
811
+
812
+ # Footer
813
+ st .markdown ("---" )
814
+ st .markdown ("""
815
+ <div style="text-align: center; color: #666;">
816
+ <p>🤖 <strong>AI Chat Assistant</strong> - Powered by Advanced Language Models</p>
817
+ <p>Ask questions in natural language | Get instant insights</p>
818
+ </div>
819
+ """ , unsafe_allow_html = True )
820
+
631
821
# Navigation
632
822
def main_navigation ():
633
823
"""Handle navigation between different pages"""
@@ -637,12 +827,14 @@ def main_navigation():
637
827
638
828
page = st .sidebar .selectbox (
639
829
"Choose a page" ,
640
- ["🏠 Home" , "⚙️ Settings" , "ℹ️ About" ],
830
+ ["🏠 Home" , "🤖 AI Chat Assistant" , " ⚙️ Settings" , "ℹ️ About" ],
641
831
index = 0
642
832
)
643
833
644
834
if page == "🏠 Home" :
645
835
main ()
836
+ elif page == "🤖 AI Chat Assistant" :
837
+ show_ai_chat_page ()
646
838
elif page == "⚙️ Settings" :
647
839
show_settings_page ()
648
840
elif page == "ℹ️ About" :
0 commit comments