Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import openai | |
| # Set your OpenAI API key | |
| # Function to get response from OpenAI API | |
| def get_openai_response(prompt): | |
| response = openai.Completion.create( | |
| engine="text-davinci-003", | |
| prompt=prompt, | |
| max_tokens=150, | |
| n=1, | |
| stop=None, | |
| temperature=0.7, | |
| ) | |
| message = response.choices[0].text.strip() | |
| return message | |
| # Streamlit UI components | |
| st.title("Insurance Policy Advisor Chatbot") | |
| st.write("Hello! I'm here to assist you in finding the best insurance policies tailored to your needs.") | |
| # Initialize session state for conversation history | |
| if 'messages' not in st.session_state: | |
| st.session_state.messages = [] | |
| # Display conversation history | |
| for msg in st.session_state.messages: | |
| st.chat_message(msg['role']).markdown(msg['content']) | |
| # User input | |
| user_input = st.chat_input("Ask me about insurance policies...") | |
| if user_input: | |
| # Display user's message | |
| st.session_state.messages.append({"role": "user", "content": user_input}) | |
| st.chat_message("user").markdown(user_input) | |
| # Generate chatbot's response | |
| prompt = f"Suggest insurance policies based on the following query: {user_input}" | |
| response = get_openai_response(prompt) | |
| # Display chatbot's response | |
| st.session_state.messages.append({"role": "assistant", "content": response}) | |
| st.chat_message("assistant").markdown(response) | |