File size: 2,395 Bytes
18e52ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63a2132
 
 
74216d1
63a2132
 
18e52ab
 
 
 
 
 
 
63a2132
18e52ab
 
63a2132
18e52ab
 
 
 
 
 
 
63a2132
18e52ab
 
 
 
 
 
 
 
 
 
 
 
63a2132
18e52ab
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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")

# Dropdown to select the model
model_choice = st.selectbox(
    "Select the model:",
    ["gpt-3.5", "gpt-3.5-turbo", "gpt-4", "gpt-4-turbo","gpt-4o"]
)

# 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, model):
    st.session_state.sessionMessages.append(HumanMessage(content=question))
    try:
        chat = ChatOpenAI(temperature=0, openai_api_key=openai_api_key, model=model)
        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 f"An error occurred: {str(e)}. 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:
    user_input = get_text()
    submit = st.button('Generate')  

    if submit:
        response = load_answer(user_input, model_choice)
        st.subheader("Answer:")
        st.write(response)
else:
    st.warning("Please enter your OpenAI API Key to use this app.")