Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds simple streamlit app for chatbot

elijahbenizzya31da71e88be
diff --git a/examples/gpt/README.md b/examples/gpt/README.md
index 27da5bcf..4a0e2330 100644
--- a/examples/gpt/README.md
+++ b/examples/gpt/README.md
@@ -35,4 +35,11 @@ To run on streamlit, you can launch the app:
streamlit run streamlit_app.py
```
-This allows you to input a prompt, watch it "think", and explore the history/"why" of the response.
+This has the state machihne displayed as well as a quick debugging/demo. You should be using the UI,
+however, as it is more powerful and responsive. You can run the simple app with:
+
+```bash
+streamlit run simple_streamlit_app.py & burr
+```
+
+This will open up both a streamlit app and a server at the same time.
diff --git a/examples/gpt/application.py b/examples/gpt/application.py
index 073a06e8..9dc60d82 100644
--- a/examples/gpt/application.py
+++ b/examples/gpt/application.py
@@ -19,9 +19,9 @@ MODES = {
@action(reads=[], writes=["chat_history", "prompt"])
def process_prompt(state: State, prompt: str) -> Tuple[dict, State]:
- result = {"processed_prompt": {"role": "user", "content": prompt, "type": "text"}}
+ result = {"chat_item": {"role": "user", "content": prompt, "type": "text"}}
return result, state.wipe(keep=["prompt", "chat_history"]).append(
- chat_history=result["processed_prompt"]
+ chat_history=result["chat_item"]
).update(prompt=prompt)
@@ -111,15 +111,15 @@ def image_response(state: State, model: str = "dall-e-2") -> Tuple[dict, State]:
def response(state: State) -> Tuple[dict, State]:
if not state["safe"]:
result = {
- "processed_response": {
+ "chat_item": {
"role": "assistant",
"content": "I'm sorry, I can't respond to that.",
"type": "text",
}
}
else:
- result = {"processed_response": state["response"]}
- return result, state.append(chat_history=result["processed_response"])
+ result = {"chat_item": state["response"]}
+ return result, state.append(chat_history=result["chat_item"])
# TODO -- add in error handling
diff --git a/examples/gpt/simple_streamlit_app.py b/examples/gpt/simple_streamlit_app.py
new file mode 100644
index 00000000..1a221fb4
--- /dev/null
+++ b/examples/gpt/simple_streamlit_app.py
@@ -0,0 +1,46 @@
+import uuid
+
+import application as chatbot_application
+import streamlit as st
+
+import burr.core
+
+
+def render_chat_message(chat_item: dict):
+ content = chat_item["content"]
+ content_type = chat_item["type"]
+ role = chat_item["role"]
+ with st.chat_message(role):
+ if content_type == "image":
+ st.image(content)
+ elif content_type == "code":
+ st.code(content)
+ elif content_type == "text":
+ st.write(content)
+
+
+def initialize_app() -> burr.core.Application:
+ if "burr_app" not in st.session_state:
+ st.session_state.burr_app = chatbot_application.application(
+ use_hamilton=False, app_id=f"chat:{str(uuid.uuid4())[0:6]}"
+ )
+ return st.session_state.burr_app
+
+
+def main():
+ st.title("Chatbot example with Burr")
+ app = initialize_app()
+
+ prompt = st.chat_input("Ask me a question!", key="chat_input")
+ for chat_message in app.state.get("chat_history", []):
+ render_chat_message(chat_message)
+ if prompt:
+ for action, result, state in app.iterate(
+ inputs={"prompt": prompt}, halt_after=["response"]
+ ):
+ if action.name in ["prompt", "response"]:
+ render_chat_message(result["chat_item"])
+
+
+if __name__ == "__main__":
+ main()