Skip to content

Commit

Permalink
Feedback score (#2355)
Browse files Browse the repository at this point in the history
  • Loading branch information
maamalama authored Aug 5, 2024
1 parent 63dd707 commit ccf94a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
20 changes: 10 additions & 10 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 @@ -155,10 +152,13 @@ export class ScoreStore extends BaseStore {
...rowContents.data,
sign: 1,
version: newVersion.version,
scores: newVersion.scores.reduce((acc, score) => {
acc[score.score_attribute_key] = score.score_attribute_value;
return acc;
}, {} as Record<string, number>),
scores: {
...rowContents.data.scores,
...newVersion.scores.reduce((acc, score) => {
acc[score.score_attribute_key] = score.score_attribute_value;
return acc;
}, {} as Record<string, number>),
},
},
]
);
Expand Down
18 changes: 18 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,23 @@ export class RequestManager extends BaseManager {
return err(feedbackResult.error.message);
}

const scoreManager = new ScoreManager(this.authParams);
const feedbackScoreResult = await scoreManager.addScoresToClickhouse(
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: 24 additions & 4 deletions valhalla/jawn/src/managers/score/ScoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ export class ScoreManager extends BaseManager {
return err(supabaseRequest.error);
}

const requestInClickhouse = await this.addScoresToClickhouse(
requestId,
mappedScores
);

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

return { data: "Scores added to Clickhouse successfully", error: null };
} catch (error: any) {
return err(error.message);
}
}

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

if (request.error || !request.data) {
Expand All @@ -61,10 +81,9 @@ export class ScoreManager extends BaseManager {
if (requestInClickhouse.error || !requestInClickhouse.data) {
return requestInClickhouse;
}

return { data: "Scores added to Clickhouse successfully", error: null };
} catch (error: any) {
return err(error.message);
} catch {
return err("Error adding scores to Clickhouse");
}
}

Expand All @@ -73,7 +92,8 @@ export class ScoreManager extends BaseManager {
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

0 comments on commit ccf94a3

Please sign in to comment.