mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-03 20:16:52 +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:
@@ -5,8 +5,9 @@ from typing import Any, Callable, Dict, List, Mapping, Optional, Union
|
||||
|
||||
import openai
|
||||
from langchain_core.language_models import LangSmithParams
|
||||
from langchain_core.pydantic_v1 import Field, SecretStr, root_validator
|
||||
from langchain_core.utils import from_env, secret_from_env
|
||||
from pydantic import Field, SecretStr, model_validator
|
||||
from typing_extensions import Self, cast
|
||||
|
||||
from langchain_openai.llms.base import BaseOpenAI
|
||||
|
||||
@@ -100,29 +101,29 @@ class AzureOpenAI(BaseOpenAI):
|
||||
"""Return whether this model can be serialized by Langchain."""
|
||||
return True
|
||||
|
||||
@root_validator(pre=False, skip_on_failure=True, allow_reuse=True)
|
||||
def validate_environment(cls, values: Dict) -> Dict:
|
||||
@model_validator(mode="after")
|
||||
def validate_environment(self) -> Self:
|
||||
"""Validate that api key and python package exists in environment."""
|
||||
if values["n"] < 1:
|
||||
if self.n < 1:
|
||||
raise ValueError("n must be at least 1.")
|
||||
if values["streaming"] and values["n"] > 1:
|
||||
if self.streaming and self.n > 1:
|
||||
raise ValueError("Cannot stream results when n > 1.")
|
||||
if values["streaming"] and values["best_of"] > 1:
|
||||
if self.streaming and self.best_of > 1:
|
||||
raise ValueError("Cannot stream results when best_of > 1.")
|
||||
# For backwards compatibility. Before openai v1, no distinction was made
|
||||
# between azure_endpoint and base_url (openai_api_base).
|
||||
openai_api_base = values["openai_api_base"]
|
||||
if openai_api_base and values["validate_base_url"]:
|
||||
openai_api_base = self.openai_api_base
|
||||
if openai_api_base and self.validate_base_url:
|
||||
if "/openai" not in openai_api_base:
|
||||
values["openai_api_base"] = (
|
||||
values["openai_api_base"].rstrip("/") + "/openai"
|
||||
self.openai_api_base = (
|
||||
cast(str, self.openai_api_base).rstrip("/") + "/openai"
|
||||
)
|
||||
raise ValueError(
|
||||
"As of openai>=1.0.0, Azure endpoints should be specified via "
|
||||
"the `azure_endpoint` param not `openai_api_base` "
|
||||
"(or alias `base_url`)."
|
||||
)
|
||||
if values["deployment_name"]:
|
||||
if self.deployment_name:
|
||||
raise ValueError(
|
||||
"As of openai>=1.0.0, if `deployment_name` (or alias "
|
||||
"`azure_deployment`) is specified then "
|
||||
@@ -130,37 +131,39 @@ class AzureOpenAI(BaseOpenAI):
|
||||
"Instead use `deployment_name` (or alias `azure_deployment`) "
|
||||
"and `azure_endpoint`."
|
||||
)
|
||||
values["deployment_name"] = None
|
||||
client_params = {
|
||||
"api_version": values["openai_api_version"],
|
||||
"azure_endpoint": values["azure_endpoint"],
|
||||
"azure_deployment": values["deployment_name"],
|
||||
"api_key": values["openai_api_key"].get_secret_value()
|
||||
if values["openai_api_key"]
|
||||
self.deployment_name = None
|
||||
client_params: dict = {
|
||||
"api_version": self.openai_api_version,
|
||||
"azure_endpoint": self.azure_endpoint,
|
||||
"azure_deployment": self.deployment_name,
|
||||
"api_key": self.openai_api_key.get_secret_value()
|
||||
if self.openai_api_key
|
||||
else None,
|
||||
"azure_ad_token": values["azure_ad_token"].get_secret_value()
|
||||
if values["azure_ad_token"]
|
||||
"azure_ad_token": self.azure_ad_token.get_secret_value()
|
||||
if self.azure_ad_token
|
||||
else None,
|
||||
"azure_ad_token_provider": values["azure_ad_token_provider"],
|
||||
"organization": values["openai_organization"],
|
||||
"base_url": values["openai_api_base"],
|
||||
"timeout": values["request_timeout"],
|
||||
"max_retries": values["max_retries"],
|
||||
"default_headers": values["default_headers"],
|
||||
"default_query": values["default_query"],
|
||||
"azure_ad_token_provider": self.azure_ad_token_provider,
|
||||
"organization": self.openai_organization,
|
||||
"base_url": self.openai_api_base,
|
||||
"timeout": self.request_timeout,
|
||||
"max_retries": self.max_retries,
|
||||
"default_headers": self.default_headers,
|
||||
"default_query": self.default_query,
|
||||
}
|
||||
if not values.get("client"):
|
||||
sync_specific = {"http_client": values["http_client"]}
|
||||
values["client"] = openai.AzureOpenAI(
|
||||
**client_params, **sync_specific
|
||||
if not self.client:
|
||||
sync_specific = {"http_client": self.http_client}
|
||||
self.client = openai.AzureOpenAI(
|
||||
**client_params,
|
||||
**sync_specific, # type: ignore[arg-type]
|
||||
).completions
|
||||
if not values.get("async_client"):
|
||||
async_specific = {"http_client": values["http_async_client"]}
|
||||
values["async_client"] = openai.AsyncAzureOpenAI(
|
||||
**client_params, **async_specific
|
||||
if not self.async_client:
|
||||
async_specific = {"http_client": self.http_async_client}
|
||||
self.async_client = openai.AsyncAzureOpenAI(
|
||||
**client_params,
|
||||
**async_specific, # type: ignore[arg-type]
|
||||
).completions
|
||||
|
||||
return values
|
||||
return self
|
||||
|
||||
@property
|
||||
def _identifying_params(self) -> Mapping[str, Any]:
|
||||
|
Reference in New Issue
Block a user