ragnar_tool() wraps a ragnar document store as an ellmer tool that takes
a search query and returns the k best-matching chunks as text. Give it to
react() so the agent can search your documents while it reasons, or
register it on an ellmer Chat. rag_module() is the alternative that
retrieves once, before every call.
Arguments
- store
A ragnar store, built with
ragnar::ragnar_store_create(),ragnar::ragnar_store_insert()andragnar::ragnar_store_build_index().- k
Number of chunks to return per search.
- name
Tool name shown to the model.
- description
Tool description shown to the model. The default says it searches the knowledge base and returns the top
kdocuments.
Value
An ellmer tool definition (see ellmer::tool()) for react()
modules or Chat$register_tool(). It can also be called directly with a
query string.
Details
Results are numbered ([Result 1], [Result 2], ...) and separated by
---. A search error is returned to the model as text instead of
stopping the conversation. Requires the ragnar package.
See also
Other integrations:
as_dsprrr_metric(),
as_dsprrr_traces(),
as_ellmer_tool(),
as_vitals_cost(),
as_vitals_samples(),
as_vitals_solver(),
as_vitals_task(),
create_search_tool(),
llm_predict(),
reasoning_effort(),
register_dsprrr_engine(),
summarize_traces_df(),
temperature(),
top_p(),
use_dsprrr_template(),
validate_workflow(),
vitals_metrics
Examples
if (FALSE) { # \dontrun{
# Create a ragnar store from documents
library(ragnar)
store <- ragnar_store_create(
embed = \(x) embed_openai(x, model = "text-embedding-3-small")
)
for (i in seq_along(my_docs)) {
doc <- MarkdownDocument(my_docs[[i]], origin = paste0("doc-", i))
ragnar_store_insert(store, markdown_chunk(doc))
}
ragnar_store_build_index(store)
# Create a search tool
search_tool <- ragnar_tool(store, k = 3L)
search_tool("How do I configure the cache?")
# Use with a ReAct module
agent <- react("question -> answer", tools = list(search_tool))
run(agent, question = "How do I turn caching off?", .llm = ellmer::chat_openai(model = "gpt-6-luna"))
# Or register it on an ellmer Chat
chat <- ellmer::chat_openai(model = "gpt-6-luna")
chat$register_tool(search_tool)
} # }