Skip to content

feat: add prompt ab testing apis #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 75 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
} from './observability/generation';
import { Step, StepType } from './observability/step';
import { CleanThreadFields, Thread } from './observability/thread';
import { Prompt } from './prompt-engineering/prompt';
import { IPromptRollout, Prompt } from './prompt-engineering/prompt';
import {
Environment,
Maybe,
Expand Down Expand Up @@ -2080,4 +2080,78 @@ export class API {

return new Prompt(this, promptData);
}

/**
* Retrieves a prompt A/B testing rollout by its name.
*
* @param name - The name of the prompt to retrieve.
* @returns A list of prompt rollout versions.
*/
public async getPromptAbTesting(
name: string
): Promise<IPromptRollout[] | null> {
const query = `
query getPromptLineageRollout($projectId: String, $lineageName: String!) {
promptLineageRollout(projectId: $projectId, lineageName: $lineageName) {
pageInfo {
startCursor
endCursor
}
edges {
node {
version
rollout
}
}
}
}
`;

const variables = { lineageName: name };
const result = await this.makeGqlCall(query, variables);

if (!result.data || !result.data.promptLineageRollout) {
return null;
}

const response = result.data.promptLineageRollout;

return response.edges.map((x: any) => x.node);
}

/**
* Update a prompt A/B testing rollout by its name.
*
* @param name - The name of the prompt to retrieve.
* @param rollouts - A list of prompt rollout versions.
* @returns A list of prompt rollout versions.
*/
public async updatePromptAbTesting(name: string, rollouts: IPromptRollout[]) {
const mutation = `
mutation updatePromptLineageRollout(
$projectId: String
$name: String!
$rollouts: [PromptVersionRolloutInput!]!
) {
updatePromptLineageRollout(
projectId: $projectId
name: $name
rollouts: $rollouts
) {
ok
message
errorCode
}
}
`;

const variables = { name: name, rollouts };
const result = await this.makeGqlCall(mutation, variables);

if (!result.data || !result.data.updatePromptLineageRollout) {
return null;
}

return result.data.promptLineageRollout;
}
}
5 changes: 5 additions & 0 deletions src/prompt-engineering/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ class PromptFields extends Utils {
variables!: IPromptVariableDefinition[];
}

export interface IPromptRollout {
version: number;
rollout: number;
}

export type PromptConstructor = OmitUtils<PromptFields>;

export class Prompt extends PromptFields {
Expand Down
15 changes: 13 additions & 2 deletions tests/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ describe('End to end tests for the SDK', function () {
it('should format a prompt with default values', async () => {
const prompt = await client.api.getPrompt('Default');

const formatted = prompt!.format();
const formatted = prompt!.formatMessages();

const expected = `Hello, this is a test value and this

Expand All @@ -617,7 +617,7 @@ is a templated list.`;
it('should format a prompt with custom values', async () => {
const prompt = await client.api.getPrompt('Default');

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

const expected = `Hello, this is a Edited value and this

Expand All @@ -630,5 +630,16 @@ is a templated list.`;
expect(formatted.length).toBe(1);
expect(formatted[0].content).toBe(expected);
});

it('should get a prompt A/B testing configuration', async () => {
await client.api.updatePromptAbTesting('Default', [
{ version: 0, rollout: 100 }
]);
const rollouts = await client.api.getPromptAbTesting('Default');
expect(rollouts).not.toBeNull();
expect(rollouts?.length).toBe(1);
expect(rollouts![0].rollout).toBe(100);
expect(rollouts![0].version).toBe(0);
});
});
});
Loading