Chat | R Documentation |
A Chat
is an sequence of sequence of user and assistant Turns sent
to a specific Provider. A Chat
is a mutable R6 object that takes care of
managing the state associated with the chat; i.e. it records the messages
that you send to the server, and the messages that you receive back.
If you register a tool (i.e. an R function that the assistant can call on
your behalf), it also takes care of the tool loop.
You should generally not create this object yourself,
but instead call chat_openai()
or friends instead.
A Chat object
new()
Chat$new(provider, turns, seed = NULL, echo = "none")
provider
A provider object.
turns
An unnamed list of turns to start the chat with (i.e.,
continuing a previous conversation). If NULL
or zero-length list, the
conversation begins from scratch.
seed
Optional integer seed that ChatGPT uses to try and make output more reproducible.
echo
One of the following options:
none
: don't emit any output (default when running in a function).
text
: echo text output as it streams in (default when running at
the console).
all
: echo all input and output.
Note this only affects the chat()
method.
get_turns()
Retrieve the turns that have been sent and received so far (optionally starting with the system prompt, if any).
Chat$get_turns(include_system_prompt = FALSE)
include_system_prompt
Whether to include the system prompt in the turns (if any exists).
set_turns()
Replace existing turns with a new list.
Chat$set_turns(value)
value
A list of Turns.
get_system_prompt()
If set, the system prompt, it not, NULL
.
Chat$get_system_prompt()
get_model()
Retrieve the model name
Chat$get_model()
set_system_prompt()
Update the system prompt
Chat$set_system_prompt(value)
value
A string giving the new system prompt
tokens()
List the number of tokens consumed by each assistant turn. Currently tokens are recorded for assistant turns only; so user turns will have zeros.
Chat$tokens()
last_turn()
The last turn returned by the assistant.
Chat$last_turn(role = c("assistant", "user", "system"))
role
Optionally, specify a role to find the last turn with for the role.
Either a Turn
or NULL
, if no turns with the specified
role have occurred.
chat()
Submit input to the chatbot, and return the response as a simple string (probably Markdown).
Chat$chat(..., echo = NULL)
...
The input to send to the chatbot. Can be strings or images
(see content_image_file()
and content_image_url()
.
echo
Whether to emit the response to stdout as it is received. If
NULL
, then the value of echo
set when the chat object was created
will be used.
extract_data()
Extract structured data
Chat$extract_data(..., type, echo = "none", convert = TRUE)
...
The input to send to the chatbot. Will typically include the phrase "extract structured data".
type
A type specification for the extracted data. Should be
created with a type_()
function.
echo
Whether to emit the response to stdout as it is received. Set to "text" to stream JSON data as it's generated (not supported by all providers).
convert
Automatically convert from JSON lists to R data types using the schema. For example, this will turn arrays of objects into data frames and arrays of strings into a character vector.
extract_data_async()
Extract structured data, asynchronously. Returns a promise that resolves to an object matching the type specification.
Chat$extract_data_async(..., type, echo = "none")
...
The input to send to the chatbot. Will typically include the phrase "extract structured data".
type
A type specification for the extracted data. Should be
created with a type_()
function.
echo
Whether to emit the response to stdout as it is received. Set to "text" to stream JSON data as it's generated (not supported by all providers).
chat_async()
Submit input to the chatbot, and receive a promise that resolves with the response all at once. Returns a promise that resolves to a string (probably Markdown).
Chat$chat_async(...)
...
The input to send to the chatbot. Can be strings or images.
stream()
Submit input to the chatbot, returning streaming results. Returns A coro generator that yields strings. While iterating, the generator will block while waiting for more content from the chatbot.
Chat$stream(...)
...
The input to send to the chatbot. Can be strings or images.
stream_async()
Submit input to the chatbot, returning asynchronously streaming results. Returns a coro async generator that yields string promises.
Chat$stream_async(...)
...
The input to send to the chatbot. Can be strings or images.
register_tool()
Register a tool (an R function) that the chatbot can use. If the chatbot decides to use the function, ellmer will automatically call it and submit the results back.
The return value of the function. Generally, this should either be a
string, or a JSON-serializable value. If you must have more direct
control of the structure of the JSON that's returned, you can return a
JSON-serializable value wrapped in base::I()
, which ellmer will leave
alone until the entire request is JSON-serialized.
Chat$register_tool(tool_def)
tool_def
Tool definition created by tool()
.
clone()
The objects of this class are cloneable with this method.
Chat$clone(deep = FALSE)
deep
Whether to make a deep clone.
chat <- chat_openai(echo = TRUE)
chat$chat("Tell me a funny joke")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.