Skip to content

Commit

Permalink
Changes the gpt app example to use inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
elijahbenizzy committed Feb 26, 2024
1 parent 692fd2c commit 5a4d722
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion burr/core/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def validate_inputs(self, inputs: Optional[Dict[str, Any]]) -> None:
if missing_inputs or additional_inputs:
raise ValueError(
f"Inputs to function {self} are invalid. "
f"Missing the following inputs: {', '.join(missing_inputs)}."
+ f"Missing the following inputs: {', '.join(missing_inputs)}."
if missing_inputs
else "" f"Additional inputs: {','.join(additional_inputs)}."
if additional_inputs
Expand Down
8 changes: 4 additions & 4 deletions examples/gpt/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
}


@action(reads=["prompt"], writes=["chat_history"])
def process_prompt(state: State) -> Tuple[dict, State]:
result = {"processed_prompt": {"role": "user", "content": state["prompt"], "type": "text"}}
@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"}}
return result, state.wipe(keep=["prompt", "chat_history"]).append(
chat_history=result["processed_prompt"]
)
).update(prompt=prompt)


@action(reads=["prompt"], writes=["safe"])
Expand Down
16 changes: 13 additions & 3 deletions examples/gpt/streamlit_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
update_state,
)

st.set_page_config(layout="wide")

"""This file is a work in progress. Note that this will be replaced shortly
with a simple chatbot + a reference to the Burr UI (to be run in a separate page),
which is more powerful and much simpler to understand. For now,
this is purely for demos."""


def render_chat_message(record: Record):
if record.action in ["prompt", "response"]:
Expand All @@ -32,6 +39,8 @@ def render_chat_message(record: Record):

def retrieve_state():
if "burr_state" not in st.session_state:
# TODO --enable usage of hamilton. Currently its not wiring in inputs
# But it should be easy enough
state = AppState.from_empty(
app=chatbot_application.application(use_hamilton=False),
)
Expand All @@ -47,15 +56,17 @@ def chatbot_step(app_state: AppState, prompt: Optional[str]) -> bool:
:param prompt: Prompt to set the chatbot to. If this is None it means it should continue and not be reset.
:return:
"""
inputs = None
if prompt is not None:
# We need to update
app_state.app.update_state(app_state.app.state.update(prompt=prompt))
inputs = {"prompt": prompt}
# app_state.app.update_state(app_state.app.state.update(prompt=prompt))
st.session_state.running = True # set to running
# if its not running this is a no-op
if not st.session_state.get("running", False):
return False
application = app_state.app
step_output = application.step()
step_output = application.step(inputs=inputs)
if step_output is None:
st.session_state.running = False
return False
Expand All @@ -70,7 +81,6 @@ def chatbot_step(app_state: AppState, prompt: Optional[str]) -> bool:


def main():
st.set_page_config(layout="wide")
st.title("Chatbot example with Burr")
app_state = retrieve_state() # retrieve first so we can use for the ret of the step
columns = st.columns(2)
Expand Down

0 comments on commit 5a4d722

Please sign in to comment.