|
import streamlit as st |
|
from langchain_openai import ChatOpenAI |
|
from langchain.schema import AIMessage, HumanMessage, SystemMessage |
|
|
|
|
|
st.set_page_config(page_title="EcoCoinBot", page_icon=":robot:") |
|
|
|
|
|
def add_image(image, width): |
|
st.image(image, width=width) |
|
|
|
|
|
add_image("logo.jpg", width=200) |
|
|
|
st.header("Hey, I'm EcoCoinBot. I'm a sustainable cryptocurrency advisor. I'm your friend") |
|
|
|
|
|
openai_api_key = st.text_input("Enter your OpenAI API Key:", type="password") |
|
|
|
|
|
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.'") |
|
] |
|
|
|
|
|
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." |
|
|
|
|
|
def get_text(): |
|
input_text = st.text_input("User: ") |
|
return input_text |
|
|
|
|
|
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.") |
|
|