当前位置:   article > 正文

构建LangChain应用出现的TypeError错误_expected a runnable, callable or dict.instead got

expected a runnable, callable or dict.instead got an unsupported type:

在阅读LangChain官网给出的一些案列时,实际运行却报错,案列代码如下:

from langchain.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser

prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
model = ChatOpenAI()
output_parser = StrOutputParser()

chain = prompt | model | output_parser

chain.invoke({"topic": "ice cream"})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

错误1:TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class ‘langchain_core.output_parsers.string.StrOutputParser’>

完整报错信息如下:

Traceback (most recent call last):
  File "~/PycharmProjects/LangChain/main.py", line 14, in <module>
    chain = prompt | model | output_parser
  File "~/opt/anaconda3/envs/langchain/lib/python3.8/site-packages/langchain/schema/runnable/base.py", line 1165, in __or__
    last=coerce_to_runnable(other),
  File "~/opt/anaconda3/envs/langchain/lib/python3.8/site-packages/langchain/schema/runnable/base.py", line 2774, in coerce_to_runnable
    raise TypeError(
TypeError: Expected a Runnable, callable or dict.Instead got an unsupported type: <class 'langchain_core.output_parsers.string.StrOutputParser'>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

出现这个错误的原因是因为输出解析器不正确,不支持StrOutputParser而是要使用BaseOutputParser,因此我们可以自己来实现一个BaseOutputParser:
如下:

class CommaSeparatedListOutputParser(BaseOutputParser):
    """Parse the output of an LLM call to a comma-separated list."""

    def parse(self, text: str):
        """Parse the output of an LLM call."""

        return text.strip()

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

即更新代码如下:

import os
from langchain.prompts import ChatPromptTemplate
from langchain_community.chat_models import ChatOpenAI
from langchain.schema import BaseOutputParser

os.environ["OPENAI_API_KEY"] = "xxx"

class CommaSeparatedListOutputParser(BaseOutputParser):
    """Parse the output of an LLM call to a comma-separated list."""

    def parse(self, text: str):
        """Parse the output of an LLM call."""

        return text.strip()


prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
model = ChatOpenAI()
output_parser = CommaSeparatedListOutputParser()

chain = prompt | model | output_parser

chain.invoke({"topic": "ice cream"})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

仍然报错2

错误2:TypeError: Got unknown type (‘messages’, [HumanMessage(content=‘tell me a short joke about ice cream’)])

错误原因是引入ChatOpenAI的包不对,原始的引入是from langchain_community.chat_models import ChatOpenAI改为from langchain.chat_models import ChatOpenAI即可,修改上面2处问题后,即可正确运行代码

完整正确的代码如下

import os
from langchain.prompts import ChatPromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.schema import BaseOutputParser

os.environ["OPENAI_API_KEY"] = "xxx"


class CommaSeparatedListOutputParser(BaseOutputParser):
    """Parse the output of an LLM call to a comma-separated list."""

    def parse(self, text: str):
        """Parse the output of an LLM call."""

        return text.strip()


prompt = ChatPromptTemplate.from_template("tell me a short joke about {topic}")
model = ChatOpenAI()
output_parser = CommaSeparatedListOutputParser()

chain = prompt | model | output_parser

res = chain.invoke({"topic": "ice cream"})
print(res)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

输出:

Why did the ice cream go to therapy?
Because it had too many toppings and couldn't keep its sprinkles together!
  • 1
  • 2
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/692352
推荐阅读
相关标签