pydantic has been upgraded from 1.10.13 to version 2.7.1. How to modify the following code?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from pydantic.schema import model_schema
@classmethod
def create_prompt(
cls,
tools: Sequence[BaseTool],
prompt: str = None,
input_variables: Optional[List[str]] = None,
memory_prompts: Optional[List[BasePromptTemplate]] = None,
) -> BasePromptTemplate:
tools_json = []
tool_names = []
for tool in tools:
tool_schema = model_schema(tool.args_schema) if tool.args_schema else {}
simplified_config_langchain = {
"name": tool.name,
"description": tool.description,
"parameters": tool_schema.get("properties", {})
}
tools_json.append(simplified_config_langchain)
tool_names.append(tool.name)
formatted_tools = "n".join([
f"{tool['name']}: {tool['description']}, args: {tool['parameters']}"
for tool in tools_json
])
formatted_tools = formatted_tools.replace("'", "\'").replace("{", "{{").replace("}", "}}")
template = prompt.format(tool_names=tool_names,
tools=formatted_tools,
history="None",
input="{input}",
agent_scratchpad="{agent_scratchpad}")
if input_variables is None:
input_variables = ["input", "agent_scratchpad"]
_memory_prompts = memory_prompts or []
messages = [
SystemMessagePromptTemplate.from_template(template),
*_memory_prompts,
]
return ChatPromptTemplate(input_variables=input_variables, messages=messages)
</code>
<code>from pydantic.schema import model_schema @classmethod def create_prompt( cls, tools: Sequence[BaseTool], prompt: str = None, input_variables: Optional[List[str]] = None, memory_prompts: Optional[List[BasePromptTemplate]] = None, ) -> BasePromptTemplate: tools_json = [] tool_names = [] for tool in tools: tool_schema = model_schema(tool.args_schema) if tool.args_schema else {} simplified_config_langchain = { "name": tool.name, "description": tool.description, "parameters": tool_schema.get("properties", {}) } tools_json.append(simplified_config_langchain) tool_names.append(tool.name) formatted_tools = "n".join([ f"{tool['name']}: {tool['description']}, args: {tool['parameters']}" for tool in tools_json ]) formatted_tools = formatted_tools.replace("'", "\'").replace("{", "{{").replace("}", "}}") template = prompt.format(tool_names=tool_names, tools=formatted_tools, history="None", input="{input}", agent_scratchpad="{agent_scratchpad}") if input_variables is None: input_variables = ["input", "agent_scratchpad"] _memory_prompts = memory_prompts or [] messages = [ SystemMessagePromptTemplate.from_template(template), *_memory_prompts, ] return ChatPromptTemplate(input_variables=input_variables, messages=messages) </code>
from pydantic.schema import model_schema

@classmethod
    def create_prompt(
            cls,
            tools: Sequence[BaseTool],
            prompt: str = None,
            input_variables: Optional[List[str]] = None,
            memory_prompts: Optional[List[BasePromptTemplate]] = None,
    ) -> BasePromptTemplate:
        tools_json = []
        tool_names = []
        for tool in tools:
            tool_schema = model_schema(tool.args_schema) if tool.args_schema else {}
            simplified_config_langchain = {
                "name": tool.name,
                "description": tool.description,
                "parameters": tool_schema.get("properties", {})
            }
            tools_json.append(simplified_config_langchain)
            tool_names.append(tool.name)
        formatted_tools = "n".join([
            f"{tool['name']}: {tool['description']}, args: {tool['parameters']}"
            for tool in tools_json
        ])
        formatted_tools = formatted_tools.replace("'", "\'").replace("{", "{{").replace("}", "}}")
        template = prompt.format(tool_names=tool_names,
                                 tools=formatted_tools,
                                 history="None",
                                 input="{input}",
                                 agent_scratchpad="{agent_scratchpad}")

        if input_variables is None:
            input_variables = ["input", "agent_scratchpad"]
        _memory_prompts = memory_prompts or []
        messages = [
            SystemMessagePromptTemplate.from_template(template),
            *_memory_prompts,
        ]
        return ChatPromptTemplate(input_variables=input_variables, messages=messages)

Code after modification

model_schema can be used normally in pydantic1.0, but it is deprecated in pydantic2.0. Is there any replacement plan?

Error message:

/home/xqxls/git/Langchain-Chatchat/venv/lib/python3.11/site-packages/pydantic/_internal/_config.py:334: UserWarning: Valid config keys have changed in V2:

  • ‘schema_extra’ has been renamed to ‘json_schema_extra’
    warnings.warn(message, UserWarning)
    /home/xqxls/git/Langchain-Chatchat/venv/lib/python3.11/site-packages/pydantic/_internal/fields.py:160: UserWarning: Field “model_name” has conflict with protected namespace “model“.

You may be able to resolve this warning by setting model_config['protected_namespaces'] = ().
warnings.warn(
Process API Server:
Traceback (most recent call last):
File “/home/xqxls/anaconda3/lib/python3.11/multiprocessing/process.py”, line 314, in _bootstrap
self.run()
File “/home/xqxls/anaconda3/lib/python3.11/multiprocessing/process.py”, line 108, in run
self._target(*self._args, **self._kwargs)
File “/home/xqxls/git/Langchain-Chatchat/startup.py”, line 456, in run_api_server
app = create_app(run_mode=run_mode)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/xqxls/git/Langchain-Chatchat/server/api.py”, line 51, in create_app
mount_app_routes(app, run_mode=run_mode)
File “/home/xqxls/git/Langchain-Chatchat/server/api.py”, line 77, in mount_app_routes
mount_knowledge_routes(app)
File “/home/xqxls/git/Langchain-Chatchat/server/api.py”, line 142, in mount_knowledge_routes
from server.chat.agent_chat import agent_chat
File “/home/xqxls/git/Langchain-Chatchat/server/chat/agent_chat.py”, line 15, in
from server.agent.custom_agent.ChatGLM3Agent import initialize_glm3_agent
File “/home/xqxls/git/Langchain-Chatchat/server/agent/custom_agent/ChatGLM3Agent.py”, line 9, in
from pydantic.schema import model_schema
ImportError: cannot import name ‘model_schema’ from ‘pydantic.schema’ (/home/xqxls/git/Langchain-Chatchat/venv/lib/python3.11/site-packages/pydantic/schema.py)

New contributor

xqxls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật