Skip to contents

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.

Usage

ragnar_tool(store, k = 5L, name = "search_knowledge", description = NULL)

Arguments

store

A ragnar store, built with ragnar::ragnar_store_create(), ragnar::ragnar_store_insert() and ragnar::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 k documents.

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.

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)
} # }