import streamlit as st import openai import json from datetime import datetime # Page configuration st.set_page_config( page_title="Agent Interview Context Generation Demo", layout="wide" ) # Custom CSS for chat bubbles and Font Awesome st.markdown(""" """, unsafe_allow_html=True) # Initialize session state variables if 'messages' not in st.session_state: st.session_state.messages = [] if 'interview_complete' not in st.session_state: st.session_state.interview_complete = False if 'context_data' not in st.session_state: st.session_state.context_data = "" if 'interview_started' not in st.session_state: st.session_state.interview_started = False if 'context_focus' not in st.session_state: st.session_state.context_focus = None def get_random_question(api_key, focus_area=None): """Get a random question from OpenAI based on optional focus area.""" try: client = openai.OpenAI(api_key=api_key) system_content = "You are an interviewer gathering context about the user. " if focus_area and focus_area != "general": system_content += f"Focus specifically on questions about their {focus_area}. " system_content += "Ask one random, open-ended question that reveals meaningful information about the user. Be creative and never repeat questions. Each response should be just one engaging question." response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": system_content }, { "role": "user", "content": "Please ask me a random question." } ] ) return response.choices[0].message.content if hasattr(response.choices[0].message, 'content') else str(response.choices[0].message) except Exception as e: return f"Error: {str(e)}" def extract_context(api_key, conversation): """Extract context from the conversation using OpenAI.""" try: client = openai.OpenAI(api_key=api_key) conversation_text = "\n".join([f"{'Bot' if i%2==0 else 'User'}: {msg}" for i, msg in enumerate(conversation)]) response = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ { "role": "system", "content": "Analyze the following conversation and extract key information about the user. Create a well-organized summary in markdown format, grouping similar information under appropriate headings. Write in third person perspective." }, { "role": "user", "content": f"Please analyze this conversation and create a context summary:\n\n{conversation_text}" } ] ) return response.choices[0].message.content if hasattr(response.choices[0].message, 'content') else str(response.choices[0].message) except Exception as e: return f"Error: {str(e)}" # Sidebar for API key and controls with st.sidebar: st.title("Settings") api_key = st.text_input("Enter OpenAI API Key", type="password") if st.button("Clear/Reset"): st.session_state.messages = [] st.session_state.interview_complete = False st.session_state.context_data = "" st.session_state.interview_started = False st.session_state.context_focus = None st.rerun() # Main content st.title("Agent Interview Context Generation Demo") st.markdown(""" This project demonstrates how AI agents can proactively gather and generate rich contextual data through intelligent interviewing. By focusing on specific areas of interest, the agent builds a comprehensive understanding that enhances AI-human interactions and enables more personalized experiences. """) # Create tabs for different sections tab1, tab2, tab3, tab4 = st.tabs(["Instructions", "Interview", "Gallery", "Generated Context"]) with tab1: st.header("How it Works") st.markdown(""" This application helps gather and extract contextual information about you through an interactive interview process. Created by [Daniel Rosehill](https://danielrosehill.com) and Claude (Anthropic). View the source code on [GitHub](https://github.com/danielrosehill/Context-Extraction-Demo). ### Process: 1. Enter your OpenAI API key in the sidebar 2. Choose your preferred context focus area 3. Click the "Start Interview" button in the Interview tab 4. The AI interviewer will ask targeted questions based on your chosen focus 5. Answer each question naturally - you can type or use voice input 6. Click "Submit Answer" after each response 7. Continue the conversation until you're ready to end 8. Click "End Interview" to generate your context summary 9. Review the extracted context and export it as needed ### Features: - **Focus Areas**: Choose to focus on specific aspects like professional background, technical skills, or keep it general - **Voice Input**: Use Chrome's built-in speech-to-text by clicking the microphone icon - **Targeted Questions**: The AI asks questions relevant to your chosen focus area - **Context Extraction**: Automatically organizes your information into a structured summary - **Export Options**: Copy or download your context data in markdown format ### Tips: - Provide detailed, honest answers for better context extraction - Use voice input to make the process faster and more natural - Take your time with each response - You can reset and start over at any time using the Clear/Reset button """) with tab2: # Create two columns for the main interface col1, col2 = st.columns([2, 1]) with col1: st.subheader("Conversation") # Display conversation history with chat bubbles st.markdown('