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

Fetch trace from root span for user evals #212

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
adding eval fix
  • Loading branch information
dylanzuber-scale3 committed Jul 24, 2024
commit c7f054c85166d8844f94dd8390b30e5fa25710ce
42 changes: 38 additions & 4 deletions app/api/evaluation/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { authOptions } from "@/lib/auth/options";
import prisma from "@/lib/prisma";
import { TraceService } from "@/lib/services/trace_service";
import { authApiKey } from "@/lib/utils";
import { getServerSession } from "next-auth";
import { NextRequest, NextResponse } from "next/server";
Expand All @@ -15,7 +16,24 @@ export async function POST(req: NextRequest) {
const projectData = await response.json();
const projectId = projectData.data.project.id;
const data = await req.json();
const { traceId, spanId, userScore, userId, reason, dataId } = data;
let { traceId, spanId, userScore, userId, reason, dataId } = data;

const traceService = await new TraceService();

// just gets the first span in the trace that has this type
// can expand by having the SDK send an additional data point in the request specifying the llm type
const correctSpan = await traceService.GetSpansWithAttribute(
"langtrace.service.type",
projectId,
1,
1,
traceId
);

if (correctSpan.result.length > 0) {
spanId = correctSpan.result[0].span_id;
}

// check if an evaluation already exists for the spanId
const existingEvaluation = await prisma.evaluation.findFirst({
where: {
Expand Down Expand Up @@ -220,16 +238,17 @@ export async function GET(req: NextRequest) {
},
});
} else {
const traceService = await new TraceService();
const tempSpanId = await traceService.GetSpanById(spanId, projectId);
karthikscale3 marked this conversation as resolved.
Show resolved Hide resolved
evaluations = await prisma.evaluation.findMany({
where: {
spanId,
traceId: tempSpanId.trace_id,
},
include: {
Test: includeTest,
},
});
}

if (!evaluations) {
return NextResponse.json({
evaluations: [],
Expand Down Expand Up @@ -308,10 +327,26 @@ export async function PUT(req: NextRequest) {
{ status: 400 }
);
}

const traceService = await new TraceService();
const tempSpanId = await traceService.GetSpanById(spanId, projectId);
const correctSpan = await traceService.GetSpansWithAttribute(
"langtrace.service.type",
projectId,
1,
1,
tempSpanId.trace_id
);

if (correctSpan.result.length > 0) {
spanId = correctSpan.result[0].span_id;
}

const evaluation = await prisma.evaluation.findFirst({
where: {
projectId,
spanId,
testId: null,
},
});
if (!evaluation) {
Expand Down Expand Up @@ -384,7 +419,6 @@ export async function PUT(req: NextRequest) {
{ status: 404 }
);
}

return NextResponse.json({
data: evaluation,
});
Expand Down
22 changes: 13 additions & 9 deletions lib/services/trace_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ export interface ITraceService {
attribute: string,
project_id: string,
page: number,
pageSize: number
pageSize: number,
trace_id?: string
) => Promise<PaginationResult<Span>>;
GetFailedSpans: (project_id: string) => Promise<Span[]>;
GetSpanLatencyPerProject: (project_id: string) => Promise<number>;
Expand Down Expand Up @@ -375,7 +376,8 @@ export class TraceService implements ITraceService {
attribute: string,
project_id: string,
page: number,
pageSize: number
pageSize: number,
trace_id?: string
): Promise<PaginationResult<Span>> {
try {
const tableExists = await this.client.checkTableExists(project_id);
Expand All @@ -399,13 +401,15 @@ export class TraceService implements ITraceService {
if (page! > totalPages) {
page = totalPages;
}
const query = sql.select(
`* FROM ${project_id} WHERE attributes LIKE '%${attribute}%' ORDER BY 'start_time' DESC LIMIT ${pageSize} OFFSET ${
(page - 1) * pageSize
};`
);
let spans: Span[] = await this.client.find<Span[]>(query);
// filter and remove empty attributes

let query = `* FROM ${project_id} WHERE attributes LIKE '%${attribute}%'`;
if (trace_id) {
query += ` AND trace_id = '${trace_id}'`;
}
query += ` ORDER BY 'start_time' DESC LIMIT ${pageSize} OFFSET ${
(page - 1) * pageSize
};`;
let spans: Span[] = await this.client.find<Span[]>(sql.select(query));
spans = spans.filter(
(span) => JSON.parse(span.attributes)[attribute]?.length > 0
);
Expand Down