|
| 1 | +import type { SortedResult } from '@/search'; |
| 2 | +import type Mixedbread from '@mixedbread/sdk'; |
| 3 | +import type { StoreSearchParams, StoreSearchResponse } from '@mixedbread/sdk/resources/stores'; |
| 4 | +import removeMd from 'remove-markdown'; |
| 5 | +import Slugger from 'github-slugger'; |
| 6 | +import { createEndpoint } from '@/search/orama/create-endpoint'; |
| 7 | +import type { SearchAPI } from '@/search/server'; |
| 8 | + |
| 9 | +export interface SearchMetadata { |
| 10 | + title?: string; |
| 11 | + description?: string; |
| 12 | + url?: string; |
| 13 | + tag?: string; |
| 14 | +} |
| 15 | + |
| 16 | +type StoreSearchResult = StoreSearchResponse['data'][number] & { |
| 17 | + generated_metadata: SearchMetadata; |
| 18 | +}; |
| 19 | + |
| 20 | +export interface MixedbreadSearchOptions { |
| 21 | + /** |
| 22 | + * The Mixedbread SDK client instance |
| 23 | + */ |
| 24 | + client: Mixedbread; |
| 25 | + |
| 26 | + /** |
| 27 | + * The identifier of the store to search in |
| 28 | + */ |
| 29 | + storeIdentifier: string; |
| 30 | + |
| 31 | + /** |
| 32 | + * Maximum number of results to return |
| 33 | + * |
| 34 | + * @defaultValue 10 |
| 35 | + */ |
| 36 | + topK?: number; |
| 37 | + |
| 38 | + /** |
| 39 | + * Re-rank search results for improved relevance |
| 40 | + * |
| 41 | + * @defaultValue true |
| 42 | + */ |
| 43 | + rerank?: boolean; |
| 44 | + |
| 45 | + /** |
| 46 | + * Rewrite the query for better search results |
| 47 | + */ |
| 48 | + rewriteQuery?: boolean; |
| 49 | + |
| 50 | + /** |
| 51 | + * Minimum score threshold for results |
| 52 | + */ |
| 53 | + scoreThreshold?: number; |
| 54 | + |
| 55 | + /** |
| 56 | + * Custom transform function for search results |
| 57 | + */ |
| 58 | + transform?: (results: StoreSearchResult[], query: string) => SortedResult[]; |
| 59 | +} |
| 60 | + |
| 61 | +const slugger = new Slugger(); |
| 62 | + |
| 63 | +function extractHeadingTitle(text: string): string { |
| 64 | + const trimmedText = text.trim(); |
| 65 | + |
| 66 | + if (!trimmedText.startsWith('#')) { |
| 67 | + return ''; |
| 68 | + } |
| 69 | + |
| 70 | + const lines = trimmedText.split('\n'); |
| 71 | + const firstLine = lines[0]?.trim(); |
| 72 | + |
| 73 | + if (firstLine) { |
| 74 | + return removeMd(firstLine, { |
| 75 | + useImgAltText: false, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return ''; |
| 80 | +} |
| 81 | + |
| 82 | +function defaultTransform(results: StoreSearchResult[]): SortedResult[] { |
| 83 | + return results.flatMap((item) => { |
| 84 | + const metadata = item.generated_metadata; |
| 85 | + |
| 86 | + const url = metadata.url || '#'; |
| 87 | + const title = metadata.title || 'Untitled'; |
| 88 | + |
| 89 | + const chunkResults: SortedResult[] = [ |
| 90 | + { |
| 91 | + id: `${item.file_id}-${item.chunk_index}-page`, |
| 92 | + type: 'page', |
| 93 | + content: title, |
| 94 | + url, |
| 95 | + }, |
| 96 | + ]; |
| 97 | + |
| 98 | + const headingTitle = item.type === 'text' ? extractHeadingTitle(item.text) : ''; |
| 99 | + |
| 100 | + if (headingTitle) { |
| 101 | + slugger.reset(); |
| 102 | + |
| 103 | + chunkResults.push({ |
| 104 | + id: `${item.file_id}-${item.chunk_index}-heading`, |
| 105 | + type: 'heading', |
| 106 | + content: headingTitle, |
| 107 | + url: `${url}#${slugger.slug(headingTitle)}`, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + return chunkResults; |
| 112 | + }); |
| 113 | +} |
| 114 | + |
| 115 | +export function createMixedbreadSearchAPI(options: MixedbreadSearchOptions): SearchAPI { |
| 116 | + const { |
| 117 | + client, |
| 118 | + storeIdentifier, |
| 119 | + topK = 10, |
| 120 | + rerank = true, |
| 121 | + rewriteQuery, |
| 122 | + scoreThreshold, |
| 123 | + transform, |
| 124 | + } = options; |
| 125 | + |
| 126 | + return createEndpoint({ |
| 127 | + async search(query, searchOptions) { |
| 128 | + if (!query.trim()) { |
| 129 | + return []; |
| 130 | + } |
| 131 | + |
| 132 | + const tag = searchOptions?.tag; |
| 133 | + |
| 134 | + let filters: StoreSearchParams['filters'] | undefined; |
| 135 | + if (Array.isArray(tag) && tag.length > 0) { |
| 136 | + filters = { |
| 137 | + key: 'generated_metadata.tag', |
| 138 | + operator: 'in', |
| 139 | + value: tag, |
| 140 | + }; |
| 141 | + } else if (typeof tag === 'string') { |
| 142 | + filters = { |
| 143 | + key: 'generated_metadata.tag', |
| 144 | + operator: 'eq', |
| 145 | + value: tag, |
| 146 | + }; |
| 147 | + } |
| 148 | + |
| 149 | + const res = await client.stores.search({ |
| 150 | + query, |
| 151 | + store_identifiers: [storeIdentifier], |
| 152 | + top_k: topK, |
| 153 | + filters, |
| 154 | + search_options: { |
| 155 | + return_metadata: true, |
| 156 | + rerank, |
| 157 | + rewrite_query: rewriteQuery, |
| 158 | + score_threshold: scoreThreshold, |
| 159 | + }, |
| 160 | + }); |
| 161 | + |
| 162 | + const results = res.data as StoreSearchResult[]; |
| 163 | + |
| 164 | + if (transform) { |
| 165 | + return transform(results, query); |
| 166 | + } |
| 167 | + |
| 168 | + return defaultTransform(results); |
| 169 | + }, |
| 170 | + async export() { |
| 171 | + throw new Error( |
| 172 | + 'Mixedbread search does not support exporting indexes. Use the Mixedbread dashboard to manage your store.', |
| 173 | + ); |
| 174 | + }, |
| 175 | + }); |
| 176 | +} |
0 commit comments