This project implements a Sentiment Analysis System using the LangChain framework and Ollama models. The system classifies text inputs such as product reviews or social media posts into positive, negative, or neutral sentiments. It leverages a pre-trained Ollama model for text embedding and sentiment classification, integrated into a flexible LangChain pipeline.
llama3.2
model for efficient sentiment analysis.The following Python packages are required:
pip install langchain ollama
git clone https://github.com/yourusername/sentiment-analysis-langchain-ollama.git
cd sentiment-analysis-langchain-ollama
pip install -r requirements.txt
llama3.2
).Input: Provide a list of text inputs (e.g., reviews, comments) that you wish to analyze for sentiment.
Run the analysis: Execute the script main.py
to analyze the sentiment of your input text:
python main.py
Sample Output: The system will output the sentiment for each input text:
Text: "The product was amazing and easy to use."
Sentiment: Positive
Text: "The experience was terrible. I do not recommend this."
Sentiment: Negative
Text: "The product is okay but could be improved."
Sentiment: Neutral
├── main.py # Main script for running sentiment analysis
├── README.md # This README file
├── requirements.txt # Python dependencies
└── sentiment_analysis.py # Contains the LangChain and Ollama implementation
Here’s a simplified code snippet showing how to integrate LangChain with Ollama for sentiment analysis:
from langchain import PromptTemplate, LLMChain
from langchain.llms import Ollama
# Prompt for sentiment analysis
prompt_template = PromptTemplate(
input_variables=["text"],
template="Analyze the sentiment of the following text and classify it as positive, negative, or neutral:
{text}"
)
# Ollama model
llm = Ollama(model="llama3.2")
# LangChain setup
llm_chain = LLMChain(llm=llm, prompt=prompt_template)
# Sample input
texts = ["The product was fantastic!", "I had a bad experience.", "The service was decent."]
# Process and output results
for text in texts:
result = llm_chain.run(text)
print(f"Text: {text}
Sentiment: {result}
")
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to submit issues or pull requests. Contributions are welcome!
For any questions or feedback, reach out at info@tameronline.com.