File size: 12,126 Bytes
5dfbe50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import { Hono } from 'hono';
import { streamSSE } from 'hono/streaming';
import { v4 as uuidv4 } from 'uuid';
import { z } from 'zod';
import { config } from '../config';
import { cache } from '../services/cache';
import { vespaRequest } from '../services/vespa-https';

const backendApi = new Hono();

// Search request schema
const searchQuerySchema = z.object({
  query: z.string().min(1).max(500),
  ranking: z.enum(['hybrid', 'colpali', 'bm25']).optional().default('hybrid'),
});

// Main search endpoint - /fetch_results
backendApi.get('/fetch_results', async (c) => {
  try {
    const query = c.req.query('query');
    const ranking = c.req.query('ranking') || 'hybrid';
    
    const validation = searchQuerySchema.safeParse({ query, ranking });
    
    if (!validation.success) {
      return c.json({ error: 'Invalid request', details: validation.error.issues }, 400);
    }

    const validatedData = validation.data;
    
    // Check cache
    const cacheKey = `search:${validatedData.query}:${validatedData.ranking}`;
    const cachedResult = cache.get(cacheKey);
    
    if (cachedResult) {
      c.header('X-Cache', 'HIT');
      return c.json(cachedResult);
    }

    // Build YQL query based on ranking
    let yql = '';
    let searchParams: any = {
      query: validatedData.query,
      hits: '20'
    };
    
    switch (validatedData.ranking) {
      case 'colpali':
        // Use retrieval-and-rerank profile for ColPali
        yql = `select * from linqto where userQuery() limit 20`;
        searchParams.ranking = 'retrieval-and-rerank';
        break;
      case 'bm25':
        yql = `select * from linqto where userQuery() limit 20`;
        searchParams.ranking = 'default';
        break;
      case 'hybrid':
      default:
        yql = `select * from linqto where userQuery() limit 20`;
        searchParams.ranking = 'default';
        break;
    }

    // For ColPali ranking, we need embeddings
    let body: any = {};
    let useNearestNeighbor = false;
    
    if (validatedData.ranking === 'colpali') {
      try {
        // Call embedding API to get query embeddings
        const embeddingResponse = await fetch(
          `http://localhost:7861/embed_query?query=${encodeURIComponent(validatedData.query)}`
        );
        
        if (embeddingResponse.ok) {
          const embeddingData = await embeddingResponse.json();
          
          // Create nearestNeighbor query string
          const numTokens = Object.keys(embeddingData.embeddings.binary).length;
          const maxTokens = Math.min(numTokens, 20); // Limit to 20 tokens to avoid timeouts
          const nnClauses = [];
          
          // Add individual rq tensors for nearestNeighbor
          for (let i = 0; i < maxTokens; i++) {
            body[`input.query(rq${i})`] = embeddingData.embeddings.binary[i.toString()];
            nnClauses.push(`({targetHits:10}nearestNeighbor(embedding,rq${i}))`);
          }
          
          // Update YQL for nearestNeighbor search
          if (nnClauses.length > 0) {
            yql = `select * from linqto where ${nnClauses.join(' OR ')} limit 20`;
            useNearestNeighbor = true;
          }
          
          // Add qt and qtb for ranking
          body["input.query(qt)"] = embeddingData.embeddings.float;
          body["input.query(qtb)"] = embeddingData.embeddings.binary;
          body["presentation.timing"] = true;
        } else {
          // Fall back to text-only search
          searchParams.ranking = 'default';
        }
      } catch (error) {
        console.log('Embedding API not available, falling back to text search');
        searchParams.ranking = 'default';
      }
    }

    // Query Vespa directly
    const searchUrl = `${config.vespaAppUrl}/search/`;
    const urlSearchParams = new URLSearchParams({
      yql,
      ...searchParams
    });
    
    // Use ranking.profile for Vespa instead of ranking
    if (searchParams.ranking) {
      urlSearchParams.delete('ranking');
      urlSearchParams.set('ranking.profile', searchParams.ranking);
    }
    
    const startTime = Date.now();
    let requestOptions: any = {};
    
    // Only use POST with body if we have embeddings
    if (Object.keys(body).length > 0) {
      requestOptions = {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(body)
      };
    } else {
      requestOptions = {
        method: 'GET'
      };
    }
    
    console.log('Vespa query URL:', `${searchUrl}?${urlSearchParams}`);
    console.log('Request options:', requestOptions);
    
    const response = await vespaRequest(`${searchUrl}?${urlSearchParams}`, requestOptions);

    if (!response.ok && response.status !== 504) {
      const errorText = await response.text();
      console.error('Vespa error:', errorText);
      throw new Error(`Vespa returned ${response.status}: ${errorText}`);
    }

    const data = await response.json();
    const searchTime = (Date.now() - startTime) / 1000; // Convert to seconds
    
    // Generate query_id for sim_map compatibility
    const queryId = uuidv4();
    
    // Transform to match expected format
    if (data.root && data.root.children) {
      data.root.children.forEach((hit: any, idx: number) => {
        if (!hit.fields) hit.fields = {};
        // Add sim_map identifier for compatibility
        hit.fields.sim_map = `${queryId}_${idx}`;
      });
    }

    // Add timing information
    data.timing = {
      searchtime: searchTime
    };

    // Cache the result
    cache.set(cacheKey, data);
    c.header('X-Cache', 'MISS');

    return c.json(data);
  } catch (error) {
    console.error('Search error:', error);
    return c.json({ 
      error: 'Search failed', 
      message: error instanceof Error ? error.message : 'Unknown error' 
    }, 500);
  }
});

// Full image endpoint - /full_image
backendApi.get('/full_image', async (c) => {
  try {
    const docId = c.req.query('doc_id'); // Note: backend expects doc_id, not docId
    
    if (!docId) {
      return c.json({ error: 'doc_id is required' }, 400);
    }

    // Check cache
    const cacheKey = `fullimage:${docId}`;
    const cachedImage = cache.get<{ base64_image: string }>(cacheKey);
    
    if (cachedImage) {
      c.header('X-Cache', 'HIT');
      return c.json(cachedImage);
    }

    // Query Vespa for the document
    const searchUrl = `${config.vespaAppUrl}/search/`;
    const searchParams = new URLSearchParams({
      yql: `select * from linqto where id contains "${docId}"`,
      hits: '1'
    });

    const response = await vespaRequest(`${searchUrl}?${searchParams}`);

    if (!response.ok) {
      throw new Error(`Vespa returned ${response.status}`);
    }

    const data = await response.json();
    
    if (data.root?.children?.[0]?.fields) {
      const fields = data.root.children[0].fields;
      const base64Image = fields.full_image || fields.image;
      
      if (base64Image) {
        const result = { base64_image: base64Image };
        cache.set(cacheKey, result, 86400); // 24 hours
        c.header('X-Cache', 'MISS');
        return c.json(result);
      }
    }
    
    return c.json({ error: 'Image not found' }, 404);
  } catch (error) {
    console.error('Full image error:', error);
    return c.json({ 
      error: 'Failed to fetch image', 
      message: error instanceof Error ? error.message : 'Unknown error' 
    }, 500);
  }
});

// Query suggestions endpoint - /suggestions
backendApi.get('/suggestions', async (c) => {
  try {
    const query = c.req.query('query') || '';
    
    // Static suggestions for now
    const staticSuggestions = [
      'linqto bankruptcy',
      'linqto filing date',
      'linqto creditors',
      'linqto assets',
      'linqto liabilities',
      'linqto chapter 11',
      'linqto docket',
      'linqto plan',
      'linqto disclosure statement',
      'linqto claims',
    ];
    
    if (!query) {
      return c.json({ suggestions: staticSuggestions.slice(0, 5) });
    }

    const lowerQuery = query.toLowerCase();
    const filtered = staticSuggestions
      .filter(s => s.startsWith(lowerQuery))
      .slice(0, 5);
    
    return c.json({ suggestions: filtered });
  } catch (error) {
    console.error('Suggestions error:', error);
    return c.json({ 
      error: 'Failed to fetch suggestions', 
      suggestions: [] 
    }, 500);
  }
});

// Similarity maps endpoint - /get_sim_map
backendApi.get('/get_sim_map', async (c) => {
  try {
    const queryId = c.req.query('query_id'); // Note: backend expects query_id
    const idx = c.req.query('idx');
    const token = c.req.query('token');
    const tokenIdx = c.req.query('token_idx'); // Note: backend expects token_idx
    
    if (!queryId || !idx || !token || !tokenIdx) {
      return c.json({ error: 'Missing required parameters' }, 400);
    }

    // Return placeholder HTML
    const html = `
      <div style="padding: 20px; text-align: center;">
        <h3>Similarity Map</h3>
        <p>Query: ${token}</p>
        <p>Document: ${idx}</p>
        <p style="color: #666;">
          Similarity map generation requires the ColPali model.
          This is a placeholder for the demo.
        </p>
      </div>
    `;
    
    return c.html(html);
  } catch (error) {
    console.error('Similarity map error:', error);
    return c.json({ 
      error: 'Failed to generate similarity map', 
      message: error instanceof Error ? error.message : 'Unknown error' 
    }, 500);
  }
});

// Visual RAG Chat SSE endpoint - /get-message
backendApi.get('/get-message', async (c) => {
  const queryId = c.req.query('query_id'); // Note: backend expects query_id
  const query = c.req.query('query');
  const docIds = c.req.query('doc_ids'); // Note: backend expects doc_ids

  if (!queryId || !query || !docIds) {
    return c.json({ error: 'Missing required parameters: query_id, query, doc_ids' }, 400);
  }

  return streamSSE(c, async (stream) => {
    try {
      // Mock response for now - in production this would use an LLM
      // Extract key information from the query
      const messages = [];
      
      if (query.toLowerCase().includes('when') && query.toLowerCase().includes('file')) {
        messages.push(
          `I'll analyze the search results for your query: "${query}"`,
          "",
          "Based on the documents provided:",
          "",
          "**LINQTO filed for Chapter 11 bankruptcy on July 7, 2025**",
          "",
          "The filing was made in the United States Bankruptcy Court for the Southern District of Texas under case number 25-90186.",
          "",
          "Key details:",
          "• Filing Date: July 7, 2025 (Petition Date)",
          "• Court: Southern District of Texas",
          "• Case Number: 25-90186",
          "• Chapter: 11 (Reorganization)",
          "",
          "This is a demo response. In production, an LLM would analyze the actual document contents for more details."
        );
      } else {
        messages.push(
          `I'll analyze the search results for your query: "${query}"`,
          "Based on the documents provided, here are the key findings:",
          "1. LINQTO filed for Chapter 11 bankruptcy protection on July 7, 2025",
          "2. The filing includes detailed financial statements and creditor information",
          "3. Various claims and assets are documented in the court filings",
          "",
          "This is a demo response. In production, this would analyze the actual document contents using an LLM."
        );
      }
      
      for (const msg of messages) {
        await stream.writeSSE({ data: msg });
        await new Promise(resolve => setTimeout(resolve, 500)); // Simulate typing delay
      }
    } catch (error) {
      console.error('Chat streaming error:', error);
      await stream.writeSSE({
        event: 'error',
        data: JSON.stringify({ 
          error: 'Streaming failed', 
          message: error instanceof Error ? error.message : 'Unknown error' 
        }),
      });
    }
  });
});

export { backendApi };