continue-custom/server/continuedev/plugins/steps/chroma.py
Bishwendu Kundu 0752c15c1a
Initial intelliJ plugin changes to support webview (#457)
* Initial intelliJ plugin changes to support webview

* connect webview websockets

* Starting work on downloading server binary from S3

* fix color theme in intellij

* refactor name to continuedev

* correctly set important variables for onLoad

* highlighted code now working

* keyboard shortcuts (Actions)

* fix: 🐛 hf_tgi fix, proxy support for ggml

* fix: 🐛 remove unecessary logging

* Sync the changes

* Clean up code

* updating how continue works

* ci: 🏷 Update PyPI version [skip ci]

* docs: 📝 docs for customizing chat / edit templates

* docs: 📝 headless mode docs (start)

* docs: 📝 updated setup docs

* ci: 🏷 Update PyPI version [skip ci]

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

* fix: 🐛 compatibility with python 3.8

* feat: ✨ verify_ssl option for all LLMs

* ci: 🏷 Update PyPI version [skip ci]

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

* fix: 🐛 verify_ssl and ssl_context mutual exclusivity

* ci: 🏷 Update PyPI version [skip ci]

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

* feat: 🩹 ca_bundle_path for maybeproxyopenai

* fix: 🐛 a few minor fixes

* ci: 🏷 Update PyPI version [skip ci]

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

* fix: 🐛 custom escaping instead of URI for diff paths

* ci: 🏷 Update PyPI version [skip ci]

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

* fix: 🐛 fix huggingface tgi

* ci: 🏷 Update PyPI version [skip ci]

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

* docs: 📝 small docs update on how continue works

* ci: 🏷 Update PyPI version [skip ci]

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

* Clarifying `logs` instructions in troubleshooting.md (#476)

* fix: 🐛 fixes to templating messages

* fix: some typos (#478)

* fix: 🐛 fix for lmstudio defaults

* ci: 🏷 Update PyPI version [skip ci]

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

* ci: 🏷 Update PyPI version [skip ci]

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

* Implement design patterns for better extensibility

* Fix: Ensure read operations in ContinuePluginSelectionListener are run within the correct context using runReadAction.

* feat: ✨ implement most other ideProtocolClient methods

* wait until server is running to connect

* remove unnecessary imports, update version

* reformat code and optimize the imports

* fix: 🐛 fix serialization bug

* fix: 🐛 attempt to fix error from jetbrains marketplace

* a few updates

* fix: 🐛 bringing jetbrains up to par

* feat: ✨ highlighted code updates in intellij

* fix: 🚑 fix freezing issue

* fix: 🐛 fix icons, bump version

* feat: ✨ /edit support, other fixes

* quick input, keyboard shortcuts

* fix: 🐛 fix /edit and keyboard shortcuts and lagging on scroll

* feat: 💄 welcome panel

* chore: 🔖 update version

* ide endpoint for websockets

* fix: 🚑 fix freezing problem

* quick input

* fix: 🐛 small jetbrains fixes

* fix: 💄 nicer dropdown that can "drop up"

* fix: 🐛 fix font size for dropdown

* fix: 💄 fix textarea for quickinput

* 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

* fix: 🐛 do read operation in readAction

* chore: 🔖 update version

* 🐛 small jetbrains fixes

* ⚡️ exponential backoff jetbrains

* 🐛 fix history loading bug

* 💄 many small ui fixes

* 🐛 fixes freezing problem (actually)

* 🥅 catch errors, other stuff

* 🐛 many jetbrains fixes

* 💄 when no context items found, say in dropdown

* 🎉 VS Code tutorial

* ✨ jetbrains tutorial file

* ⚡️ exponential backoff for ide websockets

* 🎉 unused work on intellij editor inlay

* ✨ server loading progress messages

* ✨ additional progress update

* 💄 fix /edit flickering, keyboard interactions

---------

Co-authored-by: Nate Sesti <sestinj@gmail.com>
Co-authored-by: Ty Dunn <ty@tydunn.com>
Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: Aaron Mihalik <amihalik@users.noreply.github.com>
Co-authored-by: LangLangBart <92653266+LangLangBart@users.noreply.github.com>
Co-authored-by: Nate Sesti <33237525+sestinj@users.noreply.github.com>
2023-10-16 23:14:00 -07:00

327 lines
10 KiB
Python

import asyncio
import os
from typing import Coroutine, Dict, List, Optional, Union
from pydantic import Field
from ...core.main import ContextItem, ContextItemDescription, ContextItemId, Step
from ...core.observation import Observation
from ...core.sdk import ContinueSDK
from ...core.steps import EditFileStep, UserInputStep
from ...libs.chroma.query import ChromaIndexManager
from ..context_providers.util import remove_meilisearch_disallowed_chars
from .chat import SimpleChatStep
from ...core.steps import EditFileStep
class CreateCodebaseIndexChroma(Step):
name: str = "Create Codebase Index"
hide: bool = True
description: str = "Generating codebase embeddings..."
openai_api_key: Optional[str] = None
ignore_files: List[str] = Field(
[],
description="Files to ignore when indexing the codebase. You can use glob patterns, such as **/*.py. This is useful for directories that contain generated code, or other directories that are not relevant to the codebase.",
)
async def describe(self, models) -> Coroutine[str, None, None]:
return "Generated codebase embeddings."
async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
index = ChromaIndexManager(
sdk.ide.workspace_directory,
openai_api_key=self.openai_api_key,
)
if not index.check_index_exists():
self.hide = False
await index.create_codebase_index(sdk, ignore_files=self.ignore_files)
async def rerank_chroma_results_group(
results: Dict[str, str], user_input: str, n: int, sdk: ContinueSDK
):
"""
Reranks the results from the codebase index based on the user input
"""
results_prompt = "\n\n".join(
map(
lambda kv: f"{kv[0]}\n```\n{kv[1][:500]}\n```",
results.items(),
)
)
include_prompt = f"""\
You will be asked to select the most relevant results from a list. You should choose the results that will be most useful in answering the user's request. For each result that you think is important, you should simply say its title on a new line. Here is an example input:
\"\"\"
add.py::0
```
def add(a, b):
return a + b
```
multiply.py::0
```
def multiply(a, b):
return a * b
```
subtract.py::0
```
def subtract(a, b):
return a - b
```
\"\"\"
And here is the output you would give if you thought that add.py and subtract.py were the most relevant results:
\"\"\"
add.py::0
subtract.py::0
\"\"\"
Now for the real task, here are the top {len(results)} results from the codebase for the user request, "{user_input}":
{results_prompt}
Here are the {n} most relevant results listed in the same format from the example:"""
remove_prompt = f"""\
You will be asked to select items from a list that are irrelevant to the given request. For each result that is unrelated, you should simply say its title on a new line. Here is an example input:
\"\"\"
add.py::0
```
def add(a, b):
return a + b
```
multiply.py::0
```
def multiply(a, b):
return a * b
```
subtract.py::0
```
def subtract(a, b):
return a - b
```
\"\"\"
And here is the output you would give if you thought that add.py and subtract.py were not useful to answer the given request:
\"\"\"
add.py::0
subtract.py::0
\"\"\"
Now for the real task, here are the top {len(results)} results from the codebase for the user request, "{user_input}":
{results_prompt}
List the results that are not useful in answering the request, using in the same format from the example. Only choose those that are completely unrelated, and no more than {n // 2}:"""
include = sdk.models.summarize.complete(include_prompt, log=False)
remove = sdk.models.summarize.complete(remove_prompt, log=False)
include_completion, remove_completion = await asyncio.gather(include, remove)
return include_completion.split("\n"), remove_completion.split("\n")
async def rerank_chroma_results(
results: Dict[str, str], user_input: str, n: int, sdk: ContinueSDK
):
"""
Reranks the results from the codebase index based on the user input
"""
# Split the results into groups
groups = []
group = {}
keys = list(results.keys())
for i in range(len(keys)):
if len(group) == 10:
groups.append(group)
group = {}
group[keys[i]] = results[keys[i]]
if len(group) > 0:
groups.append(group)
# Gather the include/remove results from each group
include = set([])
remove = set([])
tasks = []
for group in groups:
tasks.append(rerank_chroma_results_group(group, user_input, n, sdk))
reranking_results = await asyncio.gather(*tasks)
for rr in reranking_results:
include.update(rr[0])
remove.update(rr[1])
# Use these results to whittle down the results
counts_per_files = {}
for id in results:
filename = id.split("::")[0]
if filename not in counts_per_files:
counts_per_files[filename] = 0
counts_per_files[filename] += 1
repeated_files = set(filter(lambda x: counts_per_files[x] > 1, counts_per_files))
selected = {
key: value
for key, value in results.items()
if key not in remove or key.split("::")[0] in repeated_files
}
selected = {key: value for key, value in selected.items() if key in include}
for key in selected:
del results[key]
additional = n - len(selected)
for i in range(additional):
if len(results) == 0:
break
# Get an item from the results
key = list(results.keys())[0]
selected[key] = results[key]
del results[key]
if additional < 0:
# We need to remove some items
additional = -additional
for i in range(additional):
if len(selected) == 0:
break
# Get an item from the results
key = list(selected.keys())[0]
del selected[key]
return selected
def shorten_filepaths(filepaths: List[str]) -> List[str]:
"""
Shortens the filepaths to just the filename,
unless directory names are needed for uniqueness
"""
basenames = list(map(os.path.basename, filepaths))
if len(basenames) == len(filepaths):
return list(basenames)
basename_counts = {}
for filepath in filepaths:
basename = os.path.basename(filepath)
if basename not in basename_counts:
basename_counts[basename] = 0
basename_counts[basename] += 1
for i in range(0, len(filepaths)):
basename = os.path.basename(filepaths[i])
if basename_counts[basename] <= 1:
filepaths[i] = basename
else:
filepaths[i] = os.path.join(
os.path.basename(os.path.dirname(filepaths[i])), basename
)
return filepaths
class AnswerQuestionChroma(Step):
user_input: str
_answer: Union[str, None] = None
name: str = "Answer Question"
n_retrieve: Optional[int] = Field(
20, description="Number of results to initially retrieve from vector database"
)
n_final: Optional[int] = Field(
10, description="Final number of results to use after re-ranking"
)
use_reranking: bool = Field(
True,
description="Whether to use re-ranking, which will allow initial selection of n_retrieve results, then will use an LLM to select the top n_final results",
)
hide: bool = True
async def describe(self, llm) -> Coroutine[str, None, None]:
if self._answer is None:
return f"Answering the question: {self.user_input}"
else:
return self._answer
async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
index = ChromaIndexManager(sdk.ide.workspace_directory)
self.description = f"Reading from {self.n_retrieve} files..."
await sdk.update_ui()
results = index.query_codebase_index(
self.user_input, n=self.n_retrieve if self.use_reranking else self.n_final
)
shortened_filepaths = shorten_filepaths(results["ids"][0])
results_dict = {
filename: document
for filename, document in zip(shortened_filepaths, results["documents"][0])
if document.strip() != ""
}
if self.use_reranking:
results_dict = await rerank_chroma_results(
results_dict, self.user_input, self.n_final, sdk
)
filepaths = set([])
context_items: List[ContextItem] = []
for id, document in results_dict.items():
filename = id.split("::")[0]
filepath = results["ids"][0][shortened_filepaths.index(id)].split("::")[0]
if filepath in filepaths:
continue
ctx_item = ContextItem(
content=document,
description=ContextItemDescription(
name=filename,
description=filepath,
id=ContextItemId(
provider_title="file",
item_id=remove_meilisearch_disallowed_chars(filepath),
),
),
)
context_items.append(ctx_item)
await sdk.add_context_item(ctx_item)
filepaths.add(filepath)
await sdk.update_ui()
await sdk.run_step(
SimpleChatStep(
name="Answer Question",
description=f"Reading from {self.n_final} files...",
)
)
# for ctx_item in context_items:
# await sdk.delete_context_item(ctx_item.description.id)
class EditFileChroma(Step):
user_input: str
hide: bool = True
async def run(self, sdk: ContinueSDK) -> Coroutine[Observation, None, None]:
index = ChromaIndexManager(sdk.ide.workspace_directory)
results = index.query_codebase_index(self.user_input)
resource_name = list(results.source_nodes[0].node.relationships.values())[0]
filepath = resource_name[: resource_name.index("::")]
await sdk.run_step(
EditFileStep(
filepath=filepath,
prompt=f"Here is the code:\n\n{{code}}\n\nHere is the user request:\n\n{self.user_input}\n\nHere is the code after making the requested changes:\n",
)
)