← All ivy-nodes
IVYXSTUDIO · IVY NODE
Q

Question

ivy.node.question · v0.0.0

ivyx

Converts a textual question into a numerical vector representation using a text vectorizer. This node is essential for RAG (Retrieval-Augmented Generation) pipelines where questions need to be vectorized for similarity search. The output vectorize array is used with milvus-search to find similar content, while the text output preserves the original question. Requires text-vectorizer from text-vectorizer node. Typically used before milvus-search in question-answering workflows.

#Question

Inputs

FieldTypeDescription
text_vectorizerrequiredobjectA text vectorizer instance responsible for generating embeddings (Node reference)
questionrequiredstringThe question to be vectorized
featuresobjectFeatures
labelsobjectLabels

Outputs

FieldTypeDescription
vectorizerequiredarrayNot declared
textrequiredstringNot declared
featuresobjectFeatures
labelsobjectLabels

Source

python

# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
text_vectorizer = inp["text_vectorizer"]
question = inp["question"]

# Compute
from typing import List

class Question:
    """
    A class for converting a textual question into a numerical vector representation 
    using a specified text vectorizer.

    Attributes:
        text_vectorizer: An instance of a text vectorization class that provides a `vectorizer_text` method.

    Methods:
        vectorize(question: str) -> List[float]:
            Converts the given question into a numerical vector representation.
    """

    def __init__(self, text_vectorizer: TextVectorizer):
        """
        Initializes the Question class with a text vectorizer.

        Args:
            text_vectorizer (TextVectorizer): An instance of a text vectorization class that 
                                              must have a callable `vectorizer_text` method.

        Raises:
            TypeError: If `text_vectorizer` does not have a callable `vectorizer_text` method.
        """
        if not hasattr(text_vectorizer, "vectorizer_text") or not callable(text_vectorizer.vectorizer_text):
            raise TypeError("text_vectorizer must be an instance of a class with a callable 'vectorizer_text' method.")
        
        self.text_vectorizer = text_vectorizer

    def vectorize(self, question: str) -> List[float]:
        """
        Converts the given question into a vector representation.

        Args:
            question (str): The input question to be vectorized.

        Returns:
            List[float]: A numerical vector representing the given question.

        Raises:
            ValueError: If `question` is not a valid non-empty string.
            RuntimeError: If vectorization fails due to an internal error.
        """
        if not isinstance(question, str):
            raise ValueError("The question must be a string.")
        if not question.strip():
            raise ValueError("The question cannot be empty.")

        try:
            return self.text_vectorizer.vectorizer_text(question)
        except Exception as e:
            raise RuntimeError(f"Vectorization failed: {str(e)}")
        
question_obj = Question(text_vectorizer=text_vectorizer)

vectorize = question_obj.vectorize(question=question)

text = question

# Output collection (runner reads __ivy_ctx__)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["vectorize"] = vectorize
out["text"] = text

Tests

Requires: python:3.11

  • basic-question-vectorization

    Vectorize valid question (should succeed).

    questionvectorizationbasic
  • error-empty-question

    Empty question should raise an error.

    error-handlingvalidation