nyx_extras ========== .. py:module:: nyx_extras .. autoapi-nested-parse:: NYC client SDK optional extras for langchain and auto-parsing. Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/nyx_extras/langchain/index /autoapi/nyx_extras/utils/index Classes ------- .. autoapisummary:: nyx_extras.NyxLangChain nyx_extras.Parser nyx_extras.Utils Package Contents ---------------- .. py:class:: NyxLangChain(config = None, llm = None, log_level = logging.WARN, system_prompt = None) Bases: :py:obj:`nyx_client.client.NyxClient` An opinionated client wrapping langChain to evaluate user queries against contents of a Nyx network. This class extends NyxClient to provide LangChain-based functionality for querying Nyx network contents. .. note:: The LLM must support tool calling. .. py:method:: query(query, data = None, include_own = False, sqlite_file = None) Query the LLM with a user prompt and context from Nyx. This method takes a user prompt and invokes it against the LLM associated with this instance, using context from Nyx. :param query: The user input. :param data: Sequence of data to use for context. If not specified, uses all subscribed data. :param include_own: Include your own data, created in Nyx, in the query. :param sqlite_file: A file location to write the sql_lite file to. :param update_subscribed): if set to true this will re-poll Nyx for subscribed data :returns: The answer from the LLM. .. note:: If the data list is not provided, this method updates subscriptions and retrieves all subscribed data. .. py:class:: Parser A class for processing and querying datasets from Data instances. This class provides methods to convert data into SQL databases or vector representations, and to perform queries on the processed data. .. attribute:: vectors The TF-IDF vector representations of the processed content. .. attribute:: vectorizer The TfidfVectorizer instance used for creating vectors. .. attribute:: chunks The text chunks created from the processed content. .. py:method:: data_as_db(data, additional_information = None, sqlite_file = None, if_exists = 'replace') :staticmethod: Process the content of multiple Data instances into an in-memory SQLite database. This method downloads the content of each Data (if it's a CSV) and converts it to an in-memory SQLite database. The resulting database engine is then returned for use with language models. :param data: A list of Data instances to process. :param additional_information: List of additional information to be stored in the DB as a fallback :param sqlite_file: Provide a file for the database to reside in :param if_exists: What to do if a table already exists Defaults to "fail" can be "fail", "append", "replace" :returns: An SQLAlchemy `engine.Engine` instance for the in-memory SQLite database. .. note:: If the list of data is empty, an empty database engine is returned. .. py:method:: normalise_values(values) :staticmethod: Normalise names in a list of values. :param values: A sequence of values to normalise. :returns: A list of normalised values. .. py:method:: data_as_vectors(data, chunk_size = 1000) Process the content of multiple Data instances into vector representations. This method downloads the content of each Data, combines it, chunks it, and creates a TF-IDF vectorizer for the chunks. :param data: A sequence of Data instances to process. :param chunk_size: The size of each chunk when splitting the content. Defaults to 1000. :returns: The current Parser instance with updated vectors, vectorizer, and chunks. .. note:: If no content is found in any of the data, the method returns without processing. .. py:method:: query(text, k = 3) Query the processed data with a given text. This method transforms the input text into a vector using the fitted vectorizer, and then finds the most similar chunks to this query vector. :param text: The query text to search for in the processed data. :param k: The number of top matching chunks to return. Defaults to 3. :returns: An object containing the top k matching chunks, their similarities, and associated metadata. If the vectorizer is not initialized, it returns a VectorResult indicating failure. .. note:: This method assumes that self.vectorizer has been properly initialized. If self.vectorizer is None, it returns a VectorResult indicating failure. .. py:method:: find_matching_chunk(query_vector, k = 3) Find the most similar chunks to the query vector. This method computes the cosine similarity between the query vector and all document vectors, then returns the top k most similar chunks along with their similarities and metadata. :param query_vector: The vector representation of the query. :param k: The number of top matching chunks to return. Defaults to 3. :returns: An object containing the top k matching chunks, their similarities, and associated metadata. If no vectors are available, it returns a VectorResult with empty lists and a failure message. .. note:: This method assumes that self.vectors, self.chunks, and self.metadata have been properly initialized. If self.vectors is None, it returns a VectorResult indicating failure. .. py:class:: Utils Utility methods for query (system) prompt modification. .. py:method:: with_sources(prompt, **kwargs) :staticmethod: Expand prompt with clause to request the data sources considered to be included in the response. .. py:method:: build_query(prompt, **kwargs) :staticmethod: Base prompt builder. .. py:method:: with_confidence(prompt) :staticmethod: Like `with_sources` but also requests for a confidence score to be included in the result.