[Python] How to pass `self` into RunnableLambda?

Discussão em 'Python' iniciado por Stack, Fevereiro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am using LangChain and have a class with many methods. I intend to use parallel chains to process lots of data.

    Here is one of my steps, which happens to use other methods in the class in which it is contained:

    def management_plan(self, input) -> str: # uses management_plan_model
    print("PROCESSING write_notes coroutine: starting management_plan()")
    for notes_template_item in self.notes_template_item_list:
    if notes_template_item in self.similar_prompt_items['MANAGEMENT PLAN']:
    prompt = self.personality[notes_template_item].replace("{input_text}", input.messages[0].content)
    context = self.modify_context(self.personality['CONTEXT'], RAG_data=self.rag_bot.output+self.rag_bot.issues_list+self.rag_bot.differentials)
    prompt_template = ChatPromptTemplate.from_messages([("system","{context}"), ("human","{prompt}")])
    return prompt_template.format_prompt(context=context, prompt=prompt)


    This is one of my branches:

    demographics_review_chain = (RunnableLambda(lambda x: demographics_review(self, input=x)) | self.general_model | StrOutputParser())


    This is a part of the chain:

    chain = (
    input_template
    | RunnableParallel(branches={"demographics_review":demographics_review_chain, "management_plan":management_plan_chain...
    | RunnableLambda(lambda x: combine_all(self, x["branches"]["demographics_review"], x["branches"]["management_plan"]...
    )


    But this does not work. For one thing, even the model (self.general_model) cannot be reached, since it belongs to the class. So that branch provides no output.

    How do I pass self into the RunnableLambda to use methods and variables that are given in the class?

    Continue reading...

Compartilhe esta Página