Skip to content

Commit

Permalink
Merge pull request #1103 from i-dot-ai/feature/streamlit_app
Browse files Browse the repository at this point in the history
Feature/streamlit app
  • Loading branch information
MoFrod authored Oct 15, 2024
2 parents ea0417c + 0539bb8 commit 62f836e
Show file tree
Hide file tree
Showing 4 changed files with 3,370 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .vscode/redbox.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
"path": "../django_app"
},
{
<<<<<<< Updated upstream
=======
"name": "streamlit_app",
"path": "../streamlit_app"
},
{
>>>>>>> Stashed changes
"name": "Redbox Core",
"path": "../redbox-core"
},
Expand Down
60 changes: 60 additions & 0 deletions streamlit_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Set up
import streamlit as st
from redbox.app import Redbox
from redbox.models.chain import RedboxQuery, RedboxState, AISettings
from uuid import uuid4
from redbox.models.settings import Settings
from dotenv import load_dotenv
import asyncio

# Load Redbox environment variables
load_dotenv("../.env")


# Function defining how the Redbox streamlit app will work
def run_streamlit():
# Create two tabs within the interface, one labeled "chat", the other labeled "settings"
chat_tab, settings_tab = st.tabs(["chat", "settings"])
# Create a list that will store the chat history, if one does not already exist
if "messages" not in st.session_state:
st.session_state.messages = []
# Create a list that will store the AI settings, if one does not already exist
if "ai_settings" not in st.session_state:
st.session_state.ai_settings = AISettings()
# Create an instance of Redbox, if one does not already exist
if "redbox" not in st.session_state:
st.session_state.redbox = Redbox(env=Settings(), debug=True)

with chat_tab:
# Iterate over the messages list, display each message in the chat window with the role and corresponding text
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["text"])
# Present an input field where the user can type a message to Redbox
if prompt := st.chat_input("How can I help you today?"):
# New messages should be added to the chat history under the "user" role
st.session_state.messages.append({"role": "user", "text": prompt})
# Construct a Redbox object with required information
state = RedboxState(
request=RedboxQuery(
question=prompt,
s3_keys=[],
user_uuid=uuid4(),
chat_history=[],
ai_settings=st.session_state.ai_settings,
),
)
# Run an asynchronous function from the Redbox instance to get the AI's response
response: RedboxState = asyncio.run(st.session_state.redbox.run(state))
llm_answer = response["text"]
# route = response["route_name"] - to be added back in if / when routing is added
# Display the AI's response in the chat under the "ai" role and append it to the chat history
with st.chat_message("ai"):
st.write(llm_answer)
st.session_state.messages.append({"role": "ai", "text": llm_answer})


run_streamlit()

# getting the chat history to work, showing the routes, and sources, and getting it to look like Redbox.
# Go back to James re-files, data and streaming
Loading

0 comments on commit 62f836e

Please sign in to comment.