Skip to content

Commit

Permalink
Merge pull request #83 from parea-ai/PAI-1455-fetch-traces-via-api
Browse files Browse the repository at this point in the history
feat: enable fetching trace logs via api
  • Loading branch information
joschkabraun authored Aug 9, 2024
2 parents 9022d0d + 62b1fda commit d176082
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
21 changes: 21 additions & 0 deletions cookbook/fetch_trace_logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { PaginatedTraceLogsResponse, Parea } from '../src';
import * as dotenv from 'dotenv';

dotenv.config();

const p = new Parea(process.env.PAREA_API_KEY);

async function main() {
const experiments = await p.listExperiments();
console.log(`found ${experiments.length} experiments`);
const response: PaginatedTraceLogsResponse = await p.getTraceLogs({
project_name: 'default',
filter_field: 'trace_name',
filter_operator: 'like',
filter_value: 'llm',
});

console.log(response);
}

main();
18 changes: 18 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ import {
CompletionResponse,
CreateExperimentRequest,
CreateTestCaseCollection,
defaultQueryParams,
EvaluationResult,
ExperimentSchema,
ExperimentStatsSchema,
ExperimentWithStatsSchema,
FeedbackRequest,
FinishExperimentRequestSchema,
ListExperimentUUIDsFilters,
PaginatedTraceLogsResponse,
QueryParams,
TestCaseCollection,
TraceLogFilters,
TraceLogTreeSchema,
Expand Down Expand Up @@ -39,6 +42,7 @@ const ADD_TEST_CASES_ENDPOINT = '/testcases';
const LIST_EXPERIMENTS_ENDPOINT = '/experiments';
const GET_EXP_LOGS_ENDPOINT = '/experiment/{experiment_uuid}/trace_logs';
const GET_TRACE_LOG_ENDPOINT = '/trace_log/{trace_id}';
const GET_TRACE_LOGS_ENDPOINT = '/get_trace_logs';
const UPDATE_TEST_CASE_ENDPOINT = '/update_test_case/{dataset_id}/{test_case_id}';

/**
Expand Down Expand Up @@ -387,6 +391,20 @@ export class Parea {

return data;
}

/**
* Fetches trace logs for a given query.
* @param queryParams - The query parameters for the trace logs.
* @returns A paginated response of trace logs.
*/
public async getTraceLogs(queryParams: QueryParams = defaultQueryParams): Promise<PaginatedTraceLogsResponse> {
const response = await this.client.request({
method: 'POST',
endpoint: GET_TRACE_LOGS_ENDPOINT,
data: { ...defaultQueryParams, ...queryParams },
});
return response.data;
}
}

/**
Expand Down
63 changes: 51 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,20 +494,23 @@ export type ListExperimentUUIDsFilters = {
run_name_filter?: string;
};

export enum FilterOperator {
EQUALS = 'equals',
NOT_EQUALS = 'not_equals',
LIKE = 'like',
GREATER_THAN_OR_EQUAL = 'greater_than_or_equal',
LESS_THAN_OR_EQUAL = 'less_than_or_equal',
GREATER_THAN = 'greater_than',
LESS_THAN = 'less_than',
IS_NULL = 'is_null',
EXISTS = 'exists',
IN = 'in',
}

export type TraceLogFilters = {
filter_field?: string;
filter_operator?:
| 'equals'
| 'not_equals'
| 'like'
| 'greater_than_or_equal'
| 'less_than_or_equal'
| 'greater_than'
| 'less_than'
| 'is_null'
| 'exists'
| 'in'
| null;
filter_key?: string | null;
filter_operator?: FilterOperator | string;
filter_value?: string;
};

Expand Down Expand Up @@ -550,3 +553,39 @@ export type StreamingResult = {
export interface MessageConverter {
convert(message: any): Message;
}

export enum TimeRange {
NA = 'na',
LAST_1_HOUR = '1h',
LAST_3_HOURS = '3h',
LAST_6_HOURS = '6h',
LAST_24_HOURS = '24h',
LAST_7_DAYS = '7d',
LAST_1_MONTH = '1m',
LAST_3_MONTHS = '3m',
LAST_6_MONTHS = '6m',
LAST_12_MONTHS = '1y',
}

export type QueryParams = TraceLogFilters & {
project_name: string;
page?: number;
page_size?: number;
time_range?: TimeRange;
status?: string;
};

export const defaultQueryParams: QueryParams = {
project_name: 'default',
page: 1,
page_size: 10,
time_range: TimeRange.NA,
};

export type PaginatedTraceLogsResponse = {
total: number;
page: number;
total_pages: number;
page_size: number;
results: TraceLogTreeSchema[];
};

0 comments on commit d176082

Please sign in to comment.