Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

feat: add embedding flag to model metadata and automatic detection (#7031)

Akarshan Biswasddbf00b6c9a0
diff --git a/core/src/browser/extensions/engines/AIEngine.ts b/core/src/browser/extensions/engines/AIEngine.ts
index 1be977034..2ef9e99ef 100644
--- a/core/src/browser/extensions/engines/AIEngine.ts
+++ b/core/src/browser/extensions/engines/AIEngine.ts
@@ -171,6 +171,7 @@ export interface modelInfo {
tags?: string[]
path?: string // Absolute path to the model file, if applicable
// Additional provider-specific metadata can be added here
+ embedding?: boolean
[key: string]: any
}
diff --git a/extensions/llamacpp-extension/src/index.ts b/extensions/llamacpp-extension/src/index.ts
index ee50c4bc4..d46e796e9 100644
--- a/extensions/llamacpp-extension/src/index.ts
+++ b/extensions/llamacpp-extension/src/index.ts
@@ -113,6 +113,7 @@ interface ModelConfig {
sha256?: string
mmproj_sha256?: string
mmproj_size_bytes?: number
+ embedding?: boolean
}
interface EmbeddingResponse {
@@ -1037,6 +1038,8 @@ export default class llamacpp_extension extends AIEngine {
path,
})
+ const isEmbedding = await this.resolveEmbeddingConfig(modelId, modelConfig)
+
return {
id: modelId,
name: modelConfig.name ?? modelId,
@@ -1044,9 +1047,71 @@ export default class llamacpp_extension extends AIEngine {
providerId: this.provider,
port: 0, // port is not known until the model is loaded
sizeBytes: modelConfig.size_bytes ?? 0,
+ embedding: isEmbedding,
} as modelInfo
}
+ /**
+ * Checks if embedding status is known. If not, reads GGUF, detects it,
+ * and updates the model.yml for future performance.
+ */
+ private async resolveEmbeddingConfig(
+ modelId: string,
+ modelConfig: ModelConfig
+ ): Promise<boolean> {
+ // Fast exit: if explicitly set in config, return it
+ if (typeof modelConfig.embedding === 'boolean') {
+ return modelConfig.embedding
+ }
+
+ // Migration logic: Detect from GGUF
+ let isEmbedding = false
+ try {
+ const janDataFolderPath = await getJanDataFolderPath()
+ const fullModelPath = await joinPath([
+ janDataFolderPath,
+ modelConfig.model_path,
+ ])
+
+ if (await fs.existsSync(fullModelPath)) {
+ const metadata = await readGgufMetadata(fullModelPath)
+ // Check for BERT-based architectures usually used for embeddings
+ // You can expand this list (e.g., 'nomic-bert', 'xlm-roberta')
+ const arch = metadata.metadata['general.architecture']
+ if (arch === 'bert' || arch === 'nomic-bert') {
+ isEmbedding = true
+ }
+ }
+ } catch (e) {
+ // If GGUF read fails, default to false but log it
+ logger.warn(`Failed to check metadata for ${modelId}`, e)
+ return false
+ }
+
+ // Persist the result back to model.yml so we don't read GGUF next time
+ try {
+ const configPath = await joinPath([
+ await this.getProviderPath(),
+ 'models',
+ modelId,
+ 'model.yml',
+ ])
+
+ // Update the local object
+ modelConfig.embedding = isEmbedding
+
+ // Write to disk
+ await invoke<void>('write_yaml', {
+ data: modelConfig,
+ savePath: configPath,
+ })
+ } catch (e) {
+ logger.warn(`Failed to update config for ${modelId}`, e)
+ }
+
+ return isEmbedding
+ }
+
// Implement the required LocalProvider interface methods
override async list(): Promise<modelInfo[]> {
const modelsDir = await joinPath([await this.getProviderPath(), 'models'])
@@ -1089,6 +1154,7 @@ export default class llamacpp_extension extends AIEngine {
for (const modelId of modelIds) {
const path = await joinPath([modelsDir, modelId, 'model.yml'])
const modelConfig = await invoke<ModelConfig>('read_yaml', { path })
+ const isEmbedding = await this.resolveEmbeddingConfig(modelId, modelConfig)
const modelInfo = {
id: modelId,
@@ -1097,6 +1163,7 @@ export default class llamacpp_extension extends AIEngine {
providerId: this.provider,
port: 0, // port is not known until the model is loaded
sizeBytes: modelConfig.size_bytes ?? 0,
+ embedding: isEmbedding,
} as modelInfo
modelInfos.push(modelInfo)
}
@@ -1474,6 +1541,7 @@ export default class llamacpp_extension extends AIEngine {
// Validate GGUF files
const janDataFolderPath = await getJanDataFolderPath()
const fullModelPath = await joinPath([janDataFolderPath, modelPath])
+ let isEmbedding = false
try {
// Validate main model file
@@ -1482,6 +1550,12 @@ export default class llamacpp_extension extends AIEngine {
`Model GGUF validation successful: version ${modelMetadata.version}, tensors: ${modelMetadata.tensor_count}`
)
+ // check if the model is an embedding model
+ const architecture = modelMetadata.metadata['general.architecture']
+ if (architecture === 'bert' || architecture === 'nomic-bert') {
+ isEmbedding = true
+ }
+
// Validate mmproj file if present
if (mmprojPath) {
const fullMmprojPath = await joinPath([janDataFolderPath, mmprojPath])
@@ -1518,6 +1592,7 @@ export default class llamacpp_extension extends AIEngine {
model_size_bytes: opts.modelSize,
mmproj_sha256: opts.mmprojSha256,
mmproj_size_bytes: opts.mmprojSize,
+ embedding: isEmbedding,
} as ModelConfig
await fs.mkdir(await joinPath([janDataFolderPath, modelDir]))
await invoke<void>('write_yaml', {
@@ -1533,6 +1608,7 @@ export default class llamacpp_extension extends AIEngine {
model_size_bytes: opts.modelSize,
mmproj_sha256: opts.mmprojSha256,
mmproj_size_bytes: opts.mmprojSize,
+ embedding: isEmbedding,
})
}
diff --git a/web-app/src/services/providers/tauri.ts b/web-app/src/services/providers/tauri.ts
index 7f1e7e622..8782d07d3 100644
--- a/web-app/src/services/providers/tauri.ts
+++ b/web-app/src/services/providers/tauri.ts
@@ -47,7 +47,7 @@ export class TauriProvidersService extends DefaultProvidersService {
const runtimeProviders: ModelProvider[] = []
for (const [providerName, value] of EngineManager.instance().engines) {
- const models = (await value.list()) ?? []
+ const models = await value.list().then(list => list.filter(e => !e.embedding)) ?? []
const provider: ModelProvider = {
active: false,
persist: true,