mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-02 03:26:17 +00:00
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:
@@ -8,8 +8,8 @@ from typing import Any, Dict, Iterator, List, Mapping, Optional
|
||||
from langchain_core.callbacks import CallbackManagerForLLMRun
|
||||
from langchain_core.language_models.llms import LLM
|
||||
from langchain_core.outputs import GenerationChunk
|
||||
from langchain_core.pydantic_v1 import BaseModel
|
||||
from langchain_core.utils import pre_init
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
|
||||
from langchain_community.llms.utils import enforce_stop_tokens
|
||||
|
||||
@@ -61,7 +61,7 @@ class OCIAuthType(Enum):
|
||||
class OCIGenAIBase(BaseModel, ABC):
|
||||
"""Base class for OCI GenAI models"""
|
||||
|
||||
client: Any #: :meta private:
|
||||
client: Any = Field(default=None, exclude=True) #: :meta private:
|
||||
|
||||
auth_type: Optional[str] = "API_KEY"
|
||||
"""Authentication type, could be
|
||||
@@ -79,10 +79,10 @@ class OCIGenAIBase(BaseModel, ABC):
|
||||
If not specified , DEFAULT will be used
|
||||
"""
|
||||
|
||||
model_id: str = None # type: ignore[assignment]
|
||||
model_id: Optional[str] = None
|
||||
"""Id of the model to call, e.g., cohere.command"""
|
||||
|
||||
provider: str = None # type: ignore[assignment]
|
||||
provider: Optional[str] = None
|
||||
"""Provider name of the model. Default to None,
|
||||
will try to be derived from the model_id
|
||||
otherwise, requires user input
|
||||
@@ -91,15 +91,19 @@ class OCIGenAIBase(BaseModel, ABC):
|
||||
model_kwargs: Optional[Dict] = None
|
||||
"""Keyword arguments to pass to the model"""
|
||||
|
||||
service_endpoint: str = None # type: ignore[assignment]
|
||||
service_endpoint: Optional[str] = None
|
||||
"""service endpoint url"""
|
||||
|
||||
compartment_id: str = None # type: ignore[assignment]
|
||||
compartment_id: Optional[str] = None
|
||||
"""OCID of compartment"""
|
||||
|
||||
is_stream: bool = False
|
||||
"""Whether to stream back partial progress"""
|
||||
|
||||
model_config = ConfigDict(
|
||||
extra="forbid", arbitrary_types_allowed=True, protected_namespaces=()
|
||||
)
|
||||
|
||||
@pre_init
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
"""Validate that OCI config and python package exists in environment."""
|
||||
@@ -189,6 +193,12 @@ class OCIGenAIBase(BaseModel, ABC):
|
||||
if self.provider is not None:
|
||||
provider = self.provider
|
||||
else:
|
||||
if self.model_id is None:
|
||||
raise ValueError(
|
||||
"model_id is required to derive the provider, "
|
||||
"please provide the provider explicitly or specify "
|
||||
"the model_id to derive the provider."
|
||||
)
|
||||
provider = self.model_id.split(".")[0].lower()
|
||||
|
||||
if provider not in provider_map:
|
||||
@@ -230,8 +240,10 @@ class OCIGenAI(LLM, OCIGenAIBase):
|
||||
)
|
||||
"""
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
model_config = ConfigDict(
|
||||
extra="forbid",
|
||||
arbitrary_types_allowed=True,
|
||||
)
|
||||
|
||||
@property
|
||||
def _llm_type(self) -> str:
|
||||
@@ -260,6 +272,12 @@ class OCIGenAI(LLM, OCIGenAIBase):
|
||||
if stop is not None:
|
||||
_model_kwargs[self._provider.stop_sequence_key] = stop
|
||||
|
||||
if self.model_id is None:
|
||||
raise ValueError(
|
||||
"model_id is required to call the model, "
|
||||
"please provide the model_id."
|
||||
)
|
||||
|
||||
if self.model_id.startswith(CUSTOM_ENDPOINT_PREFIX):
|
||||
serving_mode = models.DedicatedServingMode(endpoint_id=self.model_id)
|
||||
else:
|
||||
|
Reference in New Issue
Block a user