Comments (4)
@NPPprojects! Hi again!
I struggled to make this work - the pipeline was very slow for me until I switched to GPU on Google Colab. I've tried with different versions of LangChain and transformers, but the only thing that I've found to work was adapting the HuggingFacePipeline
class.
This here works for me with the current LangChain version:
from typing import Any, List, Mapping, Optional
from langchain.llms import HuggingFacePipeline
from langchain_core.outputs import Generation, LLMResult
from langchain_core.callbacks import CallbackManagerForLLMRun
from langchain_community.llms.utils import enforce_stop_tokens
VALID_TASKS = ("text2text-generation", "text-generation", "summarization")
class HFP(HuggingFacePipeline):
def _generate(
self,
prompts: List[str],
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any,
) -> LLMResult:
# List to hold all results
text_generations: List[str] = []
for i in range(0, len(prompts), self.batch_size):
batch_prompts = prompts[i : i + self.batch_size]
# Process batch of prompts
responses = self.pipeline(batch_prompts)
# Process each response in the batch
for j, response in enumerate(responses):
if isinstance(response, list):
# if model returns multiple generations, pick the top one
response = response[0]
if self.pipeline.task == "text-generation":
try:
from transformers.pipelines.text_generation import ReturnType
remove_prompt = (
self.pipeline._postprocess_params.get("return_type")
!= ReturnType.NEW_TEXT
)
except Exception as e:
logger.warning(
f"Unable to extract pipeline return_type. "
f"Received error:\n\n{e}"
)
remove_prompt = True
if remove_prompt:
text = response[len(batch_prompts[j]) :]
# ["generated_text"]
else:
text = response
# ["generated_text"]
elif self.pipeline.task == "text2text-generation":
text = response["generated_text"]
elif self.pipeline.task == "summarization":
text = response["summary_text"]
else:
raise ValueError(
f"Got invalid task {self.pipeline.task}, "
f"currently only {VALID_TASKS} are supported"
)
if stop:
# Enforce stop tokens
text = enforce_stop_tokens(text, stop)
# Append the processed text to results
text_generations.append(text)
return LLMResult(
generations=[[Generation(text=text)] for text in text_generations]
)
hfp = HFP(pipeline= generate_text)
llm_chain = LLMChain(prompt=prompt, llm=hfp)
question = "What is electroencephalography?"
print(llm_chain.run(question))
from generative_ai_with_langchain.
Hi @NPPprojects,
Modifying the code produced the following error:
text = response[0]["generated_text"][len(prompt) :]
TypeError: string indices must be integers
That's why I wrote the HFP
class, see above ;)
It should be easy enough to adapt it to any previous LC version. You are right though - depending on the task, you might not need any LC abstraction at all.
from generative_ai_with_langchain.
Hi @NPPprojects. Thanks for reporting this. I remember there were quite a few changes to LLMChain
and local pipelines, and I might have missed testing this code for the pinned version.
I haven't run this yet, but have you tried wrapping the transformers pipeline with a HuggingFacePipeline
?
from langchain.llms import HuggingFacePipeline
hfp = HuggingFacePipeline(pipeline= generate_text)
Later, instead of using generate_text
you'd use hfp, like this:
llm_chain = LLMChain(prompt=prompt, llm=hfp)
Please let me know if this works - I'll try in the evening.
As for the transformers library, it's not directly included in the requirements, but it's required by sentence-transformers, which is included in the requirements.
from generative_ai_with_langchain.
Hi @NPPprojects. Thanks for reporting this. I remember there were quite a few changes to
LLMChain
and local pipelines, and I might have missed testing this code for the pinned version.I haven't run this yet, but have you tried wrapping the transformers pipeline with a
HuggingFacePipeline
?from langchain.llms import HuggingFacePipeline hfp = HuggingFacePipeline(pipeline= generate_text)Later, instead of using
generate_text
you'd use hfp, like this:llm_chain = LLMChain(prompt=prompt, llm=hfp)Please let me know if this works - I'll try in the evening.
As for the transformers library, it's not directly included in the requirements, but it's required by sentence-transformers, which is included in the requirements.
Modifying the code produced the following error:
text = response[0]["generated_text"][len(prompt) :]
TypeError: string indices must be integers
Thrown by huggingface_pipeline.py
Currently just going through the textbook and testing the samples before I update to the latest LangChain version. This is a pretty rudimentary function where I'm not even too certain LangChain is all that useful as something like this works fine, without introducing any abstraction:
generate_text = pipeline(
model="aisquared/dlite-v1-355m",
torch_dtype=torch.bfloat16,
trust_remote_code=True,
device_map="auto",
framework="pt",
token= 1500
)
#No Langchain Abstraction
question = "What is electroencephalography?"
template = f"""Question: {question}
Answer: Let's think step by step."""
print(generate_text(template))
from generative_ai_with_langchain.
Related Issues (20)
- Replicate Example not working in Chapter 3 HOT 5
- can't build docker image HOT 1
- ResolvePackageNotFound of Readline and ncurses HOT 5
- LangChain: 1 validation error for LLMChain - value is not a valid dict (type=type_error.dict) HOT 3
- LLama.cpp model 3B is not available HOT 1
- from langchain.llms import FakeLLM in python throws an error HOT 4
- python_repl not found error HOT 9
- Pydantic format error HOT 3
- langchain[docarray] doesn't install HOT 2
- information_extraction/__init__.py all ways want a validator on BaseModel classes HOT 1
- ModuleNotFoundError: No module named 'chat_with_retrieval' in chat_with_documents.py HOT 7
- Hugging Face Transformers, code issue in Chapter 3 HOT 7
- AttributeError: module 'openai' has no attribute 'OpenAI' HOT 7
- No PythonREPLTool,no python_repl
- Issue with reading Resume HOT 2
- chapter 3 - Using Hugging Face HOT 1
- Conda and docker setups both fail HOT 2
- Summarizing texts : working alternative for ChatOpenAI HOT 2
- chat_with_retrieval not running HOT 1
- I am not able in create conda env using .yaml file. Here is the message - (base) admin_user@llm-vm:~$ conda env create --file langchain_ai.yaml --force Solving environment: failed ResolvePackageNotFound: -- I am using following Linux env - Description: Ubuntu 22.04.4 LTS Release: 22.04 Codename: jammy, anaconda version is 2023.09-0 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from generative_ai_with_langchain.