diff --git a/README.md b/README.md index 10dbdedd6091..6368f75f22fb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,15 @@ -# Welcome to Streamlit! +Install requirements: -Edit `/streamlit_app.py` to customize this app to your heart's desire. :heart: +pip install -r requirements.txt + +on VS Code: +py -m pip install -r requirements.txt + + + +Run app : +streamlit run main.py + +on VS Code: +py -m streamlit run main.py -If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community -forums](https://discuss.streamlit.io). diff --git a/main.py b/main.py new file mode 100644 index 000000000000..c70352b0f86b --- /dev/null +++ b/main.py @@ -0,0 +1,56 @@ +import streamlit as st +from streamlit_chat import message + +from langchain.chat_models import ChatOpenAI +from langchain.schema import ( + SystemMessage, + HumanMessage, + AIMessage +) + + +def init(): + + # setup streamlit page + st.set_page_config( + page_title="Your own ChatGPT", + page_icon="🤖" + ) + + +def main(): + init() + + OPENAI_API_KEY = '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' + llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo", openai_api_key=OPENAI_API_KEY) + # initialize message history + if "messages" not in st.session_state: + st.session_state.messages = [ + SystemMessage(content="You are a helpful assistant.") + ] + + st.header("Your own ChatGPT 🤖") + + # sidebar with user input + with st.sidebar: + user_input = st.text_input("Your message: ", key="user_input") + + # handle user input + if user_input: + st.session_state.messages.append(HumanMessage(content=user_input)) + with st.spinner("Thinking..."): + response = chat(st.session_state.messages) + st.session_state.messages.append( + AIMessage(content=response.content)) + + # display message history + messages = st.session_state.get('messages', []) + for i, msg in enumerate(messages[1:]): + if i % 2 == 0: + message(msg.content, is_user=True, key=str(i) + '_user') + else: + message(msg.content, is_user=False, key=str(i) + '_ai') + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 502d7d1a0d19..cbde836f99b6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,9 @@ altair pandas streamlit +streamlit_chat +aiohttp==3.8.2 +yarl==1.8.1 +frozenlist==1.3.1 +openai ==1.1.1 +langchain \ No newline at end of file diff --git a/streamlit_app.py b/streamlit_app.py deleted file mode 100644 index 7a0ed1272052..000000000000 --- a/streamlit_app.py +++ /dev/null @@ -1,40 +0,0 @@ -import altair as alt -import numpy as np -import pandas as pd -import streamlit as st - -""" -# Welcome to Streamlit! - -Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:. -If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community -forums](https://discuss.streamlit.io). - -In the meantime, below is an example of what you can do with just a few lines of code: -""" - -num_points = st.slider("Number of points in spiral", 1, 10000, 1100) -num_turns = st.slider("Number of turns in spiral", 1, 300, 31) - -indices = np.linspace(0, 1, num_points) -theta = 2 * np.pi * num_turns * indices -radius = indices - -x = radius * np.cos(theta) -y = radius * np.sin(theta) - -df = pd.DataFrame({ - "x": x, - "y": y, - "idx": indices, - "rand": np.random.randn(num_points), -}) - -st.altair_chart(alt.Chart(df, height=700, width=700) - .mark_point(filled=True) - .encode( - x=alt.X("x", axis=None), - y=alt.Y("y", axis=None), - color=alt.Color("idx", legend=None, scale=alt.Scale()), - size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])), - ))