Skip to content

Commit

Permalink
fix: faiss installation with yaspin spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnfluegge committed Sep 26, 2023
1 parent 6f00743 commit e8dc67d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 12 deletions.
9 changes: 2 additions & 7 deletions codeqai/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from yaspin import yaspin

from codeqai import codeparser, repo, utils
from codeqai.config import create_cache_dir, create_config, get_cache_path, load_config
from codeqai.config import (create_cache_dir, create_config, get_cache_path,
load_config)
from codeqai.constants import EmbeddingsModel, LllmHost
from codeqai.embeddings import Embeddings
from codeqai.llm import LLM
Expand Down Expand Up @@ -55,19 +56,13 @@ def run():
files = repo.load_files()
documents = codeparser.parse_code_files(files)
spinner.stop()
spinner = yaspin(text="💾 Indexing vector store...", color="green")
spinner.start()
vector_store = VectorStore(
repo_name,
embeddings=embeddings_model.embeddings,
documents=documents,
)
spinner.stop()
else:
spinner = yaspin(text="💾 Loading vector store...", color="green")
spinner.start()
vector_store = VectorStore(repo_name, embeddings=embeddings_model.embeddings)
spinner.stop()

llm = LLM(
llm_host=LllmHost[config["llm-host"].upper().replace("-", "_")],
Expand Down
4 changes: 4 additions & 0 deletions codeqai/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ def _install_sentence_transformers(self):
)
except subprocess.CalledProcessError as e:
print(f"Error during sentence_transformers installation: {e}")
else:
exit("sentence_transformers is required for local embeddings.")

def _install_instructor_embedding(self):
question = [
Expand Down Expand Up @@ -96,3 +98,5 @@ def _install_instructor_embedding(self):
)
except subprocess.CalledProcessError as e:
print(f"Error during sentence_transformers installation: {e}")
else:
exit("InstructorEmbedding is required for local embeddings.")
3 changes: 1 addition & 2 deletions codeqai/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,4 @@ def supports_metal():
)

else:
print("", "Installation cancelled. Exiting.", "")
return None
exit("llama-cpp-python is required for local LLM.")
13 changes: 11 additions & 2 deletions codeqai/vector_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from langchain.embeddings.base import Embeddings
from langchain.schema import Document
from langchain.vectorstores import FAISS
from yaspin import yaspin

from codeqai import utils
from codeqai.config import get_cache_path
Expand All @@ -15,27 +16,33 @@ def __init__(
self.embeddings = embeddings
self.install_faiss()
if not documents:
spinner = yaspin(text="💾 Loading vector store...", color="green")
spinner.start()
self.db = FAISS.load_local(
index_name=self.name,
folder_path=get_cache_path(),
embeddings=self.embeddings,
)
spinner.stop()
else:
spinner = yaspin(text="💾 Indexing vector store...", color="green")
spinner.start()
self.db = FAISS.from_documents(documents, embeddings)
self.db.save_local(index_name=self.name, folder_path=get_cache_path())
spinner.stop()
self.retriever = self.db.as_retriever(search_type="mmr", search_kwargs={"k": 8})

def similarity_search(self, query: str):
return self.db.similarity_search(query, k=4)

def install_faiss(self):
try:
import faiss
import faiss # noqa: F401
except ImportError:
question = [
inquirer.Confirm(
"confirm",
message=f"{utils.get_bold_text('FAISS')} not found in this python environment. Do you want to install it now?",
message=f"{utils.get_bold_text('faiss')} package not found in this python environment. Do you want to install it now?",
default=True,
),
]
Expand Down Expand Up @@ -72,3 +79,5 @@ def install_faiss(self):
)
except subprocess.CalledProcessError as e:
print(f"Error during faiss installation: {e}")
else:
exit("faiss package is required for codeqai to work.")
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "codeqai"
version = "0.0.1"
version = "0.0.2"
description = ""
authors = ["fynnfluegge <fynnfluegge@gmx.de>"]
readme = "README.md"
Expand Down

0 comments on commit e8dc67d

Please sign in to comment.