diff --git a/src/content/docs/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai.mdx b/src/content/docs/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai.mdx index 711aca436523c39..64d0b6832d40a5a 100644 --- a/src/content/docs/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai.mdx +++ b/src/content/docs/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai.mdx @@ -280,40 +280,47 @@ const app = new Hono(); // Existing post route... // app.post('/notes', async (c) => { ... }) -app.get('/', async (c) => { - const question = c.req.query('text') || "What is the square root of 9?" - - const embeddings = await c.env.AI.run('@cf/baai/bge-base-en-v1.5', { text: question }) - const vectors = embeddings.data[0] - - const vectorQuery = await c.env.VECTOR_INDEX.query(vectors, { topK: 1 }); - const vecId = vectorQuery.matches[0].id - - let notes = [] - if (vecId) { - const query = `SELECT * FROM notes WHERE id = ?` - const { results } = await c.env.DB.prepare(query).bind(vecId).all() - if (results) notes = results.map(vec => vec.text) - } - - const contextMessage = notes.length - ? `Context:\n${notes.map(note => `- ${note}`).join("\n")}` - : "" - - const systemPrompt = `When answering the question or responding, use the context provided, if it is provided and relevant.` - - const { response: answer } = await c.env.AI.run( - '@cf/meta/llama-3-8b-instruct', - { - messages: [ - ...(notes.length ? [{ role: 'system', content: contextMessage }] : []), - { role: 'system', content: systemPrompt }, - { role: 'user', content: question } - ] - } - ) - - return c.text(answer); +app.get("/", async (c) => { + const question = c.req.query("text") || "What is the square root of 9?"; + + const embeddings = await c.env.AI.run("@cf/baai/bge-base-en-v1.5", { + text: question, + }); + const vectors = embeddings.data[0]; + + const vectorQuery = await c.env.VECTOR_INDEX.query(vectors, { topK: 1 }); + let vecId; + if (vectorQuery?.matches?.length) { + vecId = vectorQuery.matches[0].id; + } else { + console.log("No matching vector found or vectorQuery.matches is empty"); + } + + let notes = []; + if (vecId) { + const query = `SELECT * FROM notes WHERE id = ?`; + const { results } = await c.env.DB.prepare(query).bind(vecId).all(); + if (results) notes = results.map((vec) => vec.text); + } + + const contextMessage = notes.length + ? `Context:\n${notes.map((note) => `- ${note}`).join("\n")}` + : ""; + + const systemPrompt = `When answering the question or responding, use the context provided, if it is provided and relevant.`; + + const { response: answer } = await c.env.AI.run( + "@cf/meta/llama-3-8b-instruct", + { + messages: [ + ...(notes.length ? [{ role: "system", content: contextMessage }] : []), + { role: "system", content: systemPrompt }, + { role: "user", content: question }, + ], + }, + ); + + return c.text(answer); }); app.onError((err, c) => {