Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.

Commit 4e203ba

Browse files
Willy/prompt variant (#80)
* feat: add prompt variant * fix: return desc * fix: remove .only * chore: bump version to 0.1.0 --------- Co-authored-by: Hugues de Saxcé <[email protected]>
1 parent 670d457 commit 4e203ba

File tree

4 files changed

+108
-7
lines changed

4 files changed

+108
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@literalai/client",
3-
"version": "0.0.602",
3+
"version": "0.1.0",
44
"description": "",
55
"exports": {
66
".": {

src/api.ts

Lines changed: 91 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,65 @@ export class API {
18281828
return Object.values(result.data).map((x: any) => new DatasetItem(x));
18291829
}
18301830

1831+
/**
1832+
* Creates a prompt variation for an experiment.
1833+
* This variation is not an official version until manually saved.
1834+
*
1835+
* @param name The name of the prompt to retrieve or create.
1836+
* @param templateMessages A list of template messages for the prompt.
1837+
* @param settings Optional settings for the prompt.
1838+
* @param tools Optional tools for the prompt.
1839+
* @returns The prompt variant id to link with the experiment.
1840+
*/
1841+
public async createPromptVariant(
1842+
name: string,
1843+
templateMessages: IGenerationMessage[],
1844+
settings?: Maybe<Record<string, any>>,
1845+
tools?: Maybe<Record<string, any>>
1846+
): Promise<string | undefined> {
1847+
const mutation = `mutation createPromptExperiment(
1848+
$fromLineageId: String
1849+
$fromVersion: Int
1850+
$scoreTemplateId: String
1851+
$templateMessages: Json
1852+
$settings: Json
1853+
$tools: Json
1854+
$variables: Json
1855+
) {
1856+
createPromptExperiment(
1857+
fromLineageId: $fromLineageId
1858+
fromVersion: $fromVersion
1859+
scoreTemplateId: $scoreTemplateId
1860+
templateMessages: $templateMessages
1861+
settings: $settings
1862+
tools: $tools
1863+
variables: $variables
1864+
) {
1865+
id
1866+
fromLineageId
1867+
fromVersion
1868+
scoreTemplateId
1869+
projectId
1870+
projectUserId
1871+
tools
1872+
settings
1873+
variables
1874+
templateMessages
1875+
}
1876+
}
1877+
`;
1878+
1879+
const lineage = await this.getPromptLineageByName(name);
1880+
const result = await this.makeGqlCall(mutation, {
1881+
fromLineageId: lineage?.id,
1882+
templateMessages,
1883+
settings,
1884+
tools
1885+
});
1886+
1887+
return result.data.createPromptExperiment?.id;
1888+
}
1889+
18311890
/**
18321891
* Creates a new dataset experiment.
18331892
* @param datasetExperiment
@@ -1840,20 +1899,20 @@ export class API {
18401899
public async createExperiment(datasetExperiment: {
18411900
name: string;
18421901
datasetId?: string;
1843-
promptId?: string;
1902+
promptVariantId?: string;
18441903
params?: Record<string, any> | Array<Record<string, any>>;
18451904
}) {
18461905
const query = `
1847-
mutation CreateDatasetExperiment($name: String!, $datasetId: String $promptId: String, $params: Json) {
1848-
createDatasetExperiment(name: $name, datasetId: $datasetId, promptId: $promptId, params: $params) {
1906+
mutation CreateDatasetExperiment($name: String!, $datasetId: String, $promptExperimentId: String, $params: Json) {
1907+
createDatasetExperiment(name: $name, datasetId: $datasetId, promptExperimentId: $promptExperimentId, params: $params) {
18491908
id
18501909
}
18511910
}
18521911
`;
18531912
const datasetExperimentInput = {
18541913
name: datasetExperiment.name,
18551914
datasetId: datasetExperiment.datasetId,
1856-
promptId: datasetExperiment.promptId,
1915+
promptExperimentId: datasetExperiment.promptVariantId,
18571916
params: datasetExperiment.params
18581917
};
18591918
const result = await this.makeGqlCall(query, datasetExperimentInput);
@@ -1947,6 +2006,34 @@ export class API {
19472006
return result.data.createPromptLineage;
19482007
}
19492008

2009+
/**
2010+
* Get an existing prompt lineage by name.
2011+
*
2012+
* @param name - The name of the prompt lineage. This parameter is required.
2013+
* @returns The existing prompt lineage object, or null.
2014+
*/
2015+
public async getPromptLineageByName(name: string) {
2016+
const query = `query promptLineage(
2017+
$name: String!
2018+
) {
2019+
promptLineage(
2020+
name: $name
2021+
) {
2022+
id
2023+
}
2024+
}`;
2025+
2026+
const result = await this.makeGqlCall(query, {
2027+
name
2028+
});
2029+
2030+
if (!result.data || !result.data.promptLineage) {
2031+
return null;
2032+
}
2033+
2034+
return result.data.promptLineage;
2035+
}
2036+
19502037
/**
19512038
* @deprecated Please use getOrCreatePrompt instead.
19522039
*/

src/evaluation/dataset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ export class Dataset extends DatasetFields {
9696
*/
9797
async createExperiment(experiment: {
9898
name: string;
99-
promptId?: string;
99+
promptVariantId?: string;
100100
params?: Record<string, any> | Array<Record<string, any>>;
101101
}) {
102102
const datasetExperiment = await this.api.createExperiment({
103103
name: experiment.name,
104104
datasetId: this.id,
105-
promptId: experiment.promptId,
105+
promptVariantId: experiment.promptVariantId,
106106
params: experiment.params
107107
});
108108
return new DatasetExperiment(this.api, datasetExperiment);

tests/api.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,20 @@ describe('End to end tests for the SDK', function () {
448448
expect(experiment.id).not.toBeNull();
449449
dataset.delete();
450450
});
451+
452+
it('should create a dataset experiment with a prompt variant', async () => {
453+
const promptVariantId = await client.api.createPromptVariant(
454+
'Default',
455+
[{ role: 'user', content: 'hello' }],
456+
{ temperature: 0.5 }
457+
);
458+
const experiment = await client.api.createExperiment({
459+
name: `test_${uuidv4()}`,
460+
promptVariantId: promptVariantId
461+
});
462+
expect(promptVariantId).toBeDefined();
463+
expect(experiment.id).not.toBeNull();
464+
});
451465
});
452466

453467
describe('dataset item api', () => {

0 commit comments

Comments
 (0)