Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add ollama embedding config and fix sqlite_vec db #1255

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions llama_stack/providers/inline/vector_io/sqlite_vec/sqlite_vec.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
import sqlite3
import struct
import threading
import uuid
from typing import Any, Dict, List, Optional

Expand Down Expand Up @@ -158,11 +159,21 @@ def __init__(self, config, inference_api: Api.inference) -> None:
self.config = config
self.inference_api = inference_api
self.cache: Dict[str, VectorDBWithIndex] = {}
self.connection: Optional[sqlite3.Connection] = None
self._local = threading.local()

def _get_connection(self):
"""Get a thread-local database connection."""
if not hasattr(self._local, "conn"):
try:
self._local.conn = sqlite3.connect(self.config.db_path)
except Exception as e:
print(f"Error connecting to SQLite database: {e}")
raise e
return self._local.conn

async def initialize(self) -> None:
# Open a connection to the SQLite database (the file is specified in the config).
self.connection = sqlite3.connect(self.config.db_path)
self.connection = self._get_connection()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this isn't quite right. you cannot assign whatever value you get into an instance variable. you must use conn = self._get_connection() anywhere you need to use a connection.

self.connection.enable_load_extension(True)
sqlite_vec.load(self.connection)
self.connection.enable_load_extension(False)
Expand All @@ -185,9 +196,14 @@ async def initialize(self) -> None:
self.cache[vector_db.identifier] = VectorDBWithIndex(vector_db, index, self.inference_api)

async def shutdown(self) -> None:
if self.connection:
self.connection.close()
self.connection = None
# We can't access other threads' connections, so we just close our own
if hasattr(self._local, "conn"):
try:
self._local.conn.close()
except Exception as e:
print(f"Error closing SQLite connection: {e}")
finally:
del self._local.conn

async def register_vector_db(self, vector_db: VectorDB) -> None:
if self.connection is None:
Expand Down
2 changes: 1 addition & 1 deletion llama_stack/templates/ollama/ollama.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_distribution_template() -> DistributionTemplate:
"inference": [inference_provider],
"vector_io": [vector_io_provider_sqlite],
},
default_models=[inference_model],
default_models=[inference_model, embedding_model],
default_tool_groups=default_tool_groups,
),
"run-with-safety.yaml": RunConfigSettings(
Expand Down
6 changes: 6 additions & 0 deletions llama_stack/templates/ollama/run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ models:
model_id: ${env.INFERENCE_MODEL}
provider_id: ollama
model_type: llm
- metadata:
embedding_dimension: 384
model_id: all-MiniLM-L6-v2
provider_id: ollama
provider_model_id: all-minilm:latest
model_type: embedding
shields: []
vector_dbs: []
datasets: []
Expand Down