scrimba
Mistral AI
RAG - Improve the retrieval and complete the generation
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

RAG - Improve the retrieval and complete the generation
AboutCommentsNotes
RAG - Improve the retrieval and complete the generation
Expand for more info
index.js
run
preview
console
import MistralClient from "@mistralai/mistralai";
import { createClient } from "@supabase/supabase-js";

const mistralClient = new MistralClient(process.env.MISTRAL_API_KEY);
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_API_KEY);

// 1. Getting the user input
const input = "December 25th is on a Sunday, do I get any extra time off to account for that?";

// 2. Creating an embedding of the input
const embedding = await createEmbedding(input);

// 3. Retrieving similar embeddings / text chunks (aka "context")
const context = await retrieveMatches(embedding);
console.log(context);

// 4. Combining the input and the context in a prompt
// and using the chat API to generate a response
const response = await generateChatResponse(context, input);


async function createEmbedding(input) {
const embeddingResponse = await mistralClient.embeddings({
model: 'mistral-embed',
input: [input]
});
return embeddingResponse.data[0].embedding;
}

async function retrieveMatches(embedding) {
const { data } = await supabase.rpc('match_handbook_docs', {
query_embedding: embedding,
match_threshold: 0.78,
match_count: 1
});
// Challenge 1: Return the text from 5 matches instead of 1
return data[0].content;
}


async function generateChatResponse(context, query) {
// Challenge 2:
// Generate a reply to the user by combining both their
// question and the context into a prompt. Send the prompt
// to Mistral's API, deciding for yourself what model
// and settings you'd like to use.
}
Console
"Christmas Day Full‐time employees (employees who regularly work at least 35 hours per week) receive one (1) paid day off for each full day of holiday time. Holiday benefits for Part‐Time employees"
,
/index.html
-4:53