Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/search/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const runSearch = async (
tldr,
prevMessages: lastTurn,
userUniversity,
supabase,
emit,
progress,
updateProgress,
Expand Down
69 changes: 69 additions & 0 deletions lib/search/general/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,72 @@ export function extractOutputText(resp: Response): string {
}
return parts.join("\n").trim();
}

type KBSearchFilters = {
uni?: string;
corpus?: "su" | "official";
year?: number;
kb_slug?: string;
};

function pretty(obj: any) {
return JSON.stringify(obj, null, 2);
}

export async function debugVectorSearch(opts: {
openai: any; // OpenAI client
vectorStoreId: string;
query: string;
topK?: number;
filters: KBSearchFilters;
label?: string;
}) {
const { openai, vectorStoreId, query, topK = 5, filters, label } = opts;

// 1) LOG THE INPUTS (this answers your “what filters were applied?” question)
console.log(
"\n[KB VS SEARCH] " +
(label ? `${label} ` : "") +
`query=${JSON.stringify(query)} topK=${topK}\n` +
`[KB VS SEARCH] vectorStoreId=${vectorStoreId}\n` +
`[KB VS SEARCH] filters=\n${pretty(filters)}\n`
);

// 2) RUN THE SEARCH (adjust to your SDK call shape)
// If your code uses openai.vectorStores.search(...) or openai.responses with tool=file_search,
// keep the actual call you already have, but add logs before/after.
const res = await openai.vectorStores.search(vectorStoreId, {
query,
top_k: topK,
// If your SDK supports metadata filtering here, pass it through.
// Different SDK versions may call this 'filter' or 'filters'. Keep whatever your current code uses.
filter: filters,
});

// 3) LOG WHAT CAME BACK (esp. attributes)
const results = res?.data ?? res?.results ?? [];
console.log(`[KB VS SEARCH] results=${results.length}`);

for (let i = 0; i < Math.min(results.length, topK); i++) {
const r = results[i];
const fileId = r?.file_id ?? r?.file?.id ?? r?.id;
const attrs = r?.attributes ?? r?.file?.attributes ?? {};
const score = r?.score ?? r?.relevance_score;

console.log(
`\n[KB VS SEARCH] #${i + 1} file_id=${fileId} score=${score}\n` +
`[KB VS SEARCH] attrs=${pretty({
uni: attrs.uni,
corpus: attrs.corpus,
kb_slug: attrs.kb_slug,
site: attrs.site,
year: attrs.year,
canonical_url: attrs.canonical_url,
doc_id: attrs.doc_id,
section_key: attrs.section_key,
})}`
);
}

return res;
}
22 changes: 1 addition & 21 deletions lib/search/general/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,19 @@ import type { ResponseInput } from "openai/resources/responses/responses.mjs";
// Types
// ============================================================================

/** Supported universities with their vector store IDs */
/** Supported universities for routing */
export const SUPPORTED_UNIVERSITIES = {
unimelb: {
name: "University of Melbourne",
official: process.env.OPENAI_VS_UNIMELB_OFFICIAL,
union: process.env.OPENAI_VS_UNIMELB_SU,
},
monash: {
name: "Monash University",
official: process.env.OPENAI_VS_MONASH_OFFICIAL,
union: process.env.OPENAI_VS_MONASH_SU,
},
rmit: {
name: "RMIT University",
official: process.env.OPENAI_VS_RMIT_OFFICIAL,
union: process.env.OPENAI_VS_RMIT_SU,
},
uwa: {
name: "University of Western Australia",
official: process.env.OPENAI_VS_UWA_OFFICIAL,
union: process.env.OPENAI_VS_UWA_SU,
},
} as const;

Expand Down Expand Up @@ -196,15 +188,3 @@ export async function routeQuery(
return { route: "web", university: null, intent: "both", reason: "error" };
}
}

/**
* Get vector store IDs for a university.
*/
export function getVectorStores(slug: UniversitySlug | null): {
official?: string;
union?: string;
} {
if (!slug || !SUPPORTED_UNIVERSITIES[slug]) return {};
const uni = SUPPORTED_UNIVERSITIES[slug];
return { official: uni.official, union: uni.union };
}
11 changes: 7 additions & 4 deletions lib/search/general/runGeneral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
import OpenAI from "openai";
import type { ResponseInput } from "openai/resources/responses/responses.mjs";

import { routeQuery, getVectorStores } from "./router";
import { routeQuery } from "./router";
import { runUniversityGeneral } from "./runUniversityGeneral";
import { runConnect3General } from "./runConnect3General";
import { countWebSearchCalls } from "./countWebSearches";
import type { SearchResponse } from "../types";
import { ProgressAction } from "@/components/search/utils";
import { SupabaseClient } from "@supabase/supabase-js";

type RunGeneralArgs = {
openai: OpenAI;
query: string;
tldr: string;
prevMessages: ResponseInput;
userUniversity?: string | null;
supabase: SupabaseClient;
emit?: (event: string, data: unknown) => void;
progress: ProgressAction[];
updateProgress: (
Expand All @@ -35,6 +37,7 @@ export async function runGeneral({
tldr,
prevMessages,
userUniversity,
supabase,
emit,
progress,
updateProgress,
Expand Down Expand Up @@ -86,9 +89,9 @@ export async function runGeneral({

// -------- University-specific query --------
case "university": {
const vectorStores = getVectorStores(routing.university);
const centralVectorStoreId = process.env.OPENAI_UNI_VECTOR_STORE_ID;

if (vectorStores.official || vectorStores.union) {
if (centralVectorStoreId) {
progress = updateProgress(
progress,
{
Expand All @@ -101,9 +104,9 @@ export async function runGeneral({

return runUniversityGeneral(
openai,
supabase,
query,
routing.university!,
vectorStores,
{ emit, intent: routing.intent },
);
}
Expand Down
Loading