mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-25 11:20:07 +08:00
* style(linter): enable consistent-type-imports rule in typescript * chore: add biome to lint script for improved code formatting * chore: add oxlint-specific lint script for faster linting * refactor: use type-only imports for better type safety and clarity
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import type { BaseEmbeddings } from '@cherrystudio/embedjs-interfaces'
|
|
import { OllamaEmbeddings } from '@cherrystudio/embedjs-ollama'
|
|
import { OpenAiEmbeddings } from '@cherrystudio/embedjs-openai'
|
|
import { AzureOpenAiEmbeddings } from '@cherrystudio/embedjs-openai/src/azure-openai-embeddings'
|
|
import type { ApiClient } from '@types'
|
|
|
|
import { VoyageEmbeddings } from './VoyageEmbeddings'
|
|
|
|
export default class EmbeddingsFactory {
|
|
static create({ embedApiClient, dimensions }: { embedApiClient: ApiClient; dimensions?: number }): BaseEmbeddings {
|
|
const batchSize = 10
|
|
const { model, provider, apiKey, apiVersion, baseURL } = embedApiClient
|
|
if (provider === 'voyageai') {
|
|
return new VoyageEmbeddings({
|
|
modelName: model,
|
|
apiKey,
|
|
outputDimension: dimensions,
|
|
batchSize: 8
|
|
})
|
|
}
|
|
if (provider === 'ollama') {
|
|
if (baseURL.includes('v1/')) {
|
|
return new OllamaEmbeddings({
|
|
model: model,
|
|
baseUrl: baseURL.replace('v1/', ''),
|
|
requestOptions: {
|
|
// @ts-ignore expected
|
|
'encoding-format': 'float'
|
|
}
|
|
})
|
|
}
|
|
return new OllamaEmbeddings({
|
|
model: model,
|
|
baseUrl: baseURL,
|
|
requestOptions: {
|
|
// @ts-ignore expected
|
|
'encoding-format': 'float'
|
|
}
|
|
})
|
|
}
|
|
if (apiVersion !== undefined) {
|
|
return new AzureOpenAiEmbeddings({
|
|
azureOpenAIApiKey: apiKey,
|
|
azureOpenAIApiVersion: apiVersion,
|
|
azureOpenAIApiDeploymentName: model,
|
|
azureOpenAIEndpoint: baseURL,
|
|
dimensions,
|
|
batchSize
|
|
})
|
|
}
|
|
return new OpenAiEmbeddings({
|
|
model,
|
|
apiKey,
|
|
dimensions,
|
|
batchSize,
|
|
configuration: { baseURL }
|
|
})
|
|
}
|
|
}
|