Skip to content

Wrong value assigned to the "question" at chain pipeline #2

@DmitryRoss

Description

@DmitryRoss

"question": RunnablePassthrough(),

First of all, thank you for such good course, I've learned really a lot from you. Respect!

I found a bug at "question": RunnablePassthrough(),
Here you are assigning entire input dictionary to the "question". On the other hand "question" is used at prompt Question: {question}

I've built a small debug function to output what LLM takes as a prompt:

def debug_prompt(prompt_template, name="PROMPT DEBUG"):
    """
    Creates runnable that prints formatted prompt before sending it to the LLM

    :param prompt_template: Your ChatPromptTemplate or PromptTemplate.
    :param name: Label for output.
    """
    def printer(x):
        print(f"\n=== {name} ===")
        formatted_prompt = prompt_template.format(**x)
        print(formatted_prompt)
        print("="*30)
        return x  # returns dict w/o changes to not break down the pipeline
    
    return RunnableLambda(printer)

Added it to the chain pipeline:

chain = (
    RunnableParallel(
        {
            "context": _search_query | retriever,
            "question": RunnablePassthrough(), # replace to fix "question": RunnableLambda(lambda x: x["question"]), 
        }
    )
    | debug_prompt(prompt)
    | prompt
    | chat
    | StrOutputParser()
)

Here is how Question look:
Question: {'question': 'What did Romulus?', 'chat_history': [('Who was the first emperor?', 'Augustus was the first emperor.')]}
Use natural language and be concise.
Answer:

If I were you, I'de replaced "question": RunnablePassthrough(), with "question": RunnableLambda(lambda x: x["question"]). That worked fine for me.

Again, thank you for your work it made me smarter :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions