import streamlit as st from langchain_openai import ChatOpenAI from langchain.schema import AIMessage, HumanMessage, SystemMessage # StreamLit UI st.set_page_config(page_title="EcoCoinBot", page_icon=":robot:") # Define the function to add an image at the top center def add_image(image, width): st.image(image, width=width) # Add the image at the top center with adjusted width add_image("logo.jpg", width=200) # Adjust the width as needed st.header("Hey, I'm EcoCoinBot. I'm a sustainable cryptocurrency advisor. I'm your friend") # Input OpenAI API key openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password") # Create sessionMessages if not already exists if "sessionMessages" not in st.session_state: st.session_state.sessionMessages = [ SystemMessage(content="Your name is EcoCoinBot. You are a sustainable cryptocurrency investment advisor. Please only answer questions related to sustainable cryptocurrency. If a question is not related to this field, simply respond with, 'This question is not relevant to sustainable cryptocurrency. Please ask another question.'") ] # Function to load answer def load_answer(question): st.session_state.sessionMessages.append(HumanMessage(content=question)) try: assistant_answer = chat.invoke(st.session_state.sessionMessages) st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content)) return assistant_answer.content except Exception as e: if "Incorrect API key" in str(e): return "The provided OpenAI API Key is incorrect. Please try again." else: return "An error occurred. Please try again later." # Function to get user input def get_text(): input_text = st.text_input("User: ") return input_text # Proceed if OpenAI API key is provided if openai_api_key: chat = ChatOpenAI(temperature=0, openai_api_key=openai_api_key) user_input = get_text() submit = st.button('Generate') if submit: response = load_answer(user_input) st.subheader("Answer:") st.write(response) else: st.warning("Please enter your OpenAI API Key to use this app.")