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 demo for tracing feature
elijahbenizzya4728eb7894d
diff --git a/burr/cli/demo_data.py b/burr/cli/demo_data.pyindex ea9e9612..6ee5e1ed 100644--- a/burr/cli/demo_data.py+++ b/burr/cli/demo_data.py@@ -7,6 +7,7 @@ from burr.lifecycle import PostRunStepHook, PreRunStepHookfrom examples.conversational_rag import application as conversational_rag_applicationfrom examples.counter import application as counter_applicationfrom examples.gpt import application as chatbot_application+from examples.gpt import application_with_traces as chatbot_application_with_tracesclass ProgressHook(@@ -27,7 +28,7 @@ class ProgressHook(print(f">>> Action {action.name} completed")-def generate_chatbot_data(data_dir: str):+def generate_chatbot_data(data_dir: str, use_traces: bool):working_conversations = {"chat-1-giraffe": ["Please draw a giraffe.", # Answered by the image mode@@ -63,11 +64,11 @@ def generate_chatbot_data(data_dir: str):broken_conversations = {"chat-6-demonstrate-errors": working_conversations["chat-1-giraffe"]}def _run_conversation(app_id, prompts):- app = chatbot_application.application(- use_hamilton=False,+ app = (chatbot_application_with_traces if use_traces else chatbot_application).application(app_id=app_id,storage_dir=data_dir,hooks=[ProgressHook()],+ **({"use_hamilton": False} if not use_traces else {}),)for prompt in prompts:app.run(halt_after=["response"], inputs={"prompt": prompt})@@ -136,6 +137,10 @@ def generate_rag_data(data_dir: str = "~/.burr"):def generate_all(data_dir: str):- generate_chatbot_data(data_dir)+ generate_chatbot_data(data_dir, False)+ generate_chatbot_data(data_dir, True)generate_counter_data(data_dir)generate_rag_data(data_dir)+++#diff --git a/burr/tracking/server/backend.py b/burr/tracking/server/backend.pyindex 52236022..2ae7c8ad 100644--- a/burr/tracking/server/backend.py+++ b/burr/tracking/server/backend.py@@ -60,6 +60,7 @@ class BackendBase(abc.ABC):def get_uri(project_id: str) -> str:project_id_map = {"demo:counter": "https://github.com/DAGWorks-Inc/burr/tree/main/examples/counter",+ "demo:tracing": "https://github.com/DAGWorks-Inc/burr/tree/main/examples/gpt/application_with_traces.py","demo:chatbot": "https://github.com/DAGWorks-Inc/burr/tree/main/examples/gpt","demo:conversational-rag": "https://github.com/DAGWorks-Inc/burr/tree/main/examples/conversational-rag",}diff --git a/examples/gpt/README.md b/examples/gpt/README.mdindex 4a0e2330..5119006c 100644--- a/examples/gpt/README.md+++ b/examples/gpt/README.md@@ -26,7 +26,7 @@ We have three files:- [application.py](application.py) -- This contains a mainline to generate the graph portrayal.- [requirements.txt](requirements.txt) -- Just the requirements. All this needs is Burr/Streamlit/openai- [streamlit_app.py](streamlit_app.py) -- This contains a simple Streamlit app to interact with the cow-+- [application_with_traces.py](application_with_traces.py) -- This showcases how to use the tracing feature.You must have your `OPENAI_API_KEY` set in your environment. You can do this by running.To run on streamlit, you can launch the app:diff --git a/examples/gpt/application_with_traces.py b/examples/gpt/application_with_traces.pynew file mode 100644index 00000000..99196fb6--- /dev/null+++ b/examples/gpt/application_with_traces.py@@ -0,0 +1,208 @@+from typing import List, Optional, Tuple++import openai++from burr.core import Application, ApplicationBuilder, State, default, when+from burr.core.action import action+from burr.lifecycle import LifecycleAdapter+from burr.visibility import TracerFactory++MODES = {+ "answer_question": "text",+ "generate_image": "image",+ "generate_code": "code",+ "unknown": "text",+}+++@action(reads=[], writes=["chat_history", "prompt"])+def process_prompt(state: State, prompt: str, __tracer: TracerFactory) -> Tuple[dict, State]:+ with __tracer("process_prompt"):+ result = {"chat_item": {"role": "user", "content": prompt, "type": "text"}}+ return result, state.wipe(keep=["prompt", "chat_history"]).append(+ chat_history=result["chat_item"]+ ).update(prompt=prompt)+++@action(reads=["prompt"], writes=["safe"])+def check_safety(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:+ with __tracer("check_safety"):+ result = {"safe": "unsafe" not in state["prompt"]} # quick hack to demonstrate+ return result, state.update(safe=result["safe"])+++def _get_openai_client():+ return openai.Client()+++@action(reads=["prompt"], writes=["mode"])+def choose_mode(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:+ with __tracer("generate_prompt"):+ prompt = (+ f"You are a chatbot. You've been prompted this: {state['prompt']}. "+ f"You have the capability of responding in the following modes: {', '.join(MODES)}. "+ "Please respond with *only* a single word representing the mode that most accurately "+ "corresponds to the prompt. Fr instance, if the prompt is 'draw a picture of a cat', "+ "the mode would be 'generate_image'. If the prompt is 'what is the capital of France', the mode would be 'answer_question'."+ "If none of these modes apply, please respond with 'unknown'."+ )+ with __tracer("query_openai", span_dependencies=["generate_prompt"]):+ with __tracer("create_openai_client"):+ client = _get_openai_client()+ with __tracer("query_openai"):+ result = client.chat.completions.create(+ model="gpt-4",+ messages=[+ {"role": "system", "content": "You are a helpful assistant"},+ {"role": "user", "content": prompt},+ ],+ )+ with __tracer("process_openai_response", span_dependencies=["query_openai"]):+ content = result.choices[0].message.content+ mode = content.lower()+ if mode not in MODES:+ mode = "unknown"+ result = {"mode": mode}+ return result, state.update(**result)+++@action(reads=["prompt", "chat_history"], writes=["response"])+def prompt_for_more(state: State) -> Tuple[dict, State]:+ result = {+ "response": {+ "content": "None of the response modes I support apply to your question. Please clarify?",+ "type": "text",+ "role": "assistant",+ }+ }+ return result, state.update(**result)+++@action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def chat_response(+ state: State,+ prepend_prompt: str,+ __tracer: TracerFactory,+ model: str = "gpt-3.5-turbo",+) -> Tuple[dict, State]:+ with __tracer("process_chat_history"):+ chat_history = state["chat_history"].copy()+ chat_history[-1]["content"] = f"{prepend_prompt}: {chat_history[-1]['content']}"+ chat_history_api_format = [+ {+ "role": chat["role"],+ "content": chat["content"],+ }+ for chat in chat_history+ ]+ with __tracer("query_openai", span_dependencies=["change_chat_history"]):+ with __tracer("create_openai_client"):+ client = _get_openai_client()+ with __tracer("query_openai", span_dependencies=["create_openai_client"]):+ result = client.chat.completions.create(+ model=model,+ messages=chat_history_api_format,+ )+ with __tracer("process_openai_response", span_dependencies=["query_openai"]):+ response = result.choices[0].message.content+ result = {+ "response": {"content": response, "type": MODES[state["mode"]], "role": "assistant"}+ }+ return result, state.update(**result)+++@action(reads=["prompt", "chat_history", "mode"], writes=["response"])+def image_response(+ state: State, __tracer: TracerFactory, model: str = "dall-e-2"+) -> Tuple[dict, State]:+ with __tracer("create_openai_client"):+ client = _get_openai_client()+ with __tracer("query_openai_image", span_dependencies=["create_openai_client"]):+ result = client.images.generate(+ model=model, prompt=state["prompt"], size="1024x1024", quality="standard", n=1+ )+ response = result.data[0].url+ with __tracer("process_openai_response", span_dependencies=["query_openai_image"]):+ result = {+ "response": {"content": response, "type": MODES[state["mode"]], "role": "assistant"}+ }+ return result, state.update(**result)+++@action(reads=["response", "safe", "mode"], writes=["chat_history"])+def response(state: State, __tracer: TracerFactory) -> Tuple[dict, State]:+ with __tracer("process_response"):+ if not state["safe"]:+ with __tracer("unsafe"):+ result = {+ "chat_item": {+ "role": "assistant",+ "content": "I'm sorry, I can't respond to that.",+ "type": "text",+ }+ }+ else:+ with __tracer("safe"):+ result = {"chat_item": state["response"]}+ return result, state.append(chat_history=result["chat_item"])+++# TODO -- add in error handling+# @action(reads=["error"], writes=["chat_history"])+# def error(state: State) -> Tuple[dict, State]:+# result = {"chat_record": {"role": "assistant", "content": str(state["error"]), "type": "error"}}+# return result, state.append(chat_history=result["chat_record"])+++def base_application(hooks: List[LifecycleAdapter], app_id: str, storage_dir: str):+ if hooks is None:+ hooks = []+ 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_hooks(*hooks)+ .with_tracker("demo:tracing", params={"app_id": app_id, "storage_dir": storage_dir})+ .build()+ )+++def application(+ app_id: Optional[str] = None,+ storage_dir: Optional[str] = "~/.burr",+ hooks: Optional[List[LifecycleAdapter]] = None,+) -> Application:+ return base_application(hooks, app_id, storage_dir)+++if __name__ == "__main__":+ app = application()+ app.visualize(output_file_path="digraph", include_conditions=False, view=True, format="png")diff --git a/telemetry/ui/src/components/routes/ProjectList.tsx b/telemetry/ui/src/components/routes/ProjectList.tsxindex aa7d4fa9..559c6dfa 100644--- a/telemetry/ui/src/components/routes/ProjectList.tsx+++ b/telemetry/ui/src/components/routes/ProjectList.tsx@@ -5,6 +5,7 @@ import { Loading } from '../common/loading';import { DateDisplay } from '../common/dates';import { Button } from '../common/button';import { Chip } from '../common/chip';+import { LinkText } from '../common/href';/*** Table of a project list. Uses the tailwind catalyst component.@@ -58,14 +59,10 @@ export const ProjectListTable = (props: { projects: Project[] }) => {<DateDisplay date={project.last_written} /></TableCell><TableCell>- <a+ <LinkTexthref={project.uri}- className="text-dwlightblue hover:underline"- target="_blank"- rel="noreferrer"- >- {project.uri.replace('https://github.com/DAGWorks-Inc/burr/tree/main/', '')}- </a>+ text={project.uri.replace('https://github.com/DAGWorks-Inc/burr/tree/main/', '')}+ /></TableCell><TableCell>{project.num_apps}</TableCell><TableCell>