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.

Updates examples to use graph builder instead of application API

elijahbenizzy9e38d8653b56
diff --git a/examples/email-assistant/application.py b/examples/email-assistant/application.py
index 14b429bd..3310fa41 100644
--- a/examples/email-assistant/application.py
+++ b/examples/email-assistant/application.py
@@ -5,6 +5,7 @@ from typing import Tuple
import openai
from burr.core import Application, ApplicationBuilder, State, action, expr
+from burr.core.graph import GraphBuilder
from burr.tracking import LocalTrackingClient
@@ -142,33 +143,39 @@ def final_result(state: State) -> Tuple[dict, State]:
return result, state.update(final_draft=result["final_draft"])
+graph = (
+ GraphBuilder()
+ .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"),
+ )
+).build()
+
+
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_graph(graph)
.with_tracker("local", project=project)
.with_identifiers(app_id=app_id, partition_key=username)
.initialize_from(
diff --git a/examples/email-assistant/server.py b/examples/email-assistant/server.py
index 256b43ba..6e9a366d 100644
--- a/examples/email-assistant/server.py
+++ b/examples/email-assistant/server.py
@@ -1,5 +1,7 @@
import functools
+from burr.tracking import LocalTrackingClient
+
"""
This module contains the FastAPI code that interacts with the Burr application.
"""
@@ -11,7 +13,7 @@ from typing import Any, Dict, List, Literal, Optional
import pydantic
from fastapi import APIRouter, FastAPI
-from burr.core import Application
+from burr.core import Application, ApplicationBuilder
email_assistant_application = importlib.import_module(
"burr.examples.email-assistant.application"
@@ -65,7 +67,22 @@ class EmailAssistantState(pydantic.BaseModel):
def _get_application(project_id: str, app_id: str) -> Application:
"""Utility to get the application. Depending on
your use-case you might want to reload from state every time (or consider cache invalidation)"""
- app = email_assistant_application.application(app_id=app_id, project=project_id)
+ # graph = email_assistant_application.application(app_id=app_id, project=project_id)
+ graph = email_assistant_application.graph
+ tracker = LocalTrackingClient(project=project_id)
+ builder = (
+ ApplicationBuilder()
+ .with_graph(graph)
+ .with_tracker(tracker := LocalTrackingClient(project=project_id))
+ .with_identifiers(app_id=app_id)
+ .initialize_from(
+ tracker,
+ resume_at_next_action=True,
+ default_state={"draft_history": []},
+ default_entrypoint="process_input",
+ )
+ )
+ return builder.build()
return app
diff --git a/examples/multi-modal-chatbot/application.py b/examples/multi-modal-chatbot/application.py
index e8a0f0f5..03b95d99 100644
--- a/examples/multi-modal-chatbot/application.py
+++ b/examples/multi-modal-chatbot/application.py
@@ -4,7 +4,7 @@ from typing import List, Optional
import openai
-from burr.core import Application, ApplicationBuilder, State, default, when
+from burr.core import Application, ApplicationBuilder, State, default, graph, when
from burr.core.action import action
from burr.lifecycle import LifecycleAdapter
from burr.tracking import LocalTrackingClient
@@ -142,6 +142,43 @@ def response(state: State) -> State:
return state.append(chat_history=result["chat_item"])
+base_graph = (
+ graph.GraphBuilder()
+ .with_actions(
+ prompt=process_prompt,
+ check_openai_key=check_openai_key,
+ check_safety=check_safety,
+ decide_mode=choose_mode,
+ generate_image=image_response,
+ generate_code=chat_response.bind(
+ prepend_prompt="Please respond with *only* code and no other text (at all) to the following:",
+ ),
+ answer_question=chat_response.bind(
+ prepend_prompt="Please answer the following question:",
+ ),
+ prompt_for_more=prompt_for_more,
+ response=response,
+ )
+ .with_transitions(
+ ("prompt", "check_openai_key", default),
+ ("check_openai_key", "check_safety", when(has_openai_key=True)),
+ ("check_openai_key", "response", default),
+ ("check_safety", "decide_mode", when(safe=True)),
+ ("check_safety", "response", default),
+ ("decide_mode", "generate_image", when(mode="generate_image")),
+ ("decide_mode", "generate_code", when(mode="generate_code")),
+ ("decide_mode", "answer_question", when(mode="answer_question")),
+ ("decide_mode", "prompt_for_more", default),
+ (
+ ["generate_image", "answer_question", "generate_code", "prompt_for_more"],
+ "response",
+ ),
+ ("response", "prompt", default),
+ )
+ .build()
+)
+
+
def base_application(
hooks: List[LifecycleAdapter],
app_id: str,
@@ -155,37 +192,7 @@ def base_application(
tracker = LocalTrackingClient(project=project_id, storage_dir=storage_dir)
return (
ApplicationBuilder()
- .with_actions(
- prompt=process_prompt,
- check_openai_key=check_openai_key,
- check_safety=check_safety,
- decide_mode=choose_mode,
- generate_image=image_response,
- generate_code=chat_response.bind(
- prepend_prompt="Please respond with *only* code and no other text (at all) to the following:",
- ),
- answer_question=chat_response.bind(
- prepend_prompt="Please answer the following question:",
- ),
- prompt_for_more=prompt_for_more,
- response=response,
- )
- .with_transitions(
- ("prompt", "check_openai_key", default),
- ("check_openai_key", "check_safety", when(has_openai_key=True)),
- ("check_openai_key", "response", default),
- ("check_safety", "decide_mode", when(safe=True)),
- ("check_safety", "response", default),
- ("decide_mode", "generate_image", when(mode="generate_image")),
- ("decide_mode", "generate_code", when(mode="generate_code")),
- ("decide_mode", "answer_question", when(mode="answer_question")),
- ("decide_mode", "prompt_for_more", default),
- (
- ["generate_image", "answer_question", "generate_code", "prompt_for_more"],
- "response",
- ),
- ("response", "prompt", default),
- )
+ .with_graph(base_graph)
# initializes from the tracking log if it does not already exist
.initialize_from(
tracker,
diff --git a/examples/multi-modal-chatbot/server.py b/examples/multi-modal-chatbot/server.py
index 0718f7f3..f009f4ae 100644
--- a/examples/multi-modal-chatbot/server.py
+++ b/examples/multi-modal-chatbot/server.py
@@ -5,7 +5,8 @@ from typing import List, Literal
import pydantic
from fastapi import APIRouter
-from burr.core import Application
+from burr.core import Application, ApplicationBuilder
+from burr.tracking import LocalTrackingClient
"""This file represents a simple chatbot API backed with Burr.
We manage an application, write to it with post endpoints, and read with
@@ -24,6 +25,8 @@ chat_application = importlib.import_module(
router = APIRouter()
+graph = chat_application.base_graph
+
class ChatItem(pydantic.BaseModel):
"""Pydantic model for a chat item. This is used to render the chat history."""
@@ -36,8 +39,21 @@ class ChatItem(pydantic.BaseModel):
@functools.lru_cache(maxsize=128)
def _get_application(project_id: str, app_id: str) -> Application:
"""Quick tool to get the application -- caches it"""
- chat_app = chat_application.application(app_id=app_id, project_id=project_id)
- return chat_app
+ tracker = LocalTrackingClient(project=project_id, storage_dir="~/.burr")
+ return (
+ ApplicationBuilder()
+ .with_graph(graph)
+ # initializes from the tracking log if it does not already exist
+ .initialize_from(
+ tracker,
+ resume_at_next_action=False, # always resume from entrypoint in the case of failure
+ default_state={"chat_history": []},
+ default_entrypoint="prompt",
+ )
+ .with_tracker(tracker)
+ .with_identifiers(app_id=app_id)
+ .build()
+ )
@router.post("/response/{{project_id}}/{{app_id}}", response_model=List[ChatItem])
@@ -78,7 +94,7 @@ async def create_new_application(project_id: str, app_id: str) -> str:
:return: The app ID
"""
# side-effect of this persists it -- see the application function for details
- chat_application.application(app_id=app_id, project_id=project_id)
+ _get_application(app_id=app_id, project_id=project_id)
return app_id # just return it for now