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
Changes chatbot API to be a better demo for FastAPI integration
elijahbenizzy56edbfe393f1
diff --git a/burr/tracking/server/examples/chatbot.py b/burr/tracking/server/examples/chatbot.pyindex e5351bd5..c6a6b5aa 100644--- a/burr/tracking/server/examples/chatbot.py+++ b/burr/tracking/server/examples/chatbot.py@@ -3,17 +3,31 @@ import importlibfrom typing import List, Literalimport pydantic-from fastapi import FastAPI-from starlette.requests import Request+from fastapi import APIRouterfrom burr.core import Application+"""This file represents a simple chatbot API backed with Burr.+We manage an application, write to it with post endpoints, and read with+get/ endpoints.++This demonstrates how+"""+# We're doing dynamic import cause this lives within examples/+# navigate to the examples directory to read more about this!chat_application = importlib.import_module("burr.examples.multi-modal-chatbot.application") # noqa: F401+# the app is commented out as we dynamically register it+# app = FastAPI()++router = APIRouter()+class ChatItem(pydantic.BaseModel):+ """Pydantic model for a chat item. This is used to render the chat history."""+content: strtype: Literal["image", "text", "code", "error"]role: Literal["user", "assistant"]@@ -21,12 +35,15 @@ class ChatItem(pydantic.BaseModel):@functools.lru_cache(maxsize=128)def _get_application(project_id: str, app_id: str) -> Application:- app = chat_application.application(app_id=app_id, project_id=project_id)- return app+ """Quick tool to get the application -- caches it"""+ chat_app = chat_application.application(app_id=app_id, project_id=project_id)+ return chat_app[email protected]("/response/{{project_id}}/{{app_id}}", response_model=List[ChatItem])def chat_response(project_id: str, app_id: str, prompt: str) -> List[ChatItem]:- """Chat response endpoint.+ """Chat response endpoint. User passes in a prompt and the system returns the+ full chat history, so its easier to render.:param project_id: Project ID to run:param app_id: Application ID to run@@ -38,28 +55,40 @@ def chat_response(project_id: str, app_id: str, prompt: str) -> List[ChatItem]:return state.get("chat_history", [])[email protected]("/response/{project_id}/{app_id}", response_model=List[ChatItem])def chat_history(project_id: str, app_id: str) -> List[ChatItem]:- burr_app = _get_application(project_id, app_id)- state = burr_app.state+ """Endpoint to get chat history. Gets the application and returns the chat history from state.++ :param project_id: Project ID+ :param app_id: App ID.+ :return: The list of chat items in the state+ """+ chat_app = _get_application(project_id, app_id)+ state = chat_app.statereturn 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 creates the logging so it exists[email protected]("/create/{project_id}/{app_id}", response_model=str)+async def create_new_application(project_id: str, app_id: str) -> str:+ """Endpoint to create a new application -- used by the FE when+ the user types in a new App ID++ :param project_id: Project ID+ :param app_id: App ID+ :return: The app ID+ """+ # side-effect of this persists it -- see the application function for detailschat_application.application(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- )+# 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 ddc63144..7379f22c 100644--- a/burr/tracking/server/run.py+++ b/burr/tracking/server/run.py@@ -78,7 +78,7 @@ async def ready() -> bool:# Examples -- todo -- put them behind `if` statements-chatbot.register(app, "/api/v0/chatbot")+app.include_router(chatbot.router, prefix="/api/v0/chatbot")if SERVE_STATIC:diff --git a/telemetry/ui/src/api/models/ChatItem.ts b/telemetry/ui/src/api/models/ChatItem.tsindex 74bc96e3..55d047e1 100644--- a/telemetry/ui/src/api/models/ChatItem.ts+++ b/telemetry/ui/src/api/models/ChatItem.ts@@ -2,6 +2,9 @@/* istanbul ignore file *//* tslint:disable *//* eslint-disable */+/**+ * Pydantic model for a chat item. This is used to render the chat history.+ */export type ChatItem = {content: string;type: ChatItem.type;diff --git a/telemetry/ui/src/api/services/DefaultService.ts b/telemetry/ui/src/api/services/DefaultService.tsindex a5d081c2..2cb1be82 100644--- a/telemetry/ui/src/api/services/DefaultService.ts+++ b/telemetry/ui/src/api/services/DefaultService.ts@@ -94,7 +94,8 @@ export class DefaultService {}/*** Chat Response- * Chat response endpoint.+ * Chat response endpoint. User passes in a prompt and the system returns the+ * full chat history, so its easier to render.** :param project_id: Project ID to run* :param app_id: Application ID to run@@ -106,19 +107,17 @@ export class DefaultService {* @returns ChatItem Successful Response* @throws ApiError*/- public static chatResponseApiV0ChatbotProjectIdAppIdResponsePost(+ public static chatResponseApiV0ChatbotResponseProjectIdAppIdPost(projectId: string,appId: string,prompt: string): CancelablePromise<Array<ChatItem>> {return __request(OpenAPI, {method: 'POST',- url: '/api/v0/chatbot/{project_id}/{app_id}/response',- path: {- project_id: projectId,- app_id: appId- },+ url: '/api/v0/chatbot/response/{{project_id}}/{{app_id}}',query: {+ project_id: projectId,+ app_id: appId,prompt: prompt},errors: {@@ -128,18 +127,23 @@ export class DefaultService {}/*** Chat History+ * Endpoint to get chat history. Gets the application and returns the chat history from state.+ *+ * :param project_id: Project ID+ * :param app_id: App ID.+ * :return: The list of chat items in the state* @param projectId* @param appId* @returns ChatItem Successful Response* @throws ApiError*/- public static chatHistoryApiV0ChatbotProjectIdAppIdHistoryGet(+ public static chatHistoryApiV0ChatbotResponseProjectIdAppIdGet(projectId: string,appId: string): CancelablePromise<Array<ChatItem>> {return __request(OpenAPI, {method: 'GET',- url: '/api/v0/chatbot/{project_id}/{app_id}/history',+ url: '/api/v0/chatbot/response/{project_id}/{app_id}',path: {project_id: projectId,app_id: appId@@ -151,21 +155,24 @@ export class DefaultService {}/*** Create New Application- * 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+ * Endpoint to create a new application -- used by the FE when+ * the user types in a new App ID+ *+ * :param project_id: Project ID+ * :param app_id: App ID+ * :return: The app ID* @param projectId* @param appId* @returns string Successful Response* @throws ApiError*/- public static createNewApplicationApiV0ChatbotProjectIdAppIdCreatePost(+ public static createNewApplicationApiV0ChatbotCreateProjectIdAppIdPost(projectId: string,appId: string): CancelablePromise<string> {return __request(OpenAPI, {method: 'POST',- url: '/api/v0/chatbot/{project_id}/{app_id}/create',+ url: '/api/v0/chatbot/create/{project_id}/{app_id}',path: {project_id: projectId,app_id: appIddiff --git a/telemetry/ui/src/examples/AppSelector.tsx b/telemetry/ui/src/examples/AppSelector.tsxindex f3019e0b..84118e3c 100644--- a/telemetry/ui/src/examples/AppSelector.tsx+++ b/telemetry/ui/src/examples/AppSelector.tsx@@ -27,7 +27,7 @@ export const AppSelector = (props: {);const createAndUpdateMutation = useMutation((app_id: string) =>- DefaultService.createNewApplicationApiV0ChatbotProjectIdAppIdCreatePost(projectId, app_id),+ DefaultService.createNewApplicationApiV0ChatbotCreateProjectIdAppIdPost(projectId, app_id),{onSuccess: (appID) => {refetch().then((data) => {diff --git a/telemetry/ui/src/examples/Chatbot.tsx b/telemetry/ui/src/examples/Chatbot.tsxindex 4af38ee6..cebabcea 100644--- a/telemetry/ui/src/examples/Chatbot.tsx+++ b/telemetry/ui/src/examples/Chatbot.tsx@@ -115,7 +115,7 @@ export const Chatbot = (props: { projectId: string; appId: string | undefined })// TODO -- handle errors['chat', props.projectId, props.appId],() =>- DefaultService.chatHistoryApiV0ChatbotProjectIdAppIdHistoryGet(+ DefaultService.chatHistoryApiV0ChatbotResponseProjectIdAppIdGet(props.projectId,props.appId || '' // TODO -- find a cleaner way of doing a skip-token like thing here),@@ -134,7 +134,7 @@ export const Chatbot = (props: { projectId: string; appId: string | undefined })const mutation = useMutation((message: string) => {- return DefaultService.chatResponseApiV0ChatbotProjectIdAppIdResponsePost(+ return DefaultService.chatResponseApiV0ChatbotResponseProjectIdAppIdPost(props.projectId,props.appId || '',message