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
2020chat_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
0 commit comments