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
PR updates
elijahbenizzy04f7e293989f
diff --git a/examples/gpt/application.py b/examples/gpt/application.pyindex b35d62a0..9dc60d82 100644--- a/examples/gpt/application.py+++ b/examples/gpt/application.py@@ -8,7 +8,6 @@ from burr.core import Application, ApplicationBuilder, State, default, whenfrom burr.core.action import actionfrom burr.integrations.hamilton import Hamilton, append_state, from_state, update_statefrom burr.lifecycle import LifecycleAdapter-from burr.visibility import TracerFactoryMODES = {"answer_question": "text",@@ -19,18 +18,16 @@ MODES = {@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"}}+def process_prompt(state: State, prompt: str) -> Tuple[dict, State]:+ 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+def check_safety(state: State) -> Tuple[dict, State]:+ result = {"safe": "unsafe" not in state["prompt"]} # quick hack to demonstratereturn result, state.update(safe=result["safe"])@@ -39,33 +36,28 @@ def _get_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}+def choose_mode(state: State) -> Tuple[dict, State]:+ 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'."+ )++ result = _get_openai_client().chat.completions.create(+ model="gpt-4",+ messages=[+ {"role": "system", "content": "You are a helpful assistant"},+ {"role": "user", "content": prompt},+ ],+ )+ content = result.choices[0].message.content+ mode = content.lower()+ if mode not in MODES:+ mode = "unknown"+ result = {"mode": mode}return result, state.update(**result)@@ -83,70 +75,50 @@ def prompt_for_more(state: State) -> Tuple[dict, State]:@action(reads=["prompt", "chat_history", "mode"], writes=["response"])def chat_response(- state: State,- prepend_prompt: str,- __tracer: TracerFactory,- model: str = "gpt-3.5-turbo",+ state: State, prepend_prompt: str, display_type: str = "text", 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"}+ 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+ ]+ client = _get_openai_client()+ result = client.chat.completions.create(+ model=model,+ messages=chat_history_api_format,+ )+ 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"}- }+def image_response(state: State, model: str = "dall-e-2") -> Tuple[dict, State]:+ client = _get_openai_client()+ result = client.images.generate(+ model=model, prompt=state["prompt"], size="1024x1024", quality="standard", n=1+ )+ response = result.data[0].url+ 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"]}+def response(state: State) -> Tuple[dict, State]:+ if not state["safe"]:+ result = {+ "chat_item": {+ "role": "assistant",+ "content": "I'm sorry, I can't respond to that.",+ "type": "text",+ }+ }+ else:+ result = {"chat_item": state["response"]}return result, state.append(chat_history=result["chat_item"])diff --git a/telemetry/ui/src/components/routes/AppList.tsx b/telemetry/ui/src/components/routes/AppList.tsxindex da9eca95..b801864c 100644--- a/telemetry/ui/src/components/routes/AppList.tsx+++ b/telemetry/ui/src/components/routes/AppList.tsx@@ -60,7 +60,7 @@ export const AppListTable = (props: { apps: ApplicationSummary[]; projectId: str<TableBody>{appsToDisplay.map((app) => (<TableRow- key={props.projectId}+ key={app.app_id}className="hover:bg-gray-50 cursor-pointer"href={`/project/${props.projectId}/${app.app_id}`}>