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.

first round of updates

Hamza Farhan7b409c8a2c41
diff --git a/.gitignore b/.gitignore
index c9b7cd48..0d2c1c5c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -171,3 +171,4 @@ burr/tracking/server/build
# statemachine images
examples/*/statemachine
examples/*/*/statemachine
+.vscode
diff --git a/examples/adaptive_crag/README.md b/examples/adaptive_crag/README.md
index 250cf384..381fbd55 100644
--- a/examples/adaptive_crag/README.md
+++ b/examples/adaptive_crag/README.md
@@ -19,4 +19,4 @@ We stop after `ask_assistant` to get the next `query` for `router`.
## Adaptive-CRAG 🧪
![statemachine](statemachine.png)
-We have some of the core concepts from the [Burr Docs](https://burr.dagworks.io/concepts/) in a folder and we will add that to a `LanceDB` table for Hybrid Search. You can add as many tables as you want. Each table will be a different route for the `router`.
\ No newline at end of file
+We have some of the core concepts from the [Burr Docs](https://burr.dagworks.io/concepts/) in a folder and we will add that to a `LanceDB` table for Hybrid Search. You can add as many tables as you want. Each table will be a different route for the `router`.
diff --git a/examples/adaptive_crag/application.py b/examples/adaptive_crag/application.py
index 18cbc1d6..20646fcf 100644
--- a/examples/adaptive_crag/application.py
+++ b/examples/adaptive_crag/application.py
@@ -4,17 +4,19 @@ from typing import Iterable, Literal
import google.generativeai as genai
import instructor
import lancedb
-from burr.core import Application, ApplicationBuilder, State, expr, when
-from burr.core.action import action
-from burr.tracking import LocalTrackingClient
from dotenv import load_dotenv
from exa_py.api import Exa
+from instructor.exceptions import InstructorRetryException
from lancedb.db import DBConnection
from lancedb.embeddings import get_registry
from lancedb.pydantic import LanceModel, Vector
from lancedb.table import Table
from pydantic import BaseModel, Field
+from burr.core import Application, ApplicationBuilder, State, expr, when
+from burr.core.action import action
+from burr.tracking import LocalTrackingClient
+
load_dotenv() # load your GOOGLE_API_KEY and EXA_API_KEY from a .env file
@@ -55,7 +57,6 @@ def merge_short_docs(
Merge short documents into longer ones for better performance.
`file_name` is the name of the file the documents came from. It's just for metadata.
"""
- print(f"Merging {len(docs)} docs")
merged_docs = [{"text": docs[0], "file_name": file_name}]
for doc in docs[1:]:
last_text = merged_docs[-1]["text"]
@@ -63,7 +64,6 @@ def merge_short_docs(
merged_docs[-1]["text"] = last_text.strip() + "\n" + doc
else:
merged_docs.append({"text": doc, "file_name": file_name})
- print(f"Merged to {len(merged_docs)} docs\n")
return merged_docs
@@ -85,10 +85,7 @@ def load_burr_docs(
docs = []
for file in files_iter:
docs += merge_short_docs(
- docs=(burr_docs_dir / Path(file).name)
- .with_suffix(".txt")
- .read_text()
- .split("\n\n"),
+ docs=(burr_docs_dir / Path(file).name).with_suffix(".txt").read_text().split("\n\n"),
file_name=Path(file).stem,
doc_thresh=doc_thresh,
)
@@ -243,9 +240,7 @@ def get_user_query() -> str:
@action(reads=["db", "chat_history"], writes=["query", "route"])
-def router(
- state: State, query: str, attempts: int = ATTEMPTS
-) -> tuple[dict[str, str], State]:
+def router(state: State, query: str, attempts: int = ATTEMPTS) -> tuple[dict[str, str], State]:
"""
Route the user `query` to the appropriate place for an answer.
If the `query` is directly related to a table in the database, return the table name.
@@ -268,9 +263,7 @@ def router(
route = ask_gemini.create(
messages=[
user_message(
- route_template(
- table_names=table_names, query=query, chat_history=chat_history
- )
+ route_template(table_names=table_names, query=query, chat_history=chat_history)
) # type: ignore
],
response_model=routes, # type: ignore
@@ -303,9 +296,7 @@ def rewrite_query_for_lancedb(
return {"lancedb_query": query}, state.update(lancedb_query=query)
-@action(
- reads=["db", "route", "lancedb_query", "docs_limit"], writes=["lancedb_results"]
-)
+@action(reads=["db", "route", "lancedb_query", "docs_limit"], writes=["lancedb_results"])
def search_lancedb(state: State) -> tuple[dict[str, list[str]], State]:
db: DBConnection = state["db"]
try:
@@ -319,9 +310,7 @@ def search_lancedb(state: State) -> tuple[dict[str, list[str]], State]:
except Exception as e:
print(f"Error in search_lancedb: {e}")
lancedb_results = []
- return {"lancedb_results": lancedb_results}, state.update(
- lancedb_results=lancedb_results
- )
+ return {"lancedb_results": lancedb_results}, state.update(lancedb_results=lancedb_results)
@action(reads=["query", "lancedb_results"], writes=["lancedb_results"])
@@ -341,9 +330,7 @@ def remove_irrelevant_lancedb_results(
is_relevant = ask_gemini.create(
messages=[
user_message(
- evaluator_template(
- query=state["query"], document=lancedb_result
- )
+ evaluator_template(query=state["query"], document=lancedb_result)
) # type: ignore
],
response_model=bool, # type: ignore
@@ -354,9 +341,7 @@ def remove_irrelevant_lancedb_results(
is_relevant = True
if is_relevant:
filtered_results.append(lancedb_result)
- return {"lancedb_results": filtered_results}, state.update(
- lancedb_results=filtered_results
- )
+ return {"lancedb_results": filtered_results}, state.update(lancedb_results=filtered_results)
@action(reads=["query"], writes=["exa_search_keywords"])
@@ -407,9 +392,7 @@ def search_exa(state: State) -> tuple[dict[str, list[str]], State]:
reads=["query", "lancedb_results", "exa_search_results", "chat_history"],
writes=["chat_history", "lancedb_results", "exa_search_results"],
)
-def ask_assistant(
- state: State, attempts: int = ATTEMPTS
-) -> tuple[dict[str, str], State]:
+def ask_assistant(state: State, attempts: int = ATTEMPTS) -> tuple[dict[str, str], State]:
"""
Combine the `chat_history`, `query`, `lancedb_results`, and `exa_search_results` to ask the assistant for an answer.
`chat_history`, `lancedb_results`, and `exa_search_results` are used as context for the assistant and can be empty lists.
@@ -421,26 +404,21 @@ def ask_assistant(
context = lancedb_results + exa_search_results
if context:
context = "\n".join(context)
- query = (
- query.strip()
- + "\n<additional_context>\n"
- + context
- + "\n</additional_context>"
- )
+ query = query.strip() + "\n<additional_context>\n" + context + "\n</additional_context>"
messages.append({"role": "user", "content": query.strip()})
try:
- response = ask_gemini.create(
- messages=messages, response_model=str, max_retries=attempts
- )
- except Exception as e:
+ response = ask_gemini.create(messages=messages, response_model=str, max_retries=attempts)
+ except InstructorRetryException as e:
print(f"Error in ask_assistant: {e}")
response = "Sorry, please try again."
+ # lancedb_results and exa_search_results have very specific additional context that's relevant only for the latest query
+ # that specific context is no longer needed after the assistant responds, so we clear it
+ # the assistant will still have the chat_history as overall context
+ # chat_history has the user messages and the assistant responses, so it's more than enough
return (
{"assistant_response": response}, # type: ignore
state.append(chat_history=user_message(query))
- .append(
- chat_history=assistant_message(response) # type: ignore
- )
+ .append(chat_history=assistant_message(response)) # type: ignore
.update(lancedb_results=[], exa_search_results=[]),
)
@@ -509,11 +487,7 @@ def application(
if __name__ == "__main__":
- lance_model = (
- get_registry()
- .get("sentence-transformers")
- .create(name=EMS_MODEL, device=DEVICE)
- )
+ lance_model = get_registry().get("sentence-transformers").create(name=EMS_MODEL, device=DEVICE)
# define the schema of your data using Pydantic
class LanceDoc(LanceModel):
@@ -524,9 +498,7 @@ if __name__ == "__main__":
lance_db = lancedb.connect(LANCE_URI)
burr_docs = load_burr_docs(burr_docs_dir=BURR_DOCS_DIR)
- burr_table = add_table(
- db=lance_db, table_name="burr_docs", data=burr_docs, schema=LanceDoc
- )
+ burr_table = add_table(db=lance_db, table_name="burr_docs", data=burr_docs, schema=LanceDoc)
app = application(db=lance_db)
app.visualize(
diff --git a/examples/adaptive_crag/burr_docs/actions.txt b/examples/adaptive_crag/burr_docs/actions.txt
index 468e65a1..a5d0322d 100644
--- a/examples/adaptive_crag/burr_docs/actions.txt
+++ b/examples/adaptive_crag/burr_docs/actions.txt
@@ -163,4 +163,4 @@ app.run( # or app.step, app.iterate, app.astep, etc.\n"
halt_..., # your halt logic\n"
inputs={"prompt": "this will be passed into `prompt`"} # <-- we pass in values here
)
-For instance, say you have a chatbot. The first step will likely declare the input parameter prompt – it will take that, process it, and put the result in state. The subsequent steps will read the result of that from state.
\ No newline at end of file
+For instance, say you have a chatbot. The first step will likely declare the input parameter prompt – it will take that, process it, and put the result in state. The subsequent steps will read the result of that from state.
diff --git a/examples/adaptive_crag/burr_docs/applications.txt b/examples/adaptive_crag/burr_docs/applications.txt
index 60afe9c4..bad76408 100644
--- a/examples/adaptive_crag/burr_docs/applications.txt
+++ b/examples/adaptive_crag/burr_docs/applications.txt
@@ -95,4 +95,4 @@ You can ask various questions of the state machine using publicly-supported APIs
application.graph will give you a static representation of the state machine with enough information to visualize
-application.state will give you the current state of the state machine. Note that if you modify it the results will not show up – state is immutable! Modify the state through actions.
\ No newline at end of file
+application.state will give you the current state of the state machine. Note that if you modify it the results will not show up – state is immutable! Modify the state through actions.
diff --git a/examples/adaptive_crag/burr_docs/cheat_sheet.txt b/examples/adaptive_crag/burr_docs/cheat_sheet.txt
index d3bf63df..ed7d1e54 100644
--- a/examples/adaptive_crag/burr_docs/cheat_sheet.txt
+++ b/examples/adaptive_crag/burr_docs/cheat_sheet.txt
@@ -11,4 +11,4 @@ All other production/debugging concerns are implemented as hooks, which are simp
Note that we did not mention LLMs above at all! That’s good – you want your LLM frameworks to be ever-so-slightly decoupled from them for the best experience (all of AI is just software, plain and simple, after all…).
-And that’s the basics! Let’s dive into the details.
\ No newline at end of file
+And that’s the basics! Let’s dive into the details.
diff --git a/examples/adaptive_crag/burr_docs/state.txt b/examples/adaptive_crag/burr_docs/state.txt
index 914242c6..5e8e6c0f 100644
--- a/examples/adaptive_crag/burr_docs/state.txt
+++ b/examples/adaptive_crag/burr_docs/state.txt
@@ -31,4 +31,4 @@ current_state = current_state.merge(new_state)
If you’re used to thinking about version control, this is a bit like a commit/checkout/merge mechanism.
Reloading Prior State
-Note, if state is serializable, it means that if stored, it can be reloaded. This is useful for reloading state from a previous invocation (for debugging or as part of the application), or for storing state in a database. We have capabilities here, see the state-persistence section.
\ No newline at end of file
+Note, if state is serializable, it means that if stored, it can be reloaded. This is useful for reloading state from a previous invocation (for debugging or as part of the application), or for storing state in a database. We have capabilities here, see the state-persistence section.
diff --git a/examples/adaptive_crag/burr_docs/transitions.txt b/examples/adaptive_crag/burr_docs/transitions.txt
index 08c32c5e..b13db81e 100644
--- a/examples/adaptive_crag/burr_docs/transitions.txt
+++ b/examples/adaptive_crag/burr_docs/transitions.txt
@@ -32,4 +32,4 @@ Note
The default condition is a special case, and will always evaluate to True. It is useful for defining a “catch-all” transition that will be selected if no other condition is met. If you pass a tuple of length 2 to with_transitions, the default condition will be used.
-See the transition docs for more information on the transition API.
\ No newline at end of file
+See the transition docs for more information on the transition API.
diff --git a/examples/adaptive_crag/notebook.ipynb b/examples/adaptive_crag/notebook.ipynb
index 99c4838d..626744c4 100644
--- a/examples/adaptive_crag/notebook.ipynb
+++ b/examples/adaptive_crag/notebook.ipynb
@@ -567,7 +567,6 @@
" if action.name == \"terminate\":\n",
" break\n",
" elif action.name == \"ask_assistant\":\n",
- " time.sleep(1.5)\n",
" inputs = {\"query\": get_user_query()}"
]
},
diff --git a/examples/adaptive_crag/requirements.txt b/examples/adaptive_crag/requirements.txt
index 37ba1e12..a985ef23 100644
--- a/examples/adaptive_crag/requirements.txt
+++ b/examples/adaptive_crag/requirements.txt
@@ -1,7 +1,7 @@
burr[start]
+exa_py
google-generativeai
instructor
+lancedb
pydantic
python-dotenv
-lancedb
-exa_py