Method new()
Create a new llm_provider object
Usage
llm_provider-class$new(
complete_chat_function,
parameters = list(),
verbose = TRUE,
url = NULL,
api_key = NULL,
api_type = "unspecified"
)
Arguments
complete_chat_function
Function that will be called by the llm_provider to complete a chat.
This function should take a list containing at least '$chat_history'
(a data frame with 'role' and 'content' columns) and return a response
object, which contains:
'completed': A dataframe with 'role' and 'content' columns,
containing the completed chat history
'http': A list containing a list 'requests' and a list 'responses',
containing the HTTP requests and responses made during the chat completion
parameters
A named list of parameters to configure the llm_provider.
These parameters may be appended to the request body when interacting with
the LLM provider.
For example, the model
parameter may often be required.
The 'stream' parameter may be used to indicate that the API should stream.
Parameters should not include the chat_history, or 'api_key' or 'url', which
are handled separately by the llm_provider and '$complete_chat()'.
Parameters should also not be set when they are handled by prompt wraps
verbose
A logical indicating whether interaction with the LLM
provider should be printed to the console
url
The URL to the LLM provider API endpoint for chat completion
(typically required, but may be left NULL in some cases, for instance
when creating a fake LLM provider)
api_key
The API key to use for authentication with the LLM
provider API (optional, not required for, for instance, Ollama)
api_type
The type of API to use (e.g., "openai", "ollama").
This is used to determine certain specific behaviors for different APIs
(see for example the answer_as_json()
function)
Returns
A new llm_provider R6 object
Method set_parameters()
Helper function to set the parameters of the llm_provider
object.
This function appends new parameters to the existing parameters
list.
Usage
llm_provider-class$set_parameters(new_parameters)
Arguments
new_parameters
A named list of new parameters to append to the
existing parameters list
Returns
The modified llm_provider object
Method complete_chat()
Sends a chat history (see chat_history()
for details) to the LLM provider using the configured $complete_chat()
.
This function is typically called by send_prompt()
to interact with the LLM
provider, but it can also be called directly.
Usage
llm_provider-class$complete_chat(input)
Arguments
input
A string, a data frame which is a valid chat history
(see chat_history()
), or a list containing a valid chat history under key
'$chat_history'
Returns
The response from the LLM provider
Method add_handler_fn()
Helper function to add a handler function to the
llm_provider object.
Handler functions are called after the completion of a chat and can be
used to modify the response before it is returned by the llm_provider.
Each handler function should take the response object
as input (first argument) as well as 'self' (the llm_provider
object) and return a modified response object.
The functions will be called in the order they are added to the list.
Usage
llm_provider-class$add_handler_fn(handler_fn)
Arguments
handler_fn
A function that takes the response object plus
'self' (the llm_provider object) as input and
returns a modified response object
Details
If a handler function returns a list with a 'break' field set to TRUE
,
the chat completion will be interrupted and the response will be returned
at that point.
If a handler function returns a list with a 'done' field set to FALSE
,
the handler functions will continue to be called in a loop until the 'done'
field is not set to FALSE
.
Method set_handler_fns()
Helper function to set the handler functions of the
llm_provider object.
This function replaces the existing
handler functions list with a new list of handler functions.
See $add_handler_fn()
for more information
Usage
llm_provider-class$set_handler_fns(handler_fns)
Arguments
handler_fns
A list of handler functions to set
Method clone()
The objects of this class are cloneable with this method.
Usage
llm_provider-class$clone(deep = FALSE)
Arguments
deep
Whether to make a deep clone.