Skip to content
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

Feedback score #2355

Merged
merged 8 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
95 changes: 89 additions & 6 deletions valhalla/jawn/src/lib/stores/ScoreStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,14 @@ export type Score = {
score_attribute_key: string;
score_attribute_type: string;
score_attribute_value: number;
}
};

export class ScoreStore extends BaseStore {
constructor(organizationId: string) {
super(organizationId);
}

public async putScoresIntoSupabase(
requestId: string,
scores: Score[]
) {
public async putScoresIntoSupabase(requestId: string, scores: Score[]) {
try {
const scoreKeys = scores.map((score) => score.score_attribute_key);
const scoreTypes = scores.map((score) => score.score_attribute_type);
Expand Down Expand Up @@ -51,7 +48,7 @@ export class ScoreStore extends BaseStore {
SELECT id, score_key
FROM upserted_attributes;
`;

const { data: upsertedAttributes, error: upsertError } = await dbExecute(
upsertQuery,
[scoreKeys, scoreTypes, organizationIds]
Expand Down Expand Up @@ -169,6 +166,92 @@ export class ScoreStore extends BaseStore {
return ok(rowContents.data);
}

public async putScoreIntoClickhouse(newVersion: {
id: string;
version: number;
provider: string;
score: Score;
}): Promise<Result<InsertRequestResponseVersioned, string>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just reuse the putScoresIntoClickhouse function here? We are copying the entire implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chitalian make sense! fixed this

let rowContents = resultMap(
await clickhouseDb.dbQuery<InsertRequestResponseVersioned>(
`
SELECT *
FROM request_response_versioned
WHERE request_id = {val_0: UUID}
AND version = {val_1: UInt64}
AND organization_id = {val_2: String}
AND provider = {val_3: String}
`,
[
newVersion.id,
newVersion.version - 1,
this.organizationId,
newVersion.provider,
]
),
(x) => x[0]
);

if (rowContents.error) {
return rowContents;
}
if (!rowContents.data) {
rowContents = resultMap(
await clickhouseDb.dbQuery<InsertRequestResponseVersioned>(
`
SELECT *
FROM request_response_versioned
WHERE request_id = {val_0: UUID}
AND organization_id = {val_1: String}
AND provider = {val_2: String}
ORDER BY version DESC
LIMIT 1
`,
[newVersion.id, this.organizationId, newVersion.provider]
),
(x) => x[0]
);
}

if (rowContents.error || !rowContents.data) {
return err("Could not find previous version of request");
}

const res = await clickhouseDb.dbInsertClickhouse(
"request_response_versioned",
[
// Delete the previous version
{
sign: -1,
version: rowContents.data.version,
request_id: newVersion.id,
organization_id: this.organizationId,
provider: newVersion.provider,
model: rowContents.data.model,
request_created_at: rowContents.data.request_created_at,
},
// Insert the new version
{
...rowContents.data,
sign: 1,
version: newVersion.version,
scores: {
...rowContents.data.scores,
...{
[newVersion.score.score_attribute_key]:
newVersion.score.score_attribute_value,
},
},
},
]
);
if (res.error) {
return err(res.error);
}

return ok(rowContents.data);
}

public async bumpRequestVersion(requestId: string) {
return await dbExecute<{
id: string;
Expand Down
16 changes: 16 additions & 0 deletions valhalla/jawn/src/managers/request/RequestManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "../../lib/stores/request/request";
import { costOfPrompt } from "../../packages/cost";
import { BaseManager } from "../BaseManager";
import { ScoreManager } from "../score/ScoreManager";

export class RequestManager extends BaseManager {
private versionedRequestStore: VersionedRequestStore;
Expand Down Expand Up @@ -147,6 +148,21 @@ export class RequestManager extends BaseManager {
return err(feedbackResult.error.message);
}

const scoreManager = new ScoreManager(this.authParams);
const feedbackScoreResult = await scoreManager.addSignleScoreToClickhouse(
requestId,
{
score_attribute_key: "helicone-score-feedback",
score_attribute_type: "number",
score_attribute_value: feedback ? 1 : 0,
}
);

if (feedbackScoreResult.error) {
console.error("Error upserting feedback:", feedbackScoreResult.error);
return err(feedbackScoreResult.error);
}

return ok(null);
}

Expand Down
28 changes: 27 additions & 1 deletion valhalla/jawn/src/managers/score/ScoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,38 @@ export class ScoreManager extends BaseManager {
}
}

public async addSignleScoreToClickhouse(
requestId: string,
mappedScores: Score
): Promise<Result<string, string>> {
const request = await this.scoreStore.bumpRequestVersion(requestId);

if (request.error || !request.data) {
return err(request.error);
}

if (request.data.length === 0) {
return err(`Request not found: ${requestId}`);
}

const requestInClickhouse = await this.scoreStore.putScoreIntoClickhouse({
...request.data[0],
score: mappedScores,
});

if (requestInClickhouse.error || !requestInClickhouse.data) {
return requestInClickhouse;
}
return { data: "Scores added to Clickhouse successfully", error: null };
}

private mapScores(scores: Scores): Score[] {
return Object.entries(scores).map(([key, value]) => {
return {
score_attribute_key: key,
score_attribute_type: typeof value === "boolean" ? "boolean" : "number",
score_attribute_value: typeof value === "boolean" ? (value ? 1 : 0) : value,
score_attribute_value:
typeof value === "boolean" ? (value ? 1 : 0) : value,
};
});
}
Expand Down
1 change: 0 additions & 1 deletion web/components/templates/playground/playgroundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ export default PlaygroundPage;
interface PlaygroundPageProps {
showNewButton?: boolean;
request?: string;
showNewButton: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chitalian Removed duplicate showNewButton from @YashKarthik's PR

}

export type PlaygroundModel = {
Expand Down
Loading