Class Conversation<TOptions>

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.

Type Parameters

Hierarchy

  • Conversation

Constructors

Properties

chatCompletionService: ChatCompletionService
id: string

A UUID generated by the library for this conversation. Not the same as the conversation ID returned by the OpenAI API.

plugins: ConversationPlugins<PluginsFromConversationOptions<TOptions>, ConversationPluginDefinitionFromPlugin<PluginsFromConversationOptions<TOptions>[number]>, string & {} | PluginsFromConversationOptions<TOptions>[number]["name"]>
globalPlugins: ConversationPlugin<string, any, any>[] = []

Plugins that will be used for all conversations.

Remarks

Only applies to conversations created after this property is set. Previous conversations will not be affected.

Example

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;

Methods

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

    Type Parameters

    • T = any

    Parameters

    • name: string

      The name of the function used to generate the result. This function must be defined in the functions config option.

    • result: T

      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: PromptOptions

      Additional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.

    • Optional requestOptions: {
          headers?: Record<string, string>;
          proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; };
      }

      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; }

    Returns Promise<Message>

    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.

    Parameters

    • Rest ...args: [options: PromptOptions, requestOptions: {
          headers?: Record<string, string>;
          proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; };
      }]

    Returns Promise<Message>

    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.

    Remarks

    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.

    Parameters

    • prompt: string

      The prompt to send to the assistant.

    • Optional options: PromptOptions

      Additional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.

    • Optional requestOptions: {
          headers?: Record<string, string>;
          proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; };
      }

      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; }

    Returns Promise<Message>

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

    Parameters

    • fromMessageOrId: string | Message

      The message to re-prompt from. This can be either a message ID or a Message instance.

    • Optional newPrompt: string

      The new prompt to use for the previous user message. If not provided, the previous user's message content will be reused.

    • Optional options: PromptOptions

      Additional options to pass to the Create Chat Completion API endpoint. This overrides the config passed to the constructor.

    • Optional requestOptions: {
          headers?: Record<string, string>;
          proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; };
      }

      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; }

    Returns Promise<Message>

    The assistant's response as a Message instance.

    Example

    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.

    Returns {
        callableFunctions?: { functions?: { name: string; id?: string | undefined; description?: string | undefined; parameters?: { type: "object"; title?: string | undefined; description?: string | undefined; default?: any; ... 11 more ...; additionalProperties?: boolean | ... 1 more ... | undefined; } | undefined; }[] | undefined; };
        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; };
        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; };
        id?: string;
        pluginsData?: Record<string, any>;
        requestOptions?: { headers?: Record<string, string> | undefined; proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; } | undefined; };
    }

    A JSON representation of the Conversation instance.

    • Optional callableFunctions?: { functions?: { name: string; id?: string | undefined; description?: string | undefined; parameters?: { type: "object"; title?: string | undefined; description?: string | undefined; default?: any; ... 11 more ...; additionalProperties?: boolean | ... 1 more ... | undefined; } | undefined; }[] | undefined; }
    • Optional 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?: string
    • Optional pluginsData?: Record<string, any>
    • Optional requestOptions?: { headers?: Record<string, string> | undefined; proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; } | undefined; }
  • Creates a new Conversation instance from a JSON object.

    Parameters

    • json: {
          callableFunctions?: { functions?: { name: string; id?: string | undefined; description?: string | undefined; parameters?: { type: "object"; title?: string | undefined; description?: string | undefined; default?: any; ... 11 more ...; additionalProperties?: boolean | ... 1 more ... | undefined; } | undefined; }[] | undefined; };
          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; };
          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; };
          id?: string;
          pluginsData?: Record<string, any>;
          requestOptions?: { headers?: Record<string, string> | undefined; proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; } | undefined; };
      }

      The JSON object of the Conversation instance.

      • Optional callableFunctions?: { functions?: { name: string; id?: string | undefined; description?: string | undefined; parameters?: { type: "object"; title?: string | undefined; description?: string | undefined; default?: any; ... 11 more ...; additionalProperties?: boolean | ... 1 more ... | undefined; } | undefined; }[] | undefined; }
      • Optional 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?: string
      • Optional pluginsData?: Record<string, any>
      • Optional requestOptions?: { headers?: Record<string, string> | undefined; proxy?: { host: string; port?: number | undefined; protocol?: "http" | "https" | undefined; auth?: { username: string; password: string; } | undefined; } | undefined; }
    • Optional plugins: ConversationPlugin<string, any, any>[]

      The plugins to use for the Conversation instance.

    Returns Conversation<ConversationOptions>

    The new Conversation instance.

Generated using TypeDoc