Class Message

A message in a Conversation.

Hierarchy

  • Message

Constructors

Properties

_content: null | string
_flags: null | string[] = null
_functionCall: undefined | {
    arguments: Record<string, any>;
    name: string;
}

Type declaration

  • arguments: Record<string, any>
  • name: string
_isStreaming: boolean = false
_model: string
_name: undefined | string
id: string = ...

A UUID generated for this message by the library.

messageContentStreamEvents: EventManager<MessageContentStreamListener> = ...
messageStreamingEvents: EventManager<MessageStreamingListener> = ...
messageStreamingStartEvents: EventManager<MessageStreamingStartListener> = ...
messageStreamingStopEvents: EventManager<MessageStreamingStopListener> = ...
messageUpdateEvents: EventManager<MessageUpdateListener> = ...

Accessors

  • get functionCall(): undefined | {
        arguments: Record<string, any>;
        name: string;
    }
  • Returns undefined | {
        arguments: Record<string, any>;
        name: string;
    }

  • set functionCall(functionCall): void
  • Parameters

    • functionCall: undefined | {
          arguments: Record<string, any>;
          name: string;
      }

    Returns void

  • get isFlagged(): boolean
  • Whether the message is flagged by OpenAI's moderation API. Always false unless moderate has been called.

    Returns boolean

Methods

  • Call the OpenAI moderation API to check if the message is flagged. Only called once for the same content.

    Parameters

    • apiKey: string

      The OpenAI API key

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

      The request options to pass to fetch

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

    Returns Promise<string[]>

    The flags applied on the message

  • Removes a message streaming content listener, previously set with onContentStream.

    Parameters

    Returns void

    Remarks

    You do not need to call this method manually, as this listener automatically unsubscribes all listeners when streaming ends. You should use this if you want to unsubscribe a listener before streaming ends.

  • Adds a listener that is fired whenever the message content is updated during streaming. Also fires when streaming starts/ends.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

    Remarks

    Unlike the other listeners which behave like normal event listeners, this special listener is unsubscribed automatically when streaming ends. If this is not desired, use onUpdate and onStreamingStart/onStreamingStop instead.

  • Adds a listener for message streaming state changes.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Add a listener for message content changes.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Adds a listener that is fired only once whenever the message content is updated during streaming. Also fires when streaming starts/ends.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

    Remarks

    Unlike the other listeners which behave like normal event listeners, this special listener is unsubscribed automatically when streaming ends, regardless of whether it was called once or not. If this is not desired, use onceUpdate and onceStreamingStart/onceStreamingStop instead.

  • Adds a listener that is called only once when the message streaming starts.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Adds a listener that is called only once when the message streaming stops.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Adds a listener that is called only once when the message streaming state changes.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Add a listener that is called only once when the message content changes.

    Parameters

    Returns (() => void)

    An unsubscribe function for this listener

      • (): void
      • Returns void

  • Progessively adds content to the message from a streamed response type.

    Parameters

    • response: ReadableStream<any>

      The ReadableStream from the OpenAI API

    Returns Promise<void>

  • Serializes the message to JSON.

    Returns {
        content: null | string;
        flags?: null | string[];
        function_call?: { name: string; arguments: Record<string, any>; };
        id?: string;
        model?: string;
        name?: string;
        role: "function" | "user" | "assistant" | "system";
    }

    The Message as a JSON object.

    • content: null | string
    • Optional flags?: null | string[]
    • Optional function_call?: { name: string; arguments: Record<string, any>; }
    • Optional id?: string
    • Optional model?: string
    • Optional name?: string
    • role: "function" | "user" | "assistant" | "system"
  • Creates a new Message instance from a serialized message.

    Parameters

    • json: {
          content: null | string;
          flags?: null | string[];
          function_call?: { name: string; arguments: Record<string, any>; };
          id?: string;
          model?: string;
          name?: string;
          role: "function" | "user" | "assistant" | "system";
      }

      The JSON object of the Message instance.

      • content: null | string
      • Optional flags?: null | string[]
      • Optional function_call?: { name: string; arguments: Record<string, any>; }
      • Optional id?: string
      • Optional model?: string
      • Optional name?: string
      • role: "function" | "user" | "assistant" | "system"

    Returns Message

    A new Message instance

Generated using TypeDoc