Skip to content

Commit ffa94bc

Browse files
committed
Update signature
1 parent d2f8686 commit ffa94bc

File tree

2 files changed

+28
-33
lines changed

2 files changed

+28
-33
lines changed

R/ragnar-chat.R

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
#' Creates an `ellmer::Chat` with increased capabilities
22
#' powered by `ragnar::RagnarStore`.
33
#'
4-
#' @param chat_fun A function that returns an `ellmer::Chat` object,
5-
#' sunch as [ellmer::chat_openai()] , etc.
6-
#' @param ... Additional parameters passed to `chat_fun`.
7-
#' @param .store An `ragnar::RagnarStore` object that contains the knowledge base that powers
4+
#' @param chat A function that returns an `ellmer::Chat` object,
5+
#' such as [ellmer::chat_openai()] , etc.
6+
#' @param store An `ragnar::RagnarStore` object that contains the knowledge base that powers
87
#' this chat.
9-
#' @param .on_user_turn A function that is called when the user sends a message.
8+
#' @param register_store_tool If `TRUE`, the `store` is registered as a tool in the chat.
9+
#' @param on_user_turn A function that is called when the user sends a message.
1010
#' It's called with `self` (the instance of `RagnarChat`), and `...` the message
1111
#' sent by the user. It's output is passed to `ellmer::Chat$chat()`.
1212
#' Eg, the identity is simply `function(self, ...) list(...)`.
1313
#' The default callback prunes the previous tool calls from the chat history and
1414
#' inserts a tool call request, so that the LLM always sees retrieval results.
15-
#' @param .retrieve A function that takes `self` (the instance of `RagnarChat`) and `query`
15+
#' @param retrieve A function that takes `self` (the instance of `RagnarChat`) and `query`
1616
#' (the query to retrieve results for) and returns a data.frame of chunks as results.
1717
#' The default implementation calls `ragnar_retrieve()` on chunks, after filtering those
1818
#' already present in the chat history.
1919
#' @export
2020
chat_ragnar <- function(
21-
chat_fun,
22-
...,
23-
.store,
24-
.register_store = TRUE,
25-
.on_user_turn = function(self, ...) {
21+
chat,
22+
store,
23+
register_store_tool = TRUE,
24+
on_user_turn = function(self, ...) {
2625
# prunes previously inserted tool calls
2726
self$turns_prune_chunks(keep_last_n = 0)
2827
# inserts a new tool call request with the user's input
2928
self$turns_insert_tool_call_request(..., query = paste(..., collapse = " "))
3029
},
31-
.retrieve = function(self, query) {
30+
retrieve = function(self, query) {
3231
retrieved_ids <- self$turns_list_chunks() |>
3332
sapply(\(x) x$id)
3433

@@ -38,8 +37,8 @@ chat_ragnar <- function(
3837
ragnar::ragnar_retrieve(query, top_k = 10)
3938
}
4039
) {
41-
chat <- chat_fun(...)
42-
RagnarChat$new(chat, .store, .register_store, .on_user_turn, .on_retrieval, .retrieve)
40+
chat <- chat()
41+
RagnarChat$new(chat, store, register_store_tool, on_user_turn, on_retrieval, retrieve)
4342
}
4443

4544
#' Adds extra capabilities to a `ellmer::Chat` object.
@@ -67,7 +66,7 @@ RagnarChat <- R6::R6Class(
6766
initialize = function(
6867
chat,
6968
store,
70-
register_store,
69+
register_store_tool,
7170
on_user_turn,
7271
on_retrieval,
7372
ragnar_retrieve
@@ -86,7 +85,7 @@ RagnarChat <- R6::R6Class(
8685
"The text to find most relevant matches for."
8786
)
8887
)
89-
if (register_store) {
88+
if (register_store_tool) {
9089
self$register_tool(self$ragnar_tool_def)
9190
}
9291
self$on_user_turn <- on_user_turn

tests/testthat/test-ragnar-chat.R

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
test_that("RagnarChat", {
22
store <- test_store()
3-
chat <- chat_ragnar(ellmer::chat_openai, model = "gpt-4.1-nano", .store = store)
3+
chat <- chat_ragnar(\() ellmer::chat_openai(model = "gpt-4.1-nano"), store = store)
44

55
out <- chat$chat("advanced R")
66

@@ -38,10 +38,9 @@ test_that("Implementing query rewriting", {
3838

3939
store <- test_store()
4040
chat <- chat_ragnar(
41-
ellmer::chat_openai,
42-
model = "gpt-4.1-nano",
43-
.store = store,
44-
.on_user_turn = function(self, ...) {
41+
\() ellmer::chat_openai(model = "gpt-4.1-nano"),
42+
store = store,
43+
on_user_turn = function(self, ...) {
4544

4645
self$turns_prune_tool_calls()
4746
self$turns_insert_tool_call_request(
@@ -61,10 +60,9 @@ test_that("Implementing query rewriting", {
6160
test_that("remove chunks by id works", {
6261
store <- test_store()
6362
chat <- chat_ragnar(
64-
ellmer::chat_openai,
65-
model = "gpt-4.1-nano",
66-
.store = store,
67-
.on_user_turn = function(self, ...) {
63+
\() ellmer::chat_openai(model = "gpt-4.1-nano"),
64+
store = store,
65+
on_user_turn = function(self, ...) {
6866
self$turns_insert_tool_call_request(
6967
...,
7068
query = paste(..., collapse = " ")
@@ -91,10 +89,9 @@ test_that("remove chunks by id works", {
9189
test_that("duplicated chunks are not returned", {
9290
store <- test_store()
9391
chat <- chat_ragnar(
94-
ellmer::chat_openai,
95-
model = "gpt-4.1-nano",
96-
.store = store,
97-
.on_user_turn = function(self, ...) {
92+
\() ellmer::chat_openai(model = "gpt-4.1-nano"),
93+
store = store,
94+
on_user_turn = function(self, ...) {
9895
self$turns_insert_tool_call_request(
9996
...,
10097
query = paste(..., collapse = " ")
@@ -115,10 +112,9 @@ test_that("duplicated chunks are not returned", {
115112
test_that("Can insert chunks premptively in the user chat", {
116113
store <- test_store()
117114
chat <- chat_ragnar(
118-
ellmer::chat_openai,
119-
model = "gpt-4.1-nano",
120-
.store = store,
121-
.on_user_turn = function(self, ...) {
115+
\() ellmer::chat_openai(model = "gpt-4.1-nano"),
116+
store = store,
117+
on_user_turn = function(self, ...) {
122118
self$turns_insert_documents(
123119
...,
124120
query = paste(..., collapse = " ")

0 commit comments

Comments
 (0)