- localization-svc: defaultLocale ru, resolveLocale only by geo - web-svc: DEFAULT_LOCALE ru, layout lang=ru, embeddedTranslations fallback ru - countryToLocale: default ru when no country or unknown country Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.0 KiB
TypeScript
43 lines
1.0 KiB
TypeScript
import OpenAI from 'openai';
|
|
import BaseEmbedding from '../../base/embedding.js';
|
|
import { Chunk } from '../../../types.js';
|
|
|
|
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;
|