### Install KeyBERT via pip Source: https://maartengr.github.io/KeyBERT/ Basic installation command for KeyBERT from PyPI. ```bash pip install keybert ``` -------------------------------- ### Install LiteLLM package Source: https://maartengr.github.io/KeyBERT/guides/llms.html Install the litellm package to use any closed-source LLM with KeyLLM. ```bash pip install litellm ``` -------------------------------- ### Install langchain and openai packages Source: https://maartengr.github.io/KeyBERT/api/langchain.html Required installation commands for using the LangChain class. ```bash pip install langchain ``` ```bash pip install openai ``` -------------------------------- ### Sample documents for KeyLLM Source: https://maartengr.github.io/KeyBERT/guides/keyllm.html Example documents used to demonstrate KeyLLM. Documents 1 and 2 are similar, while document 3 is about a different subject. ```python documents = [ "The website mentions that it only takes a couple of days to deliver but I still have not received mine.", "I received my package!", "Whereas the most powerful LLMs have generally been accessible only through limited APIs (if at all), Meta released LLaMA's model weights to the research community under a noncommercial license." ] ``` -------------------------------- ### Install KeyBERT with optional backends Source: https://maartengr.github.io/KeyBERT/ Optional installations for additional transformers and language backends: flair, gensim, spacy, and use. ```bash pip install keybert[flair] ``` ```bash pip install keybert[gensim] ``` ```bash pip install keybert[spacy] ``` ```bash pip install keybert[use] ``` -------------------------------- ### Install cohere Source: https://maartengr.github.io/KeyBERT/api/cohere.html Install the cohere package using pip. ```bash pip install cohere ``` -------------------------------- ### Install LangChain Source: https://maartengr.github.io/KeyBERT/guides/llms.html Install the langchain package using pip. ```bash pip install langchain ``` -------------------------------- ### Install openai package Source: https://maartengr.github.io/KeyBERT/api/openai.html Install the required openai package before using the OpenAI class. ```bash pip install openai ``` -------------------------------- ### Install keyphrase-vectorizers package Source: https://maartengr.github.io/KeyBERT/guides/countvectorizer.html Install the KeyphraseVectorizers package using pip. ```bash pip install keyphrase-vectorizers ``` -------------------------------- ### Working example with matching parameters Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html This example works because the parameters min_df and stop_words are identical in both extract_embeddings and extract_keywords calls. ```python from keybert import KeyBERT kw_model = KeyBERT() doc_embeddings, word_embeddings = kw_model.extract_embeddings(docs, min_df=1, stop_words="english") keywords = kw_model.extract_keywords(docs, min_df=1, stop_words="english", doc_embeddings=doc_embeddings, word_embeddings=word_embeddings) ``` -------------------------------- ### Use LiteLLM with OpenAI example Source: https://maartengr.github.io/KeyBERT/guides/llms.html Set the OPENAI_API_KEY environment variable and create a LiteLLM instance with a model name (e.g., gpt-3.5-turbo). ```python import os from keybert.llm import LiteLLM from keybert import KeyLLM # Select LLM os.environ["OPENAI_API_KEY"] = "sk-..." llm = LiteLLM("gpt-3.5-turbo") # Load it in KeyLLM kw_model = KeyLLM(llm) ``` -------------------------------- ### Install light-weight KeyBERT Source: https://maartengr.github.io/KeyBERT/changelog.html Command to install a light-weight version of KeyBERT without dependencies, using scikit-learn and model2vec. ```bash pip install keybert --no-deps scikit-learn model2vec ``` -------------------------------- ### Example output of keyword extraction Source: https://maartengr.github.io/KeyBERT/guides/keyllm.html Shows the resulting keywords for the example documents, with each sublist corresponding to a document. ```python [['Website', 'Delivery', 'Timeframe', 'Mention', 'Order fulfillment', 'Not received', 'Waiting'], ['Package', 'Received', 'Confirmation', 'Delivery', 'Order fulfillment'], ['LLMs', 'Limited APIs', 'Meta', 'LLaMA', 'Model weights', '']] ``` -------------------------------- ### Create Custom Backend with BaseEmbedder Source: https://maartengr.github.io/KeyBERT/guides/embeddings.html Shows how to create a custom embedding backend by subclassing keybert.backend.BaseEmbedder. The example implements a SentenceTransformer backend and passes it to KeyBERT. ```python from keybert.backend import BaseEmbedder from sentence_transformers import SentenceTransformer class CustomEmbedder(BaseEmbedder): def __init__(self, embedding_model): super().__init__() self.embedding_model = embedding_model def embed(self, documents, verbose=False): embeddings = self.embedding_model.encode(documents, show_progress_bar=verbose) return embeddings # Create custom backend distilbert = SentenceTransformer("paraphrase-MiniLM-L6-v2") custom_embedder = CustomEmbedder(embedding_model=distilbert) # Pass custom backend to keybert kw_model = KeyBERT(model=custom_embedder) ``` -------------------------------- ### Extract keyphrases with keyphrase_ngram_range=(1, 2) Source: https://maartengr.github.io/KeyBERT/ Shows how to set keyphrase_ngram_range to (1, 2) to get keyphrases of one or two words, with example output. ```python >>> kw_model.extract_keywords(doc, keyphrase_ngram_range=(1, 2), stop_words=None) [('learning algorithm', 0.6978), ('machine learning', 0.6305), ('supervised learning', 0.5985), ('algorithm analyzes', 0.5860), ('learning function', 0.5850)] ``` -------------------------------- ### Minimal KeyBERT keyword extraction example Source: https://maartengr.github.io/KeyBERT/ Shows the most minimal usage: import KeyBERT, define a document, create a model, and extract keywords. ```python from keybert import KeyBERT doc = """ Supervised learning is the machine learning task of learning a function that maps an input to an output based on example input-output pairs. It infers a function from labeled training data consisting of a set of training examples. In supervised learning, each example is a pair consisting of an input object (typically a vector) and a desired output value (also called the supervisory signal). A supervised learning algorithm analyzes the training data and produces an inferred function, which can be used for mapping new examples. An optimal scenario will allow for the algorithm to correctly determine the class labels for unseen instances. This requires the learning algorithm to generalize from the training data to unseen situations in a 'reasonable' way (see inductive bias). """ kw_model = KeyBERT() keywords = kw_model.extract_keywords(doc) ``` -------------------------------- ### Failing example with mismatched parameters Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html This example will throw an error because the values for min_df and stop_words differ between extract_embeddings and extract_keywords. ```python from keybert import KeyBERT kw_model = KeyBERT() doc_embeddings, word_embeddings = kw_model.extract_embeddings(docs, min_df=3, stop_words="dutch") keywords = kw_model.extract_keywords(docs, min_df=1, stop_words="english", doc_embeddings=doc_embeddings, word_embeddings=word_embeddings) ``` -------------------------------- ### Use KeyLLM with OpenAI (legacy API key) Source: https://maartengr.github.io/KeyBERT/changelog.html For KeyBERT version 0.8.0, set the OpenAI API key globally and create an OpenAI LLM instance without a client. This example is from the initial KeyLLM release. ```python import openai from keybert.llm import OpenAI from keybert import KeyLLM # Create your LLM openai.api_key = "sk-..." llm = OpenAI() # Load it in KeyLLM kw_model = KeyLLM(llm) ``` -------------------------------- ### Keywords extracted by KeyLLM Source: https://maartengr.github.io/KeyBERT/guides/keyllm.html Example output of the keywords extracted by KeyLLM for the sample documents. ```python [['Website', 'Delivery', 'Mention', 'Timeframe', 'Not received', 'Order fulfillment'], ['Package', 'Received', 'Delivery', 'Order fulfillment'], ['Powerful LLMs', 'Limited APIs', 'Meta', 'Model weights', 'Research community', '']] ``` -------------------------------- ### Use Sentence Transformers model with KeyBERT Source: https://maartengr.github.io/KeyBERT/guides/embeddings.html Select any model from sentence-transformers and pass it to KeyBERT using the `model` parameter. The example uses 'all-MiniLM-L6-v2'. ```python from keybert import KeyBERT kw_model = KeyBERT(model="all-MiniLM-L6-v2") ``` -------------------------------- ### Use seed keywords with KeyBERT Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html Shows how to define a list of seeded keywords and pass them to KeyBERT's extract_keywords method via the seed_keywords parameter to guide extraction. ```python from keybert import KeyBERT kw_model = KeyBERT() # Define our seeded term seed_keywords = ["information"] keywords = kw_model.extract_keywords(doc, seed_keywords=seed_keywords) ``` -------------------------------- ### Extract keywords from multiple documents Source: https://maartengr.github.io/KeyBERT/api/keybert.html Usage example for extracting keywords from multiple documents, which is typically faster. Creates a KeyBERT model and calls extract_keywords with a list of documents. ```python from keybert import KeyBERT kw_model = KeyBERT() keywords = kw_model.extract_keywords(docs) ``` -------------------------------- ### Maximal Marginal Relevance with low diversity Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html Uses use_mmr=True with diversity=0.2 to produce less diverse keywords. The example uses keyphrase_ngram_range=(3, 3) and stop_words='english'. The output shows the extracted keyphrases with their similarity scores. ```python >>> kw_model.extract_keywords(doc, keyphrase_ngram_range=(3, 3), stop_words='english', use_mmr=True, diversity=0.2) [('algorithm generalize training', 0.7727), ('supervised learning algorithm', 0.7502), ('learning machine learning', 0.7577), ('learning algorithm analyzes', 0.7587), ('learning algorithm generalize', 0.7514)] ``` -------------------------------- ### Maximal Marginal Relevance with high diversity Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html Uses use_mmr=True with diversity=0.7 to produce highly diverse keywords. The example uses keyphrase_ngram_range=(3, 3) and stop_words='english'. The output shows the extracted keyphrases with their similarity scores. ```python kw_model.extract_keywords(doc, keyphrase_ngram_range=(3, 3), stop_words='english', use_mmr=True, diversity=0.7) [('algorithm generalize training', 0.7727), ('labels unseen instances', 0.1649), ('new examples optimal', 0.4185), ('determine class labels', 0.4774), ('supervised learning algorithm', 0.7502)] ``` -------------------------------- ### Extract keywords with keyphrase_ngram_range=(1, 1) Source: https://maartengr.github.io/KeyBERT/ Shows how to set keyphrase_ngram_range to (1, 1) to get single-word keywords, with example output. ```python >>> kw_model.extract_keywords(doc, keyphrase_ngram_range=(1, 1), stop_words=None) [('learning', 0.4604), ('algorithm', 0.4556), ('training', 0.4487), ('class', 0.4086), ('mapping', 0.3700)] ``` -------------------------------- ### LiteLLM.__init__(model, prompt, system_prompt, generator_kwargs, delay_in_seconds, verbose) Source: https://maartengr.github.io/KeyBERT/api/litellm.html Initializes the LiteLLM instance with model, prompt, system prompt, generator kwargs, delay, and verbosity settings. ```APIDOC ## LiteLLM.__init__(model, prompt, system_prompt, generator_kwargs, delay_in_seconds, verbose) ### Description Initializes the LiteLLM instance with model, prompt, system prompt, generator kwargs, delay, and verbosity settings. ### Method Python constructor ### Signature ```python def __init__( self, model: str = "gpt-3.5-turbo", prompt: str = None, system_prompt: str = "You are a helpful assistant.", generator_kwargs: Mapping[str, Any] = {}, delay_in_seconds: float = None, verbose: bool = False, ) ``` ### Parameters - **model** (str) - Optional - Model to use within LiteLLM, defaults to OpenAI's `"gpt-3.5-turbo"`. - **prompt** (str) - Optional - The prompt to be used in the model. If no prompt is given, `self.default_prompt_` is used instead. NOTE: Use `"[DOCUMENT]"` in the prompt to decide where the document needs to be inserted. - **system_prompt** (str) - Optional - The message that sets the behavior of the assistant. It's typically used to provide high-level instructions for the conversation. - **generator_kwargs** (Mapping[str, Any]) - Optional - Kwargs passed to `litellm.completion` for fine-tuning the output. - **delay_in_seconds** (float) - Optional - The delay in seconds between consecutive prompts in order to prevent RateLimitErrors. - **verbose** (bool) - Optional - Set this to True if you want to see a progress bar for the keyword extraction. ### Usage Example ```python import os from keybert.llm import LiteLLM # Select LLM os.environ["OPENAI_API_KEY"] = "sk-..." llm = LiteLLM("gpt-3.5-turbo") # You can also use a custom prompt: prompt = "I have the following document: [DOCUMENT] \nThis document contains the following keywords separated by commas: '" llm = LiteLLM("gpt-3.5-turbo", prompt=prompt) ``` ``` -------------------------------- ### Basic usage of OpenAI with KeyLLM Source: https://maartengr.github.io/KeyBERT/api/openai.html Create an OpenAI client, instantiate the OpenAI LLM, load it into KeyLLM, and extract keywords from a document. ```python import openai from keybert.llm import OpenAI from keybert import KeyLLM # Create your LLM client = openai.OpenAI(api_key=MY_API_KEY) llm = OpenAI(client) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` -------------------------------- ### OpenAI(client, model='gpt-3.5-turbo-instruct', generator_kwargs={}, prompt=None, system_prompt='You are a helpful assistant.', delay_in_seconds=None, exponential_backoff=False, chat=False, verbose=False) Source: https://maartengr.github.io/KeyBERT/api/openai.html Initializes an OpenAI LLM instance for use with KeyLLM. The constructor accepts an OpenAI client and various configuration parameters to control model selection, prompting, rate limiting, and chat mode. ```APIDOC ## OpenAI(client, model='gpt-3.5-turbo-instruct', generator_kwargs={}, prompt=None, system_prompt='You are a helpful assistant.', delay_in_seconds=None, exponential_backoff=False, chat=False, verbose=False) ### Description Initializes an OpenAI LLM instance for keyword extraction. This class uses the OpenAI API to generate keywords based on a given document and prompt. ### Method Constructor ### Endpoint N/A (SDK class constructor) ### Parameters - **client** (openai.OpenAI) - Required - An `openai.OpenAI` client instance. - **model** (str) - Optional - Model to use within OpenAI, defaults to `"text-ada-001"`. NOTE: If a `gpt-3.5-turbo` model is used, make sure to set `chat` to True. Default: `'gpt-3.5-turbo-instruct'`. - **generator_kwargs** (Mapping[str, Any]) - Optional - Kwargs passed to `openai.Completion.create` for fine-tuning the output. Default: `{}`. - **prompt** (str) - Optional - The prompt to be used in the model. If no prompt is given, `self.default_prompt_` is used instead. NOTE: Use `"[DOCUMENT]"` in the prompt to decide where the document needs to be inserted. Default: `None`. - **system_prompt** (str) - Optional - The message that sets the behavior of the assistant. It's typically used to provide high-level instructions for the conversation. Default: `'You are a helpful assistant.'`. - **delay_in_seconds** (float) - Optional - The delay in seconds between consecutive prompts in order to prevent RateLimitErrors. Default: `None`. - **exponential_backoff** (bool) - Optional - Retry requests with a random exponential backoff. A short sleep is used when a rate limit error is hit, then the requests is retried. Increase the sleep length if errors are hit until 10 unsuccesfull requests. If True, overrides `delay_in_seconds`. Default: `False`. - **chat** (bool) - Optional - Set this to True if a chat model is used. Generally, this GPT 3.5 or higher. Default: `False`. - **verbose** (bool) - Optional - Set this to True if you want to see a progress bar for the keyword extraction. Default: `False`. ### Request Example ```python import openai from keybert.llm import OpenAI from keybert import KeyLLM # Create your LLM client = openai.OpenAI(api_key=MY_API_KEY) llm = OpenAI(client) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` ### Response #### Success Response - **keywords** (list) - List of extracted keywords. #### Response Example ```python keywords = kw_model.extract_keywords(document) ``` ``` -------------------------------- ### Define text and keyword model Source: https://maartengr.github.io/KeyBERT/guides/countvectorizer.html Sets up the document and KeyBERT model used throughout the tutorial. ```python from keybert import KeyBERT doc = """ Supervised learning is the machine learning task of learning a function that maps an input to an output based on example input-output pairs.[1] It infers a function from labeled training data consisting of a set of training examples.[2] In supervised learning, each example is a pair consisting of an input object (typically a vector) and a desired output value (also called the supervisory signal). A supervised learning algorithm analyzes the training data and produces an inferred function, which can be used for mapping new examples. An optimal scenario will allow for the algorithm to correctly determine the class labels for unseen instances. This requires the learning algorithm to generalize from the training data to unseen situations in a 'reasonable' way (see inductive bias). """ kw_model = KeyBERT() ``` -------------------------------- ### KeyLLM.__init__(llm) Source: https://maartengr.github.io/KeyBERT/api/keyllm.html Initializes the KeyLLM class with a Large Language Model instance. ```APIDOC ## KeyLLM.__init__(llm) ### Description Initializes the KeyLLM class with a Large Language Model (LLM) instance. This constructor stores the provided LLM for later use in keyword extraction. ### Method Constructor ### Parameters - **llm** (object) - Required - The Large Language Model to use for keyword extraction. ### Request Example ```python from keybert._llm import KeyLLM # Assuming an LLM instance is available llm = SomeLLM() keyllm = KeyLLM(llm) ``` ### Response No return value. The instance is initialized with the provided LLM. ``` -------------------------------- ### KeyLLM __init__ method Source: https://maartengr.github.io/KeyBERT/api/keyllm.html Initializes the KeyLLM instance by storing the provided large language model in self.llm. The llm parameter is required. ```python def __init__(self, llm): """KeyBERT initialization. Arguments: llm: The Large Language Model to use """ self.llm = llm ``` -------------------------------- ### Use Cohere with KeyLLM Source: https://maartengr.github.io/KeyBERT/guides/llms.html Initialize a Cohere client with your API key, create a KeyLLM instance, and extract keywords. ```python import cohere from keybert.llm import Cohere from keybert import KeyLLM # Create your OpenAI LLM co = cohere.Client(my_api_key) llm = Cohere(co) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords keywords = kw_model.extract_keywords(MY_DOCUMENTS) ``` -------------------------------- ### Usage with OpenAI Source: https://maartengr.github.io/KeyBERT/api/litellm.html Shows how to set up LiteLLM with OpenAI and use it with KeyLLM to extract keywords from a document. Requires setting the OPENAI_API_KEY environment variable. ```python import os from keybert.llm import LiteLLM from keybert import KeyLLM # Select LLM os.environ["OPENAI_API_KEY"] = "sk-..." llm = LiteLLM("gpt-3.5-turbo") # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` -------------------------------- ### OpenAI(client, model, prompt, system_prompt, generator_kwargs, delay_in_seconds, exponential_backoff, chat, verbose) Source: https://maartengr.github.io/KeyBERT/api/openai.html Initializes an OpenAI LLM instance for use with KeyLLM. Configures the OpenAI client, model, prompt, and generation settings. ```APIDOC ## OpenAI(client, model, prompt, system_prompt, generator_kwargs, delay_in_seconds, exponential_backoff, chat, verbose) ### Description Initializes an OpenAI LLM instance for use with KeyLLM. Configures the OpenAI client, model, prompt, and generation settings. ### Method Constructor ### Parameters - **client** (openai.OpenAI) - Required - An OpenAI client instance. - **model** (str) - Optional - Model to use within OpenAI, defaults to "gpt-3.5-turbo-instruct". NOTE: If a gpt-3.5-turbo model is used, set chat to True. - **prompt** (str) - Optional - The prompt to be used in the model. If no prompt is given, self.default_prompt_ is used instead. Use "[DOCUMENT]" in the prompt to decide where the document needs to be inserted. - **system_prompt** (str) - Optional - The message that sets the behavior of the assistant. Defaults to "You are a helpful assistant.". - **generator_kwargs** (Mapping[str, Any]) - Optional - Kwargs passed to openai.Completion.create for fine-tuning the output. - **delay_in_seconds** (float) - Optional - The delay in seconds between consecutive prompts to prevent RateLimitErrors. - **exponential_backoff** (bool) - Optional - Retry requests with a random exponential backoff. If True, overrides delay_in_seconds. - **chat** (bool) - Optional - Set to True if a chat model is used (GPT 3.5 or higher). - **verbose** (bool) - Optional - Set to True to see a progress bar for keyword extraction. ### Usage Example ```python import openai from keybert.llm import OpenAI from keybert import KeyLLM # Create your LLM client = openai.OpenAI(api_key=MY_API_KEY) llm = OpenAI(client) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` ### Custom Prompt Example ```python prompt = "I have the following document: [DOCUMENT] \nThis document contains the following keywords separated by commas: '" llm = OpenAI(client, prompt=prompt, delay_in_seconds=5) ``` ### Chat Model Example ```python llm = OpenAI(client, model="gpt-3.5-turbo", delay_in_seconds=10, chat=True) ``` ``` -------------------------------- ### extract_embeddings method implementation Source: https://maartengr.github.io/KeyBERT/api/keybert.html Full implementation of the extract_embeddings method, including parameter handling, vectorizer setup, and embedding extraction. ```python def extract_embeddings( self, docs: Union[str, List[str]], candidates: List[str] = None, keyphrase_ngram_range: Tuple[int, int] = (1, 1), stop_words: Union[str, List[str]] = "english", min_df: int = 1, vectorizer: CountVectorizer = None, ) -> Union[List[Tuple[str, float]], List[List[Tuple[str, float]]]]: """Extract document and word embeddings for the input documents and the generated candidate keywords/keyphrases respectively. Note that all potential keywords/keyphrases are not returned but only their word embeddings. This means that the values of `candidates`, `keyphrase_ngram_range`, `stop_words`, and `min_df` need to be the same between using `.extract_embeddings` and `.extract_keywords`. Arguments: docs: The document(s) for which to extract keywords/keyphrases candidates: Candidate keywords/keyphrases to use instead of extracting them from the document(s) NOTE: This is not used if you passed a `vectorizer`. keyphrase_ngram_range: Length, in words, of the extracted keywords/keyphrases. NOTE: This is not used if you passed a `vectorizer`. stop_words: Stopwords to remove from the document. NOTE: This is not used if you passed a `vectorizer`. min_df: Minimum document frequency of a word across all documents if keywords for multiple documents need to be extracted. NOTE: This is not used if you passed a `vectorizer`. vectorizer: Pass in your own `CountVectorizer` from `sklearn.feature_extraction.text.CountVectorizer` Returns: doc_embeddings: The embeddings of each document. word_embeddings: The embeddings of each potential keyword/keyphrase across across the vocabulary of the set of input documents. NOTE: The `word_embeddings` should be generated through `.extract_embeddings` as the order of these embeddings depend on the vectorizer that was used to generate its vocabulary. Usage: To generate the word and document embeddings from a set of documents: ```python from keybert import KeyBERT kw_model = KeyBERT() doc_embeddings, word_embeddings = kw_model.extract_embeddings(docs) ``` You can then use these embeddings and pass them to `.extract_keywords` to speed up the tuning the model: ```python keywords = kw_model.extract_keywords(docs, doc_embeddings=doc_embeddings, word_embeddings=word_embeddings) ``` """ # Check for a single, empty document if isinstance(docs, str): if docs: docs = [docs] else: return [] # Extract potential words using a vectorizer / tokenizer if vectorizer: count = vectorizer.fit(docs) else: try: count = CountVectorizer( ngram_range=keyphrase_ngram_range, stop_words=stop_words, min_df=min_df, vocabulary=candidates, ).fit(docs) except ValueError: return [] # Scikit-Learn Deprecation: get_feature_names is deprecated in 1.0 # and will be removed in 1.2. Please use get_feature_names_out instead. if version.parse(sklearn_version) >= version.parse("1.0.0"): words = count.get_feature_names_out() else: words = count.get_feature_names() doc_embeddings = self.model.embed(docs) word_embeddings = self.model.embed(words) return doc_embeddings, word_embeddings ``` -------------------------------- ### KeyLLM.__init__ Source: https://maartengr.github.io/KeyBERT/api/keyllm.html Initializes a KeyLLM instance with a Large Language Model (LLM) to be used for keyword extraction. ```APIDOC ## KeyLLM.__init__ ### Description Initializes the KeyLLM class with a Large Language Model (LLM) instance that will be used for keyword extraction. ### Method `__init__` ### Signature ```python def __init__(self, llm): ``` ### Parameters - **llm** - Required - The Large Language Model to use. ### Usage Example ```python from keybert import KeyLLM from keybert.llm import OpenAI llm = OpenAI(client) kw_model = KeyLLM(llm) ``` ``` -------------------------------- ### Extract keywords from a single document Source: https://maartengr.github.io/KeyBERT/api/keybert.html Usage example for extracting keywords from a single document using KeyBERT. Creates a KeyBERT model and calls extract_keywords with a single document. ```python from keybert import KeyBERT kw_model = KeyBERT() keywords = kw_model.extract_keywords(doc) ``` -------------------------------- ### Use KeyLLM with OpenAI client (openai>=1) Source: https://maartengr.github.io/KeyBERT/changelog.html For KeyBERT version 0.8.3 and later, use the OpenAI client to create an LLM instance and pass it to KeyLLM. Requires an API key. ```python import openai from keybert.llm import OpenAI from keybert import KeyLLM # Create your LLM client = openai.OpenAI(api_key=MY_API_KEY) llm = OpenAI(client) # Load it in KeyLLM kw_model = KeyLLM(llm) ``` -------------------------------- ### Use Cohere with KeyLLM Source: https://maartengr.github.io/KeyBERT/api/cohere.html Create a Cohere client, pass it to the Cohere LLM, load it into KeyLLM, and extract keywords from a document. ```python import cohere from keybert.llm import Cohere from keybert import KeyLLM # Create your LLM co = cohere.Client(my_api_key) llm = Cohere(co) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` -------------------------------- ### Use a custom prompt with LangChain Source: https://maartengr.github.io/KeyBERT/api/langchain.html Shows how to pass a custom prompt to the LangChain instance. ```python prompt = "What are these documents about? Please give a single label." llm = LangChain(chain, prompt=prompt) ``` -------------------------------- ### Max Sum Distance keyword extraction Source: https://maartengr.github.io/KeyBERT/guides/quickstart.html Uses use_maxsum=True to diversify keywords by selecting the least similar combinations from the top candidates. The example uses keyphrase_ngram_range=(3, 3), stop_words='english', nr_candidates=20, and top_n=5. The output shows the extracted keyphrases with their cosine similarity scores. ```python >>> kw_model.extract_keywords(doc, keyphrase_ngram_range=(3, 3), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=5) [('set training examples', 0.7504), ('generalize training data', 0.7727), ('requires learning algorithm', 0.5050), ('supervised learning algorithm', 0.3779), ('learning machine learning', 0.2891)] ``` -------------------------------- ### Usage: gpt-like model with TextGeneration Source: https://maartengr.github.io/KeyBERT/api/textgeneration.html Shows how to create a TextGeneration instance with a 'text-generation' pipeline (e.g., gpt2) and use it with KeyLLM to extract keywords from a document. ```python from keybert.llm import TextGeneration from keybert import KeyLLM # Create your LLM generator = pipeline('text-generation', model='gpt2') llm = TextGeneration(generator) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` -------------------------------- ### LangChain(chain, prompt=None, verbose=False) Source: https://maartengr.github.io/KeyBERT/api/langchain.html Initializes a LangChain instance with a langchain chain, an optional custom prompt, and a verbose flag. The chain must accept input_documents and query parameters. ```APIDOC ## LangChain(chain, prompt=None, verbose=False) ### Description Initializes a LangChain instance that uses a langchain chain to generate keywords. The chain must have two input parameters: `input_documents` and `query`. If no prompt is provided, the default prompt is used. ### Parameters - **chain** (object) - Required - A langchain chain that has two input parameters, `input_documents` and `query`. - **prompt** (str) - Optional - The prompt to be used in the model. If not given, `self.default_prompt_` is used. - **verbose** (bool) - Optional - Set to True to see a progress bar for keyword extraction. Default is False. ### Usage Example ```python from langchain.chains.question_answering import load_qa_chain from langchain.llms import OpenAI chain = load_qa_chain(OpenAI(temperature=0, openai_api_key=my_openai_api_key), chain_type="stuff") from keybert.llm import LangChain from keybert import KeyLLM llm = LangChain(chain) kw_model = KeyLLM(llm) document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` ### Custom Prompt Example ```python prompt = "What are these documents about? Please give a single label." llm = LangChain(chain, prompt=prompt) ``` ### Notes - The resulting keywords are expected to be separated by commas. Any changes to the prompt must ensure comma-separated output. - Requires installation of `langchain` and an underlying LLM (e.g., `openai`). ``` -------------------------------- ### KeyBERT extract_keywords implementation Source: https://maartengr.github.io/KeyBERT/api/keybert.html Core implementation of the extract_keywords method. Handles scikit-learn version differences for get_feature_names, validates word embeddings, extracts document and word embeddings, applies guided embeddings with seed keywords, selects keywords using MMR, Max Sum Distance, or cosine similarity, and optionally fine-tunes with an LLM. ```python # Scikit-Learn Deprecation: get_feature_names is deprecated in 1.0 # and will be removed in 1.2. Please use get_feature_names_out instead. if version.parse(sklearn_version) >= version.parse("1.0.0"): words = count.get_feature_names_out() else: words = count.get_feature_names() df = count.transform(docs) # Check if the right number of word embeddings are generated compared with the vectorizer if word_embeddings is not None: if word_embeddings.shape[0] != len(words): raise ValueError( "Make sure that the `word_embeddings` are generated from the function " "`.extract_embeddings`. \nMoreover, the `candidates`, `keyphrase_ngram_range`," "`stop_words`, and `min_df` parameters need to have the same values in both " "`.extract_embeddings` and `.extract_keywords`." ) # Extract embeddings if doc_embeddings is None: doc_embeddings = self.model.embed(docs) if word_embeddings is None: word_embeddings = self.model.embed(words) # Guided KeyBERT either local (keywords shared among documents) or global (keywords per document) if seed_keywords is not None: if isinstance(seed_keywords[0], str): seed_embeddings = self.model.embed(seed_keywords).mean(axis=0, keepdims=True) elif len(docs) != len(seed_keywords): raise ValueError("The length of docs must match the length of seed_keywords") else: seed_embeddings = np.vstack( [self.model.embed(keywords).mean(axis=0, keepdims=True) for keywords in seed_keywords] ) doc_embeddings = (doc_embeddings * 3 + seed_embeddings) / 4 # Find keywords all_keywords = [] for index, _ in enumerate(docs): try: # Select embeddings candidate_indices = df[index].nonzero()[1] candidates = [words[index] for index in candidate_indices] candidate_embeddings = word_embeddings[candidate_indices] doc_embedding = doc_embeddings[index].reshape(1, -1) # Maximal Marginal Relevance (MMR) if use_mmr: keywords = mmr( doc_embedding, candidate_embeddings, candidates, top_n, diversity, ) # Max Sum Distance elif use_maxsum: keywords = max_sum_distance( doc_embedding, candidate_embeddings, candidates, top_n, nr_candidates, ) # Cosine-based keyword extraction else: distances = cosine_similarity(doc_embedding, candidate_embeddings) keywords = [ (candidates[index], round(float(distances[0][index]), 4)) for index in distances.argsort()[0][-top_n:] ][::-1] all_keywords.append(keywords) # Capturing empty keywords except ValueError: all_keywords.append([]) # Highlight keywords in the document if len(all_keywords) == 1: if highlight: highlight_document(docs[0], all_keywords[0], count) all_keywords = all_keywords[0] # Fine-tune keywords using an LLM if self.llm is not None: import torch doc_embeddings = torch.from_numpy(doc_embeddings).float() if torch.cuda.is_available(): doc_embeddings = doc_embeddings.to("cuda") if isinstance(all_keywords[0], tuple): candidate_keywords = [[keyword[0] for keyword in all_keywords]] else: candidate_keywords = [[keyword[0] for keyword in keywords] for keywords in all_keywords] keywords = self.llm.extract_keywords( docs, embeddings=doc_embeddings, candidate_keywords=candidate_keywords, threshold=threshold, ) return keywords return all_keywords ``` -------------------------------- ### Cohere.__init__ Source: https://maartengr.github.io/KeyBERT/api/cohere.html Initializes a Cohere instance with a Cohere client, model name, custom prompt, delay between requests, and verbosity flag. ```APIDOC ## Cohere.__init__ ### Description Initializes a Cohere instance for keyword extraction using the Cohere API. The constructor accepts a Cohere client, model name, optional custom prompt, delay between requests to avoid rate limits, and a verbose flag for progress display. ### Method Constructor ### Parameters - **client** (cohere.Client) - Required - A Cohere client instance. - **model** (str) - Optional - Model to use within Cohere, defaults to `"command"`. - **prompt** (str) - Optional - Custom prompt to use. If not provided, the default prompt is used. Use `[DOCUMENT]` and `[CANDIDATES]` placeholders. - **delay_in_seconds** (float) - Optional - Delay between consecutive prompts to prevent RateLimitErrors. - **verbose** (bool) - Optional - Set to True to show a progress bar during keyword extraction. ### Usage Example ```python import cohere from keybert.llm import Cohere co = cohere.Client(my_api_key) llm = Cohere(co) ``` ``` -------------------------------- ### KeyBERT.__init__ Source: https://maartengr.github.io/KeyBERT/api/keybert.html Initializes the KeyBERT model with a custom embedding model or backend, and optionally an LLM for keyword extraction. ```APIDOC ## KeyBERT.__init__ ### Description Initializes the KeyBERT model. The model parameter can be a string pointing to a sentence-transformers model or a KeyBERT backend (SentenceTransformers, Transformers, Flair, Spacy, Gensim, USE). The llm parameter accepts a BaseLLM instance for LLM-based keyword extraction. ### Method Constructor ### Signature ```python KeyBERT(model="all-MiniLM-L6-v2", llm: BaseLLM = None) ``` ### Parameters - **model** (str or backend) - Optional - Custom embedding model or KeyBERT backend. Defaults to "all-MiniLM-L6-v2". - **llm** (BaseLLM) - Optional - Large Language Model used to extract keywords. ### Notes - Supported backends: SentenceTransformers, Transformers, Flair, Spacy, Gensim, USE (TF-Hub). - For sentence-transformers models, see https://www.sbert.net/docs/pretrained_models.html. ``` -------------------------------- ### KeyBERT.__init__ Source: https://maartengr.github.io/KeyBERT/api/keybert.html Initializes a KeyBERT model with a custom embedding model or backend and an optional Large Language Model for keyword extraction. ```APIDOC ## KeyBERT.__init__ ### Description Initializes a KeyBERT model with a custom embedding model or backend and an optional Large Language Model for keyword extraction. ### Method Constructor ### Signature `__init__(model='all-MiniLM-L6-v2', llm=None)` ### Parameters - **model** (str or backend) - Optional - Use a custom embedding model or a specific KeyBERT Backend. Supported backends: SentenceTransformers, Transformers, Flair, Spacy, Gensim, USE (TF-Hub). Can also be a string pointing to a sentence-transformers model. Default: `'all-MiniLM-L6-v2'`. - **llm** (BaseLLM) - Optional - The Large Language Model used to extract keywords. Default: `None`. ### Source Code ```python def __init__( self, model="all-MiniLM-L6-v2", llm: BaseLLM = None, ): """KeyBERT initialization. Arguments: model: Use a custom embedding model or a specific KeyBERT Backend. The following backends are currently supported: * SentenceTransformers * Transformers * Flair * Spacy * Gensim * USE (TF-Hub) You can also pass in a string that points to one of the following sentence-transformers models: * https://www.sbert.net/docs/pretrained_models.html llm: The Large Language Model used to extract keywords """ self.model = select_backend(model) if isinstance(llm, BaseLLM): self.llm = KeyLLM(llm) else: self.llm = llm ``` ``` -------------------------------- ### Load Llama 2 with Hugging Face Transformers Source: https://maartengr.github.io/KeyBERT/guides/llms.html Set up a text-generation pipeline with Llama 2 using 4-bit quantization to reduce GPU memory usage. ```python from torch import cuda, bfloat16 import transformers model_id = 'meta-llama/Llama-2-7b-chat-hf' # 4-bit Quantization to load Llama 2 with less GPU memory bnb_config = transformers.BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type='nf4', bnb_4bit_use_double_quant=True, bnb_4bit_compute_dtype=bfloat16 ) # Llama 2 Model & Tokenizer tokenizer = transformers.AutoTokenizer.from_pretrained(model_id) model = transformers.AutoModelForCausalLM.from_pretrained( model_id, trust_remote_code=True, quantization_config=bnb_config, device_map='auto', ) model.eval() # Our text generator generator = transformers.pipeline( model=model, tokenizer=tokenizer, task='text-generation', temperature=0.1, max_new_tokens=500, repetition_penalty=1.1 ) ``` -------------------------------- ### Custom prompt usage Source: https://maartengr.github.io/KeyBERT/api/litellm.html Shows how to pass a custom prompt to LiteLLM. The prompt must contain [DOCUMENT] where the document will be inserted. ```python prompt = "I have the following document: [DOCUMENT] \nThis document contains the following keywords separated by commas: '" llm = LiteLLM("gpt-3.5-turbo", prompt=prompt) ``` -------------------------------- ### KeyBERT.__init__ source code Source: https://maartengr.github.io/KeyBERT/api/keybert.html Constructor for the KeyBERT class. Accepts a model string or backend and an optional BaseLLM instance. If llm is a BaseLLM, it is wrapped in KeyLLM; otherwise it is stored as-is. ```python def __init__( self, model="all-MiniLM-L6-v2", llm: BaseLLM = None, ): """KeyBERT initialization. Arguments: model: Use a custom embedding model or a specific KeyBERT Backend. The following backends are currently supported: * SentenceTransformers * 🤗 Transformers * Flair * Spacy * Gensim * USE (TF-Hub) You can also pass in a string that points to one of the following sentence-transformers models: * https://www.sbert.net/docs/pretrained_models.html llm: The Large Language Model used to extract keywords """ self.model = select_backend(model) if isinstance(llm, BaseLLM): self.llm = KeyLLM(llm) else: self.llm = llm ``` -------------------------------- ### Pass LangChain to KeyLLM Source: https://maartengr.github.io/KeyBERT/guides/llms.html Create a LangChain LLM wrapper and load it into KeyLLM for keyword extraction. ```python from keybert.llm import LangChain from keybert import KeyLLM # Create your LLM llm = LangChain(chain) # Load it in KeyLLM kw_model = KeyLLM(llm) ``` -------------------------------- ### Pass chain to KeyBERT via KeyLLM Source: https://maartengr.github.io/KeyBERT/api/langchain.html Shows how to instantiate LangChain with the chain and use it with KeyLLM to extract keywords from a document. ```python from keybert.llm import LangChain from keybert import KeyLLM # Create your LLM llm = LangChain(chain) # Load it in KeyLLM kw_model = KeyLLM(llm) # Extract keywords document = "The website mentions that it only takes a couple of days to deliver but I still have not received mine." keywords = kw_model.extract_keywords(document) ``` -------------------------------- ### Use a custom prompt with Cohere Source: https://maartengr.github.io/KeyBERT/api/cohere.html Define a custom prompt and pass it to the Cohere LLM. The prompt must use [DOCUMENT] and ensure comma-separated keywords. ```python prompt = "I have the following document: [DOCUMENT]. What keywords does it contain? Make sure to separate the keywords with commas." llm = Cohere(co, prompt=prompt) ```