-
Notifications
You must be signed in to change notification settings - Fork 0
feat: full-text search and schema discovery tools for LangChain agents #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76f02dd
deps: require hotdata-framework>=0.9.0 and hotdata>=0.8.0
rohan-hotdata 10bbfd6
feat: full-text search and schema discovery tools
rohan-hotdata f5f4acc
fix: address review findings on search, schema and the demo
rohan-hotdata 3796c8f
fix: guard max_columns, and re-measure the demo on the fixed row budgets
rohan-hotdata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # BM25 search demo | ||
|
|
||
| Takes a Hotdata workspace from empty to a LangChain agent that full-text searches real | ||
| data, in one script. Uses the public San Francisco Airbnb listings fixture (7,535 rows) | ||
| and builds a BM25 index over the free-text `description` column. | ||
|
|
||
| ## Run it | ||
|
|
||
| Steps 1–5 need only a Hotdata key and exercise the tool directly, no model involved: | ||
|
|
||
| ```bash | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py | ||
| ``` | ||
|
|
||
| The agent step (6) runs when you name a tool-calling model. Any provider works — install | ||
| its LangChain integration, set its key, and pass `<provider>:<model>`: | ||
|
|
||
| ```bash | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py \ | ||
| --model '<provider>:<model>' | ||
| ``` | ||
|
|
||
| The script is safe to re-run: it reuses the managed database, skips the load when the | ||
| table already has rows, and reuses an existing index. | ||
|
|
||
| ```bash | ||
| # different search text, more hits | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py \ | ||
| --query "quiet garden studio near the park" --k 10 | ||
|
|
||
| # stop before the agent step even with a model set | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py --skip-agent | ||
|
|
||
| # tear down the managed database it created | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py --cleanup | ||
|
|
||
| # trace the run into a named LangSmith project | ||
| LANGSMITH_TRACING=true LANGSMITH_PROJECT=hotdata-langchain-bm25 \ | ||
| uv run --group demo --env-file .env python demo/bm25_search_demo.py | ||
| ``` | ||
|
|
||
| ## Credentials | ||
|
|
||
| | Variable | Needed for | Notes | | ||
| |---|---|---| | ||
| | `HOTDATA_API_KEY` | steps 1–5 | everything except the agent run | | ||
| | `HOTDATA_WORKSPACE` | optional | pins a workspace; first available otherwise | | ||
| | your model provider's key | step 6 | skipped without it, or without `--model` | | ||
| | `LANGSMITH_API_KEY` + `LANGSMITH_TRACING=true` | optional | traces the run to LangSmith | | ||
| | `LANGSMITH_PROJECT` | optional | project the traces land in (default: `default`) | | ||
|
|
||
| BM25 needs no embedding provider — just a string column — so there is no extra | ||
| credential for the index itself, unlike a vector index. LangSmith is observability | ||
| only; the agent still needs a model provider key of its own. | ||
|
|
||
| With tracing on, every tool call becomes a run in the LangSmith project — the search | ||
| tool shows up as a `tool` run named `hotdata_search_text` carrying its query and the | ||
| ranked JSON it returned, so the generated SQL and the agent's choice between search | ||
| and SQL are both inspectable after the fact. | ||
|
|
||
| ## What each step does | ||
|
|
||
| 1. **Managed database** — creates `langchain_bm25_demo` with `public.listings` declared | ||
| up front, so the load materialises into it directly. | ||
| 2. **Listings data** — downloads the fixture parquet (cached in the system temp dir) and | ||
| loads it into the managed table. | ||
| 3. **BM25 index** — creates a `bm25` index on `description` through `IndexesApi` and | ||
| polls until it reports ready. This step is a hard prerequisite: `bm25_search` has no | ||
| brute-force fallback and errors outright when no index exists. | ||
| 4. **Tools** — reads the real table schema and narrows the projection to a handful of | ||
| useful columns. The listings table is 85 columns wide; returning all of them would | ||
| flood the agent's context. | ||
| 5. **Direct tool invocation** — prints the generated SQL, then the ranked hits with | ||
| their BM25 scores. Proves the tool against the live engine without an LLM in the loop. | ||
| 6. **Agent run** — gives a LangChain agent both `hotdata_search_text` and | ||
| `hotdata_execute_sql` and asks a question neither answers alone: find listings whose | ||
| descriptions mention a quiet garden studio, then report listing counts and average | ||
| review scores across *all* listings in those neighbourhoods. The aggregate spans the | ||
| whole dataset, not the handful search returned, so the agent has to use search to | ||
| identify the neighbourhoods and SQL to aggregate over them. The printed tool calls | ||
| show which pathway it picked for which part. | ||
|
|
||
| Set the model with `--model` or `DEMO_MODEL`; any tool-calling model works, and the | ||
| step is skipped when its provider key is absent. | ||
|
|
||
| **What this step demonstrates is the routing.** Across five runs of the current | ||
| configuration the agent called the search tool and the schema tool every time, and the | ||
| final table matched the whole-neighbourhood figures every time. Two of those runs also | ||
| recovered from a bad query after the error was handed back to them. | ||
|
|
||
| Do not read five runs as a guarantee. Answering a compound question correctly is a | ||
| property of the model rather than of these tools, and an earlier configuration — which | ||
| shared one row budget between the search and SQL tools, silently truncating the | ||
| aggregate — got it right in only four of five. The printed tool calls are the reliable | ||
| part; treat the prose answer as illustrative. | ||
|
|
||
| ## What makes the agent run work | ||
|
|
||
| **The tool descriptions, not the system prompt.** The demo's system prompt says only who | ||
| the agent is — it deliberately says nothing about which tool to use or how the engine | ||
| behaves. That guidance lives in the tool descriptions, so any application gets it without | ||
| having to teach the model about the query engine. | ||
|
|
||
| It matters. An earlier version of this demo had a one-line SQL tool description and a | ||
| system prompt spelling out the rules; the model still reached for Postgres full-text | ||
| idioms (`to_tsvector`/`plainto_tsquery`), which the engine rejects with | ||
| `Invalid function 'to_tsvector'`. With the constraint stated in the SQL tool's own | ||
| description instead, the model uses the search tool and feeds its results into SQL — | ||
| even with no system-prompt guidance at all. | ||
|
|
||
| **Tool errors have to reach the model.** The tools raise on failure, and an exception out | ||
| of a tool aborts the whole graph — so the demo wraps them (`with_error_feedback`) to | ||
| return the error as a message instead. The wrapper also digs the engine's real message | ||
| out of the exception chain: the framework raises `RuntimeError("Bad Request")` while the | ||
| useful text sits in the underlying API response body. Descriptions lower the failure | ||
| rate; readable errors are what let the model recover from what slips through. | ||
|
|
||
| ## Why the generated SQL looks the way it does | ||
|
|
||
| Step 5 prints the query. Two details in it are load-bearing: | ||
|
|
||
| - **`ORDER BY score DESC`** — the engine returns hits in rowid order, not ranked, so | ||
| ranking has to be asked for explicitly. | ||
| - **`k` appearing twice** — once as `bm25_search(...)`'s fourth argument and once as a | ||
| trailing `LIMIT`. The fourth argument is what actually bounds the search, because | ||
| `ORDER BY` blocks limit pushdown; relying on the trailing `LIMIT` alone would let the | ||
| scan fall back to the engine's much larger default bound. |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this recipe silently gives up the SQL description's search steer (not blocking).
make_hotdata_tools(client, database="sf_airbnb")here is called withoutsearch_table/search_column, sohas_searchis false andsql_tool_descriptionfalls back to the no-search branch — the SQL tool never mentionssearch_listings/search_reviews. The multi-corpus path documented here therefore loses exactly the behaviour the PR description shows to be load-bearing (12-token description →to_tsvector→ aborted run).There is currently no way to tell
make_hotdata_tools"search tools exist under these names" without also pinning one corpus:search_tool_nameis ignored unlesssearch_table/search_columnare set. Worth either accepting asearch_tool_names: Sequence[str](or lettingsearch_tool_namestand alone) sosql_tool_descriptioncan name them, or at minimum noting in this section that callers taking this path should pass their owndescription=to the SQL tool.