Adalflow is a library to build LLM task pipeline. With minimal abstractions, it helps with quick prototyping for experimentation. This snippet will use Prompt, Generate and Component classes to quickly build a movie review classifier.
A Work in progress documentation is available in here. Here are the definitions from their documentation.
The source code is available in Lightning Studio.
Steps in building the classifier
Design the prompt template.
prompt_template = """
<SYS>{{classify_task_desc}}
{# choices #}
{% for choice in choices %}
{{choice}}
{% endfor %}
</SYS>
Classify the given text: {{input_str}}
You:
"""
Use Jinja2 to insert class labels into the prompt.
2.Prepare an LLM using Generator. Provide the list of class labels and task definition to the prompt through prompt_args
classification_labels = ['positive','negative','neutral']
prompt_kwargs = {"classify_task_desc":"""You are a movie critic who should classify the user movie review
into one of the given below choices.
"""
,"choices": classification_labels
}
classifier_llm = Generator(
model_client = OllamaClient(host=host)
,model_kwargs = {"model":"qwen2:0.5b","options":{"temperature":0.7,"seed":77}}
,template = prompt_template
,prompt_kwargs = prompt_kwargs
,name ="classifier"
)
3.Finally finish it with the Component container.
class ReviewSentiment(Component):
"""
An LLM to classify the given user text into once of the
given choices.
"""
def __init__(self, choices,generator):
super().__init__()
self.choices = choices
self.generator = generator
def __input_check(self, user_query):
return profanity.censor(user_query)
def call(self, user_query):
user_query = self.__input_check(user_query)
input_str = {"input_str": str(user_query),"choices": self.choices}
result = self.generator(input_str)
return {"sentiment": result.data,"review": user_query}
A simple profanity check is performed on the review before processing it for sentiment.
Now for the complete code.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from adalflow.core.generator import Generator
from adalflow.core.component import Component
from adalflow.components.model_client.ollama_client import OllamaClient
from better_profanity import profanity
import argparse
from adalflow.core import Prompt
about_str = """
A no frills movie review program developed using Adalflow \n
https://adalflow.sylph.ai/ \n
Invoke the program with a movie review \n
python3 simplesentiment.py -r 'The movie is fit only for kids. However adults can appreciate certain moments.' \n
{'choice': 'positive', 'query': 'The movie is fit only for kids. However adults can appreciate certain moments.'}
"""
host = "127.0.0.1:11434"
prompt_template = """
<SYS>{{classify_task_desc}}
{# choices #}
{% for choice in choices %}
{{choice}}
{% endfor %}
</SYS>
Classify the given text: {{input_str}}
You:
"""
classification_labels = ['positive','negative','neutral']
prompt_kwargs = {"classify_task_desc":"""You are a movie critic who should classify the user movie review
into one of the given below choices.
"""
,"choices": classification_labels
}
classifier_llm = Generator(
model_client = OllamaClient(host=host)
,model_kwargs = {"model":"qwen2:0.5b","options":{"temperature":0.7,"seed":77}}
,template = prompt_template
,prompt_kwargs = prompt_kwargs
,name ="classifier"
)
class ReviewSentiment(Component):
"""
An LLM to classify the given user text into once of the
given choices.
"""
def __init__(self, choices,generator):
super().__init__()
self.choices = choices
self.generator = generator
def __input_check(self, user_query):
return profanity.censor(user_query)
def call(self, user_query):
user_query = self.__input_check(user_query)
input_str = {"input_str": str(user_query),"choices": self.choices}
result = self.generator(input_str)
return {"sentiment": result.data,"review": user_query}
def test_template():
movie_classify_prompt = Prompt(
template = prompt_template
,prompt_kwargs = prompt_kwargs)
user_query = """"The movie was not so good. Though our kids enjoyed certain moments,
it was dull over all for adults."""
print(movie_classify_prompt(input_str = user_query))
def main(args):Snippet
review_str = str(args["review"])
review_sentiment = ReviewSentiment(classification_labels, classifier_llm)
output = review_sentiment(review_str)
print(output)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description=about_str)
parser.add_argument('-r','--review', help='Moview review', required=True)
args = vars(parser.parse_args())
print(args)
main(args)