Skip to contents

A Chat is an sequence of sequence of user and assistant Turns sent to a specific Provider. A Chat is a mutable object because the sequence of turns grows over time as you converse with the chatbot.

You should generally not create this object yourself, but instead call chat_openai() or friends instead.

Value

A promise that resolves to a string (probably Markdown).

Active bindings

system_prompt

The system prompt, if any, as a string.

Methods


Method new()

Usage

Chat$new(provider, turns, seed = NULL, echo = "none")

Arguments

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.


Method turns()

The turns that have been sent and received so far (optionally starting with the system prompt, if any).

Usage

Chat$turns(include_system_prompt = FALSE)

Arguments

include_system_prompt

Whether to include the system prompt in the turns (if any exists).


Method 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.

Usage

Chat$tokens()


Method last_turn()

The last turn returned by the assistant.

Usage

Chat$last_turn(role = c("assistant", "user", "system"))

Arguments

role

Optionally, specify a role to find the last turn with for the role.

Returns

Either a Turn or NULL, if no turns with the specified role have occurred.


Method chat()

Submit input to the chatbot, and return the response as a simple string (probably Markdown).

Usage

Chat$chat(..., echo = NULL)

Arguments

...

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.


Method chat_async()

Submit input to the chatbot, and receive a promise that resolves with the response all at once.

Usage

Chat$chat_async(...)

Arguments

...

The input to send to the chatbot. Can be strings or images.


Method 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.

Usage

Chat$stream(...)

Arguments

...

The input to send to the chatbot. Can be strings or images.


Method stream_async()

Submit input to the chatbot, returning asynchronously streaming results. Returns a coro async generator that yields string promises.

Usage

Chat$stream_async(...)

Arguments

...

The input to send to the chatbot. Can be strings or images.


Method register_tool()

Register a tool (an R function) that the chatbot can use. If the chatbot decides to use the function, elmer will automatically call it and submit the results back.

Usage

Chat$register_tool(tool_def)

Arguments

tool_def

Tool definition created by ToolDef.


Method clone()

The objects of this class are cloneable with this method.

Usage

Chat$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.