Interface for performing AI operations using LaunchDarkly.

interface LDAIClient {
    interpolateTemplate(template: string, variables: Record<string, unknown>): string;
    modelConfig<TDefault>(key: string, context: LDContext, defaultValue: TDefault, variables?: Record<string, unknown>): Promise<LDAIConfig>;
}

Methods

  • Parses and interpolates a template string with the provided variables.

    Parameters

    • template: string

      The template string to be parsed and interpolated.

    • variables: Record<string, unknown>

      An object containing the variables to be used for interpolation.

    Returns string

    The interpolated string.

  • Retrieves and processes an AI configuration based on the provided key, LaunchDarkly context, and variables. This includes the model configuration and the processed prompts.

    Type Parameters

    Parameters

    • key: string

      The key of the AI configuration.

    • context: LDContext

      The LaunchDarkly context object that contains relevant information about the current environment, user, or session. This context may influence how the configuration is processed or personalized.

    • defaultValue: TDefault

      A fallback value containing model configuration and prompts. This will be used if the configurationuration is not available from launchdarkly.

    • Optionalvariables: Record<string, unknown>

      A map of key-value pairs representing dynamic variables to be injected into the prompt template. The keys correspond to placeholders within the template, and the values are the corresponding replacements.

    Returns Promise<LDAIConfig>

    The AI configurationuration including a processed prompt after all variables have been substituted in the stored prompt template. This will also include a tracker used to track the state of the AI operation. If the configuration cannot be accessed from LaunchDarkly, then the return value will include information from the defaultValue.

    const key = "welcome_prompt";
    const context = {...};
    const variables = {username: 'john'};
    const defaultValue = {
    enabled: false,
    };

    const result = modelConfig(key, context, defaultValue, variables);
    // Output:
    {
    enabled: true,
    config: {
    modelId: "gpt-4o",
    temperature: 0.2,
    maxTokens: 4096,
    userDefinedKey: "myValue",
    },
    prompt: [
    {
    role: "system",
    content: "You are an amazing GPT."
    },
    {
    role: "user",
    content: "Explain how you're an amazing GPT."
    }
    ],
    tracker: ...
    }