代码之家  ›  专栏  ›  技术社区  ›  Atoms

如何修复langchain中Pydantic的警告:“dict”方法已弃用;请改用“model_dump”

  •  -2
  • Atoms  · 技术社区  · 2 年前

    当我从langchain使用OpenAI运行RAG链代码时,它会向我发出这样的警告:

    PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/
      warnings.warn('The `dict` method is deprecated; use `model_dump` instead.', DeprecationWarning)
    

    我没有地方可以替换 dict 具有 model_dump 我甚至没有在代码中的任何位置对其进行编码。知道如何解决这个警告吗?

    这是我的代码:

    from client_setup import get_client
    #from langchain_community.vectorstores import Weaviate
    #from langchain_openai.OpenAI import OpenAI
    from langchain_openai import OpenAI
    from langchain_community.vectorstores import Weaviate
    from langchain.retrievers.weaviate_hybrid_search import WeaviateHybridSearchRetriever
    from langchain.prompts import ChatPromptTemplate
    from langchain.schema.runnable import RunnablePassthrough
    from langchain.schema.output_parser import StrOutputParser
    
    
    client = get_client()
    
    retriever = WeaviateHybridSearchRetriever(
            client=client, 
            index_name="Material",
            text_key="su",
            attributes=["material", "heat_treatment", "su", "sy"],
            create_schema_if_missing=True,
            )
    
    llm = OpenAI(api_key="", model_name="gpt-3.5-turbo-instruct")
    
    
    template = """You are an assistant for question-answering tasks. 
    Use the following pieces of retrieved context to answer the question. 
    If you don't know the answer, just say that you don't know. 
    Use three sentences maximum and keep the answer concise.
    Question: {question} 
    Context: {context} 
    Answer:
    """
    prompt = ChatPromptTemplate.from_template(template)
    
    #print(prompt)
    query = "What heat treatment is used for Steel SAE 1040?"
    
    rag_chain = (
            {"context": retriever,
             "question": RunnablePassthrough()}
            | prompt
            | llm
            | StrOutputParser()
            )
    
    
    result = rag_chain.invoke(query)
    print(result)
    
    0 回复  |  直到 2 年前
        1
  •  1
  •   Ziqi    2 年前

    我也有同样的问题。这是langchain实现本身的问题。如果你像我一样使用langchain 0.1.0,那么它使用的Pydantic包似乎是2.5.2。但是,一些langchain代码(在我的例子中是langchain_community/chat_models/openai.py:456)使用了Pydantic早期版本中不推荐使用的方法。

    开发人员应该意识到这一点,如果他们需要将pydantic升级到3.0,他们应该为我们相应地迁移代码。

    所以现在我们很好,除非在某个时候你的代码依赖于Pydantic3,但langchain不依赖,并且会有冲突。。。