Creates a new Conversation instance.
Optional
options: TOptionsThe options for the Conversation instance's configuration, request options, history, and callable functions.
Readonly
callablePrivate
Readonly
chatReadonly
configReadonly
historyA UUID generated by the library for this conversation. Not the same as the conversation ID returned by the OpenAI API.
Private
Readonly
pluginReadonly
pluginsReadonly
requestStatic
globalPlugins that will be used for all conversations.
Only applies to conversations created after this property is set. Previous conversations will not be affected.
For TypeScript users, you can achieve type-safe global plugins by overriding the ConversationGlobalPluginsOverride
interface.
const globalPlugins = [somePlugin, someOtherPlugin];
declare module "gpt-turbo" {
interface ConversationGlobalPluginsOverride {
globalPlugins: typeof globalPlugins;
}
}
Conversation.globalPlugins = globalPlugins;
Sends the result of a user-evaluated function call to the GPT model and gets the assistant's response.
This method should usually be called after receiving a function_call message from the assistant (using getChatCompletionResponse()
or prompt()
) and evaluating your own function with the provided arguments from that message.
The name of the function used to generate the result. This function must be defined in the functions
config option.
The result of the function call. If the result is anything other than a string, it will be JSON stringified. Since result
can be anything, the T
generic is provided for your typing convenience, but is not used internally
Optional
options: PromptOptionsAdditional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.
Optional
requestOptions: { Additional options to pass for the HTTP request. This overrides the config passed to the constructor.
Optional
headers?: Record<string, string>Optional
proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; }The assistant's response as a Message
instance.
Sends a Create Chat Completion request to the OpenAI API using the current messages stored in the conversation's history.
Rest
...args: [options: PromptOptions, requestOptions: { A new Message
instance with the role of "assistant" and the content set to the response from the OpenAI API. If the stream
config option was set to true
, the content will be progressively updated as the response is streamed from the API. Listen to the returned message's onUpdate
event to get the updated content.
This method is solely provided for client code that wants to trigger a Create Chat Completion request manually. It is not used internally by the library and does not moderate messages before sending them to the API.
This is the recommended way to interact with the GPT model. It's a wrapper method around other public methods that handles the logic of adding a user message, sending a request to the OpenAI API, and adding the assistant's response.
The prompt to send to the assistant.
Optional
options: PromptOptionsAdditional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.
Optional
requestOptions: { Additional options to pass for the HTTP request. This overrides the config passed to the constructor.
Optional
headers?: Record<string, string>Optional
proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; }The assistant's response as a Message
instance.
Removes all messages starting from (but excluding) the fromMessage
if it's a user message, or its previous user message if fromMessage
is an assistant message.
Then, the prompt
method is called using either the specified newPrompt
or the previous user message's content.
This is useful if you want to edit a previous user message (by specifying newPrompt
) or if you want to regenerate the response to a previous user message (by not specifying newPrompt
).
The message to re-prompt from. This can be either a message ID or a Message
instance.
Optional
newPrompt: stringThe new prompt to use for the previous user message. If not provided, the previous user's message content will be reused.
Optional
options: PromptOptionsAdditional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.
Optional
requestOptions: { Additional options to pass for the HTTP request. This overrides the config passed to the constructor.
Optional
headers?: Record<string, string>Optional
proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; }The assistant's response as a Message
instance.
let assistantRes1 = await conversation.prompt("Hello!"); // Hi
let assistantRes2 = await conversation.prompt("How are you?"); // I'm good, how are you?
// Regenerate the assistantRes2 response
assistantRes2 = await conversation.reprompt(assistantRes2); // Good! What about you?
// Edit the initial prompt (and remove all messages after it. In this case, assistantRes2's response)
assistantRes1 = await conversation.reprompt(assistantRes1, "Goodbye!"); // See you later!
Serializes the Conversation
to JSON.
A JSON representation of the Conversation
instance.
Optional
callableOptional
config?: { context?: string | undefined; dry?: boolean | undefined; disableModeration?: boolean | "soft" | undefined; apiKey?: string | undefined; model?: string | undefined; temperature?: number | undefined; ... 8 more ...; function_call?: "none" | ... 2 more ... | undefined; }Optional
history?: { messages?: { role: "function" | "user" | "assistant" | "system"; content: string | null; id?: string | undefined; name?: string | undefined; function_call?: { name: string; arguments: Record<...>; } | undefined; model?: string | undefined; flags?: string[] | ... 1 more ... | undefined; }[] | undefined; }Optional
id?: stringOptional
pluginsOptional
requestStatic
fromJSONCreates a new Conversation instance from a JSON object.
The JSON object of the Conversation instance.
Optional
callableOptional
config?: { context?: string | undefined; dry?: boolean | undefined; disableModeration?: boolean | "soft" | undefined; apiKey?: string | undefined; model?: string | undefined; temperature?: number | undefined; ... 8 more ...; function_call?: "none" | ... 2 more ... | undefined; }Optional
history?: { messages?: { role: "function" | "user" | "assistant" | "system"; content: string | null; id?: string | undefined; name?: string | undefined; function_call?: { name: string; arguments: Record<...>; } | undefined; model?: string | undefined; flags?: string[] | ... 1 more ... | undefined; }[] | undefined; }Optional
id?: stringOptional
pluginsOptional
requestOptional
plugins: ConversationPlugin<string, any, any>[]The plugins to use for the Conversation instance.
The new Conversation instance.
Generated using TypeDoc
A Conversation manages the messages sent to and from the OpenAI API and handles the logic for providing the message history to the API for each prompt.