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
Updates server to have chatbot example
elijahbenizzy6a3c7261ae1a
diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.pyindex 39862bf2..1c42de79 100644--- a/burr/tracking/server/backend.py+++ b/burr/tracking/server/backend.py@@ -155,10 +155,10 @@ class LocalBackend(BackendBase):if not os.path.exists(graph_file):raise fastapi.HTTPException(status_code=404,- detail=f"Graph file not found for app: "- f"{app_id} from project: {project_id}. "- f"Was this properly executed?",+ detail=f"Graph file for app: {app_id} from project: {project_id} not found",)+ async with aiofiles.open(graph_file) as f:+ str_graph = await f.read()steps_by_sequence_id = {}spans_by_id = {}if os.path.exists(log_file):@@ -189,8 +189,6 @@ class LocalBackend(BackendBase):for span in spans_by_id.values():step = steps_by_sequence_id[span.begin_entry.action_sequence_id]step.spans.append(span)- async with aiofiles.open(graph_file) as f:- str_graph = await f.read()return ApplicationLogs(application=schema.ApplicationModel.parse_raw(str_graph),steps=list(steps_by_sequence_id.values()),diff --git a/burr/tracking/server/examples/chatbot.py b/burr/tracking/server/examples/chatbot.pynew file mode 100644index 00000000..54bcb630--- /dev/null+++ b/burr/tracking/server/examples/chatbot.py@@ -0,0 +1,68 @@+import functools+from typing import List, Literal++import pydantic+from fastapi import FastAPI+from starlette.requests import Request++from burr.core import Application, State+from burr.tracking import LocalTrackingClient++from examples.gpt import application as chat_application+++class ChatItem(pydantic.BaseModel):+ content: str+ type: Literal["image", "text", "code", "error"]+ role: Literal["user", "assistant"]++[email protected]_cache(maxsize=128)+def _get_application(project_id: str, app_id: str) -> Application:+ app = chat_application.application(use_hamilton=False, app_id=app_id, project_id="demo:chatbot")+ if LocalTrackingClient.app_log_exists(project_id, app_id):+ state, _ = LocalTrackingClient.load_state(project_id, app_id) # TODO -- handle entrypoint+ app.update_state(+ State(state)+ ) # TODO -- handle the entrypoint -- this will always reset to prompt+ return app+++def chat_response(project_id: str, app_id: str, prompt: str) -> List[ChatItem]:+ """Chat response endpoint.++ :param project_id: Project ID to run+ :param app_id: Application ID to run+ :param prompt: Prompt to send to the chatbot+ :return:+ """+ burr_app = _get_application(project_id, app_id)+ _, _, state = burr_app.run(halt_after=["response"], inputs=dict(prompt=prompt))+ return state.get("chat_history", [])+++def chat_history(project_id: str, app_id: str) -> List[ChatItem]:+ burr_app = _get_application(project_id, app_id)+ state = burr_app.state+ return state.get("chat_history", [])+++async def create_new_application(request: Request, project_id: str, app_id: str) -> str:+ """Quick helper to create a new application. Just returns true, you'll want to fetch afterwards.+ In a better chatbot you'd want to either have the frontend store this and create on demand or return+ the actual application model"""+ # side-effect is to create the application+ chat_application.application(use_hamilton=False, app_id=app_id, project_id=project_id)+ return app_id # just return it for now+++def register(app: FastAPI, api_prefix: str):+ app.post(f"{api_prefix}/{{project_id}}/{{app_id}}/response", response_model=List[ChatItem])(+ chat_response+ )+ app.get(f"{api_prefix}/{{project_id}}/{{app_id}}/history", response_model=List[ChatItem])(+ chat_history+ )+ app.post(f"{api_prefix}/{{project_id}}/{{app_id}}/create", response_model=str)(+ create_new_application+ )diff --git a/burr/tracking/server/run.py b/burr/tracking/server/run.pyindex d51c5bb3..ddc63144 100644--- a/burr/tracking/server/run.py+++ b/burr/tracking/server/run.py@@ -3,6 +3,7 @@ from importlib.resources import filesfrom typing import Sequencefrom burr.integrations.base import require_plugin+from burr.tracking.server.examples import chatbottry:import uvicorn@@ -10,7 +11,8 @@ try:from fastapi.staticfiles import StaticFilesfrom starlette.templating import Jinja2Templates- from burr.tracking.server import backend, schema+ from burr.tracking.server import backend as backend_module+ from burr.tracking.server import schemafrom burr.tracking.server.schema import ApplicationLogsexcept ImportError as e:require_plugin(@@ -30,10 +32,10 @@ except ImportError as e:app = FastAPI()-backend = backend.LocalBackend()-SERVE_STATIC = os.getenv("BURR_SERVE_STATIC", "true").lower() == "true"+backend = backend_module.LocalBackend()+@app.get("/api/v0/projects", response_model=Sequence[schema.Project])async def get_projects(request: Request) -> Sequence[schema.Project]:@@ -75,6 +77,10 @@ async def ready() -> bool:return True+# Examples -- todo -- put them behind `if` statements+chatbot.register(app, "/api/v0/chatbot")++if SERVE_STATIC:BASE_ASSET_DIRECTORY = str(files("burr").joinpath("tracking/server/build"))