Skip to content

Commit

Permalink
Merge pull request #7 from cloudflare/nicer-retrieval-errors
Browse files Browse the repository at this point in the history
Nicer retrieval plugin error messages
  • Loading branch information
mhart authored May 15, 2023
2 parents 9281c27 + 8451366 commit aee11b1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
14 changes: 13 additions & 1 deletion example-retrieval-plugin/plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions example-retrieval-plugin/scheduler/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const EMBEDDINGS_KEY = "embeddings:v1";
/** @type {ExportedHandler<Env, EmbeddingMessage>} */
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");
},
Expand Down
2 changes: 1 addition & 1 deletion example-retrieval-plugin/shared/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit aee11b1

Please sign in to comment.