community[minor]: migrate bigdl-llm to ipex-llm (#19518)

- **Description**: `bigdl-llm` library has been renamed to
[`ipex-llm`](https://github.com/intel-analytics/ipex-llm). This PR
migrates the `bigdl-llm` integration to `ipex-llm` .
- **Issue**: N/A. The original PR of `bigdl-llm` is
https://github.com/langchain-ai/langchain/pull/17953
- **Dependencies**: `ipex-llm` library
- **Contribution maintainer**: @shane-huang

Updated doc:   docs/docs/integrations/llms/ipex_llm.ipynb
Updated test:
libs/community/tests/integration_tests/llms/test_ipex_llm.py
This commit is contained in:
Shengsheng Huang
2024-03-28 11:12:59 +08:00
committed by GitHub
parent a31f692f4e
commit ac1dd8ad94
8 changed files with 232 additions and 30 deletions

View File

@@ -114,6 +114,12 @@ def _import_bedrock() -> Type[BaseLLM]:
return Bedrock
def _import_bigdlllm() -> Type[BaseLLM]:
from langchain_community.llms.bigdl_llm import BigdlLLM
return BigdlLLM
def _import_bittensor() -> Type[BaseLLM]:
from langchain_community.llms.bittensor import NIBittensorLLM
@@ -278,6 +284,12 @@ def _import_human() -> Type[BaseLLM]:
return HumanInputLLM
def _import_ipex_llm() -> Type[BaseLLM]:
from langchain_community.llms.ipex_llm import IpexLLM
return IpexLLM
def _import_javelin_ai_gateway() -> Type[BaseLLM]:
from langchain_community.llms.javelin_ai_gateway import JavelinAIGateway
@@ -645,6 +657,8 @@ def __getattr__(name: str) -> Any:
return _import_beam()
elif name == "Bedrock":
return _import_bedrock()
elif name == "BigdlLLM":
return _import_bigdlllm()
elif name == "NIBittensorLLM":
return _import_bittensor()
elif name == "CerebriumAI":
@@ -695,6 +709,8 @@ def __getattr__(name: str) -> Any:
return _import_huggingface_text_gen_inference()
elif name == "HumanInputLLM":
return _import_human()
elif name == "IpexLLM":
return _import_ipex_llm()
elif name == "JavelinAIGateway":
return _import_javelin_ai_gateway()
elif name == "KoboldApiLLM":
@@ -851,6 +867,7 @@ __all__ = [
"HuggingFacePipeline",
"HuggingFaceTextGenInference",
"HumanInputLLM",
"IpexLLM",
"JavelinAIGateway",
"KoboldApiLLM",
"Konko",

View File

@@ -0,0 +1,145 @@
import logging
from typing import Any, Optional
from langchain_core.language_models.llms import LLM
from langchain_community.llms.ipex_llm import IpexLLM
logger = logging.getLogger(__name__)
class BigdlLLM(IpexLLM):
"""Wrapper around the BigdlLLM model
Example:
.. code-block:: python
from langchain_community.llms import BigdlLLM
llm = BigdlLLM.from_model_id(model_id="THUDM/chatglm-6b")
"""
@classmethod
def from_model_id(
cls,
model_id: str,
model_kwargs: Optional[dict] = None,
**kwargs: Any,
) -> LLM:
"""
Construct object from model_id
Args:
model_id: Path for the huggingface repo id to be downloaded or
the huggingface checkpoint folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of BigdlLLM.
"""
logger.warning("BigdlLLM was deprecated. Please use IpexLLM instead.")
try:
from bigdl.llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
from transformers import AutoTokenizer, LlamaTokenizer
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`."
)
_model_kwargs = model_kwargs or {}
try:
tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
except Exception:
tokenizer = LlamaTokenizer.from_pretrained(model_id, **_model_kwargs)
try:
model = AutoModelForCausalLM.from_pretrained(
model_id, load_in_4bit=True, **_model_kwargs
)
except Exception:
model = AutoModel.from_pretrained(
model_id, load_in_4bit=True, **_model_kwargs
)
if "trust_remote_code" in _model_kwargs:
_model_kwargs = {
k: v for k, v in _model_kwargs.items() if k != "trust_remote_code"
}
return cls(
model_id=model_id,
model=model,
tokenizer=tokenizer,
model_kwargs=_model_kwargs,
**kwargs,
)
@classmethod
def from_model_id_low_bit(
cls,
model_id: str,
model_kwargs: Optional[dict] = None,
**kwargs: Any,
) -> LLM:
"""
Construct low_bit object from model_id
Args:
model_id: Path for the bigdl-llm transformers low-bit model folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of BigdlLLM.
"""
logger.warning("BigdlLLM was deprecated. Please use IpexLLM instead.")
try:
from bigdl.llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
from transformers import AutoTokenizer, LlamaTokenizer
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`."
)
_model_kwargs = model_kwargs or {}
try:
tokenizer = AutoTokenizer.from_pretrained(model_id, **_model_kwargs)
except Exception:
tokenizer = LlamaTokenizer.from_pretrained(model_id, **_model_kwargs)
try:
model = AutoModelForCausalLM.load_low_bit(model_id, **_model_kwargs)
except Exception:
model = AutoModel.load_low_bit(model_id, **_model_kwargs)
if "trust_remote_code" in _model_kwargs:
_model_kwargs = {
k: v for k, v in _model_kwargs.items() if k != "trust_remote_code"
}
return cls(
model_id=model_id,
model=model,
tokenizer=tokenizer,
model_kwargs=_model_kwargs,
**kwargs,
)
@property
def _llm_type(self) -> str:
return "bigdl-llm"

View File

@@ -7,17 +7,18 @@ from langchain_core.pydantic_v1 import Extra
DEFAULT_MODEL_ID = "gpt2"
logger = logging.getLogger(__name__)
class BigdlLLM(LLM):
"""Wrapper around the BigDL-LLM Transformer-INT4 model
class IpexLLM(LLM):
"""Wrapper around the IpexLLM model
Example:
.. code-block:: python
from langchain.llms import TransformersLLM
llm = TransformersLLM.from_model_id(model_id="THUDM/chatglm-6b")
from langchain_community.llms import IpexLLM
llm = IpexLLM.from_model_id(model_id="THUDM/chatglm-6b")
"""
model_id: str = DEFAULT_MODEL_ID
@@ -25,7 +26,7 @@ class BigdlLLM(LLM):
model_kwargs: Optional[dict] = None
"""Keyword arguments passed to the model."""
model: Any #: :meta private:
"""BigDL-LLM Transformers-INT4 model."""
"""IpexLLM model."""
tokenizer: Any #: :meta private:
"""Huggingface tokenizer model."""
streaming: bool = True
@@ -53,10 +54,10 @@ class BigdlLLM(LLM):
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of TransformersLLM.
An object of IpexLLM.
"""
try:
from bigdl.llm.transformers import (
from ipex_llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
@@ -64,8 +65,8 @@ class BigdlLLM(LLM):
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`."
"Could not import ipex-llm or transformers. "
"Please install it with `pip install --pre --upgrade ipex-llm[all]`."
)
_model_kwargs = model_kwargs or {}
@@ -109,15 +110,15 @@ class BigdlLLM(LLM):
Args:
model_id: Path for the bigdl transformers low-bit model checkpoint folder.
model_id: Path for the ipex-llm transformers low-bit model folder.
model_kwargs: Keyword arguments to pass to the model and tokenizer.
kwargs: Extra arguments to pass to the model and tokenizer.
Returns:
An object of TransformersLLM.
An object of IpexLLM.
"""
try:
from bigdl.llm.transformers import (
from ipex_llm.transformers import (
AutoModel,
AutoModelForCausalLM,
)
@@ -125,8 +126,8 @@ class BigdlLLM(LLM):
except ImportError:
raise ValueError(
"Could not import bigdl-llm or transformers. "
"Please install it with `pip install --pre --upgrade bigdl-llm[all]`"
"Could not import ipex-llm or transformers. "
"Please install it with `pip install --pre --upgrade ipex-llm[all]`."
)
_model_kwargs = model_kwargs or {}
@@ -163,7 +164,7 @@ class BigdlLLM(LLM):
@property
def _llm_type(self) -> str:
return "BigDL-llm"
return "ipex-llm"
def _call(
self,

View File

@@ -1,11 +1,11 @@
"""Test BigDL LLM"""
"""Test BigdlLLM"""
from langchain_core.outputs import LLMResult
from langchain_community.llms.bigdl import BigdlLLM
from langchain_community.llms.bigdl_llm import BigdlLLM
def test_call() -> None:
"""Test valid call to baichuan."""
"""Test valid call to bigdl-llm."""
llm = BigdlLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
@@ -15,7 +15,7 @@ def test_call() -> None:
def test_generate() -> None:
"""Test valid call to baichuan."""
"""Test valid call to bigdl-llm."""
llm = BigdlLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},

View File

@@ -0,0 +1,25 @@
"""Test IPEX LLM"""
from langchain_core.outputs import LLMResult
from langchain_community.llms.ipex_llm import IpexLLM
def test_call() -> None:
"""Test valid call to ipex-llm."""
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
)
output = llm("Hello!")
assert isinstance(output, str)
def test_generate() -> None:
"""Test valid call to ipex-llm."""
llm = IpexLLM.from_model_id(
model_id="lmsys/vicuna-7b-v1.5",
model_kwargs={"temperature": 0, "max_length": 16, "trust_remote_code": True},
)
output = llm.generate(["Hello!"])
assert isinstance(output, LLMResult)
assert isinstance(output.generations, list)

View File

@@ -42,6 +42,7 @@ EXPECT_ALL = [
"HuggingFacePipeline",
"HuggingFaceTextGenInference",
"HumanInputLLM",
"IpexLLM",
"KoboldApiLLM",
"Konko",
"LlamaCpp",