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 email-assistant demo

elijahbenizzy915da97ca735
diff --git a/examples/email-assistant/README.md b/examples/email-assistant/README.md
new file mode 100644
index 00000000..40b55e8a
--- /dev/null
+++ b/examples/email-assistant/README.md
@@ -0,0 +1,30 @@
+# Email Assistant
+
+This example shows how to create a basic email assistant. This assistant
+takes in an email and will write a response. It allows you to prompt it with:
+1. An email
+2. Directions on how to respond
+
+Then it generates questions for you for clarifications, and uses the answer to write a draft.
+It will go back and forth with you until you are satisfied with the response.
+
+This demonstrates how to do multi-shot modeling interactively with an LLM. You'll learn:
+
+1. How to move in/out of program control -> user control
+2. How to process inputs at multiple points through the process
+3. How to involve multi-shot modeling with an LLM
+
+## How to run
+
+To run this example, you need to install the following dependencies:
+
+```bash
+pip install -r requirements.txt
+```
+
+Then if you run `jupyter lab` you can open/execute the notebook.
+This simulates an interactive web app. The notebook is the best way to get started.
+
+Note we will be adding two things to this demo:
+1. An integration with the burr web app
+2. a standalone server example with a walkthrough
diff --git a/examples/email-assistant/__init__.py b/examples/email-assistant/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/examples/email-assistant/application.py b/examples/email-assistant/application.py
new file mode 100644
index 00000000..8d6fd6c2
--- /dev/null
+++ b/examples/email-assistant/application.py
@@ -0,0 +1,180 @@
+import functools
+from typing import Tuple
+
+import openai
+
+from burr.core import Application, ApplicationBuilder, State, action, expr
+from burr.tracking import LocalTrackingClient
+
+
+def _get_openai_client():
+ openai_client = openai.Client()
+ return openai_client
+
+
+@action(reads=[], writes=["incoming_email", "response_instructions"])
+def process_input(
+ state: State, email_to_respond: str, response_instructions: str
+) -> Tuple[dict, State]:
+ """Processes input from user and updates state with the input."""
+ result = {"incoming_email": email_to_respond, "response_instructions": response_instructions}
+ return result, state.update(**result)
+
+
+@action(reads=["response_instructions", "incoming_email"], writes=["clarification_questions"])
+def determine_clarifications(state: State) -> Tuple[dict, State]:
+ """Determines if the response instructions require clarification."""
+ # TODO -- query an LLM to determine if the response instructions are clear, or if it needs more information
+ incoming_email = state["incoming_email"]
+ response_instructions = state["response_instructions"]
+ client = _get_openai_client()
+ # TODO -- use instructor to get a pydantic model
+ result = client.chat.completions.create(
+ model="gpt-4",
+ messages=[
+ {
+ "role": "system",
+ "content": "You are a chatbot that has the task of generating responses to an email on behalf of a user. ",
+ },
+ {
+ "role": "user",
+ "content": (
+ f"The email you are to respond to is: {incoming_email}."
+ f"Your instructions are: {response_instructions}."
+ "Your first task is to ask any clarifying questions for the user"
+ " who is asking you to respond to this email. These clarifying questions are for the user, "
+ "*not* for the original sender of the email. Please "
+ "generate a list of at most 3 questions (and you really can do less -- 2, 1, or even none are OK! joined by newlines, included only if you feel that you could leverage "
+ "clarification (my time is valuable)."
+ "The questions, joined by newlines, must be the only text you return. If you do not need clarification, return an empty string."
+ ),
+ },
+ ],
+ )
+ content = result.choices[0].message.content
+ all_questions = content.split("\n") if content else ""
+ return {"clarification_questions": all_questions}, state.update(
+ clarification_questions=all_questions
+ )
+
+
+@action(reads=["clarification_questions"], writes=["clarification_answers"])
+def clarify_instructions(state: State, clarification_inputs: list[str]) -> Tuple[dict, State]:
+ """Clarifies the response instructions if needed."""
+ clarification_answers = dict(zip(state["clarification_questions"], clarification_inputs))
+ return {"clarification_answers": clarification_answers}, state.update(
+ clarification_answers=clarification_answers
+ )
+
+
+@action(
+ reads=[
+ "incoming_email",
+ "response_instructions",
+ "clarification_answers",
+ "draft_history",
+ "feedback",
+ ],
+ writes=["current_draft", "draft_history"],
+)
+def formulate_draft(state: State) -> Tuple[dict, State]:
+ """Formulates the draft response based on the incoming email, response instructions, and any clarifications."""
+ # TODO -- query an LLM to generate the draft response
+ incoming_email = state["incoming_email"]
+ response_instructions = state["response_instructions"]
+ client = _get_openai_client()
+ # TODO -- use instructor to get a pydantic model
+ clarification_answers_formatted_q_a = "\n".join(
+ [f"Q: {q}\nA: {a}" for q, a in state["clarification_answers"].items()]
+ )
+ instructions = [
+ f"The email you are to respond to is: {incoming_email}.",
+ f"Your instructions are: {response_instructions}.",
+ "You have already asked the following questions and received the following answers: ",
+ clarification_answers_formatted_q_a,
+ ]
+ if state["draft_history"]:
+ instructions.append("Your previous draft was: ")
+ instructions.append(state["draft_history"][-1])
+ instructions.append(
+ "you received the following feedback, please incorporate this into your response: "
+ )
+ instructions.append(state["feedback"])
+ instructions.append("Please generate a draft response using all this information!")
+ prompt = " ".join(instructions)
+
+ result = client.chat.completions.create(
+ model="gpt-4",
+ messages=[
+ {
+ "role": "system",
+ "content": "You are a chatbot that has the task of generating responses to an email. ",
+ },
+ {"role": "user", "content": prompt},
+ ],
+ )
+ content = result.choices[0].message.content
+ return {"prompt": prompt, "current_draft": content}, state.update(current_draft=content).append(
+ draft_history=content
+ )
+
+
+@action(reads=[], writes=["feedback"])
+def process_feedback(state: State, feedback: str) -> Tuple[dict, State]:
+ """Processes feedback from user and updates state with the feedback."""
+ result = {"feedback": feedback}
+ return result, state.update(**result)
+
+
+@action(reads=["current_draft", "feedback"], writes=["final_draft"])
+def final_result(state: State) -> Tuple[dict, State]:
+ """Returns the final result of the process."""
+ result = {"final_draft": state["current_draft"]}
+ return result, state.update(final_draft=result["final_draft"])
+
+
+def application(
+ app_id: str = None, project: str = "demo:email_assistant", username: str = None
+) -> Application:
+ tracker = LocalTrackingClient(project=project)
+ builder = (
+ ApplicationBuilder()
+ .with_actions(
+ process_input,
+ determine_clarifications,
+ clarify_instructions,
+ formulate_draft,
+ process_feedback,
+ final_result,
+ )
+ .with_transitions(
+ ("process_input", "determine_clarifications"),
+ (
+ "determine_clarifications",
+ "clarify_instructions",
+ expr("len(clarification_questions) > 0"),
+ ),
+ ("determine_clarifications", "formulate_draft"),
+ ("clarify_instructions", "formulate_draft"),
+ ("formulate_draft", "process_feedback"),
+ ("process_feedback", "formulate_draft", expr("len(feedback) > 0")),
+ ("process_feedback", "final_result"),
+ )
+ .with_tracker("local", project=project)
+ .with_identifiers(app_id=app_id, partition_key=username)
+ .initialize_from(
+ tracker,
+ resume_at_next_action=True,
+ default_state={"draft_history": []},
+ default_entrypoint="process_input",
+ )
+ )
+ return builder.build()
+
+
+if __name__ == "__main__":
+ app = application()
+ app.visualize(
+ output_file_path="statemachine", include_conditions=True, include_state=True, format="png"
+ )
diff --git a/examples/email-assistant/notebook.ipynb b/examples/email-assistant/notebook.ipynb
new file mode 100644
index 00000000..e33f01a4
--- /dev/null
+++ b/examples/email-assistant/notebook.ipynb
@@ -0,0 +1,489 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "99997759-30e1-4290-a62f-d606687b0319",
+ "metadata": {},
+ "source": [
+ "# Email assistant\n",
+ "Developing the email assistant. This is a more complex app with lots of back-and-forth. This demonstrates:\n",
+ "\n",
+ "1. How to move in/out of program control -> user control\n",
+ "2. How to process inputs at multiple points through the process\n",
+ "3. How to involve multi-shot modeling with an LLM\n",
+ "\n",
+ "Note this can easily be extended. While the user provides feedback, for a more complex draft, a multi-shot agent with a \"editor\" agent and a \"writer\" agent could interact until the editor is happy. For an email this is overkill, but for more complex narratives this could be valauble."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "acb89287-02c8-4bfc-8bc8-a1de112e2625",
+ "metadata": {},
+ "source": [
+ "# The Application\n",
+ "\n",
+ "We're modeling this as a multi-shot model. No sane person would trust GPT-x to respond on their behalf, \n",
+ "but it can be quite valuable in helping automate the process of responding to an email and deciding how to respond.\n",
+ "There are a few interaction points:\n",
+ "\n",
+ "1. Initially, the user provides an email and response instructions (note this should probably be changed to be a chain of emails)\n",
+ "2. The LLM has the option to ask a set of clarifying questions that the user can then respond to\n",
+ "3. The user then provides feedback. If feedback is empty, the LLM will return a result\n",
+ "\n",
+ "This demonstrates how to use Burr to move in/out of more complex flows. \n",
+ "\n",
+ "First, we instantiate/visualize the application. To see the code, see [application.py](application.py)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "b59e926f-058d-4bac-b97e-ff173d3be7a4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from application import application as email_assistant_application"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "c7aba1b3-5886-4255-b2f5-b40627896196",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
+ "<!-- Generated by graphviz version 8.1.0 (20230707.0739)\n",
+ " -->\n",
+ "<!-- Pages: 1 -->\n",
+ "<svg width=\"555pt\" height=\"477pt\"\n",
+ " viewBox=\"0.00 0.00 555.44 477.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 473)\">\n",
+ "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-473 551.44,-473 551.44,4 -4,4\"/>\n",
+ "<!-- process_input -->\n",
+ "<g id=\"node1\" class=\"node\">\n",
+ "<title>process_input</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M349.13,-402C349.13,-402 282.13,-402 282.13,-402 276.13,-402 270.13,-396 270.13,-390 270.13,-390 270.13,-378 270.13,-378 270.13,-372 276.13,-366 282.13,-366 282.13,-366 349.13,-366 349.13,-366 355.13,-366 361.13,-372 361.13,-378 361.13,-378 361.13,-390 361.13,-390 361.13,-396 355.13,-402 349.13,-402\"/>\n",
+ "<text text-anchor=\"middle\" x=\"315.63\" y=\"-378.95\" font-family=\"Times,serif\" font-size=\"14.00\">process_input</text>\n",
+ "</g>\n",
+ "<!-- determine_clarifications -->\n",
+ "<g id=\"node4\" class=\"node\">\n",
+ "<title>determine_clarifications</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M377.63,-335C377.63,-335 253.63,-335 253.63,-335 247.63,-335 241.63,-329 241.63,-323 241.63,-323 241.63,-311 241.63,-311 241.63,-305 247.63,-299 253.63,-299 253.63,-299 377.63,-299 377.63,-299 383.63,-299 389.63,-305 389.63,-311 389.63,-311 389.63,-323 389.63,-323 389.63,-329 383.63,-335 377.63,-335\"/>\n",
+ "<text text-anchor=\"middle\" x=\"315.63\" y=\"-311.95\" font-family=\"Times,serif\" font-size=\"14.00\">determine_clarifications</text>\n",
+ "</g>\n",
+ "<!-- process_input&#45;&gt;determine_clarifications -->\n",
+ "<g id=\"edge5\" class=\"edge\">\n",
+ "<title>process_input&#45;&gt;determine_clarifications</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M315.63,-365.58C315.63,-359.61 315.63,-352.76 315.63,-346.15\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"319.13,-346.47 315.63,-336.47 312.13,-346.47 319.13,-346.47\"/>\n",
+ "</g>\n",
+ "<!-- input__email_to_respond -->\n",
+ "<g id=\"node2\" class=\"node\">\n",
+ "<title>input__email_to_respond</title>\n",
+ "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"198.63\" cy=\"-451\" rx=\"101.51\" ry=\"18\"/>\n",
+ "<text text-anchor=\"middle\" x=\"198.63\" y=\"-445.95\" font-family=\"Times,serif\" font-size=\"14.00\">input: email_to_respond</text>\n",
+ "</g>\n",
+ "<!-- input__email_to_respond&#45;&gt;process_input -->\n",
+ "<g id=\"edge1\" class=\"edge\">\n",
+ "<title>input__email_to_respond&#45;&gt;process_input</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M228.45,-433.43C242.58,-425.58 259.68,-416.08 274.97,-407.59\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"276.18,-410.37 283.22,-402.45 272.78,-404.25 276.18,-410.37\"/>\n",
+ "</g>\n",
+ "<!-- input__response_instructions -->\n",
+ "<g id=\"node3\" class=\"node\">\n",
+ "<title>input__response_instructions</title>\n",
+ "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"432.63\" cy=\"-451\" rx=\"114.82\" ry=\"18\"/>\n",
+ "<text text-anchor=\"middle\" x=\"432.63\" y=\"-445.95\" font-family=\"Times,serif\" font-size=\"14.00\">input: response_instructions</text>\n",
+ "</g>\n",
+ "<!-- input__response_instructions&#45;&gt;process_input -->\n",
+ "<g id=\"edge2\" class=\"edge\">\n",
+ "<title>input__response_instructions&#45;&gt;process_input</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M402.5,-433.26C388.37,-425.41 371.32,-415.94 356.09,-407.48\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"358.32,-404.16 347.88,-402.36 354.92,-410.28 358.32,-404.16\"/>\n",
+ "</g>\n",
+ "<!-- clarify_instructions -->\n",
+ "<g id=\"node5\" class=\"node\">\n",
+ "<title>clarify_instructions</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M188.13,-252.5C188.13,-252.5 91.13,-252.5 91.13,-252.5 85.13,-252.5 79.13,-246.5 79.13,-240.5 79.13,-240.5 79.13,-228.5 79.13,-228.5 79.13,-222.5 85.13,-216.5 91.13,-216.5 91.13,-216.5 188.13,-216.5 188.13,-216.5 194.13,-216.5 200.13,-222.5 200.13,-228.5 200.13,-228.5 200.13,-240.5 200.13,-240.5 200.13,-246.5 194.13,-252.5 188.13,-252.5\"/>\n",
+ "<text text-anchor=\"middle\" x=\"139.63\" y=\"-229.45\" font-family=\"Times,serif\" font-size=\"14.00\">clarify_instructions</text>\n",
+ "</g>\n",
+ "<!-- determine_clarifications&#45;&gt;clarify_instructions -->\n",
+ "<g id=\"edge6\" class=\"edge\">\n",
+ "<title>determine_clarifications&#45;&gt;clarify_instructions</title>\n",
+ "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M241.16,-302.38C193.53,-293.67 140.63,-284 140.63,-284 135.84,-278.38 134.22,-270.92 134.15,-263.54\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"137.71,-263.91 135.03,-253.66 130.73,-263.34 137.71,-263.91\"/>\n",
+ "<text text-anchor=\"middle\" x=\"226.13\" y=\"-270.7\" font-family=\"Times,serif\" font-size=\"14.00\">len(clarification_questions) &gt; 0</text>\n",
+ "</g>\n",
+ "<!-- formulate_draft -->\n",
+ "<g id=\"node7\" class=\"node\">\n",
+ "<title>formulate_draft</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M311.38,-185.5C311.38,-185.5 233.88,-185.5 233.88,-185.5 227.88,-185.5 221.88,-179.5 221.88,-173.5 221.88,-173.5 221.88,-161.5 221.88,-161.5 221.88,-155.5 227.88,-149.5 233.88,-149.5 233.88,-149.5 311.38,-149.5 311.38,-149.5 317.38,-149.5 323.38,-155.5 323.38,-161.5 323.38,-161.5 323.38,-173.5 323.38,-173.5 323.38,-179.5 317.38,-185.5 311.38,-185.5\"/>\n",
+ "<text text-anchor=\"middle\" x=\"272.63\" y=\"-162.45\" font-family=\"Times,serif\" font-size=\"14.00\">formulate_draft</text>\n",
+ "</g>\n",
+ "<!-- determine_clarifications&#45;&gt;formulate_draft -->\n",
+ "<g id=\"edge7\" class=\"edge\">\n",
+ "<title>determine_clarifications&#45;&gt;formulate_draft</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M315.43,-298.75C315.02,-289.4 314,-277.7 311.63,-267.5 305.77,-242.34 294.67,-215.12 285.81,-195.6\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"288.68,-194.47 281.29,-186.88 282.33,-197.43 288.68,-194.47\"/>\n",
+ "</g>\n",
+ "<!-- clarify_instructions&#45;&gt;formulate_draft -->\n",
+ "<g id=\"edge8\" class=\"edge\">\n",
+ "<title>clarify_instructions&#45;&gt;formulate_draft</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M175.27,-216.08C191.22,-208.29 210.22,-199 227.18,-190.71\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"228.44,-193.5 235.89,-185.97 225.37,-187.21 228.44,-193.5\"/>\n",
+ "</g>\n",
+ "<!-- input__clarification_inputs -->\n",
+ "<g id=\"node6\" class=\"node\">\n",
+ "<title>input__clarification_inputs</title>\n",
+ "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"106.63\" cy=\"-317\" rx=\"106.63\" ry=\"18\"/>\n",
+ "<text text-anchor=\"middle\" x=\"106.63\" y=\"-311.95\" font-family=\"Times,serif\" font-size=\"14.00\">input: clarification_inputs</text>\n",
+ "</g>\n",
+ "<!-- input__clarification_inputs&#45;&gt;clarify_instructions -->\n",
+ "<g id=\"edge3\" class=\"edge\">\n",
+ "<title>input__clarification_inputs&#45;&gt;clarify_instructions</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M110.51,-298.57C112.89,-289.16 116.31,-277.47 120.63,-267.5 121.43,-265.63 122.33,-263.74 123.28,-261.86\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"126.58,-264.13 128.34,-253.68 120.45,-260.75 126.58,-264.13\"/>\n",
+ "</g>\n",
+ "<!-- process_feedback -->\n",
+ "<g id=\"node8\" class=\"node\">\n",
+ "<title>process_feedback</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M283.63,-103C283.63,-103 195.63,-103 195.63,-103 189.63,-103 183.63,-97 183.63,-91 183.63,-91 183.63,-79 183.63,-79 183.63,-73 189.63,-67 195.63,-67 195.63,-67 283.63,-67 283.63,-67 289.63,-67 295.63,-73 295.63,-79 295.63,-79 295.63,-91 295.63,-91 295.63,-97 289.63,-103 283.63,-103\"/>\n",
+ "<text text-anchor=\"middle\" x=\"239.63\" y=\"-79.95\" font-family=\"Times,serif\" font-size=\"14.00\">process_feedback</text>\n",
+ "</g>\n",
+ "<!-- formulate_draft&#45;&gt;process_feedback -->\n",
+ "<g id=\"edge9\" class=\"edge\">\n",
+ "<title>formulate_draft&#45;&gt;process_feedback</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M261.34,-149.32C258.57,-144.64 255.78,-139.48 253.63,-134.5 250.79,-127.95 248.35,-120.67 246.33,-113.78\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"249.52,-113.16 243.51,-104.43 242.76,-115 249.52,-113.16\"/>\n",
+ "</g>\n",
+ "<!-- process_feedback&#45;&gt;formulate_draft -->\n",
+ "<g id=\"edge10\" class=\"edge\">\n",
+ "<title>process_feedback&#45;&gt;formulate_draft</title>\n",
+ "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M246.77,-103.43C251.05,-113.87 256.58,-127.36 261.43,-139.19\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"258.43,-140.11 265.47,-148.03 264.91,-137.45 258.43,-140.11\"/>\n",
+ "<text text-anchor=\"middle\" x=\"308\" y=\"-121.2\" font-family=\"Times,serif\" font-size=\"14.00\">len(feedback) &gt; 0</text>\n",
+ "</g>\n",
+ "<!-- final_result -->\n",
+ "<g id=\"node10\" class=\"node\">\n",
+ "<title>final_result</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M266.38,-36C266.38,-36 212.88,-36 212.88,-36 206.88,-36 200.88,-30 200.88,-24 200.88,-24 200.88,-12 200.88,-12 200.88,-6 206.88,0 212.88,0 212.88,0 266.38,0 266.38,0 272.38,0 278.38,-6 278.38,-12 278.38,-12 278.38,-24 278.38,-24 278.38,-30 272.38,-36 266.38,-36\"/>\n",
+ "<text text-anchor=\"middle\" x=\"239.63\" y=\"-12.95\" font-family=\"Times,serif\" font-size=\"14.00\">final_result</text>\n",
+ "</g>\n",
+ "<!-- process_feedback&#45;&gt;final_result -->\n",
+ "<g id=\"edge11\" class=\"edge\">\n",
+ "<title>process_feedback&#45;&gt;final_result</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M239.63,-66.58C239.63,-60.61 239.63,-53.76 239.63,-47.15\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"243.13,-47.47 239.63,-37.47 236.13,-47.47 243.13,-47.47\"/>\n",
+ "</g>\n",
+ "<!-- input__feedback -->\n",
+ "<g id=\"node9\" class=\"node\">\n",
+ "<title>input__feedback</title>\n",
+ "<ellipse fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" cx=\"135.63\" cy=\"-167.5\" rx=\"68.24\" ry=\"18\"/>\n",
+ "<text text-anchor=\"middle\" x=\"135.63\" y=\"-162.45\" font-family=\"Times,serif\" font-size=\"14.00\">input: feedback</text>\n",
+ "</g>\n",
+ "<!-- input__feedback&#45;&gt;process_feedback -->\n",
+ "<g id=\"edge4\" class=\"edge\">\n",
+ "<title>input__feedback&#45;&gt;process_feedback</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M156.92,-150.02C171.9,-138.42 192.18,-122.73 208.9,-109.78\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"210.51,-112.19 216.28,-103.3 206.23,-106.65 210.51,-112.19\"/>\n",
+ "</g>\n",
+ "</g>\n",
+ "</svg>\n"
+ ],
+ "text/plain": [
+ "<graphviz.graphs.Digraph at 0x1065c4fd0>"
+ ]
+ },
+ "execution_count": 2,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "app = email_assistant_application()\n",
+ "app.visualize(include_conditions=True, include_state=False)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7b69292e-1db9-45d6-9c45-ad1940837cda",
+ "metadata": {},
+ "source": [
+ "# Telemetry\n",
+ "\n",
+ "To see the application \"think\", let's open up telemetry. Before running the next cell ensure `burr` is run and you have the UI open. Then navigate to the following link:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "6ab6da4d-3011-4fc3-847e-93f948667073",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/markdown": [
+ "[Link to UI](http://localhost:7241/project/demo:email_assistant/a053f4c9-1973-4ef1-94ac-b0c2db19be7f)"
+ ],
+ "text/plain": [
+ "<IPython.core.display.Markdown object>"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "from IPython.display import Markdown\n",
+ "url = f\"[Link to UI](http://localhost:7241/project/demo:email_assistant/{app.uid})\"\n",
+ "Markdown(url)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "f3b1cf53-fbbe-42c4-822f-175388f8fb59",
+ "metadata": {},
+ "source": [
+ "# Prompt the machine\n",
+ "\n",
+ "I'm going to be asking that the LLM help me turn around a cold-call. We get a lot of these -- I'm curious if it can help me leverage this for OS promotion! Note that this is a dubious (at best) use-case, but hey, these emails are likely written by AI as well.\n",
+ "\n",
+ "Change these to your use-case:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "8f724b1e-e282-4e8d-9c13-8e218ee04e87",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "EMAIL = \"\"\"Hi Elijah,\n",
+ "\n",
+ "I hope you are well.\n",
+ "\n",
+ "I am connecting again to know your thoughts on my previous email regarding DAGWorks's feature in our forthcoming 8th annual edition on Artificial Intelligence. I want to explain how this recognition will help you attain more qualified prospects and increase your client base.\n",
+ "\n",
+ "Our previous clients have seen a visible increase in prospect conversion rate by effectively utilizing this recognition. We would like DAGWorks to also leverage this recognition and witness impressive results.\n",
+ "\n",
+ "Please inform me of your availability for a quick chat. I look forward to your kind response.\n",
+ "\n",
+ "\n",
+ "Regards,\n",
+ "Someone\"\"\"\n",
+ "\n",
+ "INSTRUCTIONS = \"\"\"I get a bunch of these emails and usually ignore them. I don't want to pay \n",
+ "(or really engage) but I want to see if they can help promote our tooling, \n",
+ "particularly the OS libraries Hamilton and Burr, which are tools for AI practitioners. \n",
+ "Let's see if we can turn this cold-call around.\n",
+ "\"\"\""
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "02ecf380-0da8-4f25-aa4c-7691c13bf18a",
+ "metadata": {},
+ "source": [
+ "# Prompting for user-response\n",
+ "\n",
+ "During execution, there are several points at which the user provides additional feedback. This is:\n",
+ "\n",
+ "1. When the LLM has questions for the user (`request_answers`)\n",
+ "2. When the LLM needs feedback (`request_feedback`)\n",
+ "\n",
+ "These are functions we'll call later to ensure you can interact with the LLM. In a web-service this would be the point that we go back and forth (E.G. stop before), and would be replaced by endpoints. We're just using `input()` here as its the simplest way to get user input.\n",
+ "\n",
+ "We are going to run this until we get to the `final_result` step. We're going to have two stopping conditions:\n",
+ "\n",
+ "- `halt_before`: `clarify_instructions` and `process_feedback` -- these are the two steps that require input. We want to stop before so we can feed input to the user. We will call the above functions.\n",
+ "- `halt_after` : `final_result` -- this means we're done! We can just print out the draft.\n",
+ "\n",
+ "We will break at `final_result`, as the state machine is complete."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "id": "1c8badad-496f-4d0e-848d-beee0b836243",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The email assistant wants more information:\n",
+ "\n"
+ ]
+ },
+ {
+ "name": "stdin",
+ "output_type": "stream",
+ "text": [
+ "1. Can you please provide some specific features or achievements about the Hamilton and Burr libraries that you would like highlighted? Wide usage, large number of enterprise customers, pluggable, lightweight\n",
+ "2. Are there any particular benefits or testimonials from users of Hamilton and Burr libraries that we can share? let's hold off on that\n",
+ "3. What level of engagement, if any, are you willing to have with this organization beyond this instance of promotion? let's avoid taht\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "here's a draft!: \n",
+ " Subject: Re: DAGWorks's Feature in Your 8th Annual Edition on Artificial Intelligence\n",
+ "\n",
+ "Dear [Someone],\n",
+ "\n",
+ "Thank you for your last email and the opportunity to feature DAGWorks in your forthcoming 8th annual edition on Artificial Intelligence. Your outreach is appreciated and has certainly sparked some interest given the proven positive results you've suggested.\n",
+ "\n",
+ "In the spirit of establishing a mutual understanding, we're particularly proud of our standout tools Hamilton and Burr. Both libraries have been recognized for their wide usage, tailor-made features for enterprises, and their pluggability and lightweight nature. This recognition has been supported by a large number of enterprise customers, cementing their position as formidable tools in the AI field.\n",
+ "\n",
+ "Without necessarily engaging in a full-fledged partnership, it would be interesting to consider how these libraries could be highlighted in your edition. This could help promote our tooling even further and potentially draw in more users in the AI community.\n",
+ "\n",
+ "However, let me be frank in stating the nature of our engagement. While we're interested to understand the potential visibility this placement could provide for DAGWorks, at this time, we are not looking to make a financial commitment to your organization.\n",
+ "\n",
+ "Despite this, I'd like to hear more about how this feature can bring added value to our organization and particularly these libraries. Please feel free to share any further details that can help us gauge this opportunity.\n",
+ "\n",
+ "Looking forward to hearing from you,\n",
+ "\n",
+ "Best wishes,\n",
+ "Elijah,\n",
+ "DAGWorks.\n",
+ " \n",
+ " \n",
+ " What feedback do you have? If you have no feedback then we'll finish it up.\n"
+ ]
+ },
+ {
+ "name": "stdin",
+ "output_type": "stream",
+ "text": [
+ "Write feedback or leave blank to continue (if you're happy) Make it slightly less formal\n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "here's a draft!: \n",
+ " Subject: Re: DAGWorks's Feature in Your 8th Annual Edition on Artificial Intelligence\n",
+ "\n",
+ "Hey [Someone],\n",
+ "\n",
+ "Thanks for reaching out. It's good to hear you're interested in featuring DAGWorks in your upcoming AI edition. You've definitely caught our attention with the promise of those impressive results and I'd like to explore the terms a bit before committing to anything.\n",
+ "\n",
+ "Hamilton and Burr - our pride and joy - are two OS libraries that have established a pretty solid name in the AI world. They're widely used, customizable, lightweight, and packed with enterprise features. True gems for AI practitioners!\n",
+ "\n",
+ "I’m curious about the potential to put a spotlight on these two in your edition. What better way to get the word out, right? But let's keep it real, while I fancy the idea of a wider audience, we're playing it cautiously and not currently looking to splash any cash. \n",
+ "\n",
+ "That being said, I'm all ears to hear more on how this feature might benefit DAGWorks, especially with regards to Hamilton and Burr. Can you spill some beans about what we might expect?\n",
+ "\n",
+ "Eager to hear back from you,\n",
+ "\n",
+ "Best,\n",
+ "Elijah,\n",
+ "DAGWorks. \n",
+ " \n",
+ " What feedback do you have? If you have no feedback then we'll finish it up.\n"
+ ]
+ },
+ {
+ "name": "stdin",
+ "output_type": "stream",
+ "text": [
+ "Write feedback or leave blank to continue (if you're happy) \n"
+ ]
+ },
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "final result is: Subject: Re: DAGWorks's Feature in Your 8th Annual Edition on Artificial Intelligence\n",
+ "\n",
+ "Hey [Someone],\n",
+ "\n",
+ "Thanks for reaching out. It's good to hear you're interested in featuring DAGWorks in your upcoming AI edition. You've definitely caught our attention with the promise of those impressive results and I'd like to explore the terms a bit before committing to anything.\n",
+ "\n",
+ "Hamilton and Burr - our pride and joy - are two OS libraries that have established a pretty solid name in the AI world. They're widely used, customizable, lightweight, and packed with enterprise features. True gems for AI practitioners!\n",
+ "\n",
+ "I’m curious about the potential to put a spotlight on these two in your edition. What better way to get the word out, right? But let's keep it real, while I fancy the idea of a wider audience, we're playing it cautiously and not currently looking to splash any cash. \n",
+ "\n",
+ "That being said, I'm all ears to hear more on how this feature might benefit DAGWorks, especially with regards to Hamilton and Burr. Can you spill some beans about what we might expect?\n",
+ "\n",
+ "Eager to hear back from you,\n",
+ "\n",
+ "Best,\n",
+ "Elijah,\n",
+ "DAGWorks.\n"
+ ]
+ }
+ ],
+ "source": [
+ "def request_answers(questions):\n",
+ " \"\"\"Requests answers from the user for the questions the LLM has\"\"\"\n",
+ " answers = []\n",
+ " print(\"The email assistant wants more information:\\n\")\n",
+ " for question in questions:\n",
+ " answers.append(input(question))\n",
+ " return answers\n",
+ "\n",
+ "def request_feedback(draft):\n",
+ " \"\"\"Requests feedback from the user for a draft\"\"\"\n",
+ " print( \n",
+ " f\"here's a draft!: \\n {draft} \\n \\n What feedback do you have?\",\n",
+ " \"If you have no feedback then we'll finish it up.\"\n",
+ " )\n",
+ " return input(\"Write feedback or leave blank to continue (if you're happy)\")\n",
+ "\n",
+ "inputs = {\n",
+ " \"email_to_respond\" : EMAIL,\n",
+ " \"response_instructions\" : INSTRUCTIONS\n",
+ "}\n",
+ "while True:\n",
+ " action, result, state = app.run(\n",
+ " halt_before=[\"clarify_instructions\", \"process_feedback\"], \n",
+ " halt_after=[\"final_result\"],\n",
+ " inputs=inputs\n",
+ " )\n",
+ " if action.name == \"clarify_instructions\":\n",
+ " questions = state[\"clarification_questions\"]\n",
+ " answers = request_answers(questions)\n",
+ " inputs = {\n",
+ " \"clarification_inputs\" : answers\n",
+ " }\n",
+ " if action.name == \"process_feedback\":\n",
+ " feedback = request_feedback(state[\"current_draft\"])\n",
+ " inputs = {\"feedback\" : feedback}\n",
+ " if action.name == \"final_result\":\n",
+ " print(\"final result is:\", state[\"current_draft\"])\n",
+ " break"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.11.6"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/examples/email-assistant/requirements.txt b/examples/email-assistant/requirements.txt
new file mode 100644
index 00000000..d0d56c1a
--- /dev/null
+++ b/examples/email-assistant/requirements.txt
@@ -0,0 +1,2 @@
+burr[start]
+openai
diff --git a/examples/email-assistant/statemachine.png b/examples/email-assistant/statemachine.png
new file mode 100644
index 00000000..fa137358
Binary files /dev/null and b/examples/email-assistant/statemachine.png differ