multiple: pydantic 2 compatibility, v0.3 (#26443)

Signed-off-by: ChengZi <chen.zhang@zilliz.com>
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com>
Co-authored-by: Dan O'Donovan <dan.odonovan@gmail.com>
Co-authored-by: Tom Daniel Grande <tomdgrande@gmail.com>
Co-authored-by: Grande <Tom.Daniel.Grande@statsbygg.no>
Co-authored-by: Bagatur <baskaryan@gmail.com>
Co-authored-by: ccurme <chester.curme@gmail.com>
Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
Co-authored-by: Tomaz Bratanic <bratanic.tomaz@gmail.com>
Co-authored-by: ZhangShenao <15201440436@163.com>
Co-authored-by: Friso H. Kingma <fhkingma@gmail.com>
Co-authored-by: ChengZi <chen.zhang@zilliz.com>
Co-authored-by: Nuno Campos <nuno@langchain.dev>
Co-authored-by: Morgante Pell <morgantep@google.com>
This commit is contained in:
Erick Friis
2024-09-13 14:38:45 -07:00
committed by GitHub
parent d9813bdbbc
commit c2a3021bb0
1402 changed files with 38318 additions and 30410 deletions

View File

@@ -6,8 +6,9 @@ from typing import Any, Dict, List, Optional
import numpy as np
from langchain_core._api.deprecation import deprecated
from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, root_validator
from langchain_core.runnables.config import run_in_executor
from pydantic import BaseModel, ConfigDict, model_validator
from typing_extensions import Self
@deprecated(
@@ -46,7 +47,7 @@ class BedrockEmbeddings(BaseModel, Embeddings):
)
"""
client: Any #: :meta private:
client: Any = None #: :meta private:
"""Bedrock client."""
region_name: Optional[str] = None
"""The aws region e.g., `us-west-2`. Fallsback to AWS_DEFAULT_REGION env variable
@@ -74,33 +75,32 @@ class BedrockEmbeddings(BaseModel, Embeddings):
normalize: bool = False
"""Whether the embeddings should be normalized to unit vectors"""
class Config:
extra = "forbid"
model_config = ConfigDict(extra="forbid", protected_namespaces=())
@root_validator(pre=False, skip_on_failure=True)
def validate_environment(cls, values: Dict) -> Dict:
@model_validator(mode="after")
def validate_environment(self) -> Self:
"""Validate that AWS credentials to and python package exists in environment."""
if values["client"] is not None:
return values
if self.client is not None:
return self
try:
import boto3
if values["credentials_profile_name"] is not None:
session = boto3.Session(profile_name=values["credentials_profile_name"])
if self.credentials_profile_name is not None:
session = boto3.Session(profile_name=self.credentials_profile_name)
else:
# use default credentials
session = boto3.Session()
client_params = {}
if values["region_name"]:
client_params["region_name"] = values["region_name"]
if self.region_name:
client_params["region_name"] = self.region_name
if values["endpoint_url"]:
client_params["endpoint_url"] = values["endpoint_url"]
if self.endpoint_url:
client_params["endpoint_url"] = self.endpoint_url
values["client"] = session.client("bedrock-runtime", **client_params)
self.client = session.client("bedrock-runtime", **client_params)
except ImportError:
raise ImportError(
@@ -114,7 +114,7 @@ class BedrockEmbeddings(BaseModel, Embeddings):
f"profile name are valid. Bedrock error: {e}"
) from e
return values
return self
def _embedding_func(self, text: str) -> List[float]:
"""Call out to Bedrock embedding endpoint."""