mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-04 02:33:05 +00:00
community[patch],core[patch]: Update EdenaiTool root_validator and add unit test in core (#25233)
This PR gets rid `root_validators(allow_reuse=True)` logic used in EdenAI Tool in preparation for pydantic 2 upgrade. - add another test to secret_from_env_factory
This commit is contained in:
parent
c3ced4c6ce
commit
b6f0174bb9
@ -29,8 +29,6 @@ class EdenAiSpeechToTextTool(EdenaiTool):
|
|||||||
You can find your token here: https://app.edenai.run/admin/account/settings
|
You can find your token here: https://app.edenai.run/admin/account/settings
|
||||||
"""
|
"""
|
||||||
|
|
||||||
edenai_api_key: Optional[str] = None
|
|
||||||
|
|
||||||
name: str = "edenai_speech_to_text"
|
name: str = "edenai_speech_to_text"
|
||||||
description = (
|
description = (
|
||||||
"A wrapper around edenai Services speech to text "
|
"A wrapper around edenai Services speech to text "
|
||||||
|
@ -6,9 +6,9 @@ from typing import Any, Dict, List, Optional
|
|||||||
|
|
||||||
import requests
|
import requests
|
||||||
from langchain_core.callbacks import CallbackManagerForToolRun
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
||||||
from langchain_core.pydantic_v1 import root_validator
|
from langchain_core.pydantic_v1 import Field, SecretStr
|
||||||
from langchain_core.tools import BaseTool
|
from langchain_core.tools import BaseTool
|
||||||
from langchain_core.utils import get_from_dict_or_env
|
from langchain_core.utils import secret_from_env
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -23,20 +23,14 @@ class EdenaiTool(BaseTool):
|
|||||||
|
|
||||||
feature: str
|
feature: str
|
||||||
subfeature: str
|
subfeature: str
|
||||||
edenai_api_key: Optional[str] = None
|
edenai_api_key: SecretStr = Field(
|
||||||
|
default_factory=secret_from_env("EDENAI_API_KEY", default=None)
|
||||||
|
)
|
||||||
is_async: bool = False
|
is_async: bool = False
|
||||||
|
|
||||||
providers: List[str]
|
providers: List[str]
|
||||||
"""provider to use for the API call."""
|
"""provider to use for the API call."""
|
||||||
|
|
||||||
@root_validator(allow_reuse=True)
|
|
||||||
def validate_environment(cls, values: Dict) -> Dict:
|
|
||||||
"""Validate that api key exists in environment."""
|
|
||||||
values["edenai_api_key"] = get_from_dict_or_env(
|
|
||||||
values, "edenai_api_key", "EDENAI_API_KEY"
|
|
||||||
)
|
|
||||||
return values
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_user_agent() -> str:
|
def get_user_agent() -> str:
|
||||||
from langchain_community import __version__
|
from langchain_community import __version__
|
||||||
@ -54,11 +48,8 @@ class EdenaiTool(BaseTool):
|
|||||||
requests.Response: The response from the EdenAI API call.
|
requests.Response: The response from the EdenAI API call.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# faire l'API call
|
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {self.edenai_api_key}",
|
"Authorization": f"Bearer {self.edenai_api_key.get_secret_value()}",
|
||||||
"User-Agent": self.get_user_agent(),
|
"User-Agent": self.get_user_agent(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +20,7 @@ count=$(git grep -E '(@root_validator)|(@validator)|(@pre_init)' -- "*.py" | wc
|
|||||||
# PRs that increase the current count will not be accepted.
|
# PRs that increase the current count will not be accepted.
|
||||||
# PRs that decrease update the code in the repository
|
# PRs that decrease update the code in the repository
|
||||||
# and allow decreasing the count of are welcome!
|
# and allow decreasing the count of are welcome!
|
||||||
current_count=337
|
current_count=336
|
||||||
|
|
||||||
if [ "$count" -gt "$current_count" ]; then
|
if [ "$count" -gt "$current_count" ]; then
|
||||||
echo "The PR seems to be introducing new usage of @root_validator and/or @field_validator."
|
echo "The PR seems to be introducing new usage of @root_validator and/or @field_validator."
|
||||||
|
@ -6,7 +6,9 @@ import pytest
|
|||||||
from langchain_community.tools.edenai import EdenAiTextModerationTool
|
from langchain_community.tools.edenai import EdenAiTextModerationTool
|
||||||
|
|
||||||
tool = EdenAiTextModerationTool( # type: ignore[call-arg]
|
tool = EdenAiTextModerationTool( # type: ignore[call-arg]
|
||||||
providers=["openai"], language="en", edenai_api_key="fake_key"
|
providers=["openai"],
|
||||||
|
language="en",
|
||||||
|
edenai_api_key="fake_key", # type: ignore[arg-type]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -328,14 +328,17 @@ def test_secret_from_env_with_custom_error_message(
|
|||||||
def test_using_secret_from_env_as_default_factory(
|
def test_using_secret_from_env_as_default_factory(
|
||||||
monkeypatch: pytest.MonkeyPatch,
|
monkeypatch: pytest.MonkeyPatch,
|
||||||
) -> None:
|
) -> None:
|
||||||
# Set the environment variable
|
|
||||||
monkeypatch.setenv("TEST_KEY", "secret_value")
|
|
||||||
# Get the function
|
|
||||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||||
|
|
||||||
class Foo(BaseModel):
|
class Foo(BaseModel):
|
||||||
secret: SecretStr = Field(default_factory=secret_from_env("TEST_KEY"))
|
secret: SecretStr = Field(default_factory=secret_from_env("TEST_KEY"))
|
||||||
|
|
||||||
|
# Pass the secret as a parameter
|
||||||
|
foo = Foo(secret="super_secret") # type: ignore[arg-type]
|
||||||
|
assert foo.secret.get_secret_value() == "super_secret"
|
||||||
|
|
||||||
|
# Set the environment variable
|
||||||
|
monkeypatch.setenv("TEST_KEY", "secret_value")
|
||||||
assert Foo().secret.get_secret_value() == "secret_value"
|
assert Foo().secret.get_secret_value() == "secret_value"
|
||||||
|
|
||||||
class Bar(BaseModel):
|
class Bar(BaseModel):
|
||||||
|
Loading…
Reference in New Issue
Block a user