mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-29 15:28:54 +00:00
This PR upgrades langchain-community to pydantic 2.
* Most of this PR was auto-generated using code mods with gritql
(https://github.com/eyurtsev/migrate-pydantic/tree/main)
* Subsequently, some code was fixed manually due to accommodate
differences between pydantic 1 and 2
Breaking Changes:
- Use TEXTEMBED_API_KEY and TEXTEMBEB_API_URL for env variables for text
embed integrations:
cbea780492
Other changes:
- Added pydantic_settings as a required dependency for community. This
may be removed if we have enough time to convert the dependency into an
optional one.
---------
Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com>
Co-authored-by: Bagatur <baskaryan@gmail.com>
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
from typing import Any, Dict, List, Mapping, Optional
|
|
|
|
from langchain_core.callbacks import CallbackManagerForLLMRun
|
|
from langchain_core.language_models.llms import LLM
|
|
from langchain_core.utils import pre_init
|
|
from pydantic import ConfigDict
|
|
|
|
|
|
class ManifestWrapper(LLM):
|
|
"""HazyResearch's Manifest library."""
|
|
|
|
client: Any #: :meta private:
|
|
llm_kwargs: Optional[Dict] = None
|
|
|
|
model_config = ConfigDict(
|
|
extra="forbid",
|
|
)
|
|
|
|
@pre_init
|
|
def validate_environment(cls, values: Dict) -> Dict:
|
|
"""Validate that python package exists in environment."""
|
|
try:
|
|
from manifest import Manifest
|
|
|
|
if not isinstance(values["client"], Manifest):
|
|
raise ValueError
|
|
except ImportError:
|
|
raise ImportError(
|
|
"Could not import manifest python package. "
|
|
"Please install it with `pip install manifest-ml`."
|
|
)
|
|
return values
|
|
|
|
@property
|
|
def _identifying_params(self) -> Mapping[str, Any]:
|
|
kwargs = self.llm_kwargs or {}
|
|
return {
|
|
**self.client.client_pool.get_current_client().get_model_params(),
|
|
**kwargs,
|
|
}
|
|
|
|
@property
|
|
def _llm_type(self) -> str:
|
|
"""Return type of llm."""
|
|
return "manifest"
|
|
|
|
def _call(
|
|
self,
|
|
prompt: str,
|
|
stop: Optional[List[str]] = None,
|
|
run_manager: Optional[CallbackManagerForLLMRun] = None,
|
|
**kwargs: Any,
|
|
) -> str:
|
|
"""Call out to LLM through Manifest."""
|
|
if stop is not None and len(stop) != 1:
|
|
raise NotImplementedError(
|
|
f"Manifest currently only supports a single stop token, got {stop}"
|
|
)
|
|
params = self.llm_kwargs or {}
|
|
params = {**params, **kwargs}
|
|
if stop is not None:
|
|
params["stop_token"] = stop
|
|
return self.client.run(prompt, **params)
|