diff --git a/examples/configs/rag/custom_rag_output_rails/config.py b/examples/configs/rag/custom_rag_output_rails/config.py index b565ec226..4d86269ac 100644 --- a/examples/configs/rag/custom_rag_output_rails/config.py +++ b/examples/configs/rag/custom_rag_output_rails/config.py @@ -13,8 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -from langchain.llms.base import BaseLLM from langchain.prompts import PromptTemplate +from langchain_core.language_models.llms import BaseLLM from langchain_core.output_parsers import StrOutputParser from nemoguardrails import LLMRails diff --git a/examples/configs/rag/multi_kb/config.py b/examples/configs/rag/multi_kb/config.py index 0a5d739c1..33e8939b2 100644 --- a/examples/configs/rag/multi_kb/config.py +++ b/examples/configs/rag/multi_kb/config.py @@ -23,9 +23,9 @@ from gpt4pandas import GPT4Pandas from langchain.chains import RetrievalQA from langchain.embeddings import HuggingFaceEmbeddings -from langchain.llms import BaseLLM from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import FAISS +from langchain_core.language_models.llms import BaseLLM from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline from nemoguardrails import LLMRails, RailsConfig diff --git a/examples/configs/rag/pinecone/config.py b/examples/configs/rag/pinecone/config.py index 750c9bdf5..2113596e0 100644 --- a/examples/configs/rag/pinecone/config.py +++ b/examples/configs/rag/pinecone/config.py @@ -21,8 +21,8 @@ from langchain.chains import RetrievalQA from langchain.docstore.document import Document from langchain.embeddings.openai import OpenAIEmbeddings -from langchain.llms import BaseLLM from langchain.vectorstores import Pinecone +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import LLMRails from nemoguardrails.actions import action diff --git a/examples/scripts/demo_llama_index_guardrails.py b/examples/scripts/demo_llama_index_guardrails.py index af2436aaa..3cc9d1049 100644 --- a/examples/scripts/demo_llama_index_guardrails.py +++ b/examples/scripts/demo_llama_index_guardrails.py @@ -15,7 +15,7 @@ from typing import Any, Callable, Coroutine -from langchain.llms.base import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import LLMRails, RailsConfig diff --git a/nemoguardrails/actions/llm/generation.py b/nemoguardrails/actions/llm/generation.py index 2e44c4480..119e39c07 100644 --- a/nemoguardrails/actions/llm/generation.py +++ b/nemoguardrails/actions/llm/generation.py @@ -28,7 +28,7 @@ from typing import Callable, List, Optional, cast from jinja2 import Environment, meta -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions.actions import ActionResult, action from nemoguardrails.actions.llm.utils import ( diff --git a/nemoguardrails/actions/v2_x/generation.py b/nemoguardrails/actions/v2_x/generation.py index 68114922d..e62ddd67f 100644 --- a/nemoguardrails/actions/v2_x/generation.py +++ b/nemoguardrails/actions/v2_x/generation.py @@ -21,7 +21,7 @@ from ast import literal_eval from typing import Any, List, Optional, Tuple -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from rich.text import Text from nemoguardrails.actions.actions import action diff --git a/nemoguardrails/library/content_safety/actions.py b/nemoguardrails/library/content_safety/actions.py index 4a0ae528d..110b6563a 100644 --- a/nemoguardrails/library/content_safety/actions.py +++ b/nemoguardrails/library/content_safety/actions.py @@ -16,7 +16,7 @@ import logging from typing import Dict, Optional -from langchain.llms.base import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions.actions import action from nemoguardrails.actions.llm.utils import llm_call diff --git a/nemoguardrails/library/factchecking/align_score/actions.py b/nemoguardrails/library/factchecking/align_score/actions.py index 0942b8016..a977c25c6 100644 --- a/nemoguardrails/library/factchecking/align_score/actions.py +++ b/nemoguardrails/library/factchecking/align_score/actions.py @@ -16,7 +16,7 @@ import logging from typing import Optional -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import RailsConfig from nemoguardrails.actions import action diff --git a/nemoguardrails/library/hallucination/actions.py b/nemoguardrails/library/hallucination/actions.py index 2b6de6483..684ad4103 100644 --- a/nemoguardrails/library/hallucination/actions.py +++ b/nemoguardrails/library/hallucination/actions.py @@ -17,8 +17,8 @@ from typing import Optional from langchain.chains import LLMChain -from langchain.llms.base import BaseLLM from langchain.prompts import PromptTemplate +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import RailsConfig from nemoguardrails.actions import action diff --git a/nemoguardrails/library/llama_guard/actions.py b/nemoguardrails/library/llama_guard/actions.py index 40cd70240..d52fd83f0 100644 --- a/nemoguardrails/library/llama_guard/actions.py +++ b/nemoguardrails/library/llama_guard/actions.py @@ -16,7 +16,7 @@ import logging from typing import List, Optional, Tuple -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions import action from nemoguardrails.actions.llm.utils import llm_call diff --git a/nemoguardrails/library/patronusai/actions.py b/nemoguardrails/library/patronusai/actions.py index 00c322bdc..2e8fb062e 100644 --- a/nemoguardrails/library/patronusai/actions.py +++ b/nemoguardrails/library/patronusai/actions.py @@ -17,7 +17,7 @@ import re from typing import List, Optional, Tuple, Union -from langchain.llms.base import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions import action from nemoguardrails.actions.llm.utils import llm_call diff --git a/nemoguardrails/library/self_check/facts/actions.py b/nemoguardrails/library/self_check/facts/actions.py index f18d98fb3..976c232c9 100644 --- a/nemoguardrails/library/self_check/facts/actions.py +++ b/nemoguardrails/library/self_check/facts/actions.py @@ -16,7 +16,7 @@ import logging from typing import Optional -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import RailsConfig from nemoguardrails.actions import action diff --git a/nemoguardrails/library/self_check/input_check/actions.py b/nemoguardrails/library/self_check/input_check/actions.py index b53822b92..c1ced0004 100644 --- a/nemoguardrails/library/self_check/input_check/actions.py +++ b/nemoguardrails/library/self_check/input_check/actions.py @@ -16,7 +16,7 @@ import logging from typing import Optional -from langchain.llms.base import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import RailsConfig from nemoguardrails.actions.actions import ActionResult, action diff --git a/nemoguardrails/library/self_check/output_check/actions.py b/nemoguardrails/library/self_check/output_check/actions.py index f61b2b854..400b0b87e 100644 --- a/nemoguardrails/library/self_check/output_check/actions.py +++ b/nemoguardrails/library/self_check/output_check/actions.py @@ -16,7 +16,7 @@ import logging from typing import Optional -from langchain.llms.base import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails import RailsConfig from nemoguardrails.actions import action diff --git a/nemoguardrails/llm/helpers.py b/nemoguardrails/llm/helpers.py index 03920b7ea..04a81a175 100644 --- a/nemoguardrails/llm/helpers.py +++ b/nemoguardrails/llm/helpers.py @@ -19,7 +19,7 @@ AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) -from langchain.llms.base import LLM, BaseLLM +from langchain_core.language_models.llms import LLM, BaseLLM def get_llm_instance_wrapper( diff --git a/nemoguardrails/llm/providers/_langchain_nvidia_ai_endpoints_patch.py b/nemoguardrails/llm/providers/_langchain_nvidia_ai_endpoints_patch.py index 07d82b92a..d547a40b6 100644 --- a/nemoguardrails/llm/providers/_langchain_nvidia_ai_endpoints_patch.py +++ b/nemoguardrails/llm/providers/_langchain_nvidia_ai_endpoints_patch.py @@ -21,8 +21,8 @@ from langchain_core.language_models.chat_models import generate_from_stream from langchain_core.messages import BaseMessage from langchain_core.outputs import ChatResult -from langchain_core.pydantic_v1 import Field from langchain_nvidia_ai_endpoints import ChatNVIDIA as ChatNVIDIAOriginal +from pydantic.v1 import Field log = logging.getLogger(__name__) diff --git a/nemoguardrails/llm/providers/nemollm.py b/nemoguardrails/llm/providers/nemollm.py index 605f6b789..0d51c7746 100644 --- a/nemoguardrails/llm/providers/nemollm.py +++ b/nemoguardrails/llm/providers/nemollm.py @@ -24,15 +24,15 @@ AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) -from langchain.llms.base import BaseLLM -from langchain.pydantic_v1 import BaseModel, root_validator from langchain.schema import Generation from langchain.schema.output import GenerationChunk, LLMResult +from langchain_core.language_models.llms import BaseLLM +from pydantic.v1 import root_validator log = logging.getLogger(__name__) -class NeMoLLM(BaseLLM, BaseModel): +class NeMoLLM(BaseLLM): """Wrapper around NeMo LLM large language models. If NGC_API_HOST, NGC_API_KEY and NGC_ORGANIZATION_ID environment variables are set, diff --git a/nemoguardrails/llm/providers/providers.py b/nemoguardrails/llm/providers/providers.py index 1653e28ec..f4addb034 100644 --- a/nemoguardrails/llm/providers/providers.py +++ b/nemoguardrails/llm/providers/providers.py @@ -23,30 +23,37 @@ import asyncio import logging -from importlib.metadata import PackageNotFoundError, version +import warnings +from importlib.metadata import version from typing import Any, Dict, List, Optional, Type -from langchain.base_language import BaseLanguageModel from langchain.callbacks.manager import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) -from langchain.llms.base import LLM from langchain.schema.output import GenerationChunk from langchain_community import llms from langchain_community.llms import HuggingFacePipeline -from packaging import version as pkg_version +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.rails.llm.config import Model from .nemollm import NeMoLLM from .trtllm.llm import TRTLLM +# NOTE: this is temp +# Suppress specific warnings related to protected namespaces in Pydantic models, they must update their code. +warnings.filterwarnings( + "ignore", + message=r'Field "model_.*" in .* has conflict with protected namespace "model_"', + category=UserWarning, + module=r"pydantic\._internal\._fields", +) log = logging.getLogger(__name__) -# Initialize the providers with the default ones, for now only NeMo LLM. +# Initialize the providers with the default ones # We set nvidia_ai_endpoints provider to None because it's only supported if `langchain_nvidia_ai_endpoints` is installed. -_providers: Dict[str, Type[BaseLanguageModel]] = { +_providers: Dict[str, Optional[Type[BaseLLM]]] = { "nemollm": NeMoLLM, "trt_llm": TRTLLM, "nvidia_ai_endpoints": None, @@ -193,7 +200,7 @@ def discover_langchain_providers(): # If the "_acall" method is not defined, we add it. if ( provider_cls - and issubclass(provider_cls, LLM) + and issubclass(provider_cls, BaseLLM) and "_acall" not in provider_cls.__dict__ ): log.debug("Adding async support to %s", provider_cls.__name__) @@ -204,12 +211,12 @@ def discover_langchain_providers(): discover_langchain_providers() -def register_llm_provider(name: str, provider_cls: Type[BaseLanguageModel]): +def register_llm_provider(name: str, provider_cls: Type[BaseLLM]): """Register an additional LLM provider.""" _providers[name] = provider_cls -def get_llm_provider(model_config: Model) -> Type[BaseLanguageModel]: +def get_llm_provider(model_config: Model) -> Type[BaseLLM]: if model_config.engine not in _providers: raise RuntimeError(f"Could not find LLM provider '{model_config.engine}'") diff --git a/nemoguardrails/llm/providers/trtllm/llm.py b/nemoguardrails/llm/providers/trtllm/llm.py index c45f91085..aa57b2df6 100644 --- a/nemoguardrails/llm/providers/trtllm/llm.py +++ b/nemoguardrails/llm/providers/trtllm/llm.py @@ -21,8 +21,8 @@ from typing import Any, Dict, List, Optional from langchain.callbacks.manager import CallbackManagerForLLMRun -from langchain.llms.base import LLM -from langchain.pydantic_v1 import Field, root_validator +from langchain_core.language_models.llms import BaseLLM +from pydantic.v1 import Field, root_validator from nemoguardrails.llm.providers.trtllm.client import TritonClient @@ -31,7 +31,7 @@ RANDOM_SEED = 0 -class TRTLLM(LLM): +class TRTLLM(BaseLLM): """A custom Langchain LLM class that integrates with TRTLLM triton models. Arguments: diff --git a/nemoguardrails/rails/llm/llmrails.py b/nemoguardrails/rails/llm/llmrails.py index 60fd88600..d3cd54a37 100644 --- a/nemoguardrails/rails/llm/llmrails.py +++ b/nemoguardrails/rails/llm/llmrails.py @@ -25,8 +25,8 @@ import warnings from typing import Any, AsyncIterator, Dict, List, Optional, Tuple, Type, Union, cast -from langchain.llms.base import BaseLLM from langchain_core.language_models import BaseLanguageModel +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions.llm.generation import LLMGenerationActions from nemoguardrails.actions.llm.utils import get_colang_history diff --git a/pyproject.toml b/pyproject.toml index 1a8b0b083..cb6b01b35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -42,9 +42,9 @@ dependencies = [ "httpx>=0.24.1", "jinja2>=3.1.4", # The 0.1.9 has a bug related to SparkLLM which breaks everything. - "langchain>=0.2.14,<0.3.0,!=0.1.9", - "langchain-core>=0.2.14,<0.3.0,!=0.1.26", - "langchain-community>=0.0.16,<0.3.0", + "langchain>=0.2.14,<0.4.0,!=0.1.9", + "langchain-core>=0.2.14,<0.4.0,!=0.1.26", + "langchain-community>=0.0.16,<0.4.0", "lark~=1.1.7", "nest-asyncio>=1.5.6", "prompt-toolkit>=3.0", diff --git a/tests/test_configs/with_custom_llm_prompt_action_v2_x/actions.py b/tests/test_configs/with_custom_llm_prompt_action_v2_x/actions.py index cda714c49..1ed061a4b 100644 --- a/tests/test_configs/with_custom_llm_prompt_action_v2_x/actions.py +++ b/tests/test_configs/with_custom_llm_prompt_action_v2_x/actions.py @@ -16,7 +16,7 @@ from ast import literal_eval from typing import Optional -from langchain.llms import BaseLLM +from langchain_core.language_models.llms import BaseLLM from nemoguardrails.actions import action from nemoguardrails.actions.llm.utils import llm_call diff --git a/tests/utils.py b/tests/utils.py index 2514c95b1..6bc884aca 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -23,7 +23,7 @@ AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) -from langchain.llms.base import LLM +from langchain_core.language_models.llms import LLM from nemoguardrails import LLMRails, RailsConfig from nemoguardrails.colang import parse_colang_file