Public
HTTP
Forked from yawnxyz/buildclubProjectSearch
yawnxyz-airtablesemanticsearch.web.val.run
June 5, 2024
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
import { semanticSearch } from "https://esm.town/v/yawnxyz/semanticSearch";
import Airtable from "npm:airtable";
import { Hono } from "npm:hono@3";
import { cors } from "npm:hono/cors";
const app = new Hono();
app.use('*', cors({
origin: '*',
allowMethods: ['GET', 'POST'],
allowHeaders: ['Content-Type'],
}));
const airtableApiKey = Deno.env.get("AIRTABLE_API_KEY");
const defaultBaseId = "app1wLB4irV14mxMU";
const defaultTableName = "ExampleItems";
const defaultContentColumn = "Content";
const defaultEmbeddingColumn = "Embeddings";
async function fetchAirtableData(baseId, tableName, contentColumn, embeddingColumn) {
const base = new Airtable({ apiKey: airtableApiKey }).base(baseId);
const records = await base(tableName).select().all();
const documents = records.map(record => ({
id: record.id,
content: record.get(contentColumn),
embedding: record.get(embeddingColumn).split(",").map(parseFloat)
}));
return documents;
}
app.get('/search', async (c) => {
const query = c.req.query('query');
const similarityThreshold = parseFloat(c.req.query('similarity_threshold') || '0.7');
const maxResults = parseInt(c.req.query('max_results') || '3');
const baseId = c.req.query('base_id') || defaultBaseId;
const tableName = c.req.query('table_name') || defaultTableName;
const contentColumn = c.req.query('content_column') || defaultContentColumn;
const embeddingColumn = c.req.query('embedding_column') || defaultEmbeddingColumn;
try {
const documents = await fetchAirtableData(baseId, tableName, contentColumn, embeddingColumn);
await semanticSearch.addDocuments(documents);
const results = await semanticSearch.search(query, similarityThreshold, maxResults);
return c.json(results);
} catch (error) {
console.error('Search error:', error);
return c.text('Error occurred during search', 500);
}
});
app.post('/search', async (c) => {
const { query, similarity_threshold, max_results, base_id, table_name, content_column, embedding_column } = await c.req.json();
const similarityThreshold = similarity_threshold || 0.7;
const maxResults = max_results || 3;
const baseId = base_id || defaultBaseId;
const tableName = table_name || defaultTableName;
const contentColumn = content_column || defaultContentColumn;
const embeddingColumn = embedding_column || defaultEmbeddingColumn;
try {
const documents = await fetchAirtableData(baseId, tableName, contentColumn, embeddingColumn);
await semanticSearch.addDocuments(documents);
const results = await semanticSearch.search(query, similarityThreshold, maxResults);
return c.json(results);
} catch (error) {
console.error('Search error:', error);
return c.text('Error occurred during search', 500);
}
});
export default app.fetch;
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 5, 2024