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

feat: Add e2e evals #54

Merged
merged 9 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"cSpell.words": ["reranked", "Supabase"]
"cSpell.words": ["checkpointer", "reranked", "Supabase"]
}
12 changes: 6 additions & 6 deletions jest.setup.cjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const timezoneMock = require("timezone-mock");
// const timezoneMock = require("timezone-mock");
require("dotenv").config();

// Mock the timezone to 'America/Los_Angeles'
timezoneMock.register("US/Pacific"); // Alternatively, use 'America/Los_Angeles'
// timezoneMock.register("US/Pacific"); // Alternatively, use 'America/Los_Angeles'

// Optional: Log the current timezone to verify
console.log(
"Current Timezone:",
Intl.DateTimeFormat().resolvedOptions().timeZone,
);
// console.log(
// "Current Timezone:",
// Intl.DateTimeFormat().resolvedOptions().timeZone,
// );

// If you have any global configurations or mocks, add them here
// For example, you can set up global variables, mock APIs, etc.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@
"express-session": "^1.18.1",
"file-type": "^19.6.0",
"google-auth-library": "^9.15.0",
"langsmith": "0.2.15-rc.2",
"langsmith": "0.2.15-rc.6",
"moment": "^2.30.1",
"passport": "^0.7.0",
"passport-twitter": "^1.0.4",
"playwright": "^1.49.1",
"twitter-api-v2": "^1.18.2",
"uuid": "^11.0.4",
"zod": "^3.23.8"
},
"devDependencies": {
Expand Down
38 changes: 30 additions & 8 deletions src/agents/find-images/nodes/find-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,41 @@ import {
} from "../../utils.js";
import { takeScreenshotAndUpload } from "../screenshot.js";
import { FindImagesAnnotation } from "../find-images-graph.js";
import { validate } from "uuid";

function checkIsGitHubImageUrl(url: string): boolean {
if (
url?.startsWith("https://github.com/user-attachments/assets") ||
url?.includes("githubusercontent.com/")
) {
return true;
}
try {
const parsedUrl = new URL(url);
const pathname = parsedUrl.pathname;
const split = pathname.split("/");
const lastEle = split[split.length - 1];
const thirdToLastEle = split[split.length - 3];
if (thirdToLastEle === "assets" && validate(lastEle)) {
return true;
}
return false;
} catch (_) {
return false;
}
}

export async function findImages(state: typeof FindImagesAnnotation.State) {
const { pageContents, imageOptions, relevantLinks } = state;
const link = relevantLinks[0];
const imageUrls = new Set<string>();

const screenshotUrl = await takeScreenshotAndUpload(link);
if (screenshotUrl) {
imageUrls.add(screenshotUrl);
let screenshotUrl: string | undefined;
if (getUrlType(link) !== "youtube") {
screenshotUrl = await takeScreenshotAndUpload(link);
if (screenshotUrl) {
imageUrls.add(screenshotUrl);
}
}

if (imageOptions.length) {
Expand All @@ -38,11 +64,7 @@ export async function findImages(state: typeof FindImagesAnnotation.State) {
// If a full github URL. extract the file name from the path. to do this, extract the path after `blob/<branch>`
const filePath = urlOrPathname.match(/blob\/[^/]+\/(.+)/)?.[1];
if (!filePath) {
if (
!urlOrPathname?.startsWith(
"https://github.com/user-attachments/assets",
)
) {
if (!checkIsGitHubImageUrl(urlOrPathname)) {
console.warn(
"Could not extract file path from URL",
urlOrPathname,
Expand Down
52 changes: 35 additions & 17 deletions src/agents/find-images/nodes/re-rank-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ export function parseResult(result: string): number[] {
}

export async function reRankImages(state: typeof FindImagesAnnotation.State) {
if (state.imageOptions.length === 0) {
// No need to re-rank if less than 2 images
if (state.imageOptions.length < 2) {
return {
imageOptions: [],
imageOptions: state.imageOptions,
};
}

Expand All @@ -86,21 +87,38 @@ export async function reRankImages(state: typeof FindImagesAnnotation.State) {
if (!imageMessages.length) {
continue;
}
const response = await model.invoke([
{
role: "system",
content: formattedSystemPrompt,
},
{
role: "user",
content: imageMessages,
},
]);

const chunkAnalysis = parseResult(response.content as string);
// Convert chunk indices to global indices and add to our list of re-ranked indices
const globalIndices = chunkAnalysis.map((index) => index + baseIndex);
reRankedIndices = [...reRankedIndices, ...globalIndices];

try {
const response = await model.invoke([
{
role: "system",
content: formattedSystemPrompt,
},
{
role: "user",
content: imageMessages,
},
]);

const chunkAnalysis = parseResult(response.content as string);
// Convert chunk indices to global indices and add to our list of re-ranked indices
const globalIndices = chunkAnalysis.map((index) => index + baseIndex);
reRankedIndices = [...reRankedIndices, ...globalIndices];
} catch (error) {
console.error(
`Failed to re-rank images.\nImage URLs: ${imageMessages
.filter((m) => m.fileUri)
.map((m) => m.fileUri)
.join(", ")}\n\nError:`,
error,
);
// Add all indices from the failed chunk to allIrrelevantIndices
const failedChunkIndices = Array.from(
{ length: imageChunk.length },
(_, i) => i + baseIndex,
);
reRankedIndices = [...reRankedIndices, ...failedChunkIndices];
}

baseIndex += imageChunk.length;
}
Expand Down
77 changes: 49 additions & 28 deletions src/agents/find-images/nodes/validate-images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export function parseResult(result: string): number[] {
.filter((n) => !isNaN(n));
}

const YOUTUBE_THUMBNAIL_URL = "https://i.ytimg.com/";

export async function validateImages(
state: typeof FindImagesAnnotation.State,
): Promise<{
Expand All @@ -68,22 +70,21 @@ export async function validateImages(
temperature: 0,
});

let imagesWithoutSupabase = imageOptions;
if (process.env.SUPABASE_URL) {
// Do not include the supabase screenshot in the image list to validate.
imagesWithoutSupabase = imageOptions.filter(
(fileUri) => !fileUri.startsWith(process.env.SUPABASE_URL as string),
);
}
const imagesWithoutProtected = imageOptions.filter(
(fileUri) =>
(!process.env.SUPABASE_URL ||
!fileUri.startsWith(process.env.SUPABASE_URL)) &&
!fileUri.startsWith(YOUTUBE_THUMBNAIL_URL),
);

if (imagesWithoutSupabase.length === 0) {
if (imagesWithoutProtected.length === 0) {
return {
imageOptions,
};
}

// Split images into chunks of 10
const imageChunks = chunkArray(imagesWithoutSupabase, 10);
const imageChunks = chunkArray(imagesWithoutProtected, 10);
let allIrrelevantIndices: number[] = [];
let baseIndex = 0;

Expand All @@ -99,34 +100,54 @@ export async function validateImages(
if (!imageMessages.length) {
continue;
}
const response = await model.invoke([
{
role: "system",
content: formattedSystemPrompt,
},
{
role: "user",
content: imageMessages,
},
]);

const chunkAnalysis = parseResult(response.content as string);
// Convert chunk indices to global indices and add to our list of relevant indices
const globalIndices = chunkAnalysis.map((index) => index + baseIndex);
allIrrelevantIndices = [...allIrrelevantIndices, ...globalIndices];

try {
const response = await model.invoke([
{
role: "system",
content: formattedSystemPrompt,
},
{
role: "user",
content: imageMessages,
},
]);

const chunkAnalysis = parseResult(response.content as string);
// Convert chunk indices to global indices and add to our list of relevant indices
const globalIndices = chunkAnalysis.map((index) => index + baseIndex);
allIrrelevantIndices = [...allIrrelevantIndices, ...globalIndices];
} catch (error) {
console.error(
`Failed to validate images.\nImage URLs: ${imageMessages
.filter((m) => m.fileUri)
.map((m) => m.fileUri)
.join(", ")}\n\nError:`,
error,
);
// Add all indices from the failed chunk to allIrrelevantIndices
const failedChunkIndices = Array.from(
{ length: imageChunk.length },
(_, i) => i + baseIndex,
);
allIrrelevantIndices = [...allIrrelevantIndices, ...failedChunkIndices];
}

baseIndex += imageChunk.length;
}

const supabaseUrl = imageOptions.find((fileUri) =>
fileUri.startsWith(process.env.SUPABASE_URL as string),
const protectedUrls = imageOptions.filter(
(fileUri) =>
(process.env.SUPABASE_URL &&
fileUri.startsWith(process.env.SUPABASE_URL)) ||
fileUri.startsWith(YOUTUBE_THUMBNAIL_URL),
);

// Keep only the relevant images (those whose indices are in allIrrelevantIndices)
return {
imageOptions: [
...(supabaseUrl ? [supabaseUrl] : []),
...imagesWithoutSupabase.filter((_, index) =>
...protectedUrls,
...imagesWithoutProtected.filter((_, index) =>
allIrrelevantIndices.includes(index),
),
],
Expand Down
2 changes: 1 addition & 1 deletion src/agents/shared/nodes/verify-youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export async function verifyYouTubeContent(
return {
relevantLinks: [state.link],
pageContents: [videoSummary as string],
...(videoThumbnail ? { imageUrls: [videoThumbnail] } : {}),
...(videoThumbnail ? { imageOptions: [videoThumbnail] } : {}),
};
}

Expand Down
7 changes: 7 additions & 0 deletions src/agents/verify-links/verify-links-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ export const VerifyLinksGraphAnnotation = Annotation.Root({
},
default: () => [],
}),
/**
* Image options to provide to the user.
*/
imageOptions: Annotation<string[]>({
reducer: (_state, update) => update,
default: () => [],
}),
});
Loading
Loading