diff --git a/.env b/.env index f78637c..c0057f1 100644 --- a/.env +++ b/.env @@ -1 +1,2 @@ PYTHON_KEYRING_BACKEND=keyring.backends.null.Keyring +PYTHONIOENCODING=utf-8 \ No newline at end of file diff --git a/langchain_glm/agents/zhipuai_all_tools/base.py b/langchain_glm/agents/zhipuai_all_tools/base.py index dceccee..0e76cea 100644 --- a/langchain_glm/agents/zhipuai_all_tools/base.py +++ b/langchain_glm/agents/zhipuai_all_tools/base.py @@ -26,7 +26,9 @@ from langchain_core.runnables.base import RunnableBindingBase from langchain_core.tools import BaseTool from langchain_core.utils.function_calling import convert_to_openai_tool -from pydantic.v1 import BaseModel, Field, validator +from pydantic.v1 import Field, validator +from typing_extensions import ClassVar +from zhipuai.core import PYDANTIC_V2, BaseModel, ConfigDict from langchain_glm.agent_toolkits.all_tools.registry import ( TOOL_STRUCT_TYPE_TO_TOOL_CLASS, @@ -143,8 +145,6 @@ class ZhipuAIAllToolsRunnable(RunnableSerializable[Dict, OutputType]): """工具模型""" callback: AgentExecutorAsyncIteratorCallbackHandler """ZhipuAI AgentExecutor callback.""" - check_every_ms: float = 1_000.0 - """Frequency with which to check run progress in ms.""" intermediate_steps: List[Tuple[AgentAction, BaseToolOutput]] = [] """intermediate_steps to store the data to be processed.""" history: List[Union[List, Tuple, Dict]] = [] @@ -153,9 +153,8 @@ class ZhipuAIAllToolsRunnable(RunnableSerializable[Dict, OutputType]): class Config: arbitrary_types_allowed = True - @validator("intermediate_steps", pre=True, each_item=True, allow_reuse=True) - def check_intermediate_steps(cls, v): - return v + if PYDANTIC_V2: + model_config: ClassVar[ConfigDict] = ConfigDict(arbitrary_types_allowed=True) @staticmethod def paser_all_tools( diff --git a/langchain_glm/agents/zhipuai_all_tools/schema.py b/langchain_glm/agents/zhipuai_all_tools/schema.py index 1eabb27..d999f3a 100644 --- a/langchain_glm/agents/zhipuai_all_tools/schema.py +++ b/langchain_glm/agents/zhipuai_all_tools/schema.py @@ -6,8 +6,8 @@ from numbers import Number from typing import Any, Dict, List, Optional, Union -from typing_extensions import Self -from zhipuai.core import BaseModel +from typing_extensions import ClassVar, Self +from zhipuai.core import PYDANTIC_V2, BaseModel, ConfigDict class MsgType: @@ -18,8 +18,12 @@ class MsgType: class AllToolsBaseComponent(BaseModel): - class Config: - arbitrary_types_allowed = True + if PYDANTIC_V2: + model_config: ClassVar[ConfigDict] = ConfigDict(arbitrary_types_allowed=True) + else: + + class Config: + arbitrary_types_allowed = True @classmethod @abstractmethod diff --git a/langchain_glm/chat_models/base.py b/langchain_glm/chat_models/base.py index 9ea718f..7fce408 100644 --- a/langchain_glm/chat_models/base.py +++ b/langchain_glm/chat_models/base.py @@ -77,6 +77,8 @@ ) from langchain_core.utils.json import parse_partial_json from langchain_core.utils.utils import build_extra_kwargs +from typing_extensions import ClassVar +from zhipuai.core import PYDANTIC_V2, ConfigDict from langchain_glm.chat_models.all_tools_message import ( ALLToolsMessageChunk, @@ -84,9 +86,9 @@ ) if TYPE_CHECKING: - from langchain_core.pydantic_v1 import BaseModel from langchain_core.runnables import Runnable, RunnableConfig from langchain_core.tools import BaseTool + from zhipuai.core import BaseModel logger = logging.getLogger(__name__) @@ -378,10 +380,12 @@ def is_lc_serializable(cls) -> bool: http_client: Union[Any, None] = None """Optional httpx.Client.""" - class Config: - """Configuration for this pydantic object.""" + if PYDANTIC_V2: + model_config: ClassVar[ConfigDict] = ConfigDict(populate_by_name=True) + else: - allow_population_by_field_name = True + class Config: + allow_population_by_field_name = True @root_validator(pre=True, allow_reuse=True) def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: diff --git a/langchain_glm/embeddings/base.py b/langchain_glm/embeddings/base.py index 1310c37..afb6261 100644 --- a/langchain_glm/embeddings/base.py +++ b/langchain_glm/embeddings/base.py @@ -19,12 +19,9 @@ cast, ) -import numpy as np -import tiktoken import zhipuai from langchain_core.embeddings import Embeddings from langchain_core.pydantic_v1 import ( - BaseModel, Extra, Field, SecretStr, @@ -35,6 +32,8 @@ get_from_dict_or_env, get_pydantic_field_names, ) +from typing_extensions import ClassVar +from zhipuai.core import PYDANTIC_V2, BaseModel, ConfigDict logger = logging.getLogger(__name__) @@ -85,11 +84,14 @@ class ZhipuAIEmbeddings(BaseModel, Embeddings): http_client: Union[Any, None] = None """Optional httpx.Client.""" - class Config: - """Configuration for this pydantic object.""" + if PYDANTIC_V2: + model_config: ClassVar[ConfigDict] = ConfigDict( + extra="forbid", populate_by_name=True + ) + else: - extra = Extra.forbid - allow_population_by_field_name = True + class Config: + allow_population_by_field_name = True @root_validator(pre=True) def build_extra(cls, values: Dict[str, Any]) -> Dict[str, Any]: diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 6b85a17..0000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -env = - PYTHONIOENCODING=utf-8 diff --git a/tests/assistant/server/server.py b/tests/assistant/server/server.py index a45f168..91dee1d 100644 --- a/tests/assistant/server/server.py +++ b/tests/assistant/server/server.py @@ -6,9 +6,9 @@ from fastapi import APIRouter, Body, FastAPI, status from fastapi.middleware.cors import CORSMiddleware from langchain.agents import tool -from langchain.tools.shell import ShellTool +from langchain_community.tools import ShellTool from langchain_core.agents import AgentAction -from pydantic.v1 import BaseModel, Extra, Field +from pydantic.v1 import Extra, Field from sse_starlette.sse import EventSourceResponse from uvicorn import Config, Server from zhipuai.core.logs import ( diff --git a/tests/integration_tests/all_tools/test_alltools.py b/tests/integration_tests/all_tools/test_alltools.py index f616200..050e34e 100644 --- a/tests/integration_tests/all_tools/test_alltools.py +++ b/tests/integration_tests/all_tools/test_alltools.py @@ -4,8 +4,6 @@ import pytest from langchain.agents import tool -from langchain.tools.shell import ShellTool -from pydantic.v1 import BaseModel, Extra, Field from langchain_glm.agent_toolkits import BaseToolOutput from langchain_glm.agents.zhipuai_all_tools import ( diff --git a/tests/unit_tests/output_parsers/test_message_tool_paser_chunk.py b/tests/unit_tests/output_parsers/test_message_tool_paser_chunk.py index 9b32a1d..df9c96c 100644 --- a/tests/unit_tests/output_parsers/test_message_tool_paser_chunk.py +++ b/tests/unit_tests/output_parsers/test_message_tool_paser_chunk.py @@ -38,7 +38,7 @@ def test_paser_web_browser_success_tool_calls(): { "title": "昨夜今晨,京津冀发生这些大事(2024年6月27日) - 腾讯网", "link": "https://new.qq.com/rain/a/20240627A013AI00", - "content": "北京首套房首付比例最低2成. “517”楼市新政的“靴子”在北京落地了。. " + "content": "北京首套房首付比例最低2成. “517”楼市新政的“靴子”在北京落地了。. ", } ] }, diff --git a/tests/unit_tests/test_code_interpreter.py b/tests/unit_tests/test_code_interpreter.py index 26f99ff..f63f1a2 100644 --- a/tests/unit_tests/test_code_interpreter.py +++ b/tests/unit_tests/test_code_interpreter.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- import sys + import pytest + from langchain_glm.agent_toolkits.all_tools.code_interpreter_tool import ( CodeInterpreterAllToolExecutor, ) @@ -14,8 +16,8 @@ def test_python_ast_interpreter(): ) print(out.data) assert ( - out.data - != """Access:code_interpreter,python_repl_ast, Message: print('Hello, World!') + out.data + != """Access:code_interpreter,python_repl_ast, Message: print('Hello, World!') Hello, World! """ ) diff --git a/tests/unit_tests/tools_bind/test_tools_bind.py b/tests/unit_tests/tools_bind/test_tools_bind.py index dc60dc0..248ad85 100644 --- a/tests/unit_tests/tools_bind/test_tools_bind.py +++ b/tests/unit_tests/tools_bind/test_tools_bind.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- from langchain.agents import tool as register_tool -from langchain.tools.shell import ShellTool +from langchain_community.tools import ShellTool from langchain_core.runnables import RunnableBinding -from pydantic.v1 import BaseModel, Extra, Field +from pydantic.v1 import Extra, Field from langchain_glm.agent_toolkits import BaseToolOutput from langchain_glm.agents.zhipuai_all_tools.base import _get_assistants_tool