Skip to content

armgit / burr

Public Imported from GitHub
0

Build applications that make decisions (chatbots, agents, simulations, etc...). Monitor, trace, persist, and execute on your own infrastructure.

Adds deep researcher example (#530)

Margaret Lange448f38046ed4
diff --git a/burr/tracking/server/run.py b/burr/tracking/server/run.py
index 6b5d446e..acb159a7 100644
--- a/burr/tracking/server/run.py
+++ b/burr/tracking/server/run.py
@@ -42,6 +42,7 @@ try:
email_assistant = importlib.import_module("burr.examples.email-assistant.server")
chatbot = importlib.import_module("burr.examples.multi-modal-chatbot.server")
streaming_chatbot = importlib.import_module("burr.examples.streaming-fastapi.server")
+ deep_researcher = importlib.import_module("burr.examples.deep-researcher.server")
except ImportError as e:
raise e
@@ -320,6 +321,7 @@ async def version() -> dict:
app.include_router(chatbot.router, prefix="/api/v0/chatbot")
app.include_router(email_assistant.router, prefix="/api/v0/email_assistant")
app.include_router(streaming_chatbot.router, prefix="/api/v0/streaming_chatbot")
+app.include_router(deep_researcher.router, prefix="/api/v0/deep_researcher")
if SERVE_STATIC:
BASE_ASSET_DIRECTORY = str(files("burr").joinpath("tracking/server/build"))
diff --git a/examples/deep-researcher/README.md b/examples/deep-researcher/README.md
new file mode 100644
index 00000000..934ef9c1
--- /dev/null
+++ b/examples/deep-researcher/README.md
@@ -0,0 +1,26 @@
+# Deep Researcher
+
+## Introduction
+
+The structure of the research assistant is taken from a [langchain and langgraph example](https://github.com/langchain-ai/local-deep-researcher). It is rewritten here in the Burr framework in `application.py`.
+
+![Deep Researcher](statemachine.png)
+
+The helper code in `prompts.py` and `utils.py` is directly taken from the original deep researcher codebase. The MIT license for the code is included in both those files.
+
+## Prerequisites
+
+Set the configuration variables at the beginning of the main section of `application.py`.
+
+Then install Python modules
+```sh
+pip install -r requirements.txt
+```
+
+You will need accounts for [Tavily search](https://tavily.com/) and the [OpenAI API](https://platform.openai.com/docs/overview). Once you have those accounts, set the environment variables TAVILY_API_KEY and OPENAI_API_KEY and run the script.
+
+```sh
+export OPENAI_API_KEY="YOUR_OPENAI_KEY"
+export TAVILY_API_KEY="YOUR_TAVILY_API_KEY"
+python application.py
+```
diff --git a/examples/deep-researcher/__init__.py b/examples/deep-researcher/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/examples/deep-researcher/application.py b/examples/deep-researcher/application.py
new file mode 100644
index 00000000..c22e2adf
--- /dev/null
+++ b/examples/deep-researcher/application.py
@@ -0,0 +1,292 @@
+import functools
+import importlib
+import json
+from typing import Optional
+
+import openai
+
+from burr.core import Application, ApplicationBuilder, State, action, expr, when
+from burr.core.graph import GraphBuilder
+from burr.tracking import LocalTrackingClient
+
+try:
+ prompts = importlib.import_module("burr.examples.deep-researcher.prompts")
+except ModuleNotFoundError:
+ import prompts
+
+try:
+ utils = importlib.import_module("burr.examples.deep-researcher.utils")
+except ModuleNotFoundError:
+ import utils
+
+
+def _get_openai_client():
+ """
+ Creates and caches an OpenAI client instance.
+
+ Returns:
+ openai.Client: A cached OpenAI client instance.
+ """
+ openai_client = openai.Client()
+ return openai_client
+
+
+def query_openai(system_instructions, human_message_content, stream=False):
+ """
+ Sends a query to the OpenAI API and retrieves the response.
+
+ Args:
+ system_instructions (str): Instructions for the system message.
+ human_message_content (str): Content of the human message.
+ stream (bool, optional): Whether to stream the response. Defaults to False.
+
+ Returns:
+ str: The content of the response from the OpenAI API.
+ """
+ client = _get_openai_client()
+ system_message = {"role": "system", "content": system_instructions}
+ human_message = {"role": "user", "content": human_message_content}
+ messages = []
+ messages.append(system_message)
+ messages.append(human_message)
+
+ response = client.chat.completions.create(model="gpt-4o", messages=messages, stream=stream)
+ content = response.choices[0].message.content
+ return content
+
+
+@action(
+ reads=[],
+ writes=[
+ "search_query",
+ "research_topic",
+ "sources_gathered",
+ "web_research_results",
+ "research_loop_count",
+ "running_summary",
+ ],
+)
+def generate_query(state: State, research_topic: str) -> State:
+ """
+ Generates a search query based on the research topic.
+
+ Args:
+ state (State): The current application state.
+ research_topic (str): The topic to research.
+
+ Returns:
+ State: The updated state with the research topic and generated search query.
+ """
+ system_prompt_formatted = prompts.query_writer_instructions.format(
+ research_topic=research_topic
+ )
+ human_prompt = "Generate a query for web search:"
+ my_query = query_openai(system_prompt_formatted, human_prompt)
+ as_dict = json.loads(my_query)
+ return state.update(
+ search_query=as_dict["query"],
+ research_topic=research_topic,
+ sources_gathered=[],
+ web_research_results=[],
+ research_loop_count=0,
+ running_summary=None,
+ )
+
+
+@action(
+ reads=[
+ "search_query",
+ "research_loop_count",
+ "sources_gathered",
+ "web_research_results",
+ ],
+ writes=["sources_gathered", "research_loop_count", "web_research_results"],
+)
+def web_research(state: State) -> State:
+ """
+ Performs web research based on the search query and updates the state.
+
+ Args:
+ state (State): The current application state.
+
+ Returns:
+ State: The updated state with gathered sources, research loop count, and web research results.
+ """
+ search_results = utils.tavily_search(
+ state["search_query"], include_raw_content=True, max_results=1
+ )
+ search_str = utils.deduplicate_and_format_sources(
+ search_results, max_tokens_per_source=1000, include_raw_content=True
+ )
+ sources_gathered = [utils.format_sources(search_results)]
+ sources_gathered = state["sources_gathered"] + sources_gathered
+ web_research_results = state["web_research_results"] + [search_str]
+ research_loop_count = state["research_loop_count"] + 1
+
+ return state.update(
+ sources_gathered=sources_gathered,
+ research_loop_count=research_loop_count,
+ web_research_results=web_research_results,
+ )
+
+
+@action(
+ reads=["running_summary", "web_research_results", "research_topic"],
+ writes=["running_summary"],
+)
+def summarize_sources(state: State):
+ """
+ Summarizes the gathered sources and updates the running summary.
+
+ Args:
+ state (State): The current application state.
+
+ Returns:
+ State: The updated state with the new running summary.
+ """
+ existing_summary = state["running_summary"]
+ most_recent_web_research = state["web_research_results"][-1]
+ research_topic = state["research_topic"]
+
+ if existing_summary:
+ human_message_content = (
+ f"<User Input> \n {research_topic} \n <User Input>\n\n"
+ f"<Existing Summary> \n {existing_summary} \n <Existing Summary>\n\n"
+ f"<New Search Results> \n {most_recent_web_research} \n <New Search Results>"
+ )
+ else:
+ human_message_content = (
+ f"<User Input> \n {research_topic} \n <User Input>\n\n"
+ f"<Search Results> \n {most_recent_web_research} \n <Search Results>"
+ )
+ running_summary = query_openai(prompts.summarizer_instructions, human_message_content)
+
+ while "<think>" in running_summary and "</think>" in running_summary:
+ start = running_summary.find("<think>")
+ end = running_summary.find("</think>") + len("</think>")
+ running_summary = running_summary[:start] + running_summary[end:]
+ return state.update(running_summary=running_summary)
+
+
+@action(reads=["running_summary", "research_topic"], writes=["search_query"])
+def reflect_on_summary(state: State):
+ """
+ Reflects on the running summary to identify knowledge gaps and generate a follow-up query.
+
+ Args:
+ state (State): The current application state.
+
+ Returns:
+ State: The updated state with the follow-up search query.
+ """
+ system_instructions = prompts.reflection_instructions.format(
+ research_topic=state.get("research_topic")
+ )
+ human_message_content = f"Identify a knowledge gap and generate a follow-up web search query based on our existing knowledge: {state.get('running_summary')}"
+ content = query_openai(system_instructions, human_message_content)
+ follow_up_query = json.loads(content)
+
+ query = follow_up_query.get("follow_up_query")
+ if not query:
+ fallback_query = f"Tell me more about {state.get('research_topic')}"
+ state.update(search_query=fallback_query)
+ return state.update(search_query=query or fallback_query)
+
+
+@action(
+ reads=["running_summary", "sources_gathered"],
+ writes=["running_summary", "research_loop_count"],
+)
+def finalize_summary(state: State):
+ """
+ Finalizes the summary by combining the running summary and all gathered sources.
+
+ Args:
+ state (State): The current application state.
+
+ Returns:
+ State: The updated state with the finalized summary.
+ """
+ all_sources = "\n".join(source for source in state.get("sources_gathered"))
+ running_summary = (
+ f"## Summary\n\n{state.get('running_summary')}\n\n ### Sources:\n{all_sources}"
+ )
+ # reset research loop count
+ return state.update(running_summary=running_summary, research_loop_count=0)
+
+
+graph = (
+ GraphBuilder()
+ .with_actions(
+ generate_query,
+ web_research,
+ summarize_sources,
+ reflect_on_summary,
+ finalize_summary,
+ )
+ .with_transitions(
+ ("generate_query", "web_research"),
+ ("web_research", "summarize_sources"),
+ ("summarize_sources", "reflect_on_summary"),
+ ("reflect_on_summary", "finalize_summary", when(research_loop_count=2)),
+ (
+ "reflect_on_summary",
+ "web_research",
+ expr("research_loop_count<2"),
+ ),
+ ("finalize_summary", "generate_query"),
+ )
+).build()
+
+
+def application(
+ app_id: Optional[str] = None,
+ project: str = "demo_deep_researcher",
+ username: str = None,
+) -> Application:
+ """
+ Creates and configures an application instance for conducting research.
+
+ Args:
+ app_id (Optional[str]): A unique identifier for the application instance. Defaults to None.
+ project (str): The project name for tracking. Defaults to "demo_deep_researcher".
+ username (Optional[str]): The username associated with the application instance.
+
+ Returns:
+ Application: A configured application instance ready to run.
+ """
+ tracker = LocalTrackingClient(project=project)
+ builder = (
+ ApplicationBuilder()
+ .with_graph(graph)
+ .with_tracker("local", project=project)
+ .with_identifiers(app_id=app_id, partition_key=username)
+ .initialize_from(
+ tracker,
+ resume_at_next_action=True,
+ default_state={"research_loop_count": 0, "running_summary": None},
+ default_entrypoint="generate_query",
+ )
+ )
+ return builder.build()
+
+
+if __name__ == "__main__":
+ """
+ Entry point for the application. Initializes and runs the research application.
+ """
+ research_topic = "getting a job in datascience"
+ app_id = "1"
+
+ app = application(app_id=app_id)
+ app.visualize(
+ output_file_path="statemachine",
+ include_conditions=True,
+ view=False,
+ format="png",
+ )
+ action, state, result = app.run(
+ halt_after=["finalize_summary"], inputs={"research_topic": research_topic}
+ )
+ print(app.state["running_summary"])
diff --git a/examples/deep-researcher/notebook.ipynb b/examples/deep-researcher/notebook.ipynb
new file mode 100644
index 00000000..2a3a53e6
--- /dev/null
+++ b/examples/deep-researcher/notebook.ipynb
@@ -0,0 +1,398 @@
+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "595fafdc-5e12-4ab7-8e21-18e49c453340",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Requirement already satisfied: burr[start] in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (0.39.1)\n",
+ "Requirement already satisfied: streamlit in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (1.44.1)\n",
+ "Requirement already satisfied: graphviz in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.20.3)\n",
+ "Requirement already satisfied: matplotlib in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (3.10.1)\n",
+ "Requirement already satisfied: sf-hamilton in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (1.88.0)\n",
+ "Requirement already satisfied: loguru in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.7.3)\n",
+ "Requirement already satisfied: click in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (8.1.8)\n",
+ "Requirement already satisfied: requests in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (2.32.3)\n",
+ "Requirement already satisfied: opentelemetry-api in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (1.32.1)\n",
+ "Requirement already satisfied: opentelemetry-sdk in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (1.32.1)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation-openai in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.39.2)\n",
+ "Requirement already satisfied: pydantic>1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (2.11.3)\n",
+ "Requirement already satisfied: fastapi in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.115.12)\n",
+ "Requirement already satisfied: uvicorn in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.34.1)\n",
+ "Requirement already satisfied: pydantic-settings in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (2.9.1)\n",
+ "Requirement already satisfied: fastapi-pagination in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.12.34)\n",
+ "Requirement already satisfied: fastapi-utils in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.8.0)\n",
+ "Requirement already satisfied: aiofiles in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (24.1.0)\n",
+ "Requirement already satisfied: jinja2 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (3.1.6)\n",
+ "Requirement already satisfied: openai in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (1.75.0)\n",
+ "Requirement already satisfied: typing-inspect in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from burr[start]) (0.9.0)\n",
+ "Requirement already satisfied: contourpy>=1.0.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (1.3.2)\n",
+ "Requirement already satisfied: cycler>=0.10 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (0.12.1)\n",
+ "Requirement already satisfied: fonttools>=4.22.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (4.57.0)\n",
+ "Requirement already satisfied: kiwisolver>=1.3.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (1.4.8)\n",
+ "Requirement already satisfied: numpy>=1.23 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (2.2.4)\n",
+ "Requirement already satisfied: packaging>=20.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (24.2)\n",
+ "Requirement already satisfied: pillow>=8 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (11.2.1)\n",
+ "Requirement already satisfied: pyparsing>=2.3.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (3.2.3)\n",
+ "Requirement already satisfied: python-dateutil>=2.7 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from matplotlib->burr[start]) (2.9.0.post0)\n",
+ "Requirement already satisfied: deprecated>=1.2.6 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-api->burr[start]) (1.2.18)\n",
+ "Requirement already satisfied: importlib-metadata<8.7.0,>=6.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-api->burr[start]) (8.6.1)\n",
+ "Requirement already satisfied: opentelemetry-instrumentation>=0.50b0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-instrumentation-openai->burr[start]) (0.53b1)\n",
+ "Requirement already satisfied: opentelemetry-semantic-conventions>=0.50b0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-instrumentation-openai->burr[start]) (0.53b1)\n",
+ "Requirement already satisfied: opentelemetry-semantic-conventions-ai==0.4.3 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-instrumentation-openai->burr[start]) (0.4.3)\n",
+ "Requirement already satisfied: tiktoken<1,>=0.6.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-instrumentation-openai->burr[start]) (0.9.0)\n",
+ "Requirement already satisfied: typing-extensions>=3.7.4 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from opentelemetry-sdk->burr[start]) (4.13.2)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->burr[start]) (3.4.1)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->burr[start]) (3.10)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->burr[start]) (2.4.0)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->burr[start]) (2025.1.31)\n",
+ "Requirement already satisfied: pandas in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from sf-hamilton->burr[start]) (2.2.3)\n",
+ "Requirement already satisfied: altair<6,>=4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (5.5.0)\n",
+ "Requirement already satisfied: blinker<2,>=1.0.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (1.9.0)\n",
+ "Requirement already satisfied: cachetools<6,>=4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (5.5.2)\n",
+ "Requirement already satisfied: protobuf<6,>=3.20 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (5.29.4)\n",
+ "Requirement already satisfied: pyarrow>=7.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (19.0.1)\n",
+ "Requirement already satisfied: tenacity<10,>=8.1.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (9.1.2)\n",
+ "Requirement already satisfied: toml<2,>=0.10.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (0.10.2)\n",
+ "Requirement already satisfied: watchdog<7,>=2.1.5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (6.0.0)\n",
+ "Requirement already satisfied: gitpython!=3.1.19,<4,>=3.0.7 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (3.1.44)\n",
+ "Requirement already satisfied: pydeck<1,>=0.8.0b4 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (0.9.1)\n",
+ "Requirement already satisfied: tornado<7,>=6.0.3 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from streamlit->burr[start]) (6.4.2)\n",
+ "Requirement already satisfied: jsonschema>=3.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from altair<6,>=4.0->streamlit->burr[start]) (4.23.0)\n",
+ "Requirement already satisfied: narwhals>=1.14.2 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from altair<6,>=4.0->streamlit->burr[start]) (1.35.0)\n",
+ "Requirement already satisfied: wrapt<2,>=1.10 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from deprecated>=1.2.6->opentelemetry-api->burr[start]) (1.17.2)\n",
+ "Requirement already satisfied: gitdb<5,>=4.0.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from gitpython!=3.1.19,<4,>=3.0.7->streamlit->burr[start]) (4.0.12)\n",
+ "Requirement already satisfied: zipp>=3.20 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from importlib-metadata<8.7.0,>=6.0->opentelemetry-api->burr[start]) (3.21.0)\n",
+ "Requirement already satisfied: pytz>=2020.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pandas->sf-hamilton->burr[start]) (2025.2)\n",
+ "Requirement already satisfied: tzdata>=2022.7 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pandas->sf-hamilton->burr[start]) (2025.2)\n",
+ "Requirement already satisfied: annotated-types>=0.6.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic>1->burr[start]) (0.7.0)\n",
+ "Requirement already satisfied: pydantic-core==2.33.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic>1->burr[start]) (2.33.1)\n",
+ "Requirement already satisfied: typing-inspection>=0.4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic>1->burr[start]) (0.4.0)\n",
+ "Requirement already satisfied: MarkupSafe>=2.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from jinja2->burr[start]) (3.0.2)\n",
+ "Requirement already satisfied: six>=1.5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from python-dateutil>=2.7->matplotlib->burr[start]) (1.17.0)\n",
+ "Requirement already satisfied: regex>=2022.1.18 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from tiktoken<1,>=0.6.0->opentelemetry-instrumentation-openai->burr[start]) (2024.11.6)\n",
+ "Requirement already satisfied: starlette<0.47.0,>=0.40.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from fastapi->burr[start]) (0.46.2)\n",
+ "Requirement already satisfied: psutil<6,>=5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from fastapi-utils->burr[start]) (5.9.8)\n",
+ "Requirement already satisfied: anyio<5,>=3.5.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (4.9.0)\n",
+ "Requirement already satisfied: distro<2,>=1.7.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (1.9.0)\n",
+ "Requirement already satisfied: httpx<1,>=0.23.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (0.28.1)\n",
+ "Requirement already satisfied: jiter<1,>=0.4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (0.9.0)\n",
+ "Requirement already satisfied: sniffio in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (1.3.1)\n",
+ "Requirement already satisfied: tqdm>4 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai->burr[start]) (4.67.1)\n",
+ "Requirement already satisfied: python-dotenv>=0.21.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic-settings->burr[start]) (1.1.0)\n",
+ "Requirement already satisfied: mypy-extensions>=0.3.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from typing-inspect->burr[start]) (1.0.0)\n",
+ "Requirement already satisfied: h11>=0.8 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from uvicorn->burr[start]) (0.14.0)\n",
+ "Requirement already satisfied: smmap<6,>=3.0.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.19,<4,>=3.0.7->streamlit->burr[start]) (5.0.2)\n",
+ "Requirement already satisfied: httpcore==1.* in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai->burr[start]) (1.0.8)\n",
+ "Requirement already satisfied: attrs>=22.2.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit->burr[start]) (25.3.0)\n",
+ "Requirement already satisfied: jsonschema-specifications>=2023.03.6 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit->burr[start]) (2024.10.1)\n",
+ "Requirement already satisfied: referencing>=0.28.4 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit->burr[start]) (0.36.2)\n",
+ "Requirement already satisfied: rpds-py>=0.7.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit->burr[start]) (0.24.0)\n",
+ "\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1\u001b[0m\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
+ "Requirement already satisfied: openai in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (1.75.0)\n",
+ "Requirement already satisfied: anyio<5,>=3.5.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (4.9.0)\n",
+ "Requirement already satisfied: distro<2,>=1.7.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (1.9.0)\n",
+ "Requirement already satisfied: httpx<1,>=0.23.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (0.28.1)\n",
+ "Requirement already satisfied: jiter<1,>=0.4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (0.9.0)\n",
+ "Requirement already satisfied: pydantic<3,>=1.9.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (2.11.3)\n",
+ "Requirement already satisfied: sniffio in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (1.3.1)\n",
+ "Requirement already satisfied: tqdm>4 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (4.67.1)\n",
+ "Requirement already satisfied: typing-extensions<5,>=4.11 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from openai) (4.13.2)\n",
+ "Requirement already satisfied: idna>=2.8 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
+ "Requirement already satisfied: certifi in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (2025.1.31)\n",
+ "Requirement already satisfied: httpcore==1.* in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpx<1,>=0.23.0->openai) (1.0.8)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.14.0)\n",
+ "Requirement already satisfied: annotated-types>=0.6.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
+ "Requirement already satisfied: pydantic-core==2.33.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (2.33.1)\n",
+ "Requirement already satisfied: typing-inspection>=0.4.0 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from pydantic<3,>=1.9.0->openai) (0.4.0)\n",
+ "\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1\u001b[0m\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
+ "Requirement already satisfied: tavily-python in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (0.5.4)\n",
+ "Requirement already satisfied: requests in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from tavily-python) (2.32.3)\n",
+ "Requirement already satisfied: tiktoken>=0.5.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from tavily-python) (0.9.0)\n",
+ "Requirement already satisfied: httpx in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from tavily-python) (0.28.1)\n",
+ "Requirement already satisfied: regex>=2022.1.18 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from tiktoken>=0.5.1->tavily-python) (2024.11.6)\n",
+ "Requirement already satisfied: charset-normalizer<4,>=2 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->tavily-python) (3.4.1)\n",
+ "Requirement already satisfied: idna<4,>=2.5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->tavily-python) (3.10)\n",
+ "Requirement already satisfied: urllib3<3,>=1.21.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->tavily-python) (2.4.0)\n",
+ "Requirement already satisfied: certifi>=2017.4.17 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from requests->tavily-python) (2025.1.31)\n",
+ "Requirement already satisfied: anyio in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpx->tavily-python) (4.9.0)\n",
+ "Requirement already satisfied: httpcore==1.* in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpx->tavily-python) (1.0.8)\n",
+ "Requirement already satisfied: h11<0.15,>=0.13 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from httpcore==1.*->httpx->tavily-python) (0.14.0)\n",
+ "Requirement already satisfied: sniffio>=1.1 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from anyio->httpx->tavily-python) (1.3.1)\n",
+ "Requirement already satisfied: typing_extensions>=4.5 in /home/maggie/.pyenv/versions/3.12.9/lib/python3.12/site-packages (from anyio->httpx->tavily-python) (4.13.2)\n",
+ "\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1\u001b[0m\n",
+ "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
+ ]
+ }
+ ],
+ "source": [
+ "!pip install burr[start]\n",
+ "!pip install openai\n",
+ "!pip install tavily-python"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "ed859b06-bcd5-4a6b-abcd-ab5863e17b37",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from application import application as researcher_app"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "84d778f6-79e6-4a97-9360-48745f918c78",
+ "metadata": {},
+ "source": [
+ "## Deep Researcher\n",
+ "\n",
+ "This is an example of a research assistant agent that uses llm chat and llm-friendly search together to provide a report, complete with internet citations, on a topic of the user's choosing. The graph for the agent includes optional reflection and re-writing that is implemented as a loop. This particular version of a research assistant uses the OpenAI API code for chat and the Tavily API for search. \n",
+ "\n",
+ "Given a research topic, the research assistant flow consists of several steps. On initialization, the assistant must:\n",
+ "\n",
+ "1. Use the chat API to generate a search query for web search based on the research topic.\n",
+ "\n",
+ "It then loops through the following three steps as many times as the user desires, controlled by the variable `num_loops`.\n",
+ "\n",
+ "2. Perform web research based on the search query using the search API.\n",
+ "3. Use the chat API to summarize the web research gathered in the previous step.\n",
+ "4. Reflect on the summary to generate a follow-up search query.\n",
+ "\n",
+ "When the number of loops is exhausted, the assistant must:\n",
+ "\n",
+ "5. Finalize summary with formatted sources and return it to the user.\n",
+ "\n",
+ "The graph for the application is visualized below.\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "c8d7c2c9-7f05-4447-b084-88bdd73e75f9",
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "image/svg+xml": [
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n",
+ "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\n",
+ " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
+ "<!-- Generated by graphviz version 2.43.0 (0)\n",
+ " -->\n",
+ "<!-- Title: %3 Pages: 1 -->\n",
+ "<svg width=\"399pt\" height=\"399pt\"\n",
+ " viewBox=\"0.00 0.00 399.22 399.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n",
+ "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 395)\">\n",
+ "<title>%3</title>\n",
+ "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-395 395.22,-395 395.22,4 -4,4\"/>\n",
+ "<!-- generate_query -->\n",
+ "<g id=\"node1\" class=\"node\">\n",
+ "<title>generate_query</title>\n",
+ "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M347,-323C347,-323 239,-323 239,-323 233,-323 227,-317 227,-311 227,-311 227,-298 227,-298 227,-292 233,-286 239,-286 239,-286 347,-286 347,-286 353,-286 359,-292 359,-298 359,-298 359,-311 359,-311 359,-317 353,-323 347,-323\"/>\n",
+ "<text text-anchor=\"middle\" x=\"293\" y=\"-300.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">generate_query</text>\n",
+ "</g>\n",
+ "<!-- web_research -->\n",
+ "<g id=\"node3\" class=\"node\">\n",
+ "<title>web_research</title>\n",
+ "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M242.5,-255C242.5,-255 149.5,-255 149.5,-255 143.5,-255 137.5,-249 137.5,-243 137.5,-243 137.5,-230 137.5,-230 137.5,-224 143.5,-218 149.5,-218 149.5,-218 242.5,-218 242.5,-218 248.5,-218 254.5,-224 254.5,-230 254.5,-230 254.5,-243 254.5,-243 254.5,-249 248.5,-255 242.5,-255\"/>\n",
+ "<text text-anchor=\"middle\" x=\"196\" y=\"-232.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">web_research</text>\n",
+ "</g>\n",
+ "<!-- generate_query&#45;&gt;web_research -->\n",
+ "<g id=\"edge2\" class=\"edge\">\n",
+ "<title>generate_query&#45;&gt;web_research</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M267.26,-285.99C255.97,-278.3 242.55,-269.17 230.42,-260.92\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"232.1,-257.83 221.86,-255.1 228.16,-263.61 232.1,-257.83\"/>\n",
+ "</g>\n",
+ "<!-- input__research_topic -->\n",
+ "<g id=\"node2\" class=\"node\">\n",
+ "<title>input__research_topic</title>\n",
+ "<polygon fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" points=\"376.5,-391 209.5,-391 209.5,-354 376.5,-354 376.5,-391\"/>\n",
+ "<text text-anchor=\"middle\" x=\"293\" y=\"-368.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">input: research_topic</text>\n",
+ "</g>\n",
+ "<!-- input__research_topic&#45;&gt;generate_query -->\n",
+ "<g id=\"edge1\" class=\"edge\">\n",
+ "<title>input__research_topic&#45;&gt;generate_query</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M293,-353.81C293,-347.6 293,-340.45 293,-333.6\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"296.5,-333.24 293,-323.24 289.5,-333.24 296.5,-333.24\"/>\n",
+ "</g>\n",
+ "<!-- summarize_sources -->\n",
+ "<g id=\"node4\" class=\"node\">\n",
+ "<title>summarize_sources</title>\n",
+ "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M148,-187C148,-187 12,-187 12,-187 6,-187 0,-181 0,-175 0,-175 0,-162 0,-162 0,-156 6,-150 12,-150 12,-150 148,-150 148,-150 154,-150 160,-156 160,-162 160,-162 160,-175 160,-175 160,-181 154,-187 148,-187\"/>\n",
+ "<text text-anchor=\"middle\" x=\"80\" y=\"-164.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">summarize_sources</text>\n",
+ "</g>\n",
+ "<!-- web_research&#45;&gt;summarize_sources -->\n",
+ "<g id=\"edge3\" class=\"edge\">\n",
+ "<title>web_research&#45;&gt;summarize_sources</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M165.22,-217.99C151.32,-210.08 134.73,-200.64 119.9,-192.2\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"121.35,-189 110.93,-187.1 117.89,-195.08 121.35,-189\"/>\n",
+ "</g>\n",
+ "<!-- reflect_on_summary -->\n",
+ "<g id=\"node5\" class=\"node\">\n",
+ "<title>reflect_on_summary</title>\n",
+ "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M265.5,-119C265.5,-119 126.5,-119 126.5,-119 120.5,-119 114.5,-113 114.5,-107 114.5,-107 114.5,-94 114.5,-94 114.5,-88 120.5,-82 126.5,-82 126.5,-82 265.5,-82 265.5,-82 271.5,-82 277.5,-88 277.5,-94 277.5,-94 277.5,-107 277.5,-107 277.5,-113 271.5,-119 265.5,-119\"/>\n",
+ "<text text-anchor=\"middle\" x=\"196\" y=\"-96.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">reflect_on_summary</text>\n",
+ "</g>\n",
+ "<!-- summarize_sources&#45;&gt;reflect_on_summary -->\n",
+ "<g id=\"edge4\" class=\"edge\">\n",
+ "<title>summarize_sources&#45;&gt;reflect_on_summary</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M110.78,-149.99C124.68,-142.08 141.27,-132.64 156.1,-124.2\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"158.11,-127.08 165.07,-119.1 154.65,-121 158.11,-127.08\"/>\n",
+ "</g>\n",
+ "<!-- reflect_on_summary&#45;&gt;web_research -->\n",
+ "<g id=\"edge6\" class=\"edge\">\n",
+ "<title>reflect_on_summary&#45;&gt;web_research</title>\n",
+ "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M196,-119.02C196,-141.6 196,-181.1 196,-207.77\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"192.5,-207.98 196,-217.98 199.5,-207.98 192.5,-207.98\"/>\n",
+ "<text text-anchor=\"middle\" x=\"279.5\" y=\"-164.8\" font-family=\"Times,serif\" font-size=\"14.00\">research_loop_count&lt;2</text>\n",
+ "</g>\n",
+ "<!-- finalize_summary -->\n",
+ "<g id=\"node6\" class=\"node\">\n",
+ "<title>finalize_summary</title>\n",
+ "<path fill=\"#b4d8e4\" stroke=\"black\" d=\"M354.5,-37C354.5,-37 233.5,-37 233.5,-37 227.5,-37 221.5,-31 221.5,-25 221.5,-25 221.5,-12 221.5,-12 221.5,-6 227.5,0 233.5,0 233.5,0 354.5,0 354.5,0 360.5,0 366.5,-6 366.5,-12 366.5,-12 366.5,-25 366.5,-25 366.5,-31 360.5,-37 354.5,-37\"/>\n",
+ "<text text-anchor=\"middle\" x=\"294\" y=\"-14.8\" font-family=\"Helvetica,sans-Serif\" font-size=\"14.00\">finalize_summary</text>\n",
+ "</g>\n",
+ "<!-- reflect_on_summary&#45;&gt;finalize_summary -->\n",
+ "<g id=\"edge5\" class=\"edge\">\n",
+ "<title>reflect_on_summary&#45;&gt;finalize_summary</title>\n",
+ "<path fill=\"none\" stroke=\"black\" stroke-dasharray=\"5,2\" d=\"M197.41,-81.82C199.05,-71.97 202.55,-60.1 210,-52 213.27,-48.45 216.97,-45.28 220.96,-42.46\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"222.86,-45.4 229.48,-37.13 219.15,-39.46 222.86,-45.4\"/>\n",
+ "<text text-anchor=\"middle\" x=\"293.5\" y=\"-55.8\" font-family=\"Times,serif\" font-size=\"14.00\">research_loop_count=2</text>\n",
+ "</g>\n",
+ "<!-- finalize_summary&#45;&gt;generate_query -->\n",
+ "<g id=\"edge7\" class=\"edge\">\n",
+ "<title>finalize_summary&#45;&gt;generate_query</title>\n",
+ "<path fill=\"none\" stroke=\"black\" d=\"M357.76,-37.22C364.91,-41.21 371.56,-46.07 377,-52 391.87,-68.22 391,-77.49 391,-99.5 391,-237.5 391,-237.5 391,-237.5 391,-256.89 377.64,-270.96 360.78,-280.99\"/>\n",
+ "<polygon fill=\"black\" stroke=\"black\" points=\"358.85,-278.06 351.7,-285.88 362.17,-284.22 358.85,-278.06\"/>\n",
+ "</g>\n",
+ "</g>\n",
+ "</svg>\n"
+ ],
+ "text/plain": [
+ "<graphviz.graphs.Digraph at 0x7eaec88f7a70>"
+ ]
+ },
+ "execution_count": 4,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "app = researcher_app(app_id=\"1\")\n",
+ "app.visualize(include_conditions=True)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "1601d582-1a0b-435e-8f92-ffc7a44d4ca4",
+ "metadata": {},
+ "source": [
+ "To run the application, we need tokens for the OpenAI api and the Tavily api."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "a899bf02-7f54-4cf2-ad80-bf37b62f07e1",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "import os\n",
+ "os.environ[\"OPENAI_API_KEY\"] = \"sk-*\"\n",
+ "os.environ[\"TAVILY_API_KEY\"] = \"tvly-*\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "4e18b2ad-4696-4c15-84b6-04c3ba515d30",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Warning: No raw_content found for source https://www.linkedin.com/pulse/how-crack-data-science-interview-2023-khadar-valli\n",
+ "Warning: No raw_content found for source https://dataspace.com/data-job-seekers/2023-top-data-science-skills/\n",
+ "## Summary\n",
+ "\n",
+ "To secure a job in data science, it's crucial to prepare effectively for interviews by thoroughly researching the company and understanding its culture, aligning your skills with its business objectives. Tailoring your preparation to the specific interview process of each company can significantly enhance your chances of success.\n",
+ "\n",
+ "In 2023, certain skills are particularly in demand. Python stands out as the most sought-after skill due to its versatile applications in automation, machine learning, and system building. Its extensive library support and easy-to-read syntax make it popular in the data science field. Acquiring proficiency in Python can be a strong asset for aspiring data scientists and can significantly increase job prospects in the field.\n",
+ "\n",
+ " ### Sources:\n",
+ "* HOW TO CRACK DATA SCIENCE INTERVIEW IN 2023 - LinkedIn : https://www.linkedin.com/pulse/how-crack-data-science-interview-2023-khadar-valli\n",
+ "* Top 10 Data Science Skills for 2023 - Dataspace : https://dataspace.com/data-job-seekers/2023-top-data-science-skills/\n"
+ ]
+ }
+ ],
+ "source": [
+ "my_research_topic = \"getting a job in datascience\"\n",
+ "action, state, result = app.run(halt_after=[\"finalize_summary\"], inputs={\"research_topic\": my_research_topic})\n",
+ "print(app.state[\"running_summary\"]) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "bf6eed5c-39f1-430c-9eb6-4a5809299fe9",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Warning: No raw_content found for source https://www.foodnetwork.com/how-to/packages/food-network-essentials/beginners-guide-to-canning\n"
+ ]
+ }
+ ],
+ "source": [
+ "my_research_topic = \"how to get started with canning food at home\"\n",
+ "action, state, result = app.run(halt_after=[\"finalize_summary\"], inputs={\"research_topic\": my_research_topic})\n",
+ "print(app.state[\"running_summary\"]) "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f716f5d0-f6d3-4bc3-9520-6eaa1a038951",
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.12.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/examples/deep-researcher/prompts.py b/examples/deep-researcher/prompts.py
new file mode 100644
index 00000000..a5e667a1
--- /dev/null
+++ b/examples/deep-researcher/prompts.py
@@ -0,0 +1,104 @@
+"""
+MIT License
+
+Copyright (c) 2024 Lance Martin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+"""
+
+query_writer_instructions = """Your goal is to generate a targeted web search query.
+The query will gather information related to a specific topic.
+
+<TOPIC>
+{research_topic}
+</TOPIC>
+
+<FORMAT>
+Format your response as a json object with ALL three of these exact keys:
+ - "query": The actual search query string
+ - "aspect": The specific aspect of the topic being researched
+ - "rationale": Brief explanation of why this query is relevant
+Do not use markdown tags to indicate a json code block in the output.
+</FORMAT>
+
+<EXAMPLE>
+Example output:
+{{
+ "query": "machine learning transformer architecture explained",
+ "aspect": "technical architecture",
+ "rationale": "Understanding the fundamental structure of transformer models"
+}}
+</EXAMPLE>
+
+Provide your response in json format:"""
+
+summarizer_instructions = """
+<GOAL>
+Generate a high-quality summary of the web search results and keep it concise / related to the user topic.
+</GOAL>
+
+<REQUIREMENTS>
+When creating a NEW summary:
+1. Highlight the most relevant information related to the user topic from the search results
+2. Ensure a coherent flow of information
+
+When EXTENDING an existing summary:
+1. Read the existing summary and new search results carefully.
+2. Compare the new information with the existing summary.
+3. For each piece of new information:
+ a. If it's related to existing points, integrate it into the relevant paragraph.
+ b. If it's entirely new but relevant, add a new paragraph with a smooth transition.
+ c. If it's not relevant to the user topic, skip it.
+4. Ensure all additions are relevant to the user's topic.
+5. Verify that your final output differs from the input summary.
+< /REQUIREMENTS >
+
+< FORMATTING >
+- Start directly with the updated summary, without preamble or titles. Do not use XML tags in the output.
+< /FORMATTING >"""
+
+reflection_instructions = """You are an expert research assistant analyzing a summary about {research_topic}.
+
+<GOAL>
+1. Identify knowledge gaps or areas that need deeper exploration
+2. Generate a follow-up question that would help expand your understanding
+3. Focus on technical details, implementation specifics, or emerging trends that weren't fully covered
+</GOAL>
+
+<REQUIREMENTS>
+Ensure the follow-up question is self-contained and includes necessary context for web search.
+</REQUIREMENTS>
+
+<FORMAT>
+Format your response as a json object with these exact keys:
+- knowledge_gap: Describe what information is missing or needs clarification
+- follow_up_query: Write a specific question to address this gap
+
+Do not use markdown tags to indicate a json code block in the output.
+</FORMAT>
+
+<EXAMPLE>
+Example output:
+{{
+ "knowledge_gap": "The summary lacks information about performance metrics and benchmarks",
+ "follow_up_query": "What are typical performance benchmarks and metrics used to evaluate [specific technology]?"
+}}
+</EXAMPLE>
+
+Provide your analysis in json format:"""
diff --git a/examples/deep-researcher/requirements.txt b/examples/deep-researcher/requirements.txt
new file mode 100644
index 00000000..42183b42
--- /dev/null
+++ b/examples/deep-researcher/requirements.txt
@@ -0,0 +1,3 @@
+burr[start]
+openai
+tavily-python
diff --git a/examples/deep-researcher/server.py b/examples/deep-researcher/server.py
new file mode 100644
index 00000000..f53cacc2
--- /dev/null
+++ b/examples/deep-researcher/server.py
@@ -0,0 +1,123 @@
+import functools
+import importlib
+
+# from fastapi import FastAPI
+import os
+from typing import Optional
+
+from fastapi import APIRouter
+
+from burr.core import Application, ApplicationBuilder
+from burr.tracking import LocalTrackingClient
+
+# uncomment for local testing
+# import application as deep_researcher_application
+
+# We're doing dynamic import cause this lives within examples/ (and that module has dashes)
+# navigate to the examples directory to read more about this!
+deep_researcher_application = importlib.import_module(
+ "burr.examples.deep-researcher.application"
+) # noqa: F401
+
+import pydantic
+
+# uncomment for local testing
+# app = FastAPI()
+router = APIRouter()
+
+try:
+ from opentelemetry.instrumentation.openai import OpenAIInstrumentor
+
+ OpenAIInstrumentor().instrument()
+ opentelemetry_available = True
+except ImportError:
+ opentelemetry_available = False
+
+# pydantic types
+
+
+class ResearchSummary(pydantic.BaseModel):
+ running_summary: str
+
+
[email protected]_cache(maxsize=128)
+def _get_application(project_id: str, app_id: str) -> Application:
+ """Utility to get the application. Depending on
+ your use-case you might want to reload from state every time (or consider cache invalidation)"""
+ graph = deep_researcher_application.graph
+ tracker = LocalTrackingClient(project=project_id)
+ builder = (
+ ApplicationBuilder()
+ .with_graph(graph)
+ .with_tracker(
+ tracker := LocalTrackingClient(project=project_id),
+ use_otel_tracing=opentelemetry_available,
+ )
+ .with_identifiers(app_id=app_id)
+ .initialize_from(
+ tracker,
+ resume_at_next_action=True,
+ default_state={"research_loop_count": 0, "running_summary": None},
+ default_entrypoint="generate_query",
+ )
+ )
+ return builder.build()
+
+
[email protected]("/response/{project_id}/{app_id}")
+def research_response(project_id: str, app_id: str, topic: str) -> ResearchSummary:
+ burr_app = _get_application(project_id, app_id)
+ research_topic = topic
+ action, state, result = burr_app.run(
+ halt_after=["finalize_summary"], inputs={"research_topic": research_topic}
+ )
+ summary = burr_app.state.get("running_summary")
+ # reset state machine?
+ return {"running_summary": summary}
+
+
[email protected]("/create/{project_id}/{app_id}")
+def create_new_application(project_id: str, app_id: str) -> str:
+ app = _get_application(project_id, app_id)
+ return app.uid
+
+
[email protected]("/validate")
+def validate_environment() -> Optional[str]:
+ """Validate the environment"""
+ has_openai_api_key = "OPENAI_API_KEY" in os.environ
+ has_tavily_api_key = "TAVILY_API_KEY" in os.environ
+ if has_openai_api_key and has_tavily_api_key:
+ return
+ message = ""
+ openai_api_message = """
+ You have not set an API key for [OpenAI](https://www.openai.com). Do this
+ by setting the environment variable `OPENAI_API_KEY` to your key.
+ You can get a key at https://platform.openai.com.
+ """
+ tavily_api_message = """
+ You have not set an API key for [Tavily Search](https://tavily.com). Do this
+ by setting the environment variable `TAVILY_API_KEY` to your key.
+ You can get a key at https://app.tavily.com/home.
+ """
+ if not has_openai_api_key:
+ message = message + openai_api_message
+ if not has_tavily_api_key:
+ message = message + tavily_api_message
+ return message
+
+
+# uncomment for local testing
+# @app.get("/")
+# def root():
+# return {"message": "Deep Researcher API"}
+
+
+# app.include_router(router, prefix="/deep_researcher", tags=["deep-researcher-api"])
+
+
+if __name__ == "__main__":
+ pass
+ # uncomment for local testing
+ # import uvicorn
+ # uvicorn.run(app, host="localhost", port=7242)
diff --git a/examples/deep-researcher/statemachine.png b/examples/deep-researcher/statemachine.png
new file mode 100644
index 00000000..5afcda77
Binary files /dev/null and b/examples/deep-researcher/statemachine.png differ
diff --git a/examples/deep-researcher/utils.py b/examples/deep-researcher/utils.py
new file mode 100644
index 00000000..a4608587
--- /dev/null
+++ b/examples/deep-researcher/utils.py
@@ -0,0 +1,119 @@
+"""
+MIT License
+
+Copyright (c) 2024 Lance Martin
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+"""
+
+from tavily import TavilyClient
+
+
+def deduplicate_and_format_sources(
+ search_response, max_tokens_per_source, include_raw_content=False
+):
+ """
+ Takes either a single search response or list of responses from search APIs and formats them.
+ Limits the raw_content to approximately max_tokens_per_source.
+ include_raw_content specifies whether to include the raw_content from Tavily in the formatted string.
+
+ Args:
+ search_response: Either:
+ - A dict with a 'results' key containing a list of search results
+ - A list of dicts, each containing search results
+
+ Returns:
+ str: Formatted string with deduplicated sources
+ """
+ # Convert input to list of results
+ if isinstance(search_response, dict):
+ sources_list = search_response["results"]
+ elif isinstance(search_response, list):
+ sources_list = []
+ for response in search_response:
+ if isinstance(response, dict) and "results" in response:
+ sources_list.extend(response["results"])
+ else:
+ sources_list.extend(response)
+ else:
+ raise ValueError("Input must be either a dict with 'results' or a list of search results")
+
+ # Deduplicate by URL
+ unique_sources = {}
+ for source in sources_list:
+ if source["url"] not in unique_sources:
+ unique_sources[source["url"]] = source
+
+ # Format output
+ formatted_text = "Sources:\n\n"
+ for i, source in enumerate(unique_sources.values(), 1):
+ formatted_text += f"Source {source['title']}:\n===\n"
+ formatted_text += f"URL: {source['url']}\n===\n"
+ formatted_text += f"Most relevant content from source: {source['content']}\n===\n"
+ if include_raw_content:
+ # Using rough estimate of 4 characters per token
+ char_limit = max_tokens_per_source * 4
+ # Handle None raw_content
+ raw_content = source.get("raw_content", "")
+ if raw_content is None:
+ raw_content = ""
+ print(f"Warning: No raw_content found for source {source['url']}")
+ if len(raw_content) > char_limit:
+ raw_content = raw_content[:char_limit] + "... [truncated]"
+ formatted_text += (
+ f"Full source content limited to {max_tokens_per_source} tokens: {raw_content}\n\n"
+ )
+
+ return formatted_text.strip()
+
+
+def format_sources(search_results):
+ """Format search results into a bullet-point list of sources.
+
+ Args:
+ search_results (dict): Tavily search response containing results
+
+ Returns:
+ str: Formatted string with sources and their URLs
+ """
+ return "\n".join(
+ f"* {source['title']} : {source['url']}" for source in search_results["results"]
+ )
+
+
+def tavily_search(query, include_raw_content=True, max_results=3):
+ """Search the web using the Tavily API.
+
+ Args:
+ query (str): The search query to execute
+ include_raw_content (bool): Whether to include the raw_content from Tavily in the formatted string
+ max_results (int): Maximum number of results to return
+
+ Returns:
+ dict: Search response containing:
+ - results (list): List of search result dictionaries, each containing:
+ - title (str): Title of the search result
+ - url (str): URL of the search result
+ - content (str): Snippet/summary of the content
+ - raw_content (str): Full content of the page if available"""
+
+ tavily_client = TavilyClient()
+ return tavily_client.search(
+ query, max_results=max_results, include_raw_content=include_raw_content
+ )
diff --git a/telemetry/ui/.eslintrc.js b/telemetry/ui/.eslintrc.js
index 8255b272..01becbf9 100644
--- a/telemetry/ui/.eslintrc.js
+++ b/telemetry/ui/.eslintrc.js
@@ -22,8 +22,8 @@ module.exports = {
'react/react-in-jsx-scope': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'react/prop-types': 'off', //Appears to be busted: see https://stackoverflow.com/questions/38684925/react-eslint-error-missing-in-props-validation
- 'eqeqeq': 'error',
- 'no-console': 'warn',
+ eqeqeq: 'error',
+ 'no-console': 'warn'
},
settings: {
react: {
diff --git a/telemetry/ui/src/App.tsx b/telemetry/ui/src/App.tsx
index dd384760..be8644a2 100644
--- a/telemetry/ui/src/App.tsx
+++ b/telemetry/ui/src/App.tsx
@@ -11,6 +11,7 @@ import { EmailAssistantWithTelemetry } from './examples/EmailAssistant';
import { StreamingChatbotWithTelemetry } from './examples/StreamingChatbot';
import { AdminView } from './components/routes/AdminView';
import { AnnotationsViewContainer } from './components/routes/app/AnnotationsView';
+import { DeepResearcherWithTelemetry } from './examples/DeepResearcher';
/**
* Basic application. We have an AppContainer -- this has a breadcrumb and a sidebar.
@@ -43,6 +44,7 @@ const App = () => {
<Route path="/demos/chatbot" element={<ChatbotWithTelemetry />} />
<Route path="/demos/streaming-chatbot" element={<StreamingChatbotWithTelemetry />} />
<Route path="/demos/email-assistant" element={<EmailAssistantWithTelemetry />} />
+ <Route path="/demos/deep-researcher" element={<DeepResearcherWithTelemetry />} />
<Route path="/admin" element={<AdminView />} />
<Route path="*" element={<Navigate to="/projects" />} />
</Routes>
diff --git a/telemetry/ui/src/api/core/ApiError.ts b/telemetry/ui/src/api/core/ApiError.ts
index d6b8fcc3..81f32a17 100644
--- a/telemetry/ui/src/api/core/ApiError.ts
+++ b/telemetry/ui/src/api/core/ApiError.ts
@@ -6,20 +6,20 @@ import type { ApiRequestOptions } from './ApiRequestOptions';
import type { ApiResult } from './ApiResult';
export class ApiError extends Error {
- public readonly url: string;
- public readonly status: number;
- public readonly statusText: string;
- public readonly body: any;
- public readonly request: ApiRequestOptions;
+ public readonly url: string;
+ public readonly status: number;
+ public readonly statusText: string;
+ public readonly body: any;
+ public readonly request: ApiRequestOptions;
- constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
- super(message);
+ constructor(request: ApiRequestOptions, response: ApiResult, message: string) {
+ super(message);
- this.name = 'ApiError';
- this.url = response.url;
- this.status = response.status;
- this.statusText = response.statusText;
- this.body = response.body;
- this.request = request;
- }
+ this.name = 'ApiError';
+ this.url = response.url;
+ this.status = response.status;
+ this.statusText = response.statusText;
+ this.body = response.body;
+ this.request = request;
+ }
}
diff --git a/telemetry/ui/src/api/core/ApiRequestOptions.ts b/telemetry/ui/src/api/core/ApiRequestOptions.ts
index c19adcc9..4d59ceda 100644
--- a/telemetry/ui/src/api/core/ApiRequestOptions.ts
+++ b/telemetry/ui/src/api/core/ApiRequestOptions.ts
@@ -3,15 +3,15 @@
/* tslint:disable */
/* eslint-disable */
export type ApiRequestOptions = {
- readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
- readonly url: string;
- readonly path?: Record<string, any>;
- readonly cookies?: Record<string, any>;
- readonly headers?: Record<string, any>;
- readonly query?: Record<string, any>;
- readonly formData?: Record<string, any>;
- readonly body?: any;
- readonly mediaType?: string;
- readonly responseHeader?: string;
- readonly errors?: Record<number, string>;
+ readonly method: 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH';
+ readonly url: string;
+ readonly path?: Record<string, any>;
+ readonly cookies?: Record<string, any>;
+ readonly headers?: Record<string, any>;
+ readonly query?: Record<string, any>;
+ readonly formData?: Record<string, any>;
+ readonly body?: any;
+ readonly mediaType?: string;
+ readonly responseHeader?: string;
+ readonly errors?: Record<number, string>;
};
diff --git a/telemetry/ui/src/api/core/ApiResult.ts b/telemetry/ui/src/api/core/ApiResult.ts
index ad8fef2b..63ed6c44 100644
--- a/telemetry/ui/src/api/core/ApiResult.ts
+++ b/telemetry/ui/src/api/core/ApiResult.ts
@@ -3,9 +3,9 @@
/* tslint:disable */
/* eslint-disable */
export type ApiResult = {
- readonly url: string;
- readonly ok: boolean;
- readonly status: number;
- readonly statusText: string;
- readonly body: any;
+ readonly url: string;
+ readonly ok: boolean;
+ readonly status: number;
+ readonly statusText: string;
+ readonly body: any;
};
diff --git a/telemetry/ui/src/api/core/CancelablePromise.ts b/telemetry/ui/src/api/core/CancelablePromise.ts
index eb02246c..d96101be 100644
--- a/telemetry/ui/src/api/core/CancelablePromise.ts
+++ b/telemetry/ui/src/api/core/CancelablePromise.ts
@@ -3,129 +3,128 @@
/* tslint:disable */
/* eslint-disable */
export class CancelError extends Error {
-
- constructor(message: string) {
- super(message);
- this.name = 'CancelError';
- }
-
- public get isCancelled(): boolean {
- return true;
- }
+ constructor(message: string) {
+ super(message);
+ this.name = 'CancelError';
+ }
+
+ public get isCancelled(): boolean {
+ return true;
+ }
}
export interface OnCancel {
- readonly isResolved: boolean;
- readonly isRejected: boolean;
- readonly isCancelled: boolean;
+ readonly isResolved: boolean;
+ readonly isRejected: boolean;
+ readonly isCancelled: boolean;
- (cancelHandler: () => void): void;
+ (cancelHandler: () => void): void;
}
export class CancelablePromise<T> implements Promise<T> {
- #isResolved: boolean;
- #isRejected: boolean;
- #isCancelled: boolean;
- readonly #cancelHandlers: (() => void)[];
- readonly #promise: Promise<T>;
- #resolve?: (value: T | PromiseLike<T>) => void;
- #reject?: (reason?: any) => void;
-
- constructor(
- executor: (
- resolve: (value: T | PromiseLike<T>) => void,
- reject: (reason?: any) => void,
- onCancel: OnCancel
- ) => void
- ) {
- this.#isResolved = false;
- this.#isRejected = false;
- this.#isCancelled = false;
- this.#cancelHandlers = [];
- this.#promise = new Promise<T>((resolve, reject) => {
- this.#resolve = resolve;
- this.#reject = reject;
-
- const onResolve = (value: T | PromiseLike<T>): void => {
- if (this.#isResolved || this.#isRejected || this.#isCancelled) {
- return;
- }
- this.#isResolved = true;
- if (this.#resolve) this.#resolve(value);
- };
-
- const onReject = (reason?: any): void => {
- if (this.#isResolved || this.#isRejected || this.#isCancelled) {
- return;
- }
- this.#isRejected = true;
- if (this.#reject) this.#reject(reason);
- };
-
- const onCancel = (cancelHandler: () => void): void => {
- if (this.#isResolved || this.#isRejected || this.#isCancelled) {
- return;
- }
- this.#cancelHandlers.push(cancelHandler);
- };
-
- Object.defineProperty(onCancel, 'isResolved', {
- get: (): boolean => this.#isResolved,
- });
-
- Object.defineProperty(onCancel, 'isRejected', {
- get: (): boolean => this.#isRejected,
- });
-
- Object.defineProperty(onCancel, 'isCancelled', {
- get: (): boolean => this.#isCancelled,
- });
-
- return executor(onResolve, onReject, onCancel as OnCancel);
- });
- }
-
- get [Symbol.toStringTag]() {
- return "Cancellable Promise";
- }
-
- public then<TResult1 = T, TResult2 = never>(
- onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
- onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
- ): Promise<TResult1 | TResult2> {
- return this.#promise.then(onFulfilled, onRejected);
- }
-
- public catch<TResult = never>(
- onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
- ): Promise<T | TResult> {
- return this.#promise.catch(onRejected);
- }
+ #isResolved: boolean;
+ #isRejected: boolean;
+ #isCancelled: boolean;
+ readonly #cancelHandlers: (() => void)[];
+ readonly #promise: Promise<T>;
+ #resolve?: (value: T | PromiseLike<T>) => void;
+ #reject?: (reason?: any) => void;
+
+ constructor(
+ executor: (
+ resolve: (value: T | PromiseLike<T>) => void,
+ reject: (reason?: any) => void,
+ onCancel: OnCancel
+ ) => void
+ ) {
+ this.#isResolved = false;
+ this.#isRejected = false;
+ this.#isCancelled = false;
+ this.#cancelHandlers = [];
+ this.#promise = new Promise<T>((resolve, reject) => {
+ this.#resolve = resolve;
+ this.#reject = reject;
+
+ const onResolve = (value: T | PromiseLike<T>): void => {
+ if (this.#isResolved || this.#isRejected || this.#isCancelled) {
+ return;
+ }
+ this.#isResolved = true;
+ if (this.#resolve) this.#resolve(value);
+ };
- public finally(onFinally?: (() => void) | null): Promise<T> {
- return this.#promise.finally(onFinally);
- }
+ const onReject = (reason?: any): void => {
+ if (this.#isResolved || this.#isRejected || this.#isCancelled) {
+ return;
+ }
+ this.#isRejected = true;
+ if (this.#reject) this.#reject(reason);
+ };
- public cancel(): void {
+ const onCancel = (cancelHandler: () => void): void => {
if (this.#isResolved || this.#isRejected || this.#isCancelled) {
- return;
+ return;
}
- this.#isCancelled = true;
- if (this.#cancelHandlers.length) {
- try {
- for (const cancelHandler of this.#cancelHandlers) {
- cancelHandler();
- }
- } catch (error) {
- console.warn('Cancellation threw an error', error);
- return;
- }
+ this.#cancelHandlers.push(cancelHandler);
+ };
+
+ Object.defineProperty(onCancel, 'isResolved', {
+ get: (): boolean => this.#isResolved
+ });
+
+ Object.defineProperty(onCancel, 'isRejected', {
+ get: (): boolean => this.#isRejected
+ });
+
+ Object.defineProperty(onCancel, 'isCancelled', {
+ get: (): boolean => this.#isCancelled
+ });
+
+ return executor(onResolve, onReject, onCancel as OnCancel);
+ });
+ }
+
+ get [Symbol.toStringTag]() {
+ return 'Cancellable Promise';
+ }
+
+ public then<TResult1 = T, TResult2 = never>(
+ onFulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | null,
+ onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
+ ): Promise<TResult1 | TResult2> {
+ return this.#promise.then(onFulfilled, onRejected);
+ }
+
+ public catch<TResult = never>(
+ onRejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
+ ): Promise<T | TResult> {
+ return this.#promise.catch(onRejected);
+ }
+
+ public finally(onFinally?: (() => void) | null): Promise<T> {
+ return this.#promise.finally(onFinally);
+ }
+
+ public cancel(): void {
+ if (this.#isResolved || this.#isRejected || this.#isCancelled) {
+ return;
+ }
+ this.#isCancelled = true;
+ if (this.#cancelHandlers.length) {
+ try {
+ for (const cancelHandler of this.#cancelHandlers) {
+ cancelHandler();
}
- this.#cancelHandlers.length = 0;
- if (this.#reject) this.#reject(new CancelError('Request aborted'));
+ } catch (error) {
+ console.warn('Cancellation threw an error', error);
+ return;
+ }
}
+ this.#cancelHandlers.length = 0;
+ if (this.#reject) this.#reject(new CancelError('Request aborted'));
+ }
- public get isCancelled(): boolean {
- return this.#isCancelled;
- }
+ public get isCancelled(): boolean {
+ return this.#isCancelled;
+ }
}
diff --git a/telemetry/ui/src/api/core/OpenAPI.ts b/telemetry/ui/src/api/core/OpenAPI.ts
index e357bb22..21267971 100644
--- a/telemetry/ui/src/api/core/OpenAPI.ts
+++ b/telemetry/ui/src/api/core/OpenAPI.ts
@@ -8,25 +8,25 @@ type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
type Headers = Record<string, string>;
export type OpenAPIConfig = {
- BASE: string;
- VERSION: string;
- WITH_CREDENTIALS: boolean;
- CREDENTIALS: 'include' | 'omit' | 'same-origin';
- TOKEN?: string | Resolver<string> | undefined;
- USERNAME?: string | Resolver<string> | undefined;
- PASSWORD?: string | Resolver<string> | undefined;
- HEADERS?: Headers | Resolver<Headers> | undefined;
- ENCODE_PATH?: ((path: string) => string) | undefined;
+ BASE: string;
+ VERSION: string;
+ WITH_CREDENTIALS: boolean;
+ CREDENTIALS: 'include' | 'omit' | 'same-origin';
+ TOKEN?: string | Resolver<string> | undefined;
+ USERNAME?: string | Resolver<string> | undefined;
+ PASSWORD?: string | Resolver<string> | undefined;
+ HEADERS?: Headers | Resolver<Headers> | undefined;
+ ENCODE_PATH?: ((path: string) => string) | undefined;
};
export const OpenAPI: OpenAPIConfig = {
- BASE: '',
- VERSION: '0.1.0',
- WITH_CREDENTIALS: false,
- CREDENTIALS: 'include',
- TOKEN: undefined,
- USERNAME: undefined,
- PASSWORD: undefined,
- HEADERS: undefined,
- ENCODE_PATH: undefined,
+ BASE: '',
+ VERSION: '0.1.0',
+ WITH_CREDENTIALS: false,
+ CREDENTIALS: 'include',
+ TOKEN: undefined,
+ USERNAME: undefined,
+ PASSWORD: undefined,
+ HEADERS: undefined,
+ ENCODE_PATH: undefined
};
diff --git a/telemetry/ui/src/api/core/request.ts b/telemetry/ui/src/api/core/request.ts
index 2201a0e6..3a5d724e 100644
--- a/telemetry/ui/src/api/core/request.ts
+++ b/telemetry/ui/src/api/core/request.ts
@@ -9,278 +9,294 @@ import { CancelablePromise } from './CancelablePromise';
import type { OnCancel } from './CancelablePromise';
import type { OpenAPIConfig } from './OpenAPI';
-export const isDefined = <T>(value: T | null | undefined): value is Exclude<T, null | undefined> => {
- return value !== undefined && value !== null;
+export const isDefined = <T>(
+ value: T | null | undefined
+): value is Exclude<T, null | undefined> => {
+ return value !== undefined && value !== null;
};
export const isString = (value: any): value is string => {
- return typeof value === 'string';
+ return typeof value === 'string';
};
export const isStringWithValue = (value: any): value is string => {
- return isString(value) && value !== '';
+ return isString(value) && value !== '';
};
export const isBlob = (value: any): value is Blob => {
- return (
- typeof value === 'object' &&
- typeof value.type === 'string' &&
- typeof value.stream === 'function' &&
- typeof value.arrayBuffer === 'function' &&
- typeof value.constructor === 'function' &&
- typeof value.constructor.name === 'string' &&
- /^(Blob|File)$/.test(value.constructor.name) &&
- /^(Blob|File)$/.test(value[Symbol.toStringTag])
- );
+ return (
+ typeof value === 'object' &&
+ typeof value.type === 'string' &&
+ typeof value.stream === 'function' &&
+ typeof value.arrayBuffer === 'function' &&
+ typeof value.constructor === 'function' &&
+ typeof value.constructor.name === 'string' &&
+ /^(Blob|File)$/.test(value.constructor.name) &&
+ /^(Blob|File)$/.test(value[Symbol.toStringTag])
+ );
};
export const isFormData = (value: any): value is FormData => {
- return value instanceof FormData;
+ return value instanceof FormData;
};
export const base64 = (str: string): string => {
- try {
- return btoa(str);
- } catch (err) {
- // @ts-ignore
- return Buffer.from(str).toString('base64');
- }
+ try {
+ return btoa(str);
+ } catch (err) {
+ // @ts-ignore
+ return Buffer.from(str).toString('base64');
+ }
};
export const getQueryString = (params: Record<string, any>): string => {
- const qs: string[] = [];
+ const qs: string[] = [];
- const append = (key: string, value: any) => {
- qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
- };
+ const append = (key: string, value: any) => {
+ qs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
+ };
- const process = (key: string, value: any) => {
- if (isDefined(value)) {
- if (Array.isArray(value)) {
- value.forEach(v => {
- process(key, v);
- });
- } else if (typeof value === 'object') {
- Object.entries(value).forEach(([k, v]) => {
- process(`${key}[${k}]`, v);
- });
- } else {
- append(key, value);
- }
- }
- };
+ const process = (key: string, value: any) => {
+ if (isDefined(value)) {
+ if (Array.isArray(value)) {
+ value.forEach((v) => {
+ process(key, v);
+ });
+ } else if (typeof value === 'object') {
+ Object.entries(value).forEach(([k, v]) => {
+ process(`${key}[${k}]`, v);
+ });
+ } else {
+ append(key, value);
+ }
+ }
+ };
- Object.entries(params).forEach(([key, value]) => {
- process(key, value);
- });
+ Object.entries(params).forEach(([key, value]) => {
+ process(key, value);
+ });
- if (qs.length > 0) {
- return `?${qs.join('&')}`;
- }
+ if (qs.length > 0) {
+ return `?${qs.join('&')}`;
+ }
- return '';
+ return '';
};
const getUrl = (config: OpenAPIConfig, options: ApiRequestOptions): string => {
- const encoder = config.ENCODE_PATH || encodeURI;
-
- const path = options.url
- .replace('{api-version}', config.VERSION)
- .replace(/{(.*?)}/g, (substring: string, group: string) => {
- if (options.path?.hasOwnProperty(group)) {
- return encoder(String(options.path[group]));
- }
- return substring;
- });
+ const encoder = config.ENCODE_PATH || encodeURI;
+
+ const path = options.url
+ .replace('{api-version}', config.VERSION)
+ .replace(/{(.*?)}/g, (substring: string, group: string) => {
+ if (options.path?.hasOwnProperty(group)) {
+ return encoder(String(options.path[group]));
+ }
+ return substring;
+ });
- const url = `${config.BASE}${path}`;
- if (options.query) {
- return `${url}${getQueryString(options.query)}`;
- }
- return url;
+ const url = `${config.BASE}${path}`;
+ if (options.query) {
+ return `${url}${getQueryString(options.query)}`;
+ }
+ return url;
};
export const getFormData = (options: ApiRequestOptions): FormData | undefined => {
- if (options.formData) {
- const formData = new FormData();
-
- const process = (key: string, value: any) => {
- if (isString(value) || isBlob(value)) {
- formData.append(key, value);
- } else {
- formData.append(key, JSON.stringify(value));
- }
- };
+ if (options.formData) {
+ const formData = new FormData();
- Object.entries(options.formData)
- .filter(([_, value]) => isDefined(value))
- .forEach(([key, value]) => {
- if (Array.isArray(value)) {
- value.forEach(v => process(key, v));
- } else {
- process(key, value);
- }
- });
-
- return formData;
- }
- return undefined;
+ const process = (key: string, value: any) => {
+ if (isString(value) || isBlob(value)) {
+ formData.append(key, value);
+ } else {
+ formData.append(key, JSON.stringify(value));
+ }
+ };
+
+ Object.entries(options.formData)
+ .filter(([_, value]) => isDefined(value))
+ .forEach(([key, value]) => {
+ if (Array.isArray(value)) {
+ value.forEach((v) => process(key, v));
+ } else {
+ process(key, value);
+ }
+ });
+
+ return formData;
+ }
+ return undefined;
};
type Resolver<T> = (options: ApiRequestOptions) => Promise<T>;
-export const resolve = async <T>(options: ApiRequestOptions, resolver?: T | Resolver<T>): Promise<T | undefined> => {
- if (typeof resolver === 'function') {
- return (resolver as Resolver<T>)(options);
- }
- return resolver;
+export const resolve = async <T>(
+ options: ApiRequestOptions,
+ resolver?: T | Resolver<T>
+): Promise<T | undefined> => {
+ if (typeof resolver === 'function') {
+ return (resolver as Resolver<T>)(options);
+ }
+ return resolver;
};
-export const getHeaders = async (config: OpenAPIConfig, options: ApiRequestOptions): Promise<Headers> => {
- const [token, username, password, additionalHeaders] = await Promise.all([
- resolve(options, config.TOKEN),
- resolve(options, config.USERNAME),
- resolve(options, config.PASSWORD),
- resolve(options, config.HEADERS),
- ]);
-
- const headers = Object.entries({
- Accept: 'application/json',
- ...additionalHeaders,
- ...options.headers,
- })
- .filter(([_, value]) => isDefined(value))
- .reduce((headers, [key, value]) => ({
- ...headers,
- [key]: String(value),
- }), {} as Record<string, string>);
-
- if (isStringWithValue(token)) {
- headers['Authorization'] = `Bearer ${token}`;
- }
-
- if (isStringWithValue(username) && isStringWithValue(password)) {
- const credentials = base64(`${username}:${password}`);
- headers['Authorization'] = `Basic ${credentials}`;
- }
+export const getHeaders = async (
+ config: OpenAPIConfig,
+ options: ApiRequestOptions
+): Promise<Headers> => {
+ const [token, username, password, additionalHeaders] = await Promise.all([
+ resolve(options, config.TOKEN),
+ resolve(options, config.USERNAME),
+ resolve(options, config.PASSWORD),
+ resolve(options, config.HEADERS)
+ ]);
+
+ const headers = Object.entries({
+ Accept: 'application/json',
+ ...additionalHeaders,
+ ...options.headers
+ })
+ .filter(([_, value]) => isDefined(value))
+ .reduce(
+ (headers, [key, value]) => ({
+ ...headers,
+ [key]: String(value)
+ }),
+ {} as Record<string, string>
+ );
- if (options.body) {
- if (options.mediaType) {
- headers['Content-Type'] = options.mediaType;
- } else if (isBlob(options.body)) {
- headers['Content-Type'] = options.body.type || 'application/octet-stream';
- } else if (isString(options.body)) {
- headers['Content-Type'] = 'text/plain';
- } else if (!isFormData(options.body)) {
- headers['Content-Type'] = 'application/json';
- }
+ if (isStringWithValue(token)) {
+ headers['Authorization'] = `Bearer ${token}`;
+ }
+
+ if (isStringWithValue(username) && isStringWithValue(password)) {
+ const credentials = base64(`${username}:${password}`);
+ headers['Authorization'] = `Basic ${credentials}`;
+ }
+
+ if (options.body) {
+ if (options.mediaType) {
+ headers['Content-Type'] = options.mediaType;
+ } else if (isBlob(options.body)) {
+ headers['Content-Type'] = options.body.type || 'application/octet-stream';
+ } else if (isString(options.body)) {
+ headers['Content-Type'] = 'text/plain';
+ } else if (!isFormData(options.body)) {
+ headers['Content-Type'] = 'application/json';
}
+ }
- return new Headers(headers);
+ return new Headers(headers);
};
export const getRequestBody = (options: ApiRequestOptions): any => {
- if (options.body !== undefined) {
- if (options.mediaType?.includes('/json')) {
- return JSON.stringify(options.body)
- } else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
- return options.body;
- } else {
- return JSON.stringify(options.body);
- }
+ if (options.body !== undefined) {
+ if (options.mediaType?.includes('/json')) {
+ return JSON.stringify(options.body);
+ } else if (isString(options.body) || isBlob(options.body) || isFormData(options.body)) {
+ return options.body;
+ } else {
+ return JSON.stringify(options.body);
}
- return undefined;
+ }
+ return undefined;
};
export const sendRequest = async (
- config: OpenAPIConfig,
- options: ApiRequestOptions,
- url: string,
- body: any,
- formData: FormData | undefined,
- headers: Headers,
- onCancel: OnCancel
+ config: OpenAPIConfig,
+ options: ApiRequestOptions,
+ url: string,
+ body: any,
+ formData: FormData | undefined,
+ headers: Headers,
+ onCancel: OnCancel
): Promise<Response> => {
- const controller = new AbortController();
+ const controller = new AbortController();
- const request: RequestInit = {
- headers,
- body: body ?? formData,
- method: options.method,
- signal: controller.signal,
- };
+ const request: RequestInit = {
+ headers,
+ body: body ?? formData,
+ method: options.method,
+ signal: controller.signal
+ };
- if (config.WITH_CREDENTIALS) {
- request.credentials = config.CREDENTIALS;
- }
+ if (config.WITH_CREDENTIALS) {
+ request.credentials = config.CREDENTIALS;
+ }
- onCancel(() => controller.abort());
+ onCancel(() => controller.abort());
- return await fetch(url, request);
+ return await fetch(url, request);
};
-export const getResponseHeader = (response: Response, responseHeader?: string): string | undefined => {
- if (responseHeader) {
- const content = response.headers.get(responseHeader);
- if (isString(content)) {
- return content;
- }
+export const getResponseHeader = (
+ response: Response,
+ responseHeader?: string
+): string | undefined => {
+ if (responseHeader) {
+ const content = response.headers.get(responseHeader);
+ if (isString(content)) {
+ return content;
}
- return undefined;
+ }
+ return undefined;
};
export const getResponseBody = async (response: Response): Promise<any> => {
- if (response.status !== 204) {
- try {
- const contentType = response.headers.get('Content-Type');
- if (contentType) {
- const jsonTypes = ['application/json', 'application/problem+json']
- const isJSON = jsonTypes.some(type => contentType.toLowerCase().startsWith(type));
- if (isJSON) {
- return await response.json();
- } else {
- return await response.text();
- }
- }
- } catch (error) {
- console.error(error);
+ if (response.status !== 204) {
+ try {
+ const contentType = response.headers.get('Content-Type');
+ if (contentType) {
+ const jsonTypes = ['application/json', 'application/problem+json'];
+ const isJSON = jsonTypes.some((type) => contentType.toLowerCase().startsWith(type));
+ if (isJSON) {
+ return await response.json();
+ } else {
+ return await response.text();
}
+ }
+ } catch (error) {
+ console.error(error);
}
- return undefined;
+ }
+ return undefined;
};
export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult): void => {
- const errors: Record<number, string> = {
- 400: 'Bad Request',
- 401: 'Unauthorized',
- 403: 'Forbidden',
- 404: 'Not Found',
- 500: 'Internal Server Error',
- 502: 'Bad Gateway',
- 503: 'Service Unavailable',
- ...options.errors,
- }
-
- const error = errors[result.status];
- if (error) {
- throw new ApiError(options, result, error);
- }
-
- if (!result.ok) {
- const errorStatus = result.status ?? 'unknown';
- const errorStatusText = result.statusText ?? 'unknown';
- const errorBody = (() => {
- try {
- return JSON.stringify(result.body, null, 2);
- } catch (e) {
- return undefined;
- }
- })();
-
- throw new ApiError(options, result,
- `Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`
- );
- }
+ const errors: Record<number, string> = {
+ 400: 'Bad Request',
+ 401: 'Unauthorized',
+ 403: 'Forbidden',
+ 404: 'Not Found',
+ 500: 'Internal Server Error',
+ 502: 'Bad Gateway',
+ 503: 'Service Unavailable',
+ ...options.errors
+ };
+
+ const error = errors[result.status];
+ if (error) {
+ throw new ApiError(options, result, error);
+ }
+
+ if (!result.ok) {
+ const errorStatus = result.status ?? 'unknown';
+ const errorStatusText = result.statusText ?? 'unknown';
+ const errorBody = (() => {
+ try {
+ return JSON.stringify(result.body, null, 2);
+ } catch (e) {
+ return undefined;
+ }
+ })();
+
+ throw new ApiError(
+ options,
+ result,
+ `Generic Error: status: ${errorStatus}; status text: ${errorStatusText}; body: ${errorBody}`
+ );
+ }
};
/**
@@ -290,33 +306,36 @@ export const catchErrorCodes = (options: ApiRequestOptions, result: ApiResult):
* @returns CancelablePromise<T>
* @throws ApiError
*/
-export const request = <T>(config: OpenAPIConfig, options: ApiRequestOptions): CancelablePromise<T> => {
- return new CancelablePromise(async (resolve, reject, onCancel) => {
- try {
- const url = getUrl(config, options);
- const formData = getFormData(options);
- const body = getRequestBody(options);
- const headers = await getHeaders(config, options);
-
- if (!onCancel.isCancelled) {
- const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
- const responseBody = await getResponseBody(response);
- const responseHeader = getResponseHeader(response, options.responseHeader);
-
- const result: ApiResult = {
- url,
- ok: response.ok,
- status: response.status,
- statusText: response.statusText,
- body: responseHeader ?? responseBody,
- };
-
- catchErrorCodes(options, result);
-
- resolve(result.body);
- }
- } catch (error) {
- reject(error);
- }
- });
+export const request = <T>(
+ config: OpenAPIConfig,
+ options: ApiRequestOptions
+): CancelablePromise<T> => {
+ return new CancelablePromise(async (resolve, reject, onCancel) => {
+ try {
+ const url = getUrl(config, options);
+ const formData = getFormData(options);
+ const body = getRequestBody(options);
+ const headers = await getHeaders(config, options);
+
+ if (!onCancel.isCancelled) {
+ const response = await sendRequest(config, options, url, body, formData, headers, onCancel);
+ const responseBody = await getResponseBody(response);
+ const responseHeader = getResponseHeader(response, options.responseHeader);
+
+ const result: ApiResult = {
+ url,
+ ok: response.ok,
+ status: response.status,
+ statusText: response.statusText,
+ body: responseHeader ?? responseBody
+ };
+
+ catchErrorCodes(options, result);
+
+ resolve(result.body);
+ }
+ } catch (error) {
+ reject(error);
+ }
+ });
};
diff --git a/telemetry/ui/src/api/index.ts b/telemetry/ui/src/api/index.ts
index 36d9f0da..653e31a4 100644
--- a/telemetry/ui/src/api/index.ts
+++ b/telemetry/ui/src/api/index.ts
@@ -37,6 +37,7 @@ export type { PointerModel } from './models/PointerModel';
export type { Project } from './models/Project';
export type { PromptInput } from './models/PromptInput';
export type { QuestionAnswers } from './models/QuestionAnswers';
+export type { ResearchSummary } from './models/ResearchSummary';
export type { Span } from './models/Span';
export type { Step } from './models/Step';
export type { TransitionModel } from './models/TransitionModel';
diff --git a/telemetry/ui/src/api/models/ApplicationModel.ts b/telemetry/ui/src/api/models/ApplicationModel.ts
index dab27339..64b043cf 100644
--- a/telemetry/ui/src/api/models/ApplicationModel.ts
+++ b/telemetry/ui/src/api/models/ApplicationModel.ts
@@ -8,9 +8,8 @@ import type { TransitionModel } from './TransitionModel';
* Pydantic model that represents an application for storing/visualization in the UI
*/
export type ApplicationModel = {
- type?: string;
- entrypoint: string;
- actions: Array<ActionModel>;
- transitions: Array<TransitionModel>;
+ type?: string;
+ entrypoint: string;
+ actions: Array<ActionModel>;
+ transitions: Array<TransitionModel>;
};
-
diff --git a/telemetry/ui/src/api/models/BeginEntryModel.ts b/telemetry/ui/src/api/models/BeginEntryModel.ts
index 35cb6f56..6dd46122 100644
--- a/telemetry/ui/src/api/models/BeginEntryModel.ts
+++ b/telemetry/ui/src/api/models/BeginEntryModel.ts
@@ -6,10 +6,9 @@
* Pydantic model that represents an entry for the beginning of a step
*/
export type BeginEntryModel = {
- type?: string;
- start_time: string;
- action: string;
- inputs: Record<string, any>;
- sequence_id: number;
+ type?: string;
+ start_time: string;
+ action: string;
+ inputs: Record<string, any>;
+ sequence_id: number;
};
-
diff --git a/telemetry/ui/src/api/models/BeginSpanModel.ts b/telemetry/ui/src/api/models/BeginSpanModel.ts
index feb063c5..0e63ec54 100644
--- a/telemetry/ui/src/api/models/BeginSpanModel.ts
+++ b/telemetry/ui/src/api/models/BeginSpanModel.ts
@@ -6,12 +6,11 @@
* Pydantic model that represents an entry for the beginning of a span
*/
export type BeginSpanModel = {
- type?: string;
- start_time: string;
- action_sequence_id: number;
- span_id: string;
- span_name: string;
- parent_span_id: (string | null);
- span_dependencies: Array<string>;
+ type?: string;
+ start_time: string;
+ action_sequence_id: number;
+ span_id: string;
+ span_name: string;
+ parent_span_id: string | null;
+ span_dependencies: Array<string>;
};
-
diff --git a/telemetry/ui/src/api/models/EndEntryModel.ts b/telemetry/ui/src/api/models/EndEntryModel.ts
index fc16b4e8..64f22cbd 100644
--- a/telemetry/ui/src/api/models/EndEntryModel.ts
+++ b/telemetry/ui/src/api/models/EndEntryModel.ts
@@ -6,12 +6,11 @@
* Pydantic model that represents an entry for the end of a step
*/
export type EndEntryModel = {
- type?: string;
- end_time: string;
- action: string;
- result: (Record<string, any> | null);
- exception: (string | null);
- state: Record<string, any>;
- sequence_id: number;
+ type?: string;
+ end_time: string;
+ action: string;
+ result: Record<string, any> | null;
+ exception: string | null;
+ state: Record<string, any>;
+ sequence_id: number;
};
-
diff --git a/telemetry/ui/src/api/models/EndSpanModel.ts b/telemetry/ui/src/api/models/EndSpanModel.ts
index 6aa9093b..0571fbb2 100644
--- a/telemetry/ui/src/api/models/EndSpanModel.ts
+++ b/telemetry/ui/src/api/models/EndSpanModel.ts
@@ -6,9 +6,8 @@
* Pydantic model that represents an entry for the end of a span
*/
export type EndSpanModel = {
- type?: string;
- end_time: string;
- action_sequence_id: number;
- span_id: string;
+ type?: string;
+ end_time: string;
+ action_sequence_id: number;
+ span_id: string;
};
-
diff --git a/telemetry/ui/src/api/models/HTTPValidationError.ts b/telemetry/ui/src/api/models/HTTPValidationError.ts
index 892e4257..7c4d0637 100644
--- a/telemetry/ui/src/api/models/HTTPValidationError.ts
+++ b/telemetry/ui/src/api/models/HTTPValidationError.ts
@@ -4,6 +4,5 @@
/* eslint-disable */
import type { ValidationError } from './ValidationError';
export type HTTPValidationError = {
- detail?: Array<ValidationError>;
+ detail?: Array<ValidationError>;
};
-
diff --git a/telemetry/ui/src/api/models/ResearchSummary.ts b/telemetry/ui/src/api/models/ResearchSummary.ts
new file mode 100644
index 00000000..420113d4
--- /dev/null
+++ b/telemetry/ui/src/api/models/ResearchSummary.ts
@@ -0,0 +1,7 @@
+/* generated using openapi-typescript-codegen -- do no edit */
+/* istanbul ignore file */
+/* tslint:disable */
+/* eslint-disable */
+export type ResearchSummary = {
+ running_summary: string;
+};
diff --git a/telemetry/ui/src/api/models/Span.ts b/telemetry/ui/src/api/models/Span.ts
index 0e91276f..ad43fe24 100644
--- a/telemetry/ui/src/api/models/Span.ts
+++ b/telemetry/ui/src/api/models/Span.ts
@@ -9,7 +9,6 @@ import type { EndSpanModel } from './EndSpanModel';
* them to put them in order.
*/
export type Span = {
- begin_entry: BeginSpanModel;
- end_entry: (EndSpanModel | null);
+ begin_entry: BeginSpanModel;
+ end_entry: EndSpanModel | null;
};
-
diff --git a/telemetry/ui/src/api/models/TransitionModel.ts b/telemetry/ui/src/api/models/TransitionModel.ts
index fb66ce4a..57f5915c 100644
--- a/telemetry/ui/src/api/models/TransitionModel.ts
+++ b/telemetry/ui/src/api/models/TransitionModel.ts
@@ -6,9 +6,8 @@
* Pydantic model that represents a transition for storing/visualization in the UI
*/
export type TransitionModel = {
- type?: string;
- from_: string;
- to: string;
- condition: string;
+ type?: string;
+ from_: string;
+ to: string;
+ condition: string;
};
-
diff --git a/telemetry/ui/src/api/models/ValidationError.ts b/telemetry/ui/src/api/models/ValidationError.ts
index f2ff49a2..aed8da76 100644
--- a/telemetry/ui/src/api/models/ValidationError.ts
+++ b/telemetry/ui/src/api/models/ValidationError.ts
@@ -3,8 +3,7 @@
/* tslint:disable */
/* eslint-disable */
export type ValidationError = {
- loc: Array<(string | number)>;
- msg: string;
- type: string;
+ loc: Array<string | number>;
+ msg: string;
+ type: string;
};
-
diff --git a/telemetry/ui/src/api/services/DefaultService.ts b/telemetry/ui/src/api/services/DefaultService.ts
index a4467072..52439fe7 100644
--- a/telemetry/ui/src/api/services/DefaultService.ts
+++ b/telemetry/ui/src/api/services/DefaultService.ts
@@ -16,6 +16,7 @@ import type { IndexingJob } from '../models/IndexingJob';
import type { Project } from '../models/Project';
import type { PromptInput } from '../models/PromptInput';
import type { QuestionAnswers } from '../models/QuestionAnswers';
+import type { ResearchSummary } from '../models/ResearchSummary';
import type { CancelablePromise } from '../core/CancelablePromise';
import { OpenAPI } from '../core/OpenAPI';
import { request as __request } from '../core/request';
@@ -613,23 +614,68 @@ export class DefaultService {
});
}
/**
- * React App
- * Quick trick to server the react app
- * Thanks to https://github.com/hop-along-polly/fastapi-webapp-react for the example/demo
- * @param restOfPath
- * @returns any Successful Response
+ * Research Response
+ * @param projectId
+ * @param appId
+ * @param topic
+ * @returns ResearchSummary Successful Response
* @throws ApiError
*/
- public static reactAppRestOfPathGet(restOfPath: string): CancelablePromise<any> {
+ public static researchResponseApiV0DeepResearcherResponseProjectIdAppIdPost(
+ projectId: string,
+ appId: string,
+ topic: string
+ ): CancelablePromise<ResearchSummary> {
return __request(OpenAPI, {
- method: 'GET',
- url: '/{rest_of_path}',
+ method: 'POST',
+ url: '/api/v0/deep_researcher/response/{project_id}/{app_id}',
+ path: {
+ project_id: projectId,
+ app_id: appId
+ },
+ query: {
+ topic: topic
+ },
+ errors: {
+ 422: `Validation Error`
+ }
+ });
+ }
+ /**
+ * Create New Application
+ * @param projectId
+ * @param appId
+ * @returns string Successful Response
+ * @throws ApiError
+ */
+ public static createNewApplicationApiV0DeepResearcherCreateProjectIdAppIdPost(
+ projectId: string,
+ appId: string
+ ): CancelablePromise<string> {
+ return __request(OpenAPI, {
+ method: 'POST',
+ url: '/api/v0/deep_researcher/create/{project_id}/{app_id}',
path: {
- rest_of_path: restOfPath
+ project_id: projectId,
+ app_id: appId
},
errors: {
422: `Validation Error`
}
});
}
+ /**
+ * Validate Environment
+ * Validate the environment
+ * @returns any Successful Response
+ * @throws ApiError
+ */
+ public static validateEnvironmentApiV0DeepResearcherValidateGet(): CancelablePromise<
+ string | null
+ > {
+ return __request(OpenAPI, {
+ method: 'GET',
+ url: '/api/v0/deep_researcher/validate'
+ });
+ }
}
diff --git a/telemetry/ui/src/components/common/switch.tsx b/telemetry/ui/src/components/common/switch.tsx
index 4d90db2a..3d85c8bd 100644
--- a/telemetry/ui/src/components/common/switch.tsx
+++ b/telemetry/ui/src/components/common/switch.tsx
@@ -195,7 +195,8 @@ export function Switch({
// Color specific styles
colors[color]
)}
- {...props}>
+ {...props}
+ >
<span
aria-hidden="true"
className={clsx(
diff --git a/telemetry/ui/src/components/nav/appcontainer.tsx b/telemetry/ui/src/components/nav/appcontainer.tsx
index b5568890..3690b8b7 100644
--- a/telemetry/ui/src/components/nav/appcontainer.tsx
+++ b/telemetry/ui/src/components/nav/appcontainer.tsx
@@ -108,6 +108,12 @@ export const AppContainer = (props: { children: React.ReactNode }) => {
href: '/demos/streaming-chatbot',
current: false,
linkType: 'internal'
+ },
+ {
+ name: 'deep-researcher',
+ href: '/demos/deep-researcher',
+ current: false,
+ linkType: 'internal'
}
]
}
diff --git a/telemetry/ui/src/examples/DeepResearcher.tsx b/telemetry/ui/src/examples/DeepResearcher.tsx
new file mode 100644
index 00000000..105eeda3
--- /dev/null
+++ b/telemetry/ui/src/examples/DeepResearcher.tsx
@@ -0,0 +1,122 @@
+import { KeyboardEvent, useState } from 'react';
+import { ApplicationSummary, DefaultService, ResearchSummary } from '../api';
+import { TwoColumnLayout } from '../components/common/layout';
+import { TelemetryWithSelector } from './Common';
+import { Button } from '../components/common/button';
+import { useMutation, useQuery } from 'react-query';
+import { Text } from '../components/common/text';
+import { Field, Label } from '../components/common/fieldset';
+
+const DEFAULT_RESEARCH_SUMMARY: ResearchSummary = {
+ running_summary: ''
+};
+
+export const ResearchSummaryView = (props: { summary: string }) => {
+ return (
+ <>
+ <Text>
+ <pre className="whitespace-pre-wrap text-xs">{props.summary}</pre>
+ </Text>
+ </>
+ );
+};
+
+export const DeepResearcher = (props: { projectId: string; appId: string | undefined }) => {
+ const [currentTopic, setCurrentTopic] = useState<string>('');
+ const [displayedReport, setDisplayedReport] = useState(DEFAULT_RESEARCH_SUMMARY);
+ const { data: validationData, isLoading: isValidationLoading } = useQuery(
+ ['valid'],
+ DefaultService.validateEnvironmentApiV0DeepResearcherValidateGet
+ );
+
+ const mutation = useMutation(
+ (topic: string) => {
+ return DefaultService.researchResponseApiV0DeepResearcherResponseProjectIdAppIdPost(
+ props.projectId,
+ props.appId || '',
+ topic
+ );
+ },
+ {
+ onSuccess: (data) => {
+ setDisplayedReport(data);
+ }
+ }
+ );
+ const sendTopic = () => {
+ if (currentTopic !== '') {
+ mutation.mutate(currentTopic);
+ setCurrentTopic('');
+ setDisplayedReport(DEFAULT_RESEARCH_SUMMARY);
+ }
+ };
+ const isResearcherWaiting = mutation.isLoading || isValidationLoading;
+ const displayValidationError = validationData !== null;
+
+ return (
+ <div className="px-4 bg-white w-full flex flex-col h-full gap-5 overflow-y-scroll">
+ <h1 className="text-2xl font-bold text-gray-600">{'Learn Burr '}</h1>
+ <h2 className="text-lg font-normal text-gray-500 flex flex-row">
+ The research assistant below is implemented using Burr. Watch the Burr UI (on the right)
+ change as you chat...
+ </h2>
+ <div className="flex flex-col">
+ {displayValidationError && (
+ <p className="text-lg font-normal text-dwred">{validationData}</p>
+ )}
+ {!displayValidationError && (
+ <Field>
+ <Label className="text-lg font-bold text-gray-600">Report</Label>
+ </Field>
+ )}
+ <ResearchSummaryView summary={displayedReport.running_summary} />
+ </div>
+ <div className="flex items-center pt-4 gap-2">
+ <input
+ className="flex h-10 w-full rounded-md border border-[#e5e7eb] px-3 py-2 text-sm placeholder-[#6b7280] focus:outline-none focus:ring-2 focus:ring-[#9ca3af] disabled:cursor-not-allowed disabled:opacity-50 text-[#030712] focus-visible:ring-offset-2"
+ placeholder="how to get a job in data science"
+ value={currentTopic}
+ onChange={(e) => setCurrentTopic(e.target.value)}
+ onKeyDown={(e: KeyboardEvent<HTMLInputElement>) => {
+ if (e.key === 'Enter' && !e.shiftKey) {
+ e.preventDefault();
+ sendTopic();
+ }
+ }}
+ disabled={isResearcherWaiting || props.appId === undefined || displayValidationError}
+ />
+ <Button
+ className="w-min cursor-pointer h-full"
+ color="dark"
+ disabled={isResearcherWaiting || props.appId === undefined || displayValidationError}
+ onClick={() => {
+ sendTopic();
+ }}
+ >
+ Send
+ </Button>
+ </div>
+ </div>
+ );
+};
+
+export const DeepResearcherWithTelemetry = () => {
+ const currentProject = 'demo_deep_researcher';
+ const [currentApp, setCurrentApp] = useState<ApplicationSummary | undefined>(undefined);
+ return (
+ <TwoColumnLayout
+ firstItem={<DeepResearcher projectId={currentProject} appId={currentApp?.app_id} />}
+ secondItem={
+ <TelemetryWithSelector
+ projectId={currentProject}
+ currentApp={currentApp}
+ setCurrentApp={setCurrentApp}
+ createNewApp={
+ DefaultService.createNewApplicationApiV0DeepResearcherCreateProjectIdAppIdPost
+ }
+ />
+ }
+ mode={'third'}
+ ></TwoColumnLayout>
+ );
+};