mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-20 01:54:14 +00:00
community[patch]: Use get_fields adapter for pydantic (#25191)
Change all usages of __fields__ with get_fields adapter merged into langchain_core. Code mod generated using the following grit pattern: ``` engine marzano(0.1) language python `$X.__fields__` => `get_fields($X)` where { add_import(source="langchain_core.utils.pydantic", name="get_fields") } ```
This commit is contained in:
@@ -6,6 +6,7 @@ import pytest
|
||||
from langchain_core.callbacks.manager import CallbackManager, trace_as_chain_group
|
||||
from langchain_core.outputs import LLMResult
|
||||
from langchain_core.tracers.langchain import LangChainTracer, wait_for_all_tracers
|
||||
from langchain_core.utils.pydantic import get_fields
|
||||
|
||||
from langchain_community.callbacks import get_openai_callback
|
||||
from langchain_community.callbacks.manager import get_bedrock_anthropic_callback
|
||||
@@ -44,7 +45,7 @@ def test_callback_manager_configure_context_vars(
|
||||
"completion_tokens": 1,
|
||||
"total_tokens": 3,
|
||||
},
|
||||
"model_name": BaseOpenAI.__fields__["model_name"].default,
|
||||
"model_name": get_fields(BaseOpenAI)["model_name"].default,
|
||||
},
|
||||
)
|
||||
mngr.on_llm_start({}, ["prompt"])[0].on_llm_end(response)
|
||||
@@ -74,7 +75,7 @@ def test_callback_manager_configure_context_vars(
|
||||
"completion_tokens": 1,
|
||||
"total_tokens": 3,
|
||||
},
|
||||
"model_name": BaseOpenAI.__fields__["model_name"].default,
|
||||
"model_name": get_fields(BaseOpenAI)["model_name"].default,
|
||||
},
|
||||
)
|
||||
mngr.on_llm_start({}, ["prompt"])[0].on_llm_end(response)
|
||||
|
@@ -4,6 +4,7 @@ from uuid import uuid4
|
||||
import numpy as np
|
||||
import pytest
|
||||
from langchain_core.outputs import LLMResult
|
||||
from langchain_core.utils.pydantic import get_fields
|
||||
|
||||
from langchain_community.callbacks import OpenAICallbackHandler
|
||||
from langchain_community.llms.openai import BaseOpenAI
|
||||
@@ -23,7 +24,7 @@ def test_on_llm_end(handler: OpenAICallbackHandler) -> None:
|
||||
"completion_tokens": 1,
|
||||
"total_tokens": 3,
|
||||
},
|
||||
"model_name": BaseOpenAI.__fields__["model_name"].default,
|
||||
"model_name": get_fields(BaseOpenAI)["model_name"].default,
|
||||
},
|
||||
)
|
||||
handler.on_llm_end(response)
|
||||
|
@@ -1,6 +1,7 @@
|
||||
from typing import List, Type
|
||||
|
||||
from langchain_core.tools import BaseTool, StructuredTool
|
||||
from langchain_core.utils.pydantic import get_fields
|
||||
|
||||
import langchain_community.tools
|
||||
from langchain_community.tools import _DEPRECATED_TOOLS
|
||||
@@ -22,7 +23,7 @@ def _get_tool_classes(skip_tools_without_default_names: bool) -> List[Type[BaseT
|
||||
if isinstance(tool_class, type) and issubclass(tool_class, BaseTool):
|
||||
if tool_class in _EXCLUDE:
|
||||
continue
|
||||
if skip_tools_without_default_names and tool_class.__fields__[
|
||||
if skip_tools_without_default_names and get_fields(tool_class)[
|
||||
"name"
|
||||
].default in [ # type: ignore
|
||||
None,
|
||||
@@ -36,6 +37,6 @@ def _get_tool_classes(skip_tools_without_default_names: bool) -> List[Type[BaseT
|
||||
def test_tool_names_unique() -> None:
|
||||
"""Test that the default names for our core tools are unique."""
|
||||
tool_classes = _get_tool_classes(skip_tools_without_default_names=True)
|
||||
names = sorted([tool_cls.__fields__["name"].default for tool_cls in tool_classes])
|
||||
names = sorted([get_fields(tool_cls)["name"].default for tool_cls in tool_classes])
|
||||
duplicated_names = [name for name in names if names.count(name) > 1]
|
||||
assert not duplicated_names
|
||||
|
Reference in New Issue
Block a user