# Knowledge search (/features/ai/knowledge-search)

How do you find the right answer among thousands of pages and messages? VBT's knowledge search
searches its documentation, API reference, code examples, and community answers from Python. Look up
an exact warning or ask a question in your own words, then open the original sources. You can also
collect the best matches as context for your own LLM.

```python title="Search the knowledge base and see where the best matches come from"
>>> def source(link):
...     if "discord.com" in link:
...         return "community"
...     if "/api/" in link:
...         return "API"
...     if "/tutorials/" in link or "/cookbook/" in link:
...         return "tutorials and cookbook"
...     return "documentation"

>>> results = vbt.search("trailing stop after partial exit", display=False)  # (1)
>>> len(results)
42434
>>> top = results[:20]
>>> pd.Series([source(d["link"]) for d in top]).value_counts()
community        17
documentation     2
API               1
Name: count, dtype: int64
>>> docs = [d["link"].split(".pro")[1] for d in top if source(d["link"]) != "community"]
>>> print("\n".join(dict.fromkeys(docs)))
/documentation/portfolio/from-signals/#types
/documentation/portfolio/from-signals/#creation-1
/api/signals/generators/stcx/#vectorbtpro.signals.generators.stcx.STCX.exit_place_func_nb
```

1.  Ranks all 42,434 documents for the query and returns them best first. Without `display=False`,
    the results open as an HTML page in the browser.

Searching the whole knowledge base took 2.7 seconds on an Apple M3 once its data was cached. The
first search downloads the knowledge assets and their precomputed embeddings, which took about 15
minutes here. For a question about a specific situation, community answers dominated the top 20,
alongside the documentation sections that explain how stop orders are created.

## The knowledge base \[#the-knowledge-base]

The knowledge base is a set of assets: website pages, community messages, code examples, and
concepts, each a list of records with a link, title, and content. `vbt.find_docs`, `vbt.find_api`,
`vbt.find_examples`, and `vbt.find_messages` return the records about an object or a query, and the
assets themselves can be filtered, queried, sorted, grouped, and merged like data, with methods such
as `select`, `query`, `find`, and `apply`. Message threads and page sections can be combined into
single documents before searching, and your own text can be wrapped in an asset and searched or
chatted with the same way. See the [Knowledge assets](#knowledge-assets) highlight below.

## Search and ranking \[#search-and-ranking]

Ranking runs in three steps:

*   **Splitting:** long documents are split into chunks, with code blocks and headings kept intact.
*   **Retrieval:** embeddings find documents with a similar meaning, BM25 finds documents with the
    same words, and the default hybrid mode merges both rankings with reciprocal rank fusion.
*   **Reranking:** an optional reranker from Cohere, Jina, Voyage, a local Hugging Face cross-encoder,
    or any LLM scores the top results again, as the [Reranking](#reranking) highlight below shows.

Embeddings come from OpenAI, Gemini, Voyage, Cohere, Jina, Hugging Face, a local Sentence
Transformers model, or Ollama, and are stored in memory, in files, or in LMDB. For the default
models, precomputed embeddings of the knowledge base download with each release, so only your
queries need to be embedded. Embeddings from different models cannot be mixed, so after switching
the embeddings provider, clear the cached embeddings and let VBT build them again.

The website pages and community messages are not part of the package. They download from the release
on GitHub with the same `GITHUB_TOKEN` used to install VBT, and an updated VBT fetches the matching
assets on its first search. Machines without access can point the asset settings at a local copy of
the files.

## Search tools \[#search-tools]

| Tool                         | Use it for                                                       | Needs                                                  |
| ---------------------------- | ---------------------------------------------------------------- | ------------------------------------------------------ |
| `vbt.search`                 | Questions in your own words, using meaning and keywords together | An embeddings provider                                 |
| `vbt.quick_search`           | Exact warnings, function names, and other keywords               | Nothing beyond the downloaded assets                   |
| `vbt.chat`, `vbt.quick_chat` | An answer based on the retrieved sources                         | A completions provider, plus embeddings for `vbt.chat` |

Results open as an HTML page or return as an asset for your own code, as the [SearchVBT](#searchvbt)
and [Quick search and chat](#quick-search-and-chat) highlights below show. The same ranking feeds
the [AI research assistant](/features/ai/ai-research-assistant/), and the member website has its own
search bar for pages and API.

## Find code or community answers \[#find-code-or-community-answers]

Looking for a working example? Limit a search to `asset_names="examples"`. Debugging a warning or an
unusual backtest setup? Use `asset_names="messages"` to search community discussions. This lets you
choose the kind of answer you need without searching every source at once.

If you already know the VBT object, `vbt.find_api` and `vbt.find_examples` can find its API
reference and examples directly. For custom signal logic, the [Knowledge assets](#knowledge-assets)
example below goes further and finds code containing a particular function definition.

## Search your own research notes \[#search-your-own-research-notes]

Your knowledge base can include your own material too. Wrap notes or text extracted from your
documents in a `KnowledgeAsset`, then filter, search, or chat with it. For example, keep execution
assumptions and validation plans together and find the note you need:

```python title="Find an execution assumption in personal research notes"
>>> notes = vbt.KnowledgeAsset([
...     dict(title="Execution assumptions", content="Compare slippage of 0.05% and 0.1%."),
...     dict(title="Validation plan", content="Reserve the final year for out-of-sample testing."),
... ])
>>> print(notes.find("slippage").get("title"))
['Execution assumptions']
```

This text lookup runs locally without an embeddings or chat provider. The same asset can supply
context for a model when you want to ask questions about the notes instead.

## Context for your own LLM \[#context-for-your-own-llm]

`to_context` turns ranked results into one block of text within a token budget, with optional
metadata such as links, ready to paste into any model or pass to your own pipeline. Choose how much
source material to include with `max_tokens`. Local caching keeps downloaded assets and search
results ready for reuse, and answers can stream into your notebook or be saved as text, Markdown, or
HTML.

## Reranking \[#reranking]

New in v2026.4.7

✅ After the initial document retrieval, VBT can now rerank results using a dedicated reranking model
to surface the most relevant documents. Five providers are supported out of the box: Cohere, Jina,
Voyage, Hugging Face cross-encoders (local), and any LLM via completions-based scoring.

=== "Example 1: Search with reranking"
    ```python title="Rerank search results using Cohere"
    >>> env["COHERE_API_KEY"] = "<YOUR_COHERE_API_KEY>"

    >>> vbt.search(  # (1)
    ...     "How to rebalance weekly?",
    ...     rerank=True,  # (2)
    ...     reranker="cohere",  # (3)
    ...     rerank_limit=20,  # (4)
    ... )
    ```

    1.  Works the same way with `vbt.chat()`, `vbt.quick_chat()`, and `vbt.interact()`.
    2.  Enable reranking after the initial retrieval pass.
    3.  Choose a reranker. Available options: `"cohere"`, `"jina"`, `"voyage"`, `"hf_cross_encoder"`,
        `"completions"`.
    4.  Only the top 20 documents from the initial ranking are sent to the reranker.

=== "Example 2: Chat with local reranking"
    ```python title="Rerank using a local cross-encoder model"
    >>> env["OPENAI_API_KEY"] = "<YOUR_OPENAI_API_KEY>"

    >>> vbt.chat(  # (1)
    ...     "How to use stop losses with from_signals?",
    ...     rank_kwargs=dict(
    ...         rerank=True,
    ...         reranker="hf_cross_encoder",  # (2)
    ...     ),
    ...     formatter="html",
    ... )
    ```

    1.  Pass reranking parameters through `rank_kwargs` when calling `vbt.chat()`.
    2.  Use a Hugging Face cross-encoder model for fully local, offline reranking.

!!! info "Cookbook"
    Don't use OpenAI? Explore more configurations in the knowledge section of the
    [Cookbook](/cookbook/).

## Quick search and chat \[#quick-search-and-chat]

New in v2025.5.11

✅ In addition to embeddings, which require prior generation, VBT now supports
[BM25](https://en.wikipedia.org/wiki/Okapi_BM25) for fast, fully offline lexical search. This is
perfect for quickly finding something specific.

```python title="Search VBT knowledge for a warning"
>>> env["GITHUB_TOKEN"] = "<YOUR_GITHUB_TOKEN>"

>>> vbt.quick_search("UserWarning: Symbols have mismatching index")
```

=== "Page 1"
    ![](/assets/images/features/intelligence/quick_search1.light.png#only-light)
    ![](/assets/images/features/intelligence/quick_search1.dark.png#only-dark)

=== "Page 2"
    ![](/assets/images/features/intelligence/quick_search2.light.png#only-light)
    ![](/assets/images/features/intelligence/quick_search2.dark.png#only-dark)

=== "Page 3"
    ![](/assets/images/features/intelligence/quick_search3.light.png#only-light)
    ![](/assets/images/features/intelligence/quick_search3.dark.png#only-dark)

## SearchVBT \[#searchvbt]

New in v2025.3.1

✅ Want to find specific information using natural language on the website or Discord? SearchVBT
accepts your question and generates an HTML page with ranked results and links to their sources. It
searches by meaning as well as keywords, so you can describe what you want to do even when you do
not know the VBT function name yet.

```python title="Search VBT knowledge for a warning"
>>> env["GITHUB_TOKEN"] = "<YOUR_GITHUB_TOKEN>"
>>> env["OPENAI_API_KEY"] = "<YOUR_API_KEY>"

>>> vbt.search("How to run indicator expressions?")
```

=== "Page 1"
    ![](/assets/images/features/intelligence/searchvbt1.light.png#only-light)
    ![](/assets/images/features/intelligence/searchvbt1.dark.png#only-dark)

=== "Page 2"
    ![](/assets/images/features/intelligence/searchvbt2.light.png#only-light)
    ![](/assets/images/features/intelligence/searchvbt2.dark.png#only-dark)

=== "Page 3"
    ![](/assets/images/features/intelligence/searchvbt3.light.png#only-light)
    ![](/assets/images/features/intelligence/searchvbt3.dark.png#only-dark)

## Knowledge assets \[#knowledge-assets]

New in v2024.11.12

✅ Each [release](https://github.com/polakowo/vectorbt.pro/releases) now includes valuable knowledge
assets, JSON files containing private website content and the complete "vectorbt.pro" Discord
history. These assets can be used with LLMs and services like Cursor. In addition, VBT offers a
palette of classes for working with these assets, providing functions such as converting to Markdown
and HTML files, browsing the website offline, performing targeted searches, interacting with LLMs,
and much more!

```python title="Gather all pages and messages that define signal_func_nb"
>>> env["GITHUB_TOKEN"] = "<YOUR_GITHUB_TOKEN>"

>>> pages_asset = vbt.PagesAsset.pull()  # (1)
>>> messages_asset = vbt.MessagesAsset.pull()
>>> vbt_asset = pages_asset + messages_asset
>>> code = vbt_asset.find_code("def signal_func_nb", return_type="item")
>>> code.print_sample(seed=42)  # (2)
```

1.  The first pull will download the assets, while subsequent pulls will use the cached versions.
2.  Print a random code snippet.

````yaml
link: https://discord.com/channels/x/918630948248125512/1251081573147742298
block: https://discord.com/channels/x/918630948248125512/1251081573147742298
thread: https://discord.com/channels/x/918630948248125512/1250844139952541837
reference: https://discord.com/channels/x/918630948248125512/1250844139952541837
replies:
- https://discord.com/channels/x/918630948248125512/1251083513336299610
channel: support
timestamp: '2024-06-14 07:51:31'
author: '@polakowo'
content: Something like this
mentions:
- '@fei'
attachments:
- file_name: Screenshot_2024-06-13_at_20.29.45-B4517.png
  content: |-
    Here's the text extracted from the image:

    ```python
    @njit
    def signal_func_nb(ctx, entries, exits, wait):
        is_entry = vbt.pf_nb.select_nb(ctx, entries)
        if is_entry:
            return True, False, False, False
        is_exit = vbt.pf_nb.select_nb(ctx, exits)
        if is_exit:
            if vbt.pf_nb.in_position_nb(ctx):
                last_order = vbt.pf_nb.get_last_order_nb(ctx)
                if ctx.index[ctx.i] - ctx.index[last_order["idx"]] >= wait:
                    return False, True, False, False
        return False, False, False, False

    pf = vbt.PF.from_random_signals(
        "BTC-USD",
        n=100,
        seed=42,
        signal_func_nb=signal_func_nb,
        signal_args=(
            vbt.Rep("entries"),
            vbt.Rep("exits"),
            vbt.dt.to_ns(vbt.timedelta("1000 days"))
        )
    )
    ```
reactions: 0
````


## Related pages

*   [AI research assistant](/features/ai/ai-research-assistant/): Chat with an assistant grounded in VBT documentation and examples, with tools and reasoning
*   [MCP server](/features/ai/mcp-server/): Let your coding assistant search VBT documentation, look up the API, and run code over MCP
*   [Developer tools](/features/tooling/developer-tools/): Search the API, read source, draw reference graphs, and extend classes with factories
*   [AI](/features/ai/): Get help with Python backtests, search VBT knowledge, and connect your coding assistant