Skip to content

Commit

Permalink
Refactoring and cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Nov 4, 2024
1 parent b6804f0 commit 6106c82
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 61 deletions.
67 changes: 67 additions & 0 deletions packages/sdk/ai/src/LDAIClientImpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import Mustache from 'mustache';

import { LDClient, LDContext } from '@launchdarkly/node-server-sdk';

import { LDAIClient } from './api/AIClient';
import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config';
import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl';

/**
* Metadata assorted with a model configuration variation.
*/
interface LDMeta {
versionId: string;
}

/**
* Interface for the model configuration variation returned by LaunchDarkly. This is the internal
* typing and not meant for exposure to the application developer.
*/
interface VariationContent {
model?: LDModelConfig;
prompt?: LDPrompt[];
_ldMeta?: LDMeta;
}

export class AIClientImpl implements LDAIClient {
private _ldClient: LDClient;

constructor(ldClient: LDClient) {
this._ldClient = ldClient;
}

interpolateTemplate(template: string, variables: Record<string, unknown>): string {
return Mustache.render(template, variables, undefined, { escape: (item: any) => item });
}

async modelConfig<TDefault extends LDGenerationConfig>(
key: string,
context: LDContext,
defaultValue: TDefault,
variables?: Record<string, unknown>,
): Promise<LDAIConfig> {
const value: VariationContent = await this._ldClient.variation(key, context, defaultValue);
const config: LDGenerationConfig = { ...value };
const allVariables = { ldctx: context, ...variables };

if (value.prompt) {
config.prompt = value.prompt.map((entry: any) => ({
...entry,
content: this.interpolateTemplate(entry.content, allVariables),
}));
}

return {
config,
// eslint-disable-next-line no-underscore-dangle
tracker: new LDAIConfigTrackerImpl(
this._ldClient,
key,
// eslint-disable-next-line no-underscore-dangle
value._ldMeta?.versionId ?? '',
context,
),
noConfiguration: Object.keys(value).length === 0,
};
}
}
2 changes: 1 addition & 1 deletion packages/sdk/ai/src/api/AIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { LDAIConfig, LDGenerationConfig } from './config/LDAIConfig';
* Interface for performing AI operations using LaunchDarkly.
*/

export interface AIClient {
export interface LDAIClient {
/**
* Parses and interpolates a template string with the provided variables.
*
Expand Down
64 changes: 4 additions & 60 deletions packages/sdk/ai/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,14 @@
import Mustache from 'mustache';
import { LDClient } from '@launchdarkly/node-server-sdk';

import { LDClient, LDContext } from '@launchdarkly/node-server-sdk';

import { AIClient } from './api/AIClient';
import { LDAIConfig, LDGenerationConfig, LDModelConfig, LDPrompt } from './api/config';
import { LDAIConfigTrackerImpl } from './LDAIConfigTrackerImpl';

interface LDMeta {
versionId: string;
}

interface VariationContent {
model?: LDModelConfig;
prompt?: LDPrompt[];
_ldMeta?: LDMeta;
}

export class AIClientImpl implements AIClient {
private _ldClient: LDClient;

constructor(ldClient: LDClient) {
this._ldClient = ldClient;
}

interpolateTemplate(template: string, variables: Record<string, unknown>): string {
return Mustache.render(template, variables, undefined, { escape: (item: any) => item });
}

async modelConfig<TDefault extends LDGenerationConfig>(
key: string,
context: LDContext,
defaultValue: TDefault,
variables?: Record<string, unknown>,
): Promise<LDAIConfig> {
const value: VariationContent = await this._ldClient.variation(key, context, defaultValue);
const config: LDGenerationConfig = { ...value };
const allVariables = { ldctx: context, ...variables };

if (value.prompt) {
config.prompt = value.prompt.map((entry: any) => ({
...entry,
content: this.interpolateTemplate(entry.content, allVariables),
}));
}

return {
config,
// eslint-disable-next-line no-underscore-dangle
tracker: new LDAIConfigTrackerImpl(
this._ldClient,
key,
// eslint-disable-next-line no-underscore-dangle
value._ldMeta?.versionId ?? '',
context,
),
noConfiguration: Object.keys(value).length === 0,
};
}
}
import { LDAIClient } from './api/AIClient';
import { AIClientImpl } from './LDAIClientImpl';

/**
* Initialize a new AI client. This client will be used to perform any AI operations.
* @param ldClient The base LaunchDarkly client.
* @returns A new AI client.
*/
export function initAi(ldClient: LDClient): AIClient {
export function initAi(ldClient: LDClient): LDAIClient {
return new AIClientImpl(ldClient);
}

Expand Down

0 comments on commit 6106c82

Please sign in to comment.