continue-custom/server/continuedev/libs/chroma/query.py
Nate Sesti 9b23dd9c5d
✨ Codebase Embeddings (#547)
* feat: ✨ basic codebase embeddings

* feat: 🚧 reranking after chroma retrieval

* refactor: 🚚 rename binary: run to continue_server

* ci: 💚 only do pypi step in github workflow

* ci: 🏷 Update PyPI version [skip ci]

* feat: 🐛 add openai_api_key param for chroma steps

* ci: 🏷 Update PyPI version [skip ci]

* build: 💚 update pypi package

* ci: 🏷 Update PyPI version [skip ci]

* fix: 🐛 rename run.spec

* ci: 🏷 Update PyPI version [skip ci]

* feat: ✨ params to control embeddings search

* ci: 🏷 Update PyPI version [skip ci]

* rename run.py

* feat: 🚧 improvements in progress for embeddings

* feat: 🎨 last bit of cleaning for chunked embeddings

* ci: 💚 add back pypi deployment in main.yam

* ci: 🏷 Update PyPI version [skip ci]

* fix: 🐛 error handling for chroma index in non-git folder

* ci: 🏷 Update PyPI version [skip ci]

* fix: 🐛 don't log reranking prompt

* ci: 🏷 Update PyPI version [skip ci]

* feat: ✨ add option to select embeddings model

* ci: 🏷 Update PyPI version [skip ci]

* move path decode/encode functions

* ci: 🏷 Update PyPI version [skip ci]

* update spec file

* Preview (#541)

* Strong typing (#533)

* refactor: ♻️ get rid of continuedev.src.continuedev structure

* refactor: ♻️ switching back to server folder

* feat: ✨ make config.py imports shorter

* feat: 🔖 publish as pre-release vscode extension

* refactor: ♻️ refactor and add more completion params to ui

* build: 🏗️ download from preview S3

* fix: 🐛 fix paths

* fix: 💚 package:pre-release

* ci: 💚 more time for tests

* fix: 💚 fix build scripts

* fix: 🐛 fix import in run.py

* fix: 🔖 update version to try again

* ci: 💚 Update package.json version [skip ci]

* refactor: 🔥 don't check for old extensions version

* fix: 🐛 small bug fixes

* fix: 🐛 fix config.py import paths

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 platform-specific builds test #1

* feat: 💚 ship with binary

* fix: 💚 fix copy statement to include.exe for windows

* fix: 💚 cd extension before packaging

* chore: 🔊 count tokens generated

* fix: 💚 remove npm_config_arch

* fix: 💚 publish as pre-release!

* chore: 🔖 update version

* perf: 💚 hardcode distro paths

* fix: 🐛 fix yaml syntax error

* chore: 🔖 update version

* fix: 💚 update permissions and version

* feat: 🐛 kill old server if needed

* feat: 💄 update marketplace icon for pre-release

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ auto-reload for config.py

* feat: 🔧 update default config.py imports

* feat: ✨ codelens in config.py

* feat: ✨ select model param count from UI

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ more model options, ollama error handling

* perf: ⚡ don't show server loading immediately

* fix: 🐛 fixing small UI details

* ci: 💚 Update package.json version [skip ci]

* feat: 🚀 headers param on LLM class

* fix: 🐛 fix headers for openai.;y

* feat: ✨ highlight code on cmd+shift+L

* ci: 💚 Update package.json version [skip ci]

* feat: 💄 sticky top bar in gui.tsx

* fix: 🔊 websocket logging and horizontal scrollbar

* ci: 💚 Update package.json version [skip ci]

* feat: ✨ allow AzureOpenAI Service through GGML

* ci: 💚 Update package.json version [skip ci]

* fix: 🐛 fix automigration

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 upload binaries in ci, download apple silicon

* chore: 🔥 remove notes

* fix: 💚 use curl to download binary

* fix: 💚 set permissions on apple silicon binary

* fix: 💚 testing

* fix: 💚 cleanup file

* fix: 💚 fix preview.yaml

* fix: 💚 only upload once per binary

* fix: 💚 install rosetta

* ci: 💚 download binary after tests

* ci: 💚 Update package.json version [skip ci]

* ci: 💚 prepare ci for merge to main

---------

Co-authored-by: GitHub Action <action@github.com>

* fix: 📝 fix deployent readme

* chore: 🔖 update package.json version

* fix: 🐛 fix ci to only upload from linux x64, not alpine

* ci: 💚 Update package.json version [skip ci]

* fix: 🐛 fixing up

* refactor: 🔥 remove sentence_transformers_model option

* ci: 💚 run preview.yaml

* refactor: ♻️ rename to continue_server

* ci: 💚 update python version in continue_server.spec

* ci: 💚 checking paths

* ci: 💚 find path of site-packages

* fix

* ci: 💚 deploy to pre-release

* ci: 💚 change branch to pull from in ci

* perf: 👷 build with tiktoken vocab

* chore: 🔖 upgrade version

* ci: 💚 Update package.json version [skip ci]

* fix: ⚡ update count_tokens method

* ci: 💚 download continue_server, not run

* ci: 💚 Update package.json version [skip ci]

* feat: ⚡ reduce default max_tokens

* fix: 🐛 fix set_system_message

* ci: 💚 update ci for preview branch

* build: 💚 update pypi package

---------

Co-authored-by: GitHub Action <action@github.com>
2023-10-11 18:26:19 -07:00

331 lines
9.7 KiB
Python

import asyncio
import json
import os
import subprocess
from functools import cached_property
from typing import List, Tuple
import chromadb
from chromadb.config import Settings
from chromadb.utils import embedding_functions
from dotenv import load_dotenv
from openai.error import RateLimitError
from ...core.sdk import ContinueSDK
from ..util.filter_files import DEFAULT_IGNORE_PATTERNS, should_filter_path
from ..util.logging import logger
from ..util.paths import getEmbeddingsPathForBranch
from .update import filter_ignored_files
load_dotenv()
IGNORE_PATTERNS_FOR_CHROMA = [
# File Names
"**/.DS_Store",
"**/package-lock.json",
"**/yarn.lock",
# File Types
"*.log",
"*.ttf",
"*.png",
"*.jpg",
"*.jpeg",
"*.gif",
"*.mp4",
"*.svg",
"*.ico",
"*.pdf",
"*.zip",
"*.gz",
"*.tar",
"*.tgz",
"*.rar",
"*.7z",
"*.exe",
"*.dll",
"*.obj",
"*.o",
"*.a",
"*.lib",
"*.so",
"*.dylib",
"*.ncb",
"*.sdf",
]
def chunk_document(document: str, max_length: int = 1000) -> List[str]:
"""Chunk a document into smaller pieces."""
chunks = []
chunk = ""
for line in document.split("\n"):
if len(chunk) + len(line) > max_length:
chunks.append(chunk)
chunk = ""
chunk += line + "\n"
chunks.append(chunk)
return chunks
# Mapping of workspace_dir to chromadb collection
collections = {}
class ChromaIndexManager:
workspace_dir: str
client: chromadb.Client
openai_api_key: str = None
def __init__(
self,
workspace_dir: str,
openai_api_key: str = None,
):
self.workspace_dir = workspace_dir
self.client = chromadb.PersistentClient(
path=os.path.join(
getEmbeddingsPathForBranch(self.workspace_dir, self.current_branch),
"chroma",
),
settings=Settings(anonymized_telemetry=False),
)
self.openai_api_key = openai_api_key
os.environ.setdefault("TOKENIZERS_PARALLELISM", "true")
@cached_property
def current_commit(self) -> str:
"""Get the current commit."""
try:
return (
subprocess.check_output(
["git", "rev-parse", "HEAD"], cwd=self.workspace_dir
)
.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError:
return "NONE"
@cached_property
def current_branch(self) -> str:
"""Get the current branch."""
try:
return (
subprocess.check_output(
["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=self.workspace_dir
)
.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError:
return "NONE"
@cached_property
def index_dir(self) -> str:
return getEmbeddingsPathForBranch(self.workspace_dir, self.current_branch)
@property
def index_name(self) -> str:
return f"{self.workspace_dir}/{self.current_branch}"
@cached_property
def git_root_dir(self):
"""Get the root directory of a Git repository."""
try:
return (
subprocess.check_output(
["git", "rev-parse", "--show-toplevel"], cwd=self.workspace_dir
)
.strip()
.decode()
)
except subprocess.CalledProcessError:
return None
def check_index_exists(self):
return os.path.exists(os.path.join(self.index_dir, "metadata.json"))
@property
def collection(self):
if self.workspace_dir in collections:
return collections[self.workspace_dir]
kwargs = {
"name": self.current_branch,
}
if (
self.openai_api_key is not None
):
kwargs["embedding_function"] = embedding_functions.OpenAIEmbeddingFunction(
api_key=self.openai_api_key,
model_name="text-embedding-ada-002",
)
return self.client.get_or_create_collection(**kwargs)
async def create_codebase_index(
self, sdk: ContinueSDK, ignore_files: List[str] = []
):
"""Create a new index for the current branch."""
collections[self.workspace_dir] = self.collection
if self.check_index_exists():
return
files = await sdk.ide.listDirectoryContents(sdk.ide.workspace_directory, True)
# Filter from ignore_directories
files = list(
filter(
lambda file: not should_filter_path(
file,
ignore_files + DEFAULT_IGNORE_PATTERNS + IGNORE_PATTERNS_FOR_CHROMA,
),
files,
)
)
tasks = []
for file in files:
tasks.append(sdk.ide.readFile(file))
documents = await asyncio.gather(*tasks)
chunks = [chunk_document(document) for document in documents]
flattened_chunks = []
flattened_metadata = []
flattened_ids = []
for i in range(len(chunks)):
for j in range(len(chunks[i])):
flattened_chunks.append(chunks[i][j])
flattened_metadata.append({"filepath": files[i]})
flattened_ids.append(f"{files[i]}::{j}")
for i in range(len(flattened_chunks)):
if len(flattened_chunks[i]) == 0:
flattened_chunks[i] = "EMPTY"
elif len(flattened_chunks[i]) > 6000:
flattened_chunks[i] = flattened_chunks[i][:6000]
# Attempt to avoid rate-limiting
i = 0
wait_time = 4.0
while i < len(flattened_chunks):
try:
self.collection.add(
documents=flattened_chunks[i : i + 100],
metadatas=flattened_metadata[i : i + 100],
ids=flattened_ids[i : i + 100],
)
i += 100
await asyncio.sleep(0.5)
except RateLimitError as e:
logger.debug(f"Rate limit exceeded, waiting {wait_time} seconds")
await asyncio.sleep(wait_time)
wait_time *= 2
if wait_time > 2**10:
raise e
with open(f"{self.index_dir}/metadata.json", "w") as f:
json.dump(
{
"commit": self.current_commit,
"chunks": {files[i]: len(chunks[i]) for i in range(len(files))},
},
f,
indent=4,
)
logger.debug("Codebase index created")
def get_modified_deleted_files(self) -> Tuple[List[str], List[str]]:
"""Get a list of all files that have been modified since the last commit."""
metadata = f"{self.index_dir}/metadata.json"
with open(metadata, "r") as f:
previous_commit = json.load(f)["commit"]
try:
modified_deleted_files = (
subprocess.check_output(
["git", "diff", "--name-only", previous_commit, self.current_commit]
)
.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError:
return [], []
modified_deleted_files = modified_deleted_files.split("\n")
modified_deleted_files = [f for f in modified_deleted_files if f]
deleted_files = [
f
for f in modified_deleted_files
if not os.path.exists(os.path.join(self.workspace_dir, f))
]
modified_files = [
f
for f in modified_deleted_files
if os.path.exists(os.path.join(self.workspace_dir, f))
]
return filter_ignored_files(
modified_files, self.index_dir
), filter_ignored_files(deleted_files, self.index_dir)
def update_codebase_index(self):
"""Update the index with a list of files."""
if not self.check_index_exists():
self.create_codebase_index()
else:
modified_files, deleted_files = self.get_modified_deleted_files()
with open(f"{self.index_dir}/metadata.json", "r") as f:
metadata = json.load(f)
for file in deleted_files:
num_chunks = metadata["chunks"][file]
for i in range(num_chunks):
self.collection.delete(file)
del metadata["chunks"][file]
logger.debug(f"Deleted {file}")
for file in modified_files:
if file in metadata["chunks"]:
num_chunks = metadata["chunks"][file]
for i in range(num_chunks):
self.collection.delete(file)
logger.debug(f"Deleted old version of {file}")
with open(file, "r") as f:
f.read()
# for i, text in enumerate(text_chunks):
# index.insert(Document(text=text, doc_id=f"{file}::{i}"))
# metadata["chunks"][file] = len(text_chunks)
logger.debug(f"Inserted new version of {file}")
metadata["commit"] = self.current_commit
with open(f"{self.index_dir}/metadata.json", "w") as f:
json.dump(metadata, f, indent=4)
logger.debug("Codebase index updated")
def query_codebase_index(self, query: str, n: int = 4) -> str:
"""Query the codebase index."""
if not self.check_index_exists():
logger.debug(f"No index found for the codebase at {self.index_dir}")
return []
results = self.collection.query(query_texts=[query], n_results=n)
return results