Skip to content

Commit

Permalink
add cost to sessions filter using having
Browse files Browse the repository at this point in the history
  • Loading branch information
kavinvalli committed Oct 10, 2024
1 parent 0357bda commit aea0e62
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 36 deletions.
13 changes: 10 additions & 3 deletions helicone-node/api/generatedTypes/public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ export interface components {
request_id?: components["schemas"]["Partial_TextOperators_"];
prompt_tokens?: components["schemas"]["Partial_NumberOperators_"];
completion_tokens?: components["schemas"]["Partial_NumberOperators_"];
total_tokens?: components["schemas"]["Partial_NumberOperators_"];
target_url?: components["schemas"]["Partial_TextOperators_"];
properties?: {
[key: string]: components["schemas"]["Partial_TextOperators_"];
Expand All @@ -659,8 +660,13 @@ export interface components {
request_body?: components["schemas"]["Partial_VectorOperators_"];
response_body?: components["schemas"]["Partial_VectorOperators_"];
};
/** @description Make all properties in T optional */
Partial_SessionsRequestResponseRMTToOperators_: {
total_cost?: components["schemas"]["Partial_NumberOperators_"];
total_tokens?: components["schemas"]["Partial_NumberOperators_"];
};
/** @description From T, pick a set of properties whose keys are in the union K */
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": {
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": {
cache_hits?: components["schemas"]["Partial_CacheHitsTableToOperators_"];
feedback?: components["schemas"]["Partial_FeedbackTableToOperators_"];
properties?: {
Expand All @@ -673,9 +679,10 @@ export interface components {
[key: string]: components["schemas"]["Partial_TextOperators_"];
};
request_response_rmt?: components["schemas"]["Partial_RequestResponseRMTToOperators_"];
sessions_request_response_rmt?: components["schemas"]["Partial_SessionsRequestResponseRMTToOperators_"];
};
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": components["schemas"]["Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_"];
RequestFilterNode: components["schemas"]["FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_"] | components["schemas"]["RequestFilterBranch"] | "all";
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": components["schemas"]["Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_"];
RequestFilterNode: components["schemas"]["FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_"] | components["schemas"]["RequestFilterBranch"] | "all";
RequestFilterBranch: {
right: components["schemas"]["RequestFilterNode"];
/** @enum {string} */
Expand Down
1 change: 1 addition & 0 deletions valhalla/jawn/src/controllers/public/requestController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export type RequestFilterNode =
| "request_response_search"
| "cache_hits"
| "request_response_rmt"
| "sessions_request_response_rmt"
>
| RequestFilterBranch
| "all";
Expand Down
10 changes: 10 additions & 0 deletions valhalla/jawn/src/lib/shared/filters/filterDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ interface RequestResponseRMTToOperators {
request_id: SingleKey<TextOperators>;
prompt_tokens: SingleKey<NumberOperators>;
completion_tokens: SingleKey<NumberOperators>;
total_tokens: SingleKey<NumberOperators>;
target_url: SingleKey<TextOperators>;
properties: {
[key: string]: SingleKey<TextOperators>;
Expand All @@ -196,12 +197,20 @@ interface RequestResponseRMTToOperators {
response_body: SingleKey<VectorOperators>;
}

interface SessionsRequestResponseRMTToOperators {
total_cost: SingleKey<NumberOperators>;
total_tokens: SingleKey<NumberOperators>;
}

export type FilterLeafRequestResponseLog =
SingleKey<RequestResponseLogToOperators>;

export type FilterLeafRequestResponseRMT =
SingleKey<RequestResponseRMTToOperators>;

export type FilterLeafSessionsRequestResponseRMT =
SingleKey<SessionsRequestResponseRMTToOperators>;

type PropertiesCopyV2ToOperators = {
key: SingleKey<TextOperators>;
value: SingleKey<TextOperators>;
Expand Down Expand Up @@ -332,6 +341,7 @@ export type TablesAndViews = {
// CLICKHOUSE TABLES
request_response_log: FilterLeafRequestResponseLog;
request_response_rmt: FilterLeafRequestResponseRMT;
sessions_request_response_rmt: FilterLeafSessionsRequestResponseRMT;
users_view: FilterLeafUserView;
properties_v3: FilterLeafPropertiesV3;
property_with_response_v1: FilterLeafPropertyWithResponseV1;
Expand Down
8 changes: 8 additions & 0 deletions valhalla/jawn/src/lib/shared/filters/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ const whereKeyMappings: KeyMappings = {
node_id: "request_response_rmt.node_id",
job_id: "request_response_rmt.job_id",
threat: "request_response_rmt.threat",
total_tokens: "total_tokens",
prompt_tokens: "request_response_rmt.prompt_tokens",
completion_tokens: "request_response_rmt.completion_tokens",
request_body: "request_response_rmt.request_body",
Expand Down Expand Up @@ -282,6 +283,8 @@ const whereKeyMappings: KeyMappings = {
experiment_hypothesis_run: easyKeyMappings<"experiment_hypothesis_run">({
result_request_id: "experiment_v2_hypothesis_run.result_request_id",
}),
sessions_request_response_rmt:
easyKeyMappings<"sessions_request_response_rmt">({}),

// Deprecated
values: NOT_IMPLEMENTED,
Expand Down Expand Up @@ -314,6 +317,11 @@ const havingKeyMappings: KeyMappings = {
total_prompt_token: "total_prompt_token",
cost: "cost",
}),
sessions_request_response_rmt:
easyKeyMappings<"sessions_request_response_rmt">({
total_cost: "total_cost",
total_tokens: "total_tokens",
}),
request_response_rmt: easyKeyMappings<"request_response_rmt">({}),
request_response_search: NOT_IMPLEMENTED,
score_value: NOT_IMPLEMENTED,
Expand Down
5 changes: 3 additions & 2 deletions valhalla/jawn/src/managers/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ WHERE ${buildWhereClause("duration")}
${clickhousePriceCalc("request_response_rmt")} AS total_cost,
count(*) AS total_requests,
sum(request_response_rmt.prompt_tokens) AS prompt_tokens,
sum(request_response_rmt.completion_tokens) AS completion_tokens
sum(request_response_rmt.completion_tokens) AS completion_tokens,
sum(request_response_rmt.prompt_tokens) + sum(request_response_rmt.completion_tokens) AS total_tokens
FROM request_response_rmt
WHERE (
has(properties, 'Helicone-Session-Id')
Expand All @@ -374,7 +375,7 @@ WHERE ${buildWhereClause("duration")}
...y,
completion_tokens: +y.completion_tokens,
prompt_tokens: +y.prompt_tokens,
total_tokens: +y.completion_tokens + +y.prompt_tokens,
total_tokens: +y.total_tokens,
}))
);
}
Expand Down
17 changes: 11 additions & 6 deletions valhalla/jawn/src/tsoa-build/private/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,27 +1092,32 @@ const models: TsoaRoute.Models = {
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"Partial_RequestResponseRMTToOperators_": {
"dataType": "refAlias",
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"latency":{"ref":"Partial_NumberOperators_"},"status":{"ref":"Partial_NumberOperators_"},"request_created_at":{"ref":"Partial_TimestampOperatorsTyped_"},"response_created_at":{"ref":"Partial_TimestampOperatorsTyped_"},"model":{"ref":"Partial_TextOperators_"},"user_id":{"ref":"Partial_TextOperators_"},"organization_id":{"ref":"Partial_TextOperators_"},"node_id":{"ref":"Partial_TextOperators_"},"job_id":{"ref":"Partial_TextOperators_"},"threat":{"ref":"Partial_BooleanOperators_"},"request_id":{"ref":"Partial_TextOperators_"},"prompt_tokens":{"ref":"Partial_NumberOperators_"},"completion_tokens":{"ref":"Partial_NumberOperators_"},"target_url":{"ref":"Partial_TextOperators_"},"properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"search_properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"scores":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"scores_column":{"ref":"Partial_TextOperators_"},"request_body":{"ref":"Partial_VectorOperators_"},"response_body":{"ref":"Partial_VectorOperators_"}},"validators":{}},
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"latency":{"ref":"Partial_NumberOperators_"},"status":{"ref":"Partial_NumberOperators_"},"request_created_at":{"ref":"Partial_TimestampOperatorsTyped_"},"response_created_at":{"ref":"Partial_TimestampOperatorsTyped_"},"model":{"ref":"Partial_TextOperators_"},"user_id":{"ref":"Partial_TextOperators_"},"organization_id":{"ref":"Partial_TextOperators_"},"node_id":{"ref":"Partial_TextOperators_"},"job_id":{"ref":"Partial_TextOperators_"},"threat":{"ref":"Partial_BooleanOperators_"},"request_id":{"ref":"Partial_TextOperators_"},"prompt_tokens":{"ref":"Partial_NumberOperators_"},"completion_tokens":{"ref":"Partial_NumberOperators_"},"total_tokens":{"ref":"Partial_NumberOperators_"},"target_url":{"ref":"Partial_TextOperators_"},"properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"search_properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"scores":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"scores_column":{"ref":"Partial_TextOperators_"},"request_body":{"ref":"Partial_VectorOperators_"},"response_body":{"ref":"Partial_VectorOperators_"}},"validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"Partial_SessionsRequestResponseRMTToOperators_": {
"dataType": "refAlias",
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"total_cost":{"ref":"Partial_NumberOperators_"},"total_tokens":{"ref":"Partial_NumberOperators_"}},"validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"Partial_CacheHitsTableToOperators_": {
"dataType": "refAlias",
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"organization_id":{"ref":"Partial_TextOperators_"},"request_id":{"ref":"Partial_TextOperators_"},"latency":{"ref":"Partial_NumberOperators_"},"completion_tokens":{"ref":"Partial_NumberOperators_"},"prompt_tokens":{"ref":"Partial_NumberOperators_"},"created_at":{"ref":"Partial_TimestampOperatorsTyped_"}},"validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": {
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": {
"dataType": "refAlias",
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"values":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"response":{"ref":"Partial_ResponseTableToOperators_"},"request":{"ref":"Partial_RequestTableToOperators_"},"feedback":{"ref":"Partial_FeedbackTableToOperators_"},"request_response_search":{"ref":"Partial_RequestResponseSearchToOperators_"},"request_response_rmt":{"ref":"Partial_RequestResponseRMTToOperators_"},"cache_hits":{"ref":"Partial_CacheHitsTableToOperators_"},"properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}}},"validators":{}},
"type": {"dataType":"nestedObjectLiteral","nestedProperties":{"values":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}},"response":{"ref":"Partial_ResponseTableToOperators_"},"request":{"ref":"Partial_RequestTableToOperators_"},"feedback":{"ref":"Partial_FeedbackTableToOperators_"},"request_response_search":{"ref":"Partial_RequestResponseSearchToOperators_"},"request_response_rmt":{"ref":"Partial_RequestResponseRMTToOperators_"},"sessions_request_response_rmt":{"ref":"Partial_SessionsRequestResponseRMTToOperators_"},"cache_hits":{"ref":"Partial_CacheHitsTableToOperators_"},"properties":{"dataType":"nestedObjectLiteral","nestedProperties":{},"additionalProperties":{"ref":"Partial_TextOperators_"}}},"validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": {
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": {
"dataType": "refAlias",
"type": {"ref":"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_","validators":{}},
"type": {"ref":"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_","validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"RequestFilterNode": {
"dataType": "refAlias",
"type": {"dataType":"union","subSchemas":[{"ref":"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_"},{"ref":"RequestFilterBranch"},{"dataType":"enum","enums":["all"]}],"validators":{}},
"type": {"dataType":"union","subSchemas":[{"ref":"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_"},{"ref":"RequestFilterBranch"},{"dataType":"enum","enums":["all"]}],"validators":{}},
},
// WARNING: This file was auto-generated with tsoa. Please do not modify it. Re-run tsoa to re-generate this file: https://github.com/lukeautry/tsoa
"RequestFilterBranch": {
Expand Down
26 changes: 22 additions & 4 deletions valhalla/jawn/src/tsoa-build/private/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3043,6 +3043,9 @@
"completion_tokens": {
"$ref": "#/components/schemas/Partial_NumberOperators_"
},
"total_tokens": {
"$ref": "#/components/schemas/Partial_NumberOperators_"
},
"target_url": {
"$ref": "#/components/schemas/Partial_TextOperators_"
},
Expand Down Expand Up @@ -3080,6 +3083,18 @@
"type": "object",
"description": "Make all properties in T optional"
},
"Partial_SessionsRequestResponseRMTToOperators_": {
"properties": {
"total_cost": {
"$ref": "#/components/schemas/Partial_NumberOperators_"
},
"total_tokens": {
"$ref": "#/components/schemas/Partial_NumberOperators_"
}
},
"type": "object",
"description": "Make all properties in T optional"
},
"Partial_CacheHitsTableToOperators_": {
"properties": {
"organization_id": {
Expand All @@ -3104,7 +3119,7 @@
"type": "object",
"description": "Make all properties in T optional"
},
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": {
"Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": {
"properties": {
"values": {
"properties": {},
Expand All @@ -3128,6 +3143,9 @@
"request_response_rmt": {
"$ref": "#/components/schemas/Partial_RequestResponseRMTToOperators_"
},
"sessions_request_response_rmt": {
"$ref": "#/components/schemas/Partial_SessionsRequestResponseRMTToOperators_"
},
"cache_hits": {
"$ref": "#/components/schemas/Partial_CacheHitsTableToOperators_"
},
Expand All @@ -3142,13 +3160,13 @@
"type": "object",
"description": "From T, pick a set of properties whose keys are in the union K"
},
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_": {
"$ref": "#/components/schemas/Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_"
"FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_": {
"$ref": "#/components/schemas/Pick_FilterLeaf.feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_"
},
"RequestFilterNode": {
"anyOf": [
{
"$ref": "#/components/schemas/FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt_"
"$ref": "#/components/schemas/FilterLeafSubset_feedback-or-request-or-response-or-properties-or-values-or-request_response_search-or-cache_hits-or-request_response_rmt-or-sessions_request_response_rmt_"
},
{
"$ref": "#/components/schemas/RequestFilterBranch"
Expand Down
Loading

0 comments on commit aea0e62

Please sign in to comment.