DEV Community

Cover image for Sentiment Analysis using CPU-Friendly Small Language Models 😡 😃
Will Taner for LLMWare

Posted on

Sentiment Analysis using CPU-Friendly Small Language Models 😡 😃

What are Small Language Models? 🤔

You have probably heard of Large Language Models, but what about Small Language Models (SLMs)? These compact models excel on specialized tasks, are ideal to use with limited resources, and have faster processing times. Let's explore LLMWare's SLIM models!


Harnessing SLIM Models 💡

In many real-world automated workflows, accomplishing a task requires multiple steps. LLMWare's SLIM models are specifically designed to streamline these processes. These models integrate seamlessly into your programmatic environment, acting as decision points within complex, multistep workflows. They achieve this by producing structured outputs like JSON, SQL, or Python dictionaries, ensuring smooth and efficient workflow automation.

slim gif

Additionally, these models are designed to be CPU-friendly, making them accessible to many machines. Let's explore how LLMWare's slim-sentiment-tool, equipped with 1.1 billion parameters, can enhance our workflow in sentiment analysis with two practical examples!


For the Visual Learners... 📺

Here is a video discussing the same topic as this article. A good idea would be to watch the video, and then work through the steps in this article.


Framework 🖼️

LLMWare
For our new readers, LLMWARE is a comprehensive, open-source framework that provides a unified platform for application patterns based on LLMs, including Retrieval Augmented Generation (RAG).

Please run pip3 install llmware in the command line to download the package.


Importing Libraries and Creating Transcripts 📚

from llmware.agents import LLMfx
Enter fullscreen mode Exit fullscreen mode

LLMfx: A class within llmware that provides an interface to models to interact with text, e.g. to first perform named entity recognition (ner) and then answer a question you want to have answered. It orchestrates multi-model, multi-step processes using SLIM classifier models with centralized journaling, structured work management, and information aggregation.

earnings_transcripts = [
    "This is one of the best quarters we can remember for the industrial sector with significant growth across the "
    "board in new order volume, as well as price increases in excess of inflation.  We continue to see very strong "
    "demand, especially in Asia and Europe. Accordingly, we remain bullish on the tier 1 suppliers and would be "
    "accumulating more stock on any dips. ",

    "Not the worst results, but overall we view as negative signals on the direction of the economy, and the likely "
    "short-term trajectory for the telecom sector, and especially larger market leaders, including AT&T, Comcast, and"
    "Deutsche Telekom.",

    "This quarter was a disaster for Tesla, with falling order volume, increased costs and supply, and negative "
    "guidance for future growth forecasts in 2024 and beyond.",

    "On balance, this was an average result, with earnings in line with expectations and no big surprises to either "
    "the positive or the negative."
    ]
Enter fullscreen mode Exit fullscreen mode

Here, we instantiate the transcripts we will be using for sentiment analysis. Feel free to replace it with the data you would like to analyze.


Example 1: Simple Programmatic Use of Sentiment Classification

sentiment gif

Load the Agent 🤖

agent = LLMfx(verbose=True)
agent.load_tool("sentiment")
sentiment = agent.sentiment(text)
Enter fullscreen mode Exit fullscreen mode

This code will create the agent using LLMfx, load the sentiment analysis tool, and utilize the sentiment analysis tool to make an inference on the text's sentiment.

Verbose: This parameter determines if the events written in the journal are also written in stdout. This is defaulted to True.


Print the Output 🖨️

print("sentiment: ", sentiment)
for keys, values in sentiment.items():
    print(f"{keys}-{values}")
Enter fullscreen mode Exit fullscreen mode

This will print the inferred sentiment, the confidence in that sentiment, and the probability of all three of the emotions.


Make a Decision Based on Sentiment ⚖️

sentiment_value = sentiment["llm_response"]["sentiment"]
confidence_level = sentiment["confidence_score"]

if "positive" in sentiment_value:
    print("sentiment is positive .... will take 'positive' analysis path ...", sentiment_value)

if "positive" in sentiment_value and confidence_level > 0.8:
    print("sentiment is positive with high confidence ... ", sentiment_value, confidence_level)

return sentiment
Enter fullscreen mode Exit fullscreen mode

This code extracts and stores the sentiment in the variable sentiment_value. Next, it retrieves the sentiment's confidence score and stores it in the variable confidence_level. The process then goes through two decision points: one if the sentiment is positive, and another if the sentiment is positive with a confidence level greater than 0.8.


Complete Code for First Example ✅

def get_one_sentiment_classification(text):

    agent = LLMfx(verbose=True)
    agent.load_tool("sentiment")
    sentiment = agent.sentiment(text)

    print("sentiment: ", sentiment)
    for keys, values in sentiment.items():
        print(f"{keys}-{values}")

    sentiment_value = sentiment["llm_response"]["sentiment"]
    confidence_level = sentiment["confidence_score"]

    if "positive" in sentiment_value:
        print("sentiment is positive .... will take 'positive' analysis path ...", sentiment_value)

    if "positive" in sentiment_value and confidence_level > 0.8:
        print("sentiment is positive with high confidence ... ", sentiment_value, confidence_level)

    return sentiment
Enter fullscreen mode Exit fullscreen mode

Example 2: Batch Sentiment Analysis with Iteration

simpsons gif

Load the Agent and Work 🤖

agent = LLMfx()
agent.load_tool("sentiment")
agent.load_work(earnings_transcripts)
Enter fullscreen mode Exit fullscreen mode

The first two lines are the same as in the first example, load the agent and the sentiment analysis tool. In this example, we will load the LLMfx load_work method. The load_work method is a flexible input mechanism - pass a string, list, dictionary, or combination, and it will 'package' as iterable units of processing work for the agent.


Iterate Through Transcripts and Print Results 🔁

while True:
    output = agent.sentiment()
    # print("update: test - output - ", output)
    if not agent.increment_work_iteration():
        break
Enter fullscreen mode Exit fullscreen mode

This code iterates through the transcripts and performs sentiment analysis on each one. For each iteration, it prints the sentiment, confidence score, and probability distribution of all three emotions. The loop terminates when the increment_work_iteration method of the agent returns False.


Output Response and Clear Work and State 📋

response_output = agent.response_list

agent.clear_work()
agent.clear_state()

return response_output
Enter fullscreen mode Exit fullscreen mode

The response_output variable holds a dictionary containing all the sentiment analysis responses to the transcripts. After gathering these responses, we clear the agent's work queue and key state variables.


Complete Code for Second Example ✅

def review_batch_earning_transcripts():

    agent = LLMfx()
    agent.load_tool("sentiment")

    agent.load_work(earnings_transcripts)

    while True:
        output = agent.sentiment()
        # print("update: test - output - ", output)
        if not agent.increment_work_iteration():
            break

    response_output = agent.response_list

    agent.clear_work()
    agent.clear_state()

    return response_output
Enter fullscreen mode Exit fullscreen mode

Main Block 🔌

if __name__ == "__main__":

    sentiment = get_one_sentiment_classification(earnings_transcripts[0])

    response_output = review_batch_earning_transcripts()
Enter fullscreen mode Exit fullscreen mode

In our main block, we will run both of these examples. In the first run, we will only analyze the sentiment of the first element of earnings_transcripts.


Fully Integrated Code with Example 1 and 2 🧩


from llmware.agents import LLMfx

earnings_transcripts = [
    "This is one of the best quarters we can remember for the industrial sector with significant growth across the "
    "board in new order volume, as well as price increases in excess of inflation.  We continue to see very strong "
    "demand, especially in Asia and Europe. Accordingly, we remain bullish on the tier 1 suppliers and would be "
    "accumulating more stock on any dips. ",

    "Not the worst results, but overall we view as negative signals on the direction of the economy, and the likely "
    "short-term trajectory for the telecom sector, and especially larger market leaders, including AT&T, Comcast, and"
    "Deutsche Telekom.",

    "This quarter was a disaster for Tesla, with falling order volume, increased costs and supply, and negative "
    "guidance for future growth forecasts in 2024 and beyond.",

    "On balance, this was an average result, with earnings in line with expectations and no big surprises to either "
    "the positive or the negative."
    ]


def get_one_sentiment_classification(text):

    agent = LLMfx(verbose=True)
    agent.load_tool("sentiment")
    sentiment = agent.sentiment(text)

    print("sentiment: ", sentiment)
    for keys, values in sentiment.items():
        print(f"{keys}-{values}")

    sentiment_value = sentiment["llm_response"]["sentiment"]
    confidence_level = sentiment["confidence_score"]

    if "positive" in sentiment_value:
        print("sentiment is positive .... will take 'positive' analysis path ...", sentiment_value)

    if "positive" in sentiment_value and confidence_level > 0.8:
        print("sentiment is positive with high confidence ... ", sentiment_value, confidence_level)

    return sentiment


def review_batch_earning_transcripts():

    agent = LLMfx()
    agent.load_tool("sentiment")

    agent.load_work(earnings_transcripts)

    while True:
        output = agent.sentiment()
        # print("update: test - output - ", output)
        if not agent.increment_work_iteration():
            break

    response_output = agent.response_list

    agent.clear_work()
    agent.clear_state()

    return response_output


if __name__ == "__main__":

    sentiment = get_one_sentiment_classification(earnings_transcripts[0])

    response_output = review_batch_earning_transcripts()
Enter fullscreen mode Exit fullscreen mode

You may also find the fully integrated code on our Github repo here

Additionally, the notebook version (ipynb) is available here


Conclusion 🏁

computer gif

We've shown how LLMWare's SLIM models can be effectively utilized for sentiment analysis. The model's simplicity and powerful capabilities make it an excellent tool for analyzing sentiment and simplifying the multistep process of sentiment analysis.

Thank you for exploring this topic with us. We trust you now have the understanding needed to implement LLMWare's SLIM models for sentiment analysis and leverage its full range of benefits.

Please check out our Github and leave a star! https://github.com/llmware-ai/llmware

Follow us on Discord here: https://discord.gg/MgRaZz2VAB

Top comments (1)

Collapse
 
jjbb profile image
Jason Burkes
Thanks for this detailed guide! How do SLIM models compare in accuracy with larger language models in sentiment analysis tasks? Looking forward to your post on potential limitations of these models.
Enter fullscreen mode Exit fullscreen mode