11import Foundation
22import SQLiteVec
33
4+ struct Item {
5+ let index : Int
6+ let vector : [ Float ]
7+ let title : String
8+ let content : String
9+ }
10+
411@main
512enum CLI {
613 static func main( ) async throws {
714 try SQLiteVec . initialize ( )
8- let data : [ ( index: Int , vector: [ Float ] , title: String , content: String ) ] = [
9- ( 1 , [ 0.1 , 0.1 , 0.1 , 0.1 ] , " Introduction to Machine Learning " , " Machine learning is a subset of artificial intelligence... " ) ,
10- ( 2 , [ 0.2 , 0.2 , 0.2 , 0.2 ] , " Deep Learning Basics " , " Deep learning uses neural networks to learn from data... " ) ,
11- ( 3 , [ 0.3 , 0.3 , 0.3 , 0.3 ] , " Natural Language Processing " , " NLP combines linguistics and machine learning... " ) ,
12- ( 4 , [ 0.4 , 0.4 , 0.4 , 0.4 ] , " Computer Vision " , " Computer vision enables machines to understand visual data... " ) ,
13- ( 5 , [ 0.5 , 0.5 , 0.5 , 0.5 ] , " Reinforcement Learning " , " Reinforcement learning involves agents learning through interaction... " )
15+ let data = [
16+ Item (
17+ index: 1 ,
18+ vector: [ 0.1 , 0.1 , 0.1 , 0.1 ] ,
19+ title: " Introduction to Machine Learning " ,
20+ content: " Machine learning is a subset of artificial intelligence... "
21+ ) ,
22+ Item (
23+ index: 2 ,
24+ vector: [ 0.2 , 0.2 , 0.2 , 0.2 ] ,
25+ title: " Deep Learning Basics " ,
26+ content: " Deep learning uses neural networks to learn from data... "
27+ ) ,
28+ Item (
29+ index: 3 ,
30+ vector: [ 0.3 , 0.3 , 0.3 , 0.3 ] ,
31+ title: " Natural Language Processing " ,
32+ content: " NLP combines linguistics and machine learning... "
33+ ) ,
34+ Item (
35+ index: 4 ,
36+ vector: [ 0.4 , 0.4 , 0.4 , 0.4 ] ,
37+ title: " Computer Vision " ,
38+ content: " Computer vision enables machines to understand visual data... "
39+ ) ,
40+ Item (
41+ index: 5 ,
42+ vector: [ 0.5 , 0.5 , 0.5 , 0.5 ] ,
43+ title: " Reinforcement Learning " ,
44+ content: " Reinforcement learning involves agents learning through interaction... "
45+ ) ,
1446 ]
1547 let query : [ Float ] = [ 0.3 , 0.3 , 0.3 , 0.3 ]
1648 let textQuery = " learning "
1749
1850 let db = try Database ( . inMemory)
1951 try await db. execute ( " CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[4]) " )
20- try await db. execute ( """
21- CREATE VIRTUAL TABLE docs USING fts5(
22- title,
23- content,
24- tokenize='porter'
25- )
26- """ )
52+ try await db. execute (
53+ """
54+ CREATE VIRTUAL TABLE docs USING fts5(
55+ title,
56+ content,
57+ tokenize='porter'
58+ )
59+ """ )
2760 for row in data {
2861 try await db. execute (
2962 """
30- INSERT INTO vec_items(rowid, embedding)
63+ INSERT INTO vec_items(rowid, embedding)
3164 VALUES (?, ?)
3265 """ ,
3366 params: [ row. index, row. vector]
@@ -42,10 +75,10 @@ enum CLI {
4275 }
4376 let result = try await db. query (
4477 """
45- SELECT rowid, distance
46- FROM vec_items
47- WHERE embedding MATCH ?
48- ORDER BY distance
78+ SELECT rowid, distance
79+ FROM vec_items
80+ WHERE embedding MATCH ?
81+ ORDER BY distance
4982 LIMIT 3
5083 """ ,
5184 params: [ query]
0 commit comments