Skip to content

Commit bf8e3c0

Browse files
DARREN OBERSTDARREN OBERST
authored andcommitted
consistent optional imports for all vector db
1 parent d358ca5 commit bf8e3c0

File tree

6 files changed

+40
-9
lines changed

6 files changed

+40
-9
lines changed

‎examples/Embedding/embeddings_fast_start.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
"""
33
# This example shows the general recipe for creating an embedding. This scenario uses ChromaDB in local
44
file mode for no-install laptop deployment.
5+
6+
NOTE: you may need to install separately: pip3 install chromadb.
7+
58
"""
69

710

811
import os
912
from llmware.library import Library
1013
from llmware.retrieval import Query
1114
from llmware.setup import Setup
15+
from importlib import util
16+
if not util.find_spec("chromadb"):
17+
print("\nto run this example with chromadb, you need to install the chromadb python sdk: pip3 install chromadb")
1218

1319

1420
def embeddings_fast_start (library_name, vector_db="chromadb"):
@@ -56,7 +62,7 @@ def embeddings_fast_start (library_name, vector_db="chromadb"):
5662

5763
if __name__ == "__main__":
5864

59-
# set to 'chromadb' local file storage for no-install fast start
65+
# set to 'chromadb' local file storage for no-install fast start
6066
db = "chromadb"
6167
embeddings_fast_start("embedding_test_1", vector_db=db)
6268

‎fast_start/example-2-build_embeddings.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
Note: we have updated the no-install vector db option to 'chromadb' from 'faiss' starting in
1515
llmware>=0.2.12, due to better support on Python 3.12
1616
17+
Note: you may need to install chromadb's python driver: `pip3 install chromadb`
18+
1719
-- This same basic recipe will work with any of the vector db and collection db by simply changing the name
1820
1921
"""
@@ -27,6 +29,10 @@
2729
from llmware.models import ModelCatalog
2830
from llmware.configs import LLMWareConfig
2931

32+
from importlib import util
33+
if not util.find_spec("chromadb"):
34+
print("\nto run this example with chromadb, you need to install the chromadb python sdk: pip3 install chromadb")
35+
3036

3137
def setup_library(library_name):
3238

@@ -138,7 +144,7 @@ def install_vector_embeddings(library, embedding_model_name):
138144
# library = Library().load_library("example1_library")
139145

140146
# alternatively, to use this example as self-contained, then create a new library from scratch:
141-
library = setup_library("example2_lib")
147+
library = setup_library("example2_library")
142148

143149
# Step 2 - Select any embedding model in the LLMWare catalog
144150

‎fast_start/example-5-rag-semantic-query.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
3. Select the most relevant results by document.
1313
4. Loop through all of the documents - packaging the context and asking our questions to the LLM.
1414
15+
NOTE: to use chromadb, you may need to install the python sdk: pip3 install chromadb.
16+
1517
"""
1618

1719

@@ -22,6 +24,9 @@
2224
from llmware.status import Status
2325
from llmware.prompts import Prompt
2426
from llmware.configs import LLMWareConfig
27+
from importlib import util
28+
if not util.find_spec("chromadb"):
29+
print("\nto run this example with chromadb, you need to install the chromadb python sdk: pip3 install chromadb")
2530

2631

2732
def semantic_rag (library_name, embedding_model_name, llm_model_name):
@@ -119,13 +124,14 @@ def semantic_rag (library_name, embedding_model_name, llm_model_name):
119124
# --if you are using a Python version before 3.12, please feel free to substitute for "faiss"
120125
# --for versions of Python >= 3.12, for the Fast Start examples (e.g., no install required), we
121126
# recommend using chromadb or lancedb
127+
122128
# please double-check: `pip3 install chromadb` or pull the latest llmware version to get automatically
123129
# -- if you have installed any other vector db, just change the name, e.g, "milvus" or "pg_vector"
124130

125131
vector_db = "chromadb"
126132

127133
# pick any name for the library
128-
lib_name = "example5_library"
134+
lib_name = "example_5_library"
129135

130136
example_models = ["llmware/bling-1b-0.1", "llmware/bling-tiny-llama-v0", "llmware/dragon-yi-6b-gguf"]
131137

‎llmware/embeddings.py‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1212
# implied. See the License for the specific language governing
1313
# permissions and limitations under the License.
14+
1415
"""The embeddings module implements the supported vector databases.
1516
1617
The common abstraction for all supported vector databases is the EmbeddingHandler class, which supports
@@ -26,10 +27,15 @@
2627
import time
2728
import uuid
2829
import itertools
30+
from importlib import util
2931

30-
from pymilvus import connections, utility, FieldSchema, CollectionSchema, DataType, Collection
3132
from pymongo import MongoClient
3233

34+
try:
35+
from pymilvus import connections, utility, FieldSchema, CollectionSchema, DataType, Collection
36+
except ImportError:
37+
pass
38+
3339
try:
3440
import faiss
3541
except ImportError:
@@ -384,6 +390,7 @@ def unset_text_index(self):
384390

385391

386392
class EmbeddingMilvus:
393+
387394
"""Implements the vector database Milvius.
388395
389396
``EmbeddingMivlus`` implements the interface to the ``Milvus`` vector store. It is used by the
@@ -408,6 +415,7 @@ class EmbeddingMilvus:
408415
embedding_milvus : EmbeddingMilvus
409416
A new ``EmbeddingMilvus`` object.
410417
"""
418+
411419
def __init__(self, library, model=None, model_name=None, embedding_dims=None):
412420

413421
self.library = library
@@ -416,6 +424,10 @@ def __init__(self, library, model=None, model_name=None, embedding_dims=None):
416424
self.milvus_alias = "default"
417425

418426
# Connect to milvus
427+
# Instantiate client.
428+
if not util.find_spec("pymilvus"):
429+
raise DependencyNotInstalledException("pip3 install pymilvus")
430+
419431
connections.connect(self.milvus_alias,
420432
host=MilvusConfig.get_config("host"),
421433
port=MilvusConfig.get_config("port"),
@@ -2400,6 +2412,9 @@ def __init__(self, library, model=None, model_name=None, embedding_dims=None):
24002412
host = ChromaDBConfig.get_config('host')
24012413

24022414
# Instantiate client.
2415+
if not util.find_spec("chromadb"):
2416+
raise DependencyNotInstalledException("pip3 install chromadb")
2417+
24032418
if host is None and persistent_path is None:
24042419
self.client = chromadb.EphemeralClient()
24052420

‎llmware/requirements.txt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ datasets==2.15.0
33
huggingface-hub==0.19.4
44
numpy>=1.23.2
55
openai>=1.0
6-
pymilvus>=2.3.0
76
pymongo>=4.7.0
87
sentence-transformers==2.2.2
98
tabulate==0.9.0
@@ -18,7 +17,6 @@ pgvector==0.2.4
1817
colorama==0.4.6
1918
einops==0.7.0
2019
librosa>=0.10.0
21-
chromadb>=0.4.22
2220

2321
requests~=2.31.0
2422
tqdm~=4.66.1

‎setup.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ def glob_fix(package_name, glob):
5858
'huggingface-hub==0.19.4',
5959
'numpy>=1.23.2',
6060
'openai>=1.0.0',
61-
'pymilvus>=2.3.0',
6261
'pymongo>=4.7.0',
6362
'sentence-transformers==2.2.2',
6463
'tabulate==0.9.0',
@@ -72,11 +71,12 @@ def glob_fix(package_name, glob):
7271
'pgvector==0.2.4',
7372
'colorama==0.4.6',
7473
'einops==0.7.0',
75-
'librosa>=0.10.0',
76-
'chromadb>=0.4.22'
74+
'librosa>=0.10.0'
7775
],
7876

7977
extras_require={
78+
'milvus': ['pymilvus>=2.3.0'],
79+
'chromadb': ['chromadb>=0.4.22'],
8080
'pinecone': ['pinecone-client==3.0.0'],
8181
'lancedb' :['lancedb==0.5.0'],
8282
'qdrant': ['qdrant-client==1.7.0'],

0 commit comments

Comments
 (0)