mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-11-22 21:18:15 +00:00
42 lines
993 B
TypeScript
42 lines
993 B
TypeScript
import OpenAI from 'openai';
|
|
import BaseEmbedding from '../../base/embedding';
|
|
|
|
type OpenAIConfig = {
|
|
apiKey: string;
|
|
model: string;
|
|
baseURL?: string;
|
|
};
|
|
|
|
class OpenAIEmbedding extends BaseEmbedding<OpenAIConfig> {
|
|
openAIClient: OpenAI;
|
|
|
|
constructor(protected config: OpenAIConfig) {
|
|
super(config);
|
|
|
|
this.openAIClient = new OpenAI({
|
|
apiKey: config.apiKey,
|
|
baseURL: config.baseURL,
|
|
});
|
|
}
|
|
|
|
async embedText(texts: string[]): Promise<number[][]> {
|
|
const response = await this.openAIClient.embeddings.create({
|
|
model: this.config.model,
|
|
input: texts,
|
|
});
|
|
|
|
return response.data.map((embedding) => embedding.embedding);
|
|
}
|
|
|
|
async embedChunks(chunks: Chunk[]): Promise<number[][]> {
|
|
const response = await this.openAIClient.embeddings.create({
|
|
model: this.config.model,
|
|
input: chunks.map((c) => c.content),
|
|
});
|
|
|
|
return response.data.map((embedding) => embedding.embedding);
|
|
}
|
|
}
|
|
|
|
export default OpenAIEmbedding;
|