Chat encapsulates a statful execution environment for chat. Chat session executed within a session in this environment will have acesss to session convesation history.

const ai = genkit({...});
const chat = ai.chat(); // create a Chat
let response = await chat.send('hi, my name is Genkit');
response = await chat.send('what is my name?'); // chat history aware conversation

Constructors

Properties

Accessors

Methods

Constructors

  • Parameters

    • session: Session<any>
    • requestBase: Promise<BaseGenerateOptions<ZodTypeAny, ZodTypeAny>>
    • options: {
          id: string;
          messages?: {
              content: (
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text: string;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media: { contentType?: string; url: string };
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest: { input?: unknown; name: string; ref?: string };
                      toolResponse?: undefined;
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse: { name: string; output?: unknown; ref?: string };
                  }
                  | {
                      custom?: Record<string, unknown>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
                  | {
                      custom: Record<string, any>;
                      data?: unknown;
                      media?: undefined;
                      metadata?: Record<string, unknown>;
                      text?: undefined;
                      toolRequest?: undefined;
                      toolResponse?: undefined;
                  }
              )[];
              metadata?: Record<string, unknown>;
              role: "user" | "model" | "tool" | "system";
          }[];
          thread: string;
      }

    Returns Chat

Properties

session: Session<any>
sessionId: string

Accessors

  • get messages(): {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }[]
  • Returns {
        content: (
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text: string;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media: { contentType?: string; url: string };
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest: { input?: unknown; name: string; ref?: string };
                toolResponse?: undefined;
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse: { name: string; output?: unknown; ref?: string };
            }
            | {
                custom?: Record<string, unknown>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
            | {
                custom: Record<string, any>;
                data?: unknown;
                media?: undefined;
                metadata?: Record<string, unknown>;
                text?: undefined;
                toolRequest?: undefined;
                toolResponse?: undefined;
            }
        )[];
        metadata?: Record<string, unknown>;
        role: "user" | "model" | "tool" | "system";
    }[]

Methods

  • Type Parameters

    • O extends ZodType<any, any, any, O> = ZodTypeAny
    • CustomOptions extends ZodType<any, any, any, CustomOptions> = ZodObject<
          {
              maxOutputTokens: ZodOptional<ZodNumber>;
              stopSequences: ZodOptional<ZodArray<ZodString, "many">>;
              temperature: ZodOptional<ZodNumber>;
              topK: ZodOptional<ZodNumber>;
              topP: ZodOptional<ZodNumber>;
              version: ZodOptional<ZodString>;
          },
          "strip",
          ZodTypeAny,
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
      >

    Parameters

    • options:
          | string
          | (
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text: string;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media: { contentType?: string; url: string };
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest: { input?: unknown; name: string; ref?: string };
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse: { name: string; output?: unknown; ref?: string };
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom: Record<string, any>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
          )[]
          | ChatGenerateOptions<O, CustomOptions>

    Returns Promise<GenerateResponse<TypeOf<O>>>

  • Type Parameters

    • O extends ZodType<any, any, any, O> = ZodTypeAny
    • CustomOptions extends ZodType<any, any, any, CustomOptions> = ZodObject<
          {
              maxOutputTokens: ZodOptional<ZodNumber>;
              stopSequences: ZodOptional<ZodArray<ZodString, "many">>;
              temperature: ZodOptional<ZodNumber>;
              topK: ZodOptional<ZodNumber>;
              topP: ZodOptional<ZodNumber>;
              version: ZodOptional<ZodString>;
          },
          "strip",
          ZodTypeAny,
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
          {
              maxOutputTokens?: number;
              stopSequences?: string[];
              temperature?: number;
              topK?: number;
              topP?: number;
              version?: string;
          },
      >

    Parameters

    • options:
          | string
          | (
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text: string;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media: { contentType?: string; url: string };
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest: { input?: unknown; name: string; ref?: string };
                  toolResponse?: undefined;
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse: { name: string; output?: unknown; ref?: string };
              }
              | {
                  custom?: Record<string, unknown>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
              | {
                  custom: Record<string, any>;
                  data?: unknown;
                  media?: undefined;
                  metadata?: Record<string, unknown>;
                  text?: undefined;
                  toolRequest?: undefined;
                  toolResponse?: undefined;
              }
          )[]
          | GenerateStreamOptions<O, CustomOptions>

    Returns Promise<GenerateStreamResponse<TypeOf<O>>>