Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.
Python aiburrchatbot-frameworkdagsgenerative-aigraphshacktoberfestllmopsllmsmlopspersistent-data-structurestate-machinestate-managementvisibility
Adds scripts to generate demo data on s3
elijahbenizzy911cb3546bcc
diff --git a/burr/cli/demo_data.py b/burr/cli/demo_data.pyindex 4582c2ae..25300931 100644--- a/burr/cli/demo_data.py+++ b/burr/cli/demo_data.py@@ -1,15 +1,32 @@import importlib+import loggingimport os+import uuid+from typing import Optional-from application import logger+from burr.core import ApplicationBuilder, Result, default, expr+from burr.core.graph import GraphBuilder+from burr.tracking import LocalTrackingClient+from burr.tracking.s3client import S3TrackingClient-conversational_rag_application = importlib.import_module("examples.conversational-rag.application")+logger = logging.getLogger(__name__)++conversational_rag_application = importlib.import_module(+ "examples.conversational-rag.simple_example.application"+)counter_application = importlib.import_module("examples.hello-world-counter.application")chatbot_application = importlib.import_module("examples.multi-modal-chatbot.application")chatbot_application_with_traces = importlib.import_module("examples.tracing-and-spans.application")-def generate_chatbot_data(data_dir: str, use_traces: bool):+def generate_chatbot_data(+ data_dir: Optional[str] = None,+ s3_bucket: Optional[str] = None,+ use_traces: bool = False,+ unique_app_names: bool = False,+):+ project_id = "demo_chatbot" if not use_traces else "demo_chatbot_with_traces"+ run_prefix = str(uuid.uuid4())[0:8] + "-" if unique_app_names else ""working_conversations = {"chat-1-giraffe": ["Please draw a giraffe.", # Answered by the image mode@@ -44,39 +61,87 @@ def generate_chatbot_data(data_dir: str, use_traces: bool):}broken_conversations = {"chat-6-demonstrate-errors": working_conversations["chat-1-giraffe"]}+ def _modify(app_id: str) -> str:+ return run_prefix + app_id+def _run_conversation(app_id, prompts):- app = (chatbot_application_with_traces if use_traces else chatbot_application).application(- app_id=app_id,- storage_dir=data_dir,+ tracker = (+ LocalTrackingClient(project=project_id, storage_dir=data_dir)+ if not s3_bucket+ else S3TrackingClient(project=project_id, bucket=s3_bucket)+ )+ graph = (chatbot_application_with_traces if use_traces else chatbot_application).graph+ app = (+ ApplicationBuilder()+ .with_graph(graph)+ .with_identifiers(app_id=app_id)+ .with_tracker(tracker)+ .with_entrypoint("prompt")+ .build())for prompt in prompts:app.run(halt_after=["response"], inputs={"prompt": prompt})for app_id, prompts in sorted(working_conversations.items()):- _run_conversation(app_id, prompts)+ _run_conversation(_modify(app_id), prompts)old_api_key = os.environ.get("OPENAI_API_KEY")os.environ["OPENAI_API_KEY"] = "fake"for app_id, prompts in sorted(broken_conversations.items()):try:- _run_conversation(app_id, prompts)+ _run_conversation(_modify(app_id), prompts)except Exception as e:print(f"Got an exception: {e}")os.environ["OPENAI_API_KEY"] = old_api_key-def generate_counter_data(data_dir: str = "~/.burr"):+def generate_counter_data(+ data_dir: str = "~/.burr", s3_bucket: Optional[str] = None, unique_app_names: bool = False+):+ counter = counter_application.counter+ tracker = (+ LocalTrackingClient(project="demo_counter", storage_dir=data_dir)+ if not s3_bucket+ else S3TrackingClient(project="demo_counter", bucket=s3_bucket)+ )+counts = [1, 10, 100, 50, 42]+ # This is just cause we don't want to change the code+ # TODO -- add ability to grab graph from application or something like that+ graph = (+ GraphBuilder()+ .with_actions(counter=counter, result=Result("counter"))+ .with_transitions(+ ("counter", "counter", expr("counter < count_to")),+ ("counter", "result", default),+ )+ .build()+ )for i, count in enumerate(counts):- app = counter_application.application(- count_up_to=count,- app_id=f"count-to-{count}",- storage_dir=data_dir,- partition_key=f"user_{i}",+ app_id = f"count-to-{count}"+ if unique_app_names:+ suffix = str(uuid.uuid4())[0:8]+ app_id = f"{app_id}-{suffix}"+ app = (+ ApplicationBuilder()+ .with_graph(graph)+ .with_identifiers(app_id=app_id, partition_key=f"user_{i}")+ .with_state(count_to=count, counter=0)+ .with_tracker(tracker)+ .with_entrypoint("counter")+ .build())+ # app = counter_application.application(+ # count_up_to=count,+ # app_id=f"count-to-{count}",+ # storage_dir=data_dir,+ # partition_key=f"user_{i}",+ # )app.run(halt_after=["result"])-def generate_rag_data(data_dir: str = "~/.burr"):+def generate_rag_data(+ data_dir: Optional[str] = None, s3_bucket: Optional[str] = None, unique_app_names: bool = False+):conversations = {"rag-1-food": ["What is Elijah's favorite food?",@@ -105,24 +170,43 @@ def generate_rag_data(data_dir: str = "~/.burr"):"Whose favorite food is better, Elijah's or Stefan's?" "exit",],}+ prefix = str(uuid.uuid4())[0:8] + "-" if unique_app_names else ""for app_id, prompts in sorted(conversations.items()):- app = conversational_rag_application.application(- app_id=app_id,- storage_dir=data_dir,+ graph = conversational_rag_application.graph()+ tracker = (+ LocalTrackingClient(project="demo_conversational-rag", storage_dir=data_dir)+ if not s3_bucket+ else S3TrackingClient(project="demo_conversational-rag", bucket=s3_bucket)+ )+ app_id = f"{prefix}{app_id}" if unique_app_names else app_id+ app = (+ ApplicationBuilder()+ .with_graph(graph)+ .with_identifiers(app_id=app_id)+ .with_tracker(tracker)+ .with_entrypoint("human_converse")+ .build())+ logger.warning(f"Running {app_id}...")for prompt in prompts:app.run(halt_after=["ai_converse", "terminal"], inputs={"user_question": prompt})-def generate_all(data_dir: str):+def generate_all(+ data_dir: Optional[str] = None, s3_bucket: Optional[str] = None, unique_app_names: bool = False+):logger.info("Generating chatbot data")- generate_chatbot_data(data_dir, False)+ generate_chatbot_data(+ data_dir=data_dir, s3_bucket=s3_bucket, use_traces=False, unique_app_names=unique_app_names+ )logger.info("Generating chatbot data with traces")- generate_chatbot_data(data_dir, True)+ generate_chatbot_data(+ data_dir=data_dir, s3_bucket=s3_bucket, use_traces=True, unique_app_names=unique_app_names+ )logger.info("Generating counter data")- generate_counter_data(data_dir)+ generate_counter_data(data_dir=data_dir, s3_bucket=s3_bucket, unique_app_names=unique_app_names)logger.info("Generating RAG data")- generate_rag_data(data_dir)+ generate_rag_data(data_dir=data_dir, s3_bucket=s3_bucket, unique_app_names=unique_app_names)#diff --git a/examples/conversational-rag/simple_example/application.py b/examples/conversational-rag/simple_example/application.pyindex 1f798c49..9b1c9b65 100644--- a/examples/conversational-rag/simple_example/application.py+++ b/examples/conversational-rag/simple_example/application.py@@ -6,6 +6,7 @@ from hamilton import dataflows, driverimport burr.corefrom burr.core import Action, Application, ApplicationBuilder, State, default, exprfrom burr.core.action import action+from burr.core.graph import GraphBuilderfrom burr.lifecycle import LifecycleAdapter, PostRunStepHook, PreRunStepHook# create the pipeline@@ -70,12 +71,7 @@ def human_converse(state: State, user_question: str) -> Tuple[dict, State]:return {"question": user_question}, state-def application(- app_id: Optional[str] = None,- storage_dir: Optional[str] = "~/.burr",- hooks: Optional[List[LifecycleAdapter]] = None,-) -> Application:- # our initial knowledge base+def graph():input_text = ["harrison worked at kensho","stefan worked at Stitch Fix",@@ -87,14 +83,8 @@ def application("stefan likes to bake sourdough",]vector_store = bootstrap_vector_db(conversational_rag_driver, input_text)- app = (- ApplicationBuilder()- .with_state(- **{- "question": "",- "chat_history": [],- }- )+ return (+ GraphBuilder().with_actions(# bind the vector store to the AI conversational stepai_converse=ai_converse.bind(vector_store=vector_store),@@ -106,6 +96,26 @@ def application(("human_converse", "terminal", expr("'exit' in question")),("human_converse", "ai_converse", default),)+ .build()+ )+++def application(+ app_id: Optional[str] = None,+ storage_dir: Optional[str] = "~/.burr",+ hooks: Optional[List[LifecycleAdapter]] = None,+) -> Application:+ # our initial knowledge base++ app = (+ ApplicationBuilder()+ .with_state(+ **{+ "question": "",+ "chat_history": [],+ }+ )+ .with_graph(graph()).with_entrypoint("human_converse").with_tracker(project="demo_conversational-rag", params={"storage_dir": storage_dir}).with_identifiers(app_id=app_id, partition_key="sample_user")diff --git a/examples/multi-modal-chatbot/application.py b/examples/multi-modal-chatbot/application.pyindex 03b95d99..5f40da57 100644--- a/examples/multi-modal-chatbot/application.py+++ b/examples/multi-modal-chatbot/application.py@@ -142,7 +142,7 @@ def response(state: State) -> State:return state.append(chat_history=result["chat_item"])-base_graph = (+graph = (graph.GraphBuilder().with_actions(prompt=process_prompt,@@ -192,7 +192,7 @@ def base_application(tracker = LocalTrackingClient(project=project_id, storage_dir=storage_dir)return (ApplicationBuilder()- .with_graph(base_graph)+ .with_graph(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.pyindex f009f4ae..344e819a 100644--- a/examples/multi-modal-chatbot/server.py+++ b/examples/multi-modal-chatbot/server.py@@ -25,7 +25,7 @@ chat_application = importlib.import_module(router = APIRouter()-graph = chat_application.base_graph+graph = chat_application.graphclass ChatItem(pydantic.BaseModel):diff --git a/examples/streaming-fastapi/application.py b/examples/streaming-fastapi/application.pyindex 8e90df8e..63a84d29 100644--- a/examples/streaming-fastapi/application.py+++ b/examples/streaming-fastapi/application.py@@ -7,6 +7,7 @@ import openaifrom burr.core import ApplicationBuilder, State, default, whenfrom burr.core.action import action, streaming_actionfrom burr.core.graph import GraphBuilder+from burr.tracking.s3client import S3TrackingClientMODES = ["answer_question",@@ -173,7 +174,12 @@ def application(app_id: Optional[str] = None):.with_entrypoint("prompt").with_state(chat_history=[]).with_graph(graph)- .with_tracker(project="demo_chatbot_streaming")+ # .with_tracker(project="demo_chatbot_streaming")+ .with_tracker(+ tracker=S3TrackingClient(+ bucket="burr-prod-test", project="demo_chatbot_streaming", non_blocking=True+ )+ ).with_identifiers(app_id=app_id).build())diff --git a/examples/streaming-fastapi/streamlit_app.py b/examples/streaming-fastapi/streamlit_app.pyindex 9b6bd50b..bed5445a 100644--- a/examples/streaming-fastapi/streamlit_app.py+++ b/examples/streaming-fastapi/streamlit_app.py@@ -1,12 +1,16 @@import asyncio+import loggingimport uuidimport application as chatbot_applicationimport streamlit as st+from hamilton.log_setup import setup_loggingimport burr.corefrom burr.core.action import AsyncStreamingResultContainer+setup_logging(logging.INFO)+def render_chat_message(chat_item: dict):content = chat_item["content"]diff --git a/examples/tracing-and-spans/application.py b/examples/tracing-and-spans/application.pyindex d3bb8244..f6aa8e16 100644--- a/examples/tracing-and-spans/application.py+++ b/examples/tracing-and-spans/application.py@@ -4,6 +4,7 @@ import openaifrom burr.core import Application, ApplicationBuilder, State, default, whenfrom burr.core.action import action+from burr.core.graph import GraphBuilderfrom burr.visibility import TracerFactoryMODES = {@@ -146,42 +147,49 @@ def response(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:return result, state.append(chat_history=result["chat_item"])+graph = (+ GraphBuilder()+ .with_actions(+ prompt=process_prompt,+ 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_safety", 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 application(app_id: Optional[str] = None,storage_dir: Optional[str] = "~/.burr",) -> Application:return (ApplicationBuilder()- .with_actions(- prompt=process_prompt,- 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_entrypoint("prompt").with_state(chat_history=[])- .with_transitions(- ("prompt", "check_safety", 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(graph).with_tracker(project="demo_tracing", params={"storage_dir": storage_dir}).with_identifiers(app_id=app_id).build()diff --git a/pyproject.toml b/pyproject.tomlindex f019a08a..f55e6419 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -80,11 +80,17 @@ tracking-client = ["pydantic"]+tracking-client-s3 = [+ "burr[tracking-client]",+ "aiobotocore"+]+tracking-server = ["click","fastapi","uvicorn","pydantic",+ "pydantic-settings","fastapi-pagination","aiofiles","requests",@@ -106,6 +112,15 @@ start = ["burr[learn]"]+# All the bloatware from various LLM demos+# In the future most people will be relying on simple APIs, not this+# But its good for demos!+bloat = [+ "langchain",+ "langchain-community",+ "langchain-openai"+]+# just install everything for developersdeveloper = ["burr[streamlit]",@@ -113,6 +128,7 @@ developer = ["burr[tracking]","burr[tests]","burr[documentation]",+ "burr[bloat]","build","twine","pre-commit",@@ -131,6 +147,11 @@ burr = ["burr/tracking/server/demo_data/**/*"]++[tool.aerich]+tortoise_orm = "burr.tracking.server.s3.settings.TORTOISE_ORM"+location = "./burr/tracking/server/s3/migrations"+src_folder = "./."[project.urls]Homepage = "https://github.com/dagworks-inc/burr"Documentation = "https://github.com/dagworks-inc/burr"