anonymizer chaintemplate = """Answer the question based only on the following context:{context}Question: {anonymized_question}"""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI(temperature=0.3)_inputs = RunnableMap(    question=RunnablePassthrough(),    # It is important to remember about question anonymization    anonymized_question=RunnableLambda(anonymizer.anonymize),)anonymizer_chain = (    _inputs    | {        "context": itemgetter("anonymized_question") | retriever,        "anonymized_question": itemgetter("anonymized_question"),    }    | prompt    | model    | StrOutputParser())anonymizer_chain.invoke(    "Where did the theft of the wallet occur, at what time, and who was it stolen from?")    'The theft of the wallet occurred in the vicinity of New Rita during a bike trip. It was stolen from Brian Cox DVM. The time of the theft was 02:22 AM.'# 7. Add deanonymization step to the chainchain_with_deanonymization = anonymizer_chain | RunnableLambda(anonymizer.deanonymize)print(    chain_with_deanonymization.invoke(        "Where did the theft of the wallet occur, at what time, and who was it stolen from?"    ))    The theft of the wallet occurred in the vicinity of Kilmarnock during a bike trip. It was stolen from John Doe. The time of the theft was 9:30 AM.print(    chain_with_deanonymization.invoke("What was the content of the wallet in detail?"))    The content of the wallet included a credit card with the number 4111 1111 1111 1111, registered under the name of John Doe and linked to the bank account PL61109010140000071219812874. It also contained a driver's license with the number 999000680 issued to John Doe, as well as his Social Security Number 602-76-4532. Additionally, the wallet had a Polish identity card with the number ABC123456.print(chain_with_deanonymization.invoke("Whose phone number is it: 999-888-7777?"))    The phone number 999-888-7777 belongs to John Doe.Alternative approach: local embeddings + anonymizing the
Open In Colab
Open In Colab ->: anonymizer chaintemplate = """Answer the question based only on the following context:{context}Question: {anonymized_question}"""prompt = ChatPromptTemplate.from_template(template)model = ChatOpenAI(temperature=0.3)_inputs = RunnableMap(    question=RunnablePassthrough(),    # It is important to remember about question anonymization    anonymized_question=RunnableLambda(anonymizer.anonymize),)anonymizer_chain = (    _inputs    | {        "context": itemgetter("anonymized_question") | retriever,        "anonymized_question": itemgetter("anonymized_question"),    }    | prompt    | model    | StrOutputParser())anonymizer_chain.invoke(    "Where did the theft of the wallet occur, at what time, and who was it stolen from?")    'The theft of the wallet occurred in the vicinity of New Rita during a bike trip. It was stolen from Brian Cox DVM. The time of the theft was 02:22 AM.'# 7. Add deanonymization step to the chainchain_with_deanonymization = anonymizer_chain | RunnableLambda(anonymizer.deanonymize)print(    chain_with_deanonymization.invoke(        "Where did the theft of the wallet occur, at what time, and who was it stolen from?"    ))    The theft of the wallet occurred in the vicinity of Kilmarnock during a bike trip. It was stolen from John Doe. The time of the theft was 9:30 AM.print(    chain_with_deanonymization.invoke("What was the content of the wallet in detail?"))    The content of the wallet included a credit card with the number 4111 1111 1111 1111, registered under the name of John Doe and linked to the bank account PL61109010140000071219812874. It also contained a driver's license with the number 999000680 issued to John Doe, as well as his Social Security Number 602-76-4532. Additionally, the wallet had a Polish identity card with the number ABC123456.print(chain_with_deanonymization.invoke("Whose phone number is it: 999-888-7777?"))    The phone number 999-888-7777 belongs to John Doe.Alternative approach: local embeddings + anonymizing the