-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6804f0
commit 6106c82
Showing
3 changed files
with
72 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters