22 Memory tools that use sqlite-vec as a vector database (combined w/ sqlite-lembed or sqlite-rembed for embeddings).
33
44 Note: it's best to run this in a silo w/:
5-
5+
66 ./examples/agent/serve_tools_inside_docker.sh
77
88 # Run w/o other tools:
9-
9+
1010 ## Prerequisites:
11-
11+
1212 pip install aiosqlite "fastapi[standard]" sqlite-lembed sqlite-rembed sqlite-vec uvicorn
13-
13+
1414 ## Usage w/ sqlite-rembed:
15-
15+
1616 ./llama-server --port 8081 -fa -c 0 --embeddings --rope-freq-scale 0.75 \
1717 -hfr nomic-ai/nomic-embed-text-v1.5-GGUF -hff nomic-embed-text-v1.5.Q4_K_M.gguf
1818 MEMORY_SQLITE_DB=memory_rembed.db \
1919 EMBEDDINGS_DIMS=768 \
2020 EMBEDDINGS_ENDPOINT=http://localhost:8081/v1/embeddings \
2121 python examples/agent/tools/memory.py
22-
22+
2323 ## Usage w/ sqlite-lembed:
24-
24+
2525 MEMORY_SQLITE_DB=memory_lembed.db \
2626 EMBEDDINGS_DIMS=768 \
2727 EMBEDDINGS_MODEL_FILE=~/Library/Caches/llama.cpp/nomic-embed-text-v1.5.Q4_K_M.gguf \
2828 python examples/agent/tools/memory.py
2929
3030 ## Test:
31-
31+
3232 curl -X POST "http://localhost:8000/memorize" -H "Content-Type: application/json" -d '["User is Olivier Chafik", "User is a Software Engineer"]'
3333 curl -X POST "http://localhost:8000/search_memory?text=What%20do%20we%20do%3F"
3434'''
6565
6666
6767async def setup_db (db : aiosqlite .Connection ):
68-
68+
6969 await db .enable_load_extension (True )
7070 await db .load_extension (sqlite_vec .loadable_path ())
7171 if local :
@@ -75,7 +75,7 @@ async def setup_db(db: aiosqlite.Connection):
7575 await db .enable_load_extension (False )
7676
7777 client_name = 'default'
78-
78+
7979 if local :
8080 await db .execute (f'''
8181 INSERT INTO lembed_models(name, model) VALUES (
@@ -88,7 +88,7 @@ async def setup_db(db: aiosqlite.Connection):
8888 '{ client_name } ', rembed_client_options('format', 'llamafile', 'url', ?, 'key', ?)
8989 );
9090 ''' , (embeddings_endpoint , embeddings_api_key ))
91-
91+
9292 async def create_vector_index (table_name , text_column , embedding_column ):
9393 '''
9494 Create an sqlite-vec virtual table w/ an embedding column
@@ -145,7 +145,7 @@ def search(text: str, top_n: int, columns: list[str] = ['rowid', text_column]):
145145 JOIN { table_name } USING (rowid)
146146 ''' ,
147147 (text , top_n )
148- )
148+ )
149149 return search
150150
151151 await db .execute ('''
@@ -155,9 +155,9 @@ def search(text: str, top_n: int, columns: list[str] = ['rowid', text_column]):
155155 )
156156 ''' )
157157 facts_search = await create_vector_index ('facts' , 'content' , 'embedding' )
158-
158+
159159 await db .commit ()
160-
160+
161161 return dict (
162162 facts_search = facts_search ,
163163 )
@@ -185,7 +185,7 @@ async def search_memory(text: str, top_n: int = 10):
185185 results = await cursor .fetchall ()
186186 cols = [c [0 ] for c in cursor .description ]
187187 return [dict (zip (cols , row )) for row in results ]
188-
188+
189189
190190# This main entry point is just here for easy debugging
191191if __name__ == '__main__' :
0 commit comments