WIP - enable embedings
This commit is contained in:
@@ -8,9 +8,22 @@ type MeiliConfig = {
|
||||
indexPrefix: string;
|
||||
};
|
||||
|
||||
type HybridSearchOptions = {
|
||||
embedder: string;
|
||||
semanticRatio?: number;
|
||||
};
|
||||
|
||||
type OpenAiEmbedderConfig = {
|
||||
embedderName: string;
|
||||
apiKey: string;
|
||||
model: string;
|
||||
documentTemplate: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class MeilisearchService {
|
||||
private readonly logger = new Logger(MeilisearchService.name);
|
||||
private readonly embedderCache = new Map<string, string>();
|
||||
|
||||
isEnabled(): boolean {
|
||||
return Boolean(this.getConfig());
|
||||
@@ -183,6 +196,7 @@ export class MeilisearchService {
|
||||
indexName: string,
|
||||
query: string,
|
||||
limit = 20,
|
||||
hybrid?: HybridSearchOptions,
|
||||
): Promise<{ hits: any[]; total: number }> {
|
||||
const config = this.getConfig();
|
||||
if (!config) return { hits: [], total: 0 };
|
||||
@@ -192,7 +206,11 @@ export class MeilisearchService {
|
||||
const response = await this.requestJson(
|
||||
'POST',
|
||||
url,
|
||||
{ q: query, limit },
|
||||
{
|
||||
q: query,
|
||||
limit,
|
||||
...(hybrid ? { hybrid } : {}),
|
||||
},
|
||||
this.buildHeaders(config),
|
||||
);
|
||||
|
||||
@@ -250,7 +268,7 @@ export class MeilisearchService {
|
||||
}
|
||||
|
||||
private requestJson(
|
||||
method: 'POST' | 'DELETE',
|
||||
method: 'POST' | 'DELETE' | 'PATCH',
|
||||
url: string,
|
||||
payload: any,
|
||||
headers: Record<string, string>,
|
||||
@@ -293,4 +311,49 @@ export class MeilisearchService {
|
||||
request.end();
|
||||
});
|
||||
}
|
||||
|
||||
async ensureOpenAiEmbedder(
|
||||
indexName: string,
|
||||
config: OpenAiEmbedderConfig,
|
||||
): Promise<void> {
|
||||
const meiliConfig = this.getConfig();
|
||||
if (!meiliConfig || !config?.apiKey) return;
|
||||
|
||||
const signature = JSON.stringify({
|
||||
embedderName: config.embedderName,
|
||||
model: config.model,
|
||||
documentTemplate: config.documentTemplate,
|
||||
apiKey: config.apiKey,
|
||||
});
|
||||
const cacheKey = `${indexName}:${config.embedderName}`;
|
||||
if (this.embedderCache.get(cacheKey) === signature) {
|
||||
return;
|
||||
}
|
||||
|
||||
const url = `${meiliConfig.host}/indexes/${encodeURIComponent(indexName)}/settings/embedders`;
|
||||
try {
|
||||
const response = await this.requestJson(
|
||||
'PATCH',
|
||||
url,
|
||||
{
|
||||
[config.embedderName]: {
|
||||
source: 'openAi',
|
||||
model: config.model,
|
||||
apiKey: config.apiKey,
|
||||
documentTemplate: config.documentTemplate,
|
||||
},
|
||||
},
|
||||
this.buildHeaders(meiliConfig),
|
||||
);
|
||||
if (!this.isSuccessStatus(response.status)) {
|
||||
this.logger.warn(
|
||||
`Meilisearch embedder update failed for index ${indexName}: ${response.status}`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
this.embedderCache.set(cacheKey, signature);
|
||||
} catch (error) {
|
||||
this.logger.warn(`Meilisearch embedder update failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user