Search
Code113
"link": "/blog/val-vibes", "description": "How to build semantic search with embeddings for Val Town within Val Town itself", "pubDate": "Tue, 18 Jun 2024 00:00:00 GMT", "author": "JP Posma",
```async function calculateEmbeddings(text) { const url = `https://yawnxyz-ai.web.val.run/generate?embed=true&value=${encodeURIComponent(text)}`; return data; } catch (error) { console.error('Error calculating embeddings:', error); return null; }
// Embedding endpoints async generateEmbedding(request: EmbeddingRequest): Promise<ApiResponse<EmbeddingResponse>> { return this.request<EmbeddingResponse>('/embeddings/generate', { method: 'POST', body: JSON.stringify(request), threshold?: number; }): Promise<ApiResponse<any>> { return this.request('/embeddings/search', { method: 'POST', body: JSON.stringify(query), } async embeddingsHealthCheck(): Promise<ApiResponse<any>> { return this.request('/embeddings/health'); }
const API_CACHE_PATTERNS = [ /\/api\/health/, /\/api\/embeddings\/health/, /\/api\/chat\/health/];
import { initializeDatabase } from './services/supabase.ts';import chatRoutes from './routes/chat.ts';import embeddingRoutes from './routes/embeddings.ts';const app = new Hono();// API Routesapp.route('/api/chat', chatRoutes);app.route('/api/embeddings', embeddingRoutes);// Health check
import { Hono } from 'https://esm.sh/hono@3.11.7';import { generateEmbedding, generateEmbeddings } from '../services/embeddings.ts';import { searchSimilarContent } from '../services/supabase.ts';import type { EmbeddingRequest, EmbeddingResponse } from '../../shared/types.ts';const embeddings = new Hono();// Generate embedding for a single textembeddings.post('/generate', async (c) => { try { const body: EmbeddingRequest = await c.req.json();});// Generate embeddings for multiple textsembeddings.post('/batch', async (c) => { try { const body: { texts: string[] } = await c.req.json(); } const embeddings = await generateEmbeddings(texts); return c.json({ embeddings, count: embeddings.length, dimensions: embeddings[0]?.length || 0 }); console.error('Batch embedding generation error:', error); return c.json({ error: 'Failed to generate batch embeddings', details: error.message }, 500);// Search similar content using embeddingembeddings.post('/search', async (c) => { try { const body: { // Health check endpointembeddings.get('/health', (c) => { return c.json({ status: 'healthy', service: 'nelson-gpt-embeddings', timestamp: new Date().toISOString() });});export default embeddings;
import { Hono } from 'https://esm.sh/hono@3.11.7';import { streamSSE } from 'https://esm.sh/hono@3.11.7/streaming';import { generateEmbedding } from '../services/embeddings.ts';import { searchSimilarContent } from '../services/supabase.ts';import { generateMistralResponse, streamMistralResponse } from '../services/mistral.ts';
}export async function generateEmbeddings(texts: string[]): Promise<number[][]> { try { const embeddings = await Promise.all( texts.map(text => generateEmbedding(text)) ); return embeddings; } catch (error) { console.error('Error generating batch embeddings:', error); throw error; }}// Utility function to normalize embeddings (optional, for better similarity search)export function normalizeEmbedding(embedding: number[]): number[] { const magnitude = Math.sqrt(embedding.reduce((sum, val) => sum + val * val, 0));}// Calculate cosine similarity between two embeddingsexport function cosineSimilarity(a: number[], b: number[]): number { if (a.length !== b.length) { throw new Error('Embeddings must have the same length'); }
- **Database**: Supabase with PGVector for vector search- **AI**: Mistral API for LLM responses- **Embeddings**: Hugging Face sentence-transformers- **Animations**: Lottie- **PWA**: Service Worker + IndexedDB│ ├── routes/│ │ ├── chat.ts # Chat API endpoints│ │ └── embeddings.ts # Embedding generation│ └── services/│ ├── supabase.ts # Supabase client│ ├── mistral.ts # Mistral API client│ └── embeddings.ts # HuggingFace embeddings├── frontend/│ ├── index.html # Main HTML template- `VITE_SUPABASE_ANON_KEY`: Supabase anonymous key- `VITE_SUPABASE_SERVICE_ROLE_KEY`: Supabase service role key- `VITE_HUGGINFACE_API_KEY`: Hugging Face API key for embeddings## Getting Started
const app = new Hono();// Hugging Face API for embeddingsconst HF_API_URL = "https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/all-MiniLM-L6-v2";}// Generate embeddings endpointapp.post("/generate", async (c) => { try { console.error("Batch embedding error:", error); return c.json({ error: "Failed to generate batch embeddings", details: error.message }, 500); return c.json({ status: "healthy", service: "embeddings", model: "sentence-transformers/all-MiniLM-L6-v2", dimensions: testEmbedding?.length || 0, return c.json({ status: "unhealthy", service: "embeddings", error: error.message }, 500);
Users
No users found
Embedding Vals in other sites. Copy page Copy page. Copy this page as Markdown for LLMs. View as Markdown View this page as plain text. Open in ChatGPT Ask questions
Register a new Slash Command. Section titled “Step 5: Register a new Slash Command” The embedded code below should have your name in the top-left corner. If you see anonymous,