内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list($forumlist, $model = 0, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $model . '-' . $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 不分模型 * @param int $display 0全部CMS栏目 1在首页和频道显示内容的栏目 * @param int $category 0列表 1频道 2单页 3外链 * @return array */ function category_list_show($forumlist, $display = 0, $category = 0) { if (empty($forumlist)) return NULL; static $cache = array(); $key = $display . '-' . $category; if (isset($cache[$key])) return $cache[$key]; if ($display) { foreach ($forumlist as $k => $val) { if (1 == $val['display'] && 1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } else { foreach ($forumlist as $k => $val) { if (1 == $val['type'] && $val['category'] == $category) { $cache[$key][$k] = $val; } } } return empty($cache[$key]) ? NULL : $cache[$key]; } /** * @param $forumlist 所有版块列表 * @return mixed BBS栏目数据(仅列表) 尚未开放bbs频道功能 */ function forum_list($forumlist) { if (empty($forumlist)) return array(); static $cache = array(); if (isset($cache['bbs_forum_list'])) return $cache['bbs_forum_list']; $cache['bbs_forum_list'] = array(); foreach ($forumlist as $_fid => $_forum) { if ($_forum['type']) continue; $cache['bbs_forum_list'][$_fid] = $_forum; } return $cache['bbs_forum_list']; } // 导航显示的版块 function nav_list($forumlist) { if (empty($forumlist)) return NULL; static $cache = array(); if (isset($cache['nav_list'])) return $cache['nav_list']; foreach ($forumlist as $fid => $forum) { if (0 == $forum['nav_display']) { unset($forumlist[$fid]); } } return $cache['nav_list'] = $forumlist; } ?>python - How to pass `self` into RunnableLambda? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - How to pass `self` into RunnableLambda? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked Feb 2 at 2:58 Code MonkeyCode Monkey 8441 gold badge13 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In most cases,easiest way to "pass self" in your Runnable steps is simply define a lambda that captures self inside your class method.

class MyPipeline:
    def __init__(self):
        self.general_model = SomeLangChainModel()
        ...
    def demographics_review(self,input):
        # do smth with `self` 
        return "some text"

    def management_plan(self, input):
        # do something else
        return "some other text"

    def build_chain(self):
        demographics_review_chain = (
            RunnableLambda(lambda x:self.demographics_review(x))
            | self.general_model
            | StrOutputParser()
        )
        management_plan_chain= (
            RunnableLambda(lambda x: self.management_plan(x))
            | self.general_model
            | StrOutputParser()
        )

        # now use RunnableParallel,capturing self again to combine:
        chain = (
            input_template 
            | RunnableParallel(
                {
                    "demographics_review": demographics_review_chain,
                    "management_plan":management_plan_chain
                }
            )
            | RunnableLambda(
                lambda x: selfbine_all(
                    x["branches"]["demographics_review"],
                    x["branches"]["management_plan"]
                )
            )
        )

        return chain
发布评论

评论列表(0)

  1. 暂无评论