diff --git a/example-retrieval-plugin/plugin/src/index.js b/example-retrieval-plugin/plugin/src/index.js index eba75dc..736cdb6 100644 --- a/example-retrieval-plugin/plugin/src/index.js +++ b/example-retrieval-plugin/plugin/src/index.js @@ -65,6 +65,10 @@ async function retrievalPluginQuery(request, env, ctx) { /** @type {QueryRequest} */ const { queries } = await request.json(); + if (queries == null || !Array.isArray(queries)) { + throw new Error("unexpected json payload"); + } + let topKs = await Promise.all( queries.map(({ query, top_k: topK }) => queryVectorStore(env, query, topK)) ); @@ -101,9 +105,17 @@ const CHUNK_TTL = 24 * 60 * 60; // 24 hours * @returns {Promise<(Chunk & {similarity: number})[]>} */ async function queryVectorStore(env, query, topK = 3) { + if (!query) { + throw new Error("query is empty"); + } + if (!env.OPENAI_API_KEY) { + throw new Error("OPENAI_API_KEY is not set"); + } const res = await fetchEmbeddings([query], env.OPENAI_API_KEY); if (res.error != null) { - throw new Error(res.error); + const err = new Error(res.error.message || String(res.error)); + err.cause = res.error; + throw err; } const queryEmbedding = res.data[0].embedding; diff --git a/example-retrieval-plugin/scheduler/src/index.js b/example-retrieval-plugin/scheduler/src/index.js index 6b3eacc..f1a0f70 100644 --- a/example-retrieval-plugin/scheduler/src/index.js +++ b/example-retrieval-plugin/scheduler/src/index.js @@ -14,6 +14,12 @@ const EMBEDDINGS_KEY = "embeddings:v1"; /** @type {ExportedHandler} */ export default { async scheduled(controller, env, ctx) { + if (!env.GITHUB_API_KEY) { + throw new Error("GITHUB_API_KEY is not set"); + } + if (!env.OPENAI_API_KEY) { + throw new Error("OPENAI_API_KEY is not set"); + } ctx.waitUntil(scheduleEmbeddings(env, ctx)); console.log("scheduled ok"); }, diff --git a/example-retrieval-plugin/shared/openai.js b/example-retrieval-plugin/shared/openai.js index 3846a24..eecd9f8 100644 --- a/example-retrieval-plugin/shared/openai.js +++ b/example-retrieval-plugin/shared/openai.js @@ -3,7 +3,7 @@ const OPENAI_BASE = "https://api.openai.com/v1"; /** * @param {string[]} chunks * @param {string} apiKey - * @returns {Promise<{data: {embedding: number[]}[], error?: string}>} + * @returns {Promise<{data: {embedding: number[]}[], error?: {message: string, type: string}}>} */ export async function fetchEmbeddings(chunks, apiKey) { const NUM_RETRIES = 100;