mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2026-01-03 01:56:56 +00:00
feat(transformer-provider): specify dtype
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { Chunk } from '@/lib/types';
|
import { Chunk } from '@/lib/types';
|
||||||
import BaseEmbedding from '../../base/embedding';
|
import BaseEmbedding from '../../base/embedding';
|
||||||
import { FeatureExtractionPipeline, pipeline } from '@huggingface/transformers';
|
import { FeatureExtractionPipeline } from '@huggingface/transformers';
|
||||||
|
|
||||||
type TransformerConfig = {
|
type TransformerConfig = {
|
||||||
model: string;
|
model: string;
|
||||||
@@ -21,21 +21,19 @@ class TransformerEmbedding extends BaseEmbedding<TransformerConfig> {
|
|||||||
return this.embed(chunks.map((c) => c.content));
|
return this.embed(chunks.map((c) => c.content));
|
||||||
}
|
}
|
||||||
|
|
||||||
async embed(texts: string[]): Promise<number[][]> {
|
private async embed(texts: string[]) {
|
||||||
if (!this.pipelinePromise) {
|
if (!this.pipelinePromise) {
|
||||||
this.pipelinePromise = (async () => {
|
this.pipelinePromise = (async () => {
|
||||||
const transformers = await import('@huggingface/transformers');
|
const { pipeline } = await import('@huggingface/transformers');
|
||||||
return (await transformers.pipeline(
|
const result = await pipeline('feature-extraction', this.config.model, {
|
||||||
'feature-extraction',
|
dtype: 'fp32',
|
||||||
this.config.model,
|
});
|
||||||
)) as unknown as FeatureExtractionPipeline;
|
return result as FeatureExtractionPipeline;
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
||||||
const pipeline = await this.pipelinePromise;
|
const pipe = await this.pipelinePromise;
|
||||||
|
const output = await pipe(texts, { pooling: 'mean', normalize: true });
|
||||||
const output = await pipeline(texts, { pooling: 'mean', normalize: true });
|
|
||||||
|
|
||||||
return output.tolist() as number[][];
|
return output.tolist() as number[][];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user