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

Commit 2f78319

Browse files
Merge pull request #53 from Chainlit/willy/eng-1732-remove-promote-endpoint-and-add-promptlineage-blend-change
feat: add prompt ab testing apis
2 parents adb4d3c + 3d8a068 commit 2f78319

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

src/api.ts

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
} from './observability/generation';
3333
import { Step, StepType } from './observability/step';
3434
import { CleanThreadFields, Thread } from './observability/thread';
35-
import { Prompt } from './prompt-engineering/prompt';
35+
import { IPromptRollout, Prompt } from './prompt-engineering/prompt';
3636
import {
3737
Environment,
3838
Maybe,
@@ -2080,4 +2080,78 @@ export class API {
20802080

20812081
return new Prompt(this, promptData);
20822082
}
2083+
2084+
/**
2085+
* Retrieves a prompt A/B testing rollout by its name.
2086+
*
2087+
* @param name - The name of the prompt to retrieve.
2088+
* @returns A list of prompt rollout versions.
2089+
*/
2090+
public async getPromptAbTesting(
2091+
name: string
2092+
): Promise<IPromptRollout[] | null> {
2093+
const query = `
2094+
query getPromptLineageRollout($projectId: String, $lineageName: String!) {
2095+
promptLineageRollout(projectId: $projectId, lineageName: $lineageName) {
2096+
pageInfo {
2097+
startCursor
2098+
endCursor
2099+
}
2100+
edges {
2101+
node {
2102+
version
2103+
rollout
2104+
}
2105+
}
2106+
}
2107+
}
2108+
`;
2109+
2110+
const variables = { lineageName: name };
2111+
const result = await this.makeGqlCall(query, variables);
2112+
2113+
if (!result.data || !result.data.promptLineageRollout) {
2114+
return null;
2115+
}
2116+
2117+
const response = result.data.promptLineageRollout;
2118+
2119+
return response.edges.map((x: any) => x.node);
2120+
}
2121+
2122+
/**
2123+
* Update a prompt A/B testing rollout by its name.
2124+
*
2125+
* @param name - The name of the prompt to retrieve.
2126+
* @param rollouts - A list of prompt rollout versions.
2127+
* @returns A list of prompt rollout versions.
2128+
*/
2129+
public async updatePromptAbTesting(name: string, rollouts: IPromptRollout[]) {
2130+
const mutation = `
2131+
mutation updatePromptLineageRollout(
2132+
$projectId: String
2133+
$name: String!
2134+
$rollouts: [PromptVersionRolloutInput!]!
2135+
) {
2136+
updatePromptLineageRollout(
2137+
projectId: $projectId
2138+
name: $name
2139+
rollouts: $rollouts
2140+
) {
2141+
ok
2142+
message
2143+
errorCode
2144+
}
2145+
}
2146+
`;
2147+
2148+
const variables = { name: name, rollouts };
2149+
const result = await this.makeGqlCall(mutation, variables);
2150+
2151+
if (!result.data || !result.data.updatePromptLineageRollout) {
2152+
return null;
2153+
}
2154+
2155+
return result.data.promptLineageRollout;
2156+
}
20832157
}

src/prompt-engineering/prompt.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class PromptFields extends Utils {
4646
variables!: IPromptVariableDefinition[];
4747
}
4848

49+
export interface IPromptRollout {
50+
version: number;
51+
rollout: number;
52+
}
53+
4954
export type PromptConstructor = OmitUtils<PromptFields>;
5055

5156
export class Prompt extends PromptFields {

tests/api.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ describe('End to end tests for the SDK', function () {
600600
it('should format a prompt with default values', async () => {
601601
const prompt = await client.api.getPrompt('Default');
602602

603-
const formatted = prompt!.format();
603+
const formatted = prompt!.formatMessages();
604604

605605
const expected = `Hello, this is a test value and this
606606
@@ -617,7 +617,7 @@ is a templated list.`;
617617
it('should format a prompt with custom values', async () => {
618618
const prompt = await client.api.getPrompt('Default');
619619

620-
const formatted = prompt!.format({ test_var: 'Edited value' });
620+
const formatted = prompt!.formatMessages({ test_var: 'Edited value' });
621621

622622
const expected = `Hello, this is a Edited value and this
623623
@@ -630,5 +630,16 @@ is a templated list.`;
630630
expect(formatted.length).toBe(1);
631631
expect(formatted[0].content).toBe(expected);
632632
});
633+
634+
it('should get a prompt A/B testing configuration', async () => {
635+
await client.api.updatePromptAbTesting('Default', [
636+
{ version: 0, rollout: 100 }
637+
]);
638+
const rollouts = await client.api.getPromptAbTesting('Default');
639+
expect(rollouts).not.toBeNull();
640+
expect(rollouts?.length).toBe(1);
641+
expect(rollouts![0].rollout).toBe(100);
642+
expect(rollouts![0].version).toBe(0);
643+
});
633644
});
634645
});

0 commit comments

Comments
 (0)