OpenAI/Cohere-style reranker: POST /v1/rerank {model, query, documents} → {results:[{index,relevance_score}]}.
( baseUrl: string, model: string, query: string, documents: string[], signal?: AbortSignal, )
| 119 | |
| 120 | /** OpenAI/Cohere-style reranker: POST /v1/rerank {model, query, documents} → {results:[{index,relevance_score}]}. */ |
| 121 | async function tryOpenAIRerank( |
| 122 | baseUrl: string, |
| 123 | model: string, |
| 124 | query: string, |
| 125 | documents: string[], |
| 126 | signal?: AbortSignal, |
| 127 | ): Promise<number[] | null> { |
| 128 | try { |
| 129 | const res = await proxyFetch(`${baseUrl.replace(/\/$/, '')}/v1/rerank`, { |
| 130 | method: 'POST', |
| 131 | headers: { 'Content-Type': 'application/json' }, |
| 132 | body: JSON.stringify({ model, query, documents, top_n: documents.length }), |
| 133 | signal, |
| 134 | }); |
| 135 | if (!res.ok) return null; |
| 136 | const data: any = await res.json(); |
| 137 | const arr = data?.results; |
| 138 | if (!Array.isArray(arr)) return null; |
| 139 | const scores = new Array<number>(documents.length).fill(0); |
| 140 | for (const r of arr) { |
| 141 | const idx = r.index; |
| 142 | const score = r.relevance_score ?? r.score; |
| 143 | if (typeof idx === 'number' && typeof score === 'number') scores[idx] = score; |
| 144 | } |
| 145 | return scores; |
| 146 | } catch (e: any) { |
| 147 | logger.debug('OpenAI-style rerank not available', { err: e?.message }); |
| 148 | return null; |
| 149 | } |
| 150 | } |
no test coverage detected