This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into include-config-in-pac…
…akge
- Loading branch information
Showing
19 changed files
with
1,373 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import List, Optional, cast | ||
|
||
from canopy.chat_engine.query_generator import QueryGenerator | ||
from canopy.chat_engine.history_pruner.raising import RaisingHistoryPruner | ||
from canopy.llm import BaseLLM, CohereLLM | ||
from canopy.models.data_models import Messages, Query | ||
|
||
|
||
class CohereQueryGenerator(QueryGenerator): | ||
""" | ||
Query generator for LLM clients that have a built-in feature to | ||
generate search queries from chat messages. | ||
""" | ||
_DEFAULT_COMPONENTS = { | ||
"llm": CohereLLM, | ||
} | ||
|
||
def __init__(self, | ||
*, | ||
llm: Optional[BaseLLM] = None): | ||
self._llm = llm or self._DEFAULT_COMPONENTS["llm"]() | ||
|
||
if not isinstance(self._llm, CohereLLM): | ||
raise NotImplementedError( | ||
"CohereQueryGenerator only compatible with CohereLLM" | ||
) | ||
|
||
self._history_pruner = RaisingHistoryPruner() | ||
|
||
def generate(self, | ||
messages: Messages, | ||
max_prompt_tokens: int) -> List[Query]: | ||
messages = self._history_pruner.build(chat_history=messages, | ||
max_tokens=max_prompt_tokens) | ||
llm = cast(CohereLLM, self._llm) | ||
queries = llm.generate_search_queries(messages) | ||
return [Query(text=query) for query in queries] | ||
|
||
async def agenerate(self, | ||
messages: Messages, | ||
max_prompt_tokens: int) -> List[Query]: | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# ================================================================== | ||
# Configuration file for Canopy Server with Cohere. | ||
# ================================================================== | ||
|
||
# --------------------------------------------------------------------------------- | ||
system_prompt: &system_prompt | | ||
Use the documents to answer the user question at the next messages. The documents are retrieved from a knowledge | ||
database and you should use only the facts from the documents to answer. Always remember to include the source to | ||
the documents you used from their 'source' field in the format 'Source: $SOURCE_HERE'. | ||
If you don't know the answer, just say that you don't know, don't try to make up an answer, use the documents. | ||
Don't address the documents directly, but use them to answer the user question like it's your own knowledge. | ||
|
||
|
||
# ------------------------------------------------------------------------------------------- | ||
# Tokenizer configuration | ||
# ------------------------------------------------------------------------------------------- | ||
tokenizer: | ||
type: CohereHFTokenizer | ||
params: | ||
model_name: Cohere/Command-nightly | ||
|
||
|
||
# ------------------------------------------------------------------------------------------------------------- | ||
# Chat engine configuration | ||
# ------------------------------------------------------------------------------------------------------------- | ||
chat_engine: | ||
params: | ||
system_prompt: *system_prompt | ||
|
||
# ------------------------------------------------------------------------------------------------------------- | ||
# LLM configuration | ||
# ------------------------------------------------------------------------------------------------------------- | ||
llm: &llm | ||
type: CohereLLM | ||
params: | ||
model_name: command | ||
# You can add any additional parameters which are supported by the Cohere Co.Chat API. The values set | ||
# here will be used in every Co.Chat API call. For example: | ||
# prompt_truncation: "AUTO" | ||
# citation_quality: "accurate" | ||
# temperature: 0.85 | ||
# Specifying connectors is contrary to Canopy's purpose of searching the Pinecone knowledge base only, | ||
# but technically can still be passed like this: | ||
# connectors: | ||
# - "web-search" | ||
# Uncomment to suppress errors when unrecognized or unsupported model params are sent to CohereLLM. | ||
# ignore_unrecognized_params: true | ||
|
||
# -------------------------------------------------------------------- | ||
# Configuration for the QueryBuilder subcomponent of the chat engine. | ||
# -------------------------------------------------------------------- | ||
query_builder: | ||
type: CohereQueryGenerator | ||
params: {} | ||
llm: | ||
<<: *llm | ||
|
||
|
||
# ------------------------------------------------------------------------------------------------------------- | ||
# ContextEngine configuration | ||
# ------------------------------------------------------------------------------------------------------------- | ||
context_engine: | ||
# ----------------------------------------------------------------------------------------------------------- | ||
# KnowledgeBase configuration | ||
# ----------------------------------------------------------------------------------------------------------- | ||
knowledge_base: | ||
params: | ||
default_top_k: 100 | ||
|
||
# -------------------------------------------------------------------------- | ||
# Configuration for the RecordEncoder subcomponent of the knowledge base. | ||
# -------------------------------------------------------------------------- | ||
record_encoder: | ||
type: CohereRecordEncoder | ||
params: | ||
model_name: # The name of the model to use for encoding | ||
"embed-english-v3.0" | ||
batch_size: 100 # The number of document chunks to encode in each call to the encoding model | ||
|
||
reranker: | ||
type: CohereReranker | ||
params: | ||
top_n: 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/canopy/knowledge_base/record_encoder/sentence_transformers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from typing import Optional | ||
from pinecone_text.dense import SentenceTransformerEncoder | ||
from canopy.knowledge_base.record_encoder.dense import DenseRecordEncoder | ||
from huggingface_hub.utils import RepositoryNotFoundError | ||
|
||
|
||
class SentenceTransformerRecordEncoder(DenseRecordEncoder): | ||
""" | ||
SentenceTransformerRecordEncoder is a type of DenseRecordEncoder that uses a Sentence Transformer model. | ||
The implementation uses the `SentenceTransformerEncoder` class from the `pinecone-text` library. | ||
For more information about see: https://github.com/pinecone-io/pinecone-text | ||
""" # noqa: E501 | ||
|
||
def __init__(self, | ||
*, | ||
model_name: str = "sentence-transformers/all-MiniLM-L6-v2", | ||
query_encoder_name: Optional[str] = None, | ||
batch_size: int = 400, | ||
device: Optional[str] = None, | ||
**kwargs) -> None: | ||
""" | ||
Initialize the SentenceTransformerRecordEncoder | ||
Args: | ||
model_name: The name of the embedding model to use for encoding documents. | ||
See https://huggingface.co/models?library=sentence-transformers | ||
for all possible Sentence Transformer models. | ||
query_encoder_name: The name of the embedding model to use for encoding queries. | ||
See https://huggingface.co/models?library=sentence-transformers | ||
for all possible Sentence Transformer models. | ||
Defaults to `model_name`. | ||
batch_size: The number of documents or queries to encode at once. | ||
Defaults to 400. | ||
device: The local device to use for encoding, for example "cpu", "cuda" or "mps". | ||
Defaults to "cuda" if cuda is available, otherwise to "cpu". | ||
**kwargs: Additional arguments to pass to the underlying `pinecone-text.SentenceTransformerEncoder`. | ||
""" # noqa: E501 | ||
try: | ||
encoder = SentenceTransformerEncoder( | ||
document_encoder_name=model_name, | ||
query_encoder_name=query_encoder_name, | ||
device=device, | ||
**kwargs, | ||
) | ||
except RepositoryNotFoundError as e: | ||
raise RuntimeError( | ||
"Your chosen Sentence Transformer model(s) could not be found. " | ||
f"Details: {str(e)}" | ||
) from e | ||
except ImportError: | ||
raise ImportError( | ||
f"{self.__class__.__name__} requires the `torch` and `transformers` " | ||
f"extra dependencies. Please install them using " | ||
f"`pip install canopy-sdk[torch,transformers]`." | ||
) | ||
super().__init__(dense_encoder=encoder, batch_size=batch_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.