mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2025-12-26 11:44:28 +08:00
Co-authored-by: icarus <eurfelux@gmail.com> Co-authored-by: eeee0717 <chentao020717@outlook.com>
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 { 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 }
|
|
})
|
|
}
|
|
}
|