Search
Code165
* Initialize the embedding model */async function initEmbeddings(modelId = EMBEDDING_MODELS.fast) { if (embedder && embedder.model?.config?.name_or_path === modelId) { return embedder;async function processAllFeeds() { await initDatabase(); await initEmbeddings(); let totalProcessed = 0; "slug": "val-vibes", "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", const {model, modelToken} = this.options; const response = await axios.post( "https://api.openai.com/v1/embeddings", {model, input: text }, { "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 checkimport { 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';