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

const ai = genkit({...});
const chat = ai.chat(); // create a Session
let response = await chat.send('hi'); // session/history aware conversation
response = await chat.send('tell me a story');

Type Parameters

  • S = any

Constructors

Properties

Accessors

Methods

Constructors

  • Type Parameters

    • S = any

    Parameters

    Returns Session<S>

Properties

id: string
registry: Registry

Accessors

  • get state(): undefined | S
  • Returns undefined | S

Methods

  • Create a chat session with the provided options.

    const session = ai.createSession({});
    const chat = session.chat({
    system: 'talk like a pirate',
    })
    let response = await chat.send('tell me a joke');
    response = await chat.send('another one');

    Type Parameters

    • I

    Parameters

    • Optionaloptions: ChatOptions<I, S>

    Returns Chat

  • Create a chat session with the provided preamble.

    const triageAgent = ai.definePrompt({
    system: 'help the user triage a problem',
    })
    const session = ai.createSession({});
    const chat = session.chat(triageAgent);
    const { text } = await chat.send('my phone feels hot');

    Type Parameters

    • I

    Parameters

    Returns Chat

  • Craete a separate chat conversation ("thread") within the given preamble.

    const session = ai.createSession({});
    const lawyerChat = session.chat('lawyerThread', {
    system: 'talk like a lawyer',
    });
    const pirateChat = session.chat('pirateThread', {
    system: 'talk like a pirate',
    });
    await lawyerChat.send('tell me a joke');
    await pirateChat.send('tell me a joke');

    Type Parameters

    • I

    Parameters

    Returns Chat

  • Craete a separate chat conversation ("thread").

    const session = ai.createSession({});
    const lawyerChat = session.chat('lawyerThread', {
    system: 'talk like a lawyer',
    });
    const pirateChat = session.chat('pirateThread', {
    system: 'talk like a pirate',
    });
    await lawyerChat.send('tell me a joke');
    await pirateChat.send('tell me a joke');

    Type Parameters

    • I

    Parameters

    • threadName: string
    • Optionaloptions: ChatOptions<I, S>

    Returns Chat

  • Executes provided function within this session context allowing calling ai.currentSession().state

    Type Parameters

    • O

    Parameters

    • fn: () => O

    Returns O

  • Update messages for a given thread.

    Parameters

    • thread: 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";
      }[]

    Returns Promise<void>

  • Update session state data.

    Parameters

    • data: S

    Returns Promise<void>