langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
92.11k stars 14.67k forks source link

Issue: Not sure whether my transformation using the model I created was correct, as I am getting an error. #10549

Closed SanjayDevarajan03 closed 8 months ago

SanjayDevarajan03 commented 12 months ago

Issue you'd like to raise.

The code for my model for sentiment analysis (this works, the problem is in the next part of my code)

from datasets import load_dataset,Dataset from sentence_transformers.losses import CosineSimilarityLoss from setfit import SetFitModel, SetFitTrainer, sample_dataset from transformers import pipeline import pandas as pd import langchain

df = pd.read_csv("C:/Users/sanja/OneDrive/Desktop/Trillo InternShip/train.csv",encoding='ISO-8859-1')

df = pd.read_csv("C:/Users/sanja/OneDrive/Desktop/Trillo InternShip/train-utf-8.csv")

Create a mapping from string labels to integer labels

label_mapping = {"negative": 0, "neutral": 1, "positive": 2} # Customize this mapping as needed

Apply the mapping to the "sentiment" column

df['label'] = df['label'].map(label_mapping)

Specify the columns for text (input) and label (output)

text_column = "selected_text" label_column = "label"

Assuming you have already preprocessed and tokenized your text data

dataset = Dataset.from_pandas(df)

num_samples_per_class = 8

Simulate the few-shot regime by sampling 8 examples per class

train_dataset = sample_dataset(dataset, label_column=label_column, num_samples=num_samples_per_class) eval_dataset = dataset # Assuming you want to evaluate on the same DataFrame

Load a SetFit model from Hub

model = SetFitModel.from_pretrained("sentence-transformers/paraphrase-mpnet-base-v2")

Create trainer

trainer1 = SetFitTrainer( model=model, train_dataset=train_dataset, eval_dataset=eval_dataset, loss_class=CosineSimilarityLoss, metric="accuracy", batch_size=16, num_iterations=20, # The number of text pairs to generate for contrastive learning num_epochs=1, # The number of epochs to use for contrastive learning column_mapping={text_column: "text", label_column: "label"} # Map dataset columns to text/label expected by trainer )

Train and evaluate

trainer1.train() metrics = trainer1.evaluate()

Pushing model to hub

trainer1.push_to_hub("Sanjay1234/Trillo-Project")

But here I get a problem when I do transformation,

from langchain.chains import TransformChain, LLMChain, SimpleSequentialChain from sentence_transformers.losses import CosineSimilarityLoss from setfit import SetFitModel, SetFitTrainer, sample_dataset from transformers import pipeline

def transform_func(text): shortened_text = "\n\n".join(text.split("\n\n")[:3]) return shortened_text

transform_chain = TransformChain( input_variables=["text"], output_variables=["output_text"], transform=transform_func )

I get a problem here

llm_chain = LLMChain( llm={"llm": "Sanjay1234/Trillo-Project"}, # Provide the llm parameter as a dictionary prompt={"prompt": "Summarize this text:"} )

sequential_chain = SimpleSequentialChain(chains=[transform_chain, llm_chain]) text = "This is a long text. I want to transform it to only the first 3 paragraphs." transformed_text = sequential_chain.run(text) print(transformed_text)

I get the following error.-

ValidationError Traceback (most recent call last) Cell In[26], line 1 ----> 1 llm_chain = LLMChain( 2 llm={"llm": "Sanjay1234/Trillo-Project"}, # Provide the llm parameter as a dictionary 3 prompt={"prompt": "Summarize this text:"} 4 )

File ~\AppData\Local\Programs\Python\Python39\lib\site-packages\langchain\load\serializable.py:74, in Serializable.init(self, kwargs) 73 def init(self, kwargs: Any) -> None: ---> 74 super().init(**kwargs) 75 self._lc_kwargs = kwargs

File ~\AppData\Local\Programs\Python\Python39\lib\site-packages\pydantic\main.py:341, in pydantic.main.BaseModel.init()

ValidationError: 2 validation errors for LLMChain prompt Can't instantiate abstract class BasePromptTemplate with abstract methods format, format_prompt (type=type_error) llm Can't instantiate abstract class BaseLanguageModel with abstract methods agenerate_prompt, apredict, apredict_messages, generate_prompt, invoke, predict, predict_messages (type=type_error)

Suggestion:

No response

dosubot[bot] commented 12 months ago

🤖

Hello,

The error you're encountering is due to the way you're initializing the LLMChain. The llm and prompt parameters are expected to be instances of BaseLanguageModel and BasePromptTemplate respectively, not dictionaries.

Here's how you can correctly initialize the LLMChain:

from langchain.prompt_templates import TextPromptTemplate
from langchain.language_models import HuggingFaceLanguageModel

# Define your prompt template
prompt_template = TextPromptTemplate(
    prompt="Summarize this text: {text}",
    input_variables=["text"],
)

# Define your language model
language_model = HuggingFaceLanguageModel(
    model_name="Sanjay1234/Trillo-Project",
)

# Initialize the LLMChain
llm_chain = LLMChain(
    llm=language_model,
    prompt=prompt_template,
)

In this example, TextPromptTemplate is used for the prompt, and HuggingFaceLanguageModel is used for the language model. You can replace these with your own implementations if you have them, as long as they inherit from BasePromptTemplate and BaseLanguageModel respectively.

Please try this and let me know if you encounter any further issues.

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

SanjayDevarajan03 commented 12 months ago

Hi, for this piece of code, I am getting an error from langchain.prompt_templates import TextPromptTemplate from langchain.language_models import HuggingFaceLanguageMode


ModuleNotFoundError Traceback (most recent call last) Cell In[3], line 1 ----> 1 from langchain.prompt_templates import TextPromptTemplate 2 from langchain.language_models import HuggingFaceLanguageMode

ModuleNotFoundError: No module named 'langchain.prompt_templates'

SanjayDevarajan03 commented 12 months ago

Hi, for this piece of code, I am getting an error from langchain.prompt_templates import TextPromptTemplate from langchain.language_models import HuggingFaceLanguageMode

ModuleNotFoundError Traceback (most recent call last) Cell In[3], line 1 ----> 1 from langchain.prompt_templates import TextPromptTemplate 2 from langchain.language_models import HuggingFaceLanguageMode

ModuleNotFoundError: No module named 'langchain.prompt_templates'

dosubot[bot] commented 12 months ago

🤖

Hello,

Thank you for reaching out. Based on the information you've provided and the context from the LangChain repository, it seems like you're trying to import TextPromptTemplate and HuggingFaceLanguageMode from the langchain Python framework.

From the repository context, it appears that there was a similar issue in the past where users were unable to import PromptTemplate (issue #160). The solution suggested was to update to the latest version of LangChain at that time (version 0.0.17) or use Prompt instead of PromptTemplate.

However, the current version of LangChain is v0.0.287, and it's possible that the module structure has changed since then. From the context provided, it seems that PromptTemplate has been replaced with BasePromptTemplate, Prompt, and FewShotPromptTemplate in the Prompt Templates module.

As for HuggingFaceLanguageMode, it doesn't appear to be directly available in the LangChain framework based on the provided module structure. It might be part of one of the Language Model Services, but without more information, it's hard to say for sure.

Here's how you might import the Prompt and BasePromptTemplate:

from langchain.prompt_templates import BasePromptTemplate, Prompt

If you're still encountering issues, could you please provide more information about your use case and the version of LangChain you're using? This will help me assist you better.

Best, Dosu

Sources


This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

If you want to continue the conversation, start your reply with @dosu-bot.

dosubot[bot] commented 9 months ago

Hi, @SanjayDevarajan03,

I'm helping the LangChain team manage their backlog and am marking this issue as stale. From what I understand, you raised an issue regarding an error in the transformation code using LangChain, and Dosubot provided detailed guidance on the correct way to initialize the LLMChain and suggested importing the correct modules. You encountered a ModuleNotFoundError when trying to import the required modules and received further guidance from Dosubot on how to resolve the issue.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, kindly let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or the issue will be automatically closed in 7 days.

Thank you!