Search
Code115
- **Database**: SQLite for storing resumes and job requirements- **NLP/ML**: OpenAI embeddings for semantic matchingasync function main() { const generateEmbeddings = await pipeline("feature-extraction"); const embeddings = await generateEmbeddings("Hello, World!"); console.log(embeddings);}import { searchEmojis } from "https://esm.town/v/maxm/emojiVectorEmbeddings";import { extractValInfo } from "https://esm.town/v/pomdtr/extractValInfo"; <br /> Built on Val Town with sqlite vector search and openai embeddings. <br />Uses vector embeddings to get "vibes" search on emojis
async function getEmbedding(emoji: string): Promise<number[]> { const result = await openai.embeddings.create({ input: emoji,const embeddings: EmojiEmbedding[] = [];for (const emoji of emojisWithInfo) { embeddings.push({ emoji, embedding: await getEmbedding(emoji) });} targetEmbedding: number[], allEmbeddings: EmojiEmbedding[], k: number = 50,): { emoji: string; similarity: number }[] { return allEmbeddings .map(entry => ({const toSearch = embeddings.find((r) => (r.emoji === emojiToString(["š»", emojis["š»"]])))!;console.log(findNearestNeighbors(toSearch.embedding, embeddings));```async function calculateEmbeddings(text) { const url = `https://yawnxyz-ai.web.val.run/generate?embed=true&value=${encodeURIComponent(text)}`; } catch (error) { console.error('Error calculating embeddings:', error); return null;async function getEmbedding(emoji: string): Promise<number[]> { const result = await openai.embeddings.create({ input: emoji,// async function getEmbedding(emoji: string): Promise<number[]> {// const result = await openai.embeddings.create({// input: emoji,// // Get embeddings for all emojis// async function getAllEmbeddings(): Promise<EmojiEmbedding[]> {// const emojis = getAllEmojis();// const embeddings: EmojiEmbedding[] = [];// const batchResults = await Promise.all(batchPromises);// embeddings.push(...batchResults);// return embeddings;// }// targetEmbedding: number[],// allEmbeddings: EmojiEmbedding[],// k: number = 5,// ): { emoji: string; similarity: number }[] {// return allEmbeddings// .map(entry => ({// try {// console.log("Getting embeddings for all emojis...");// const allEmbeddings = await getAllEmbeddings();// console.log(`Finding nearest neighbors for ${targetEmoji}...`);// const neighbors = findNearestNeighbors(targetEmbedding, allEmbeddings);async function generateEmbedding(text: string): Promise<number[]> { const response = await openai.embeddings.create({ model: "text-embedding-ada-002", * It uses the umap-js library for efficient UMAP computation and implements caching for improved performance. * The service accepts POST requests with JSON payloads containing high-dimensional embeddings and configuration parameters. * It returns 2D coordinates as the result of dimensionality reduction. * Common use cases include: * - Visualizing word embeddings or document vectors in NLP tasks * - Analyzing gene expression data in bioinformatics * - Exploring customer segmentation in marketing analytics * - Visualizing image embeddings in computer vision tasks */ try { const { embeddings, config } = await request.json(); // Input validation if (!Array.isArray(embeddings) || embeddings.length === 0) { return new Response("Invalid input: embeddings must be a non-empty array", { status: 400 }); } if (embeddings.length > MAX_POINTS) { return new Response(`Input too large: maximum ${MAX_POINTS} points allowed`, { status: 413 }); } if (embeddings[0].length > MAX_DIMENSIONS) { return new Response(`Input too high-dimensional: maximum ${MAX_DIMENSIONS} dimensions allowed`, { status: 413 }); const encoder = new TextEncoder(); const data = encoder.encode(JSON.stringify({ embeddings, config })); const hashBuffer = await crypto.subtle.digest("MD5", data); const result = await Promise.race([ umap.fit(embeddings), new Promise((_, reject) => setTimeout(() => reject(new Error("Computation timed out")), TIMEOUT_MS)), <ul> <li>Visualizing word embeddings or document vectors in NLP tasks</li> <li>Analyzing gene expression data in bioinformatics</li> <li>Exploring customer segmentation in marketing analytics</li> <li>Visualizing image embeddings in computer vision tasks</li> </ul> <h2>How to Use</h2> <p>Send a POST request to this endpoint with your data to use the API. The request should include an array of embeddings and optional configuration parameters.</p> body: JSON.stringify({ embeddings: [[1,2,3], [4,5,6], [7,8,9]], config: { nNeighbors: 15, minDist: 0.1, spread: 1.0 } <pre> curl -X POST -H "Content-Type: application/json" -d '{"embeddings": [[1,2,3], [4,5,6], [7,8,9]], "config": { "nNeighbors": 15, "minDist": 0.1, "spread": 1.0 }}' https://ejfox-umap.web.val.run/ </pre> <div class="example"> <h3>Example with OpenAI Embeddings:</h3> <p>This example shows how to use the UMAP service with OpenAI embeddings:</p> <pre>// First, generate embeddings using OpenAI APIimport { OpenAI } from "https://esm.town/v/std/openai";async function getEmbeddings(texts) { const response = await openai.embeddings.create({ model: "text-embedding-ada-002",// Then, use these embeddings with the UMAP serviceconst texts = ["Hello world", "OpenAI is amazing", "UMAP reduces dimensions"];const embeddings = await getEmbeddings(texts); body: JSON.stringify({ embeddings: embeddings, config: { nNeighbors: 15, minDist: 0.1, spread: 1.0 } const testData = { embeddings: Array.from({length: 100}, () => Array.from({length: 10}, () => Math.random())), config: { nNeighbors: 15, minDist: 0.1, spread: 1.0 }- Visualizing word embeddings in a scatterplotcs- Exploring customer segmentation in marketing analytics- Visualizing image embeddings in computer vision tasksimport blogPostEmbeddingsDimensionalityReduction from "https://esm.town/v/janpaul123/blogPostEmbeddingsDimensionalityReduction"; ) => p(`<a>`)); const points = await blogPostEmbeddingsDimensionalityReduction(); const chart = Plot.plot({export default async function blogPostEmbeddingsDimensionalityReduction() { const points = [ async function getEmbedding(str) { return (await openai.embeddings.create({ model: "text-embedding-3-large", } let embeddings = await blob.getJSON("blogPostEmbeddings"); if (!embeddings) { embeddings = await Promise.all(points.map((point) => getEmbedding(point))); await blob.setJSON("blogPostEmbeddings", embeddings); } const matrix = druid.Matrix.from(embeddings); const dr = new druid.UMAP(matrix, {console.log(await blogPostEmbeddingsDimensionalityReduction());Migrated from folder: semanticSearchBlogPost/blogPostEmbeddingsDimensionalityReduction
async function getEmbedding(str) { cache[str] = cache[str] || (await openai.embeddings.create({ model: "text-embedding-3-large",// const allValsBlobEmbeddingsMeta = (await blob.getJSON(`allValsBlob${dimensions}EmbeddingsMeta`)) ?? {};const allValsBlobEmbeddingsMeta = {};const existingEmbeddingsIds = new Set(Object.keys(allValsBlobEmbeddingsMeta)); const id = idForVal(val); if (!existingEmbeddingsIds.has(id)) { currentBatch.push(val); 0, ...Object.values(allValsBlobEmbeddingsMeta).map((item: any) => item.batchDataIndex + 1),); const batchDataIndex = nextDataIndex; const embeddingsBatch = new Float32Array(dimensions * newValsBatch.length); await Promise.all([...Array(newValsBatch.length).keys()].map(async (valIndex) => { const embedding = await openai.embeddings.create({ model: "text-embedding-3-small", embeddingsBatch.set(embeddingBinary, dimensions * valIndex); allValsBlobEmbeddingsMeta[id] = { batchDataIndex, valIndex }; })); const embeddingsBatchBlobName = `allValsBlob${dimensions}EmbeddingsData_${batchDataIndex}`; await blob.set(embeddingsBatchBlobName, embeddingsBatch.buffer); await blob.setJSON(`allValsBlob${dimensions}EmbeddingsMeta`, allValsBlobEmbeddingsMeta); console.log( `Saved batch to ${embeddingsBatchBlobName} with ${newValsBatch.length} records (${embeddingsBatch.byteLength} bytes) ${ batchDataIndex + 1console.log(`Finished, we have indexed ${Object.keys(allValsBlobEmbeddingsMeta).length} records`);