Skip to content

Commit

Permalink
Compiled packaged sqlite3 library with fts5 support (#9)
Browse files Browse the repository at this point in the history
* Compiled Sqlite3 with FTS5 support

* Added demo code for FTS5
  • Loading branch information
adityawasudeo authored Nov 27, 2024
1 parent 6ddf0d6 commit 7dd6b5c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
5 changes: 4 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ let package = Package(
),
.target(
name: "CSQLiteVec",
publicHeadersPath: "include"
publicHeadersPath: "include",
cSettings: [
.define("SQLITE_ENABLE_FTS5")
]
),
.testTarget(
name: "SQLiteVecTests",
Expand Down
40 changes: 34 additions & 6 deletions Sources/SQLiteVecCLI/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ import SQLiteVec
enum CLI {
static func main() async throws {
try SQLiteVec.initialize()
let data: [(index: Int, vector: [Float])] = [
(1, [0.1, 0.1, 0.1, 0.1]),
(2, [0.2, 0.2, 0.2, 0.2]),
(3, [0.3, 0.3, 0.3, 0.3]),
(4, [0.4, 0.4, 0.4, 0.4]),
(5, [0.5, 0.5, 0.5, 0.5]),
let data: [(index: Int, vector: [Float], title: String, content: String)] = [
(1, [0.1, 0.1, 0.1, 0.1], "Introduction to Machine Learning", "Machine learning is a subset of artificial intelligence..."),
(2, [0.2, 0.2, 0.2, 0.2], "Deep Learning Basics", "Deep learning uses neural networks to learn from data..."),
(3, [0.3, 0.3, 0.3, 0.3], "Natural Language Processing", "NLP combines linguistics and machine learning..."),
(4, [0.4, 0.4, 0.4, 0.4], "Computer Vision", "Computer vision enables machines to understand visual data..."),
(5, [0.5, 0.5, 0.5, 0.5], "Reinforcement Learning", "Reinforcement learning involves agents learning through interaction...")
]
let query: [Float] = [0.3, 0.3, 0.3, 0.3]
let textQuery = "learning"

let db = try Database(.inMemory)
try await db.execute("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[4])")
try await db.execute("""
CREATE VIRTUAL TABLE docs USING fts5(
title,
content,
tokenize='porter'
)
""")
for row in data {
try await db.execute(
"""
Expand All @@ -23,6 +32,13 @@ enum CLI {
""",
params: [row.index, row.vector]
)
try await db.execute(
"""
INSERT INTO docs(rowid, title, content)
VALUES (?, ?, ?)
""",
params: [row.index, row.title, row.content]
)
}
let result = try await db.query(
"""
Expand All @@ -35,5 +51,17 @@ enum CLI {
params: [query]
)
print(result)

let textResults = try await db.query(
"""
SELECT rowid, title, content, rank
FROM docs
WHERE docs MATCH ?
ORDER BY rank
LIMIT 3
""",
params: [textQuery]
)
print(textResults)
}
}

0 comments on commit 7dd6b5c

Please sign in to comment.