Toolkit

The Vectorsearches of an LLMClient, as tools that the LLM can call.

An LLMClient lists Vectorsearches in its manifest’s spec.vectorsearches. On each prompt, VectorsearchToolkit offers each enabled one to the LLM as a function tool whose one argument is a search query. When the LLM calls it, the toolkit runs the Vectorsearch with run_vectorsearch(), as the REST api does: it searches the Vectorsearch’s Vectorstore with the Vectorsearch’s settings (its search type, k, score threshold, MMR settings and metadata filter), within the Vectorsearch’s budget, and returns the passages that it found, most similar first, for the LLM to answer from.

The LLM chooses when to search, and what for. A question that the documents can’t help with costs no search, and a question that needs two searches can make them.

smarter.apps.vectorsearch.toolkit.FUNCTION_NAME_PREFIX = 'smarter_vectorsearch_'

The prefix of a Vectorsearch tool’s function name, which ends with the Vectorsearch’s id.

class smarter.apps.vectorsearch.toolkit.VectorsearchToolkit(vectorsearches, service_class=None)[source]

Bases: object

An LLMClient’s Vectorsearches, as function tools.

Parameters:
  • vectorsearches (list[Vectorsearch]) – The Vectorsearches, e.g. from vectorsearches_for().

  • service_class – A callable that returns the service that searches a Vectorstore, given the Vectorstore. By default, VectorstoreService. Tests pass one with a fake backend.

__init__(vectorsearches, service_class=None)[source]
call(function_name, arguments=None)[source]

Run a Vectorsearch with the LLM’s query, and return the passages that it found, for the LLM.

An error, such as a missing query, a budget that forbids the search, or a Vectorstore that isn’t ready, is returned as {"error": "..."}, so that the LLM can recover.

Parameters:
  • function_name (str) – The function name that the LLM called.

  • arguments (dict[str, Any] | str | None) – The function’s arguments, as a dict or a JSON string, with a query.

Return type:

TypeAliasType

Returns:

The Vectorsearch’s and its Vectorstore’s names, and the results, as VectorsearchApiResults, or an error.

is_vectorsearch_function(function_name)[source]

Return whether a function name is one of this toolkit’s tools.

Parameters:

function_name (str) – The function name that the LLM called.

Return type:

bool

Returns:

True if it is one of this toolkit’s Vectorsearches.

property tools: list[dict[str, Any]]

Return the OpenAI function tool of each Vectorsearch.

Returns:

One {"type": "function", "function": {...}} per Vectorsearch.

vectorsearch_for(function_name)[source]

Return the Vectorsearch of a function name.

Parameters:

function_name (str) – The function name that the LLM called.

Return type:

Vectorsearch | None

Returns:

The Vectorsearch, or None if the name isn’t one of this toolkit’s tools.

smarter.apps.vectorsearch.toolkit.function_name_for(vectorsearch)[source]

Return the function name of a Vectorsearch’s tool, e.g. smarter_vectorsearch_42.

Parameters:

vectorsearch (Vectorsearch) – The Vectorsearch.

Return type:

str

Returns:

The function name, unique to the Vectorsearch.