Skip to content

Commit

Permalink
edit prompt and label (#3071)
Browse files Browse the repository at this point in the history
* edit prompt and label

* edit prompt better
  • Loading branch information
kavinvalli authored Dec 19, 2024
1 parent a51d1db commit 5276f60
Show file tree
Hide file tree
Showing 23 changed files with 5,847 additions and 4,756 deletions.
475 changes: 268 additions & 207 deletions bifrost/lib/clients/jawnTypes/private.ts

Large diffs are not rendered by default.

517 changes: 289 additions & 228 deletions bifrost/lib/clients/jawnTypes/public.ts

Large diffs are not rendered by default.

2,303 changes: 1,228 additions & 1,075 deletions docs/swagger.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions helicone-cron/src/db/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ export type Database = {
parent_prompt_version: string | null
prompt_v2: string
soft_delete: boolean | null
updated_at: string
}
Insert: {
created_at?: string | null
Expand All @@ -1974,6 +1975,7 @@ export type Database = {
parent_prompt_version?: string | null
prompt_v2: string
soft_delete?: boolean | null
updated_at?: string
}
Update: {
created_at?: string | null
Expand All @@ -1988,6 +1990,7 @@ export type Database = {
parent_prompt_version?: string | null
prompt_v2?: string
soft_delete?: boolean | null
updated_at?: string
}
Relationships: [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
alter table "public"."prompts_versions" add column "updated_at" timestamp with time zone not null default now();


53 changes: 53 additions & 0 deletions valhalla/jawn/src/controllers/public/promptController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export interface PromptVersionResult extends PromptVersionResultBase {
metadata: Record<string, any>;
parent_prompt_version?: string | null;
experiment_id?: string | null;
updated_at?: string;
}

export interface PromptVersionResultCompiled extends PromptVersionResultBase {
Expand All @@ -123,6 +124,14 @@ export interface PromptCreateSubversionParams {
bumpForMajorPromptVersionId?: string;
}

export interface PromptEditSubversionLabelParams {
label: string;
}

export interface PromptEditSubversionTemplateParams {
heliconeTemplate: any;
}

export interface PromptInputRecord {
id: string;
inputs: Record<string, string>;
Expand Down Expand Up @@ -221,6 +230,50 @@ export class PromptController extends Controller {
return result;
}

@Post("version/{promptVersionId}/edit-label")
public async editPromptVersionLabel(
@Body()
requestBody: PromptEditSubversionLabelParams,
@Request() request: JawnAuthenticatedRequest,
@Path() promptVersionId: string
): Promise<Result<{ metadata: Record<string, any> }, string>> {
const promptManager = new PromptManager(request.authParams);

const result = await promptManager.editPromptVersionLabel(
promptVersionId,
requestBody
);
if (result.error || !result.data) {
console.log(result.error);
this.setStatus(500);
} else {
this.setStatus(201); // set return status 201
}
return result;
}

@Post("version/{promptVersionId}/edit-template")
public async editPromptVersionTemplate(
@Body()
requestBody: PromptEditSubversionTemplateParams,
@Request() request: JawnAuthenticatedRequest,
@Path() promptVersionId: string
): Promise<Result<null, string>> {
const promptManager = new PromptManager(request.authParams);

const result = await promptManager.editPromptVersionTemplate(
promptVersionId,
requestBody
);
if (result.error) {
console.log(result.error);
this.setStatus(500);
} else {
this.setStatus(201); // set return status 201
}
return result;
}

@Post("version/{promptVersionId}/subversion")
public async createSubversion(
@Body()
Expand Down
3 changes: 3 additions & 0 deletions valhalla/jawn/src/lib/db/database.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1960,6 +1960,7 @@ export type Database = {
parent_prompt_version: string | null
prompt_v2: string
soft_delete: boolean | null
updated_at: string
}
Insert: {
created_at?: string | null
Expand All @@ -1974,6 +1975,7 @@ export type Database = {
parent_prompt_version?: string | null
prompt_v2: string
soft_delete?: boolean | null
updated_at?: string
}
Update: {
created_at?: string | null
Expand All @@ -1988,6 +1990,7 @@ export type Database = {
parent_prompt_version?: string | null
prompt_v2?: string
soft_delete?: boolean | null
updated_at?: string
}
Relationships: [
{
Expand Down
74 changes: 68 additions & 6 deletions valhalla/jawn/src/managers/prompt/PromptManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// src/users/usersService.ts
import { autoFillInputs } from "@helicone/prompts";
import {
CreatePromptResponse,
PromptCreateSubversionParams,
PromptEditSubversionLabelParams,
PromptEditSubversionTemplateParams,
PromptQueryParams,
PromptResult,
PromptVersionResult,
Expand All @@ -10,16 +13,13 @@ import {
PromptsQueryParams,
PromptsResult,
} from "../../controllers/public/promptController";
import { Result, err, ok } from "../../lib/shared/result";
import { supabaseServer } from "../../lib/db/supabase";
import { dbExecute } from "../../lib/shared/db/dbExecute";
import { FilterNode } from "../../lib/shared/filters/filterDefs";
import { buildFilterPostgres } from "../../lib/shared/filters/filters";
import { resultMap } from "../../lib/shared/result";
import { User } from "../../models/user";
import { Result, err, ok, resultMap } from "../../lib/shared/result";
import { BaseManager } from "../BaseManager";
import { autoFillInputs } from "@helicone/prompts";
import { RequestManager } from "../request/RequestManager";
import { supabaseServer } from "../../lib/db/supabase";

export class PromptManager extends BaseManager {
async getOrCreatePromptVersionFromRequest(
Expand Down Expand Up @@ -183,6 +183,66 @@ export class PromptManager extends BaseManager {
return resultMap(result, (data) => data[0]);
}

async editPromptVersionTemplate(
promptVersionId: string,
params: PromptEditSubversionTemplateParams
): Promise<Result<null, string>> {
if (
params.heliconeTemplate &&
JSON.stringify(params.heliconeTemplate).length > 1_000_000_000
) {
return err("Helicone template too large");
}

const result = await dbExecute<PromptVersionResult>(
`
UPDATE prompts_versions
SET helicone_template = $1,
updated_at = now()
WHERE id = $2 AND organization = $3
`,
[params.heliconeTemplate, promptVersionId, this.authParams.organizationId]
);

if (result.error) {
return err(result.error);
}

return ok(null);
}

async editPromptVersionLabel(
promptVersionId: string,
params: PromptEditSubversionLabelParams
): Promise<Result<{ metadata: Record<string, any> }, string>> {
if (params.label.length > 100) {
return err("Label too long");
}

if (params.label.length === 0) {
return err("Label cannot be empty");
}

const result = await dbExecute<{
metadata: Record<string, any>;
}>(
`
UPDATE prompts_versions
SET metadata = jsonb_set(
COALESCE(metadata, '{}'::jsonb),
'{label}',
to_jsonb($1::text)
)
WHERE id = $2 AND organization = $3
RETURNING
metadata
`,
[params.label, promptVersionId, this.authParams.organizationId]
);

return resultMap(result, (data) => data[0]);
}

async promotePromptVersionToProduction(
promptVersionId: string,
previousProductionVersionId: string
Expand Down Expand Up @@ -285,6 +345,7 @@ export class PromptManager extends BaseManager {
metadata: Record<string, any>;
experiment_id?: string | null;
parent_prompt_version?: string | null;
updated_at: string;
}>(
`
SELECT
Expand All @@ -297,7 +358,8 @@ export class PromptManager extends BaseManager {
prompts_versions.created_at,
prompts_versions.metadata,
prompts_versions.experiment_id,
prompts_versions.parent_prompt_version
prompts_versions.parent_prompt_version,
prompts_versions.updated_at
FROM prompts_versions
left join prompt_v2 on prompt_v2.id = prompts_versions.prompt_v2
WHERE prompt_v2.organization = $1
Expand Down
Loading

0 comments on commit 5276f60

Please sign in to comment.