[Python] Combine multiple texts with mathematical accuracy

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    In the work I am doing right now, I have multiple (say 5, for purposes of illustration) pieces of text, (which are somewhat close in meaning, let's say for clarity). My objective is to combine these 5 texts into a single piece of text. There are obviously seq-2-seq models that can easily do this.

    The catch: I have also been provided a vector α (alpha), which contains the weights corresponding to each paragraph. I need to use alpha_i as the weight for the i-th string when creating the final combined text.

    I’m honestly stumped. I’ve thought of a few approaches, but I don’t have enough experience to tell whether they’re viable.

    Approach 1 : Text → Vector → Text: Convert each of the 5 strings into embeddings, perform weighted addition on these vectors, and then convert the resulting vector back into text.

    Problem: Vector-to-text doesn’t seem to have a straightforward solution.

    Using something like sentence-transformers in Python, I can easily go text → vector.

    But going back vector → text is not trivial.

    Possible workarounds:

    (a) Train my own decoder model: Take a dataset of strings, convert to embeddings, and train a decoder to reconstruct the text from the embeddings (original string as ground truth). Feels like the most principled solution, but may be expensive in terms of compute and time.

    (b) Generate a set of candidate texts using any LLM, embed them with the same encoder, and pick the one whose embedding is closest to the weighted vector. This is simpler but far from guaranteed to give a high-quality result.

    Approach 2 : Just ask an LLM directly: Simply instruct an LLM to generate a combined text given the strings and their weights. Example:

    “Given the following paragraphs and weights, combine them proportionally.”

    I’m not confident this would accurately respect the weights, but it’s an option.

    Approach 3 : Use a vector database: Create a large database of sentence embeddings, then find candidates whose embedding is closest to the weighted combination. This might give something plausible, but again, no guarantee.

    Continue reading...

Compartilhe esta Página