Skip to content

Commit

Permalink
refactor: knowledge base engine change to libsql
Browse files Browse the repository at this point in the history
  • Loading branch information
kangfenmao committed Dec 26, 2024
1 parent 77e0c51 commit 153e7a9
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
diff --git a/src/libsql-db.js b/src/libsql-db.js
index 58c42e4910bd0e53bc497ff9b9702b1f7a961266..250bc97c50a9b790e8798441d904d040f2d2af43 100644
--- a/src/libsql-db.js
+++ b/src/libsql-db.js
@@ -41,9 +41,9 @@ export class LibSqlDb {
}
async similaritySearch(query, k) {
const statement = `SELECT id, pageContent, uniqueLoaderId, source, metadata,
- vector_distance_cos(vector, vector32('[${query.join(',')}]'))
+ vector_distance_cos(vector, vector32('[${query.join(',')}]')) as distance
FROM ${this.tableName}
- ORDER BY vector_distance_cos(vector, vector32('[${query.join(',')}]')) ASC
+ ORDER BY distance ASC
LIMIT ${k};`;
this.debug(`Executing statement - ${truncateCenterString(statement, 700)}`);
const results = await this.client.execute(statement);
@@ -52,7 +52,7 @@ export class LibSqlDb {
return {
metadata,
pageContent: result.pageContent.toString(),
- score: 1,
+ score: 1 - result.distance,
};
});
}
10 changes: 3 additions & 7 deletions electron.vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default defineConfig({
'@llm-tools/embedjs-loader-xml',
'@llm-tools/embedjs-loader-pdf',
'@llm-tools/embedjs-loader-sitemap',
'@llm-tools/embedjs-lancedb'
'@llm-tools/embedjs-libsql'
]
}),
...visualizerPlugin('main')
Expand All @@ -34,9 +34,8 @@ export default defineConfig({
},
build: {
rollupOptions: {
external: ['@lancedb/lancedb']
},
minify: true
external: ['@libsql/client']
}
}
},
preload: {
Expand All @@ -52,9 +51,6 @@ export default defineConfig({
},
optimizeDeps: {
exclude: []
},
build: {
minify: true
}
}
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@electron-toolkit/utils": "^3.0.0",
"@electron/notarize": "^2.5.0",
"@llm-tools/embedjs": "patch:@llm-tools/embedjs@npm%3A0.1.25#~/.yarn/patches/@llm-tools-embedjs-npm-0.1.25-ec5645cf36.patch",
"@llm-tools/embedjs-lancedb": "^0.1.25",
"@llm-tools/embedjs-libsql": "patch:@llm-tools/embedjs-libsql@npm%3A0.1.25#~/.yarn/patches/@llm-tools-embedjs-libsql-npm-0.1.25-fad000d74c.patch",
"@llm-tools/embedjs-loader-csv": "^0.1.25",
"@llm-tools/embedjs-loader-markdown": "^0.1.25",
"@llm-tools/embedjs-loader-msoffice": "^0.1.25",
Expand Down
17 changes: 7 additions & 10 deletions scripts/build-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@ const { downloadNpmPackage } = require('./utils')
async function downloadNpm(platform) {
if (!platform || platform === 'mac') {
downloadNpmPackage(
'@lancedb/lancedb-darwin-arm64',
'https://registry.npmjs.org/@lancedb/lancedb-darwin-arm64/-/lancedb-darwin-arm64-0.14.0.tgz'
)
downloadNpmPackage(
'@lancedb/lancedb-darwin-x64',
'https://registry.npmjs.org/@lancedb/lancedb-darwin-x64/-/lancedb-darwin-x64-0.14.0.tgz'
'@libsql/darwin-arm64',
'https://registry.npmjs.org/@libsql/darwin-arm64/-/darwin-arm64-0.4.7.tgz'
)
downloadNpmPackage('@libsql/darwin-x64', 'https://registry.npmjs.org/@libsql/darwin-x64/-/darwin-x64-0.4.7.tgz')
}

if (!platform || platform === 'linux') {
downloadNpmPackage(
'@lancedb/lancedb-linux-arm64-gnu',
'https://registry.npmjs.org/@lancedb/lancedb-linux-arm64-gnu/-/lancedb-linux-arm64-gnu-0.14.0.tgz'
'@libsql/libsql-linux-arm64-gnu',
'https://registry.npmjs.org/@libsql/linux-arm64-gnu/-/linux-arm64-gnu-0.4.7.tgz'
)
downloadNpmPackage(
'@lancedb/lancedb-linux-x64-gnu',
'https://registry.npmjs.org/@lancedb/lancedb-linux-x64-gnu/-/lancedb-linux-x64-gnu-0.14.0.tgz'
'@libsql/libsql-linux-x64-gnu',
'https://registry.npmjs.org/@libsql/linux-x64-gnu/-/linux-x64-gnu-0.4.7.tgz'
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/services/KnowledgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'

import { LocalPathLoader, RAGApplication, RAGApplicationBuilder, TextLoader } from '@llm-tools/embedjs'
import type { AddLoaderReturn, ExtractChunkData } from '@llm-tools/embedjs-interfaces'
import { LanceDb } from '@llm-tools/embedjs-lancedb'
import { LibSqlDb } from '@llm-tools/embedjs-libsql'
import { MarkdownLoader } from '@llm-tools/embedjs-loader-markdown'
import { DocxLoader, ExcelLoader, PptLoader } from '@llm-tools/embedjs-loader-msoffice'
import { PdfLoader } from '@llm-tools/embedjs-loader-pdf'
Expand Down Expand Up @@ -44,7 +44,7 @@ class KnowledgeService {
batchSize: 20
})
)
.setVectorDatabase(new LanceDb({ path: path.join(this.storageDir, id) }))
.setVectorDatabase(new LibSqlDb({ path: path.join(this.storageDir, id) }))
.build()
}

Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/pages/knowledge/KnowledgeContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ const KnowledgeContent: FC<KnowledgeContentProps> = ({ selectedBase }) => {

return (
<MainContent>
{!base.dimensions && (
{!base?.version && (
<Alert message={t('knowledge_base.not_support')} type="error" style={{ marginBottom: 20 }} showIcon />
)}
<FileSection>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ const PopupContainer: React.FC<Props> = ({ title, resolve }) => {
dimensions,
items: [],
created_at: Date.now(),
updated_at: Date.now()
updated_at: Date.now(),
version: 1
}

await window.api.knowledgeBase.create(getKnowledgeBaseParams(newBase))
Expand Down
1 change: 1 addition & 0 deletions src/renderer/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ export interface KnowledgeBase {
items: KnowledgeItem[]
created_at: number
updated_at: number
version: number
}

export type KnowledgeBaseParams = {
Expand Down
Loading

0 comments on commit 153e7a9

Please sign in to comment.