PromptSource#

Prompt source is used to browse and discover the prompts that fit your use case.

For e.g. we want to classify emails using Eisenhower matrix - Now, Later, Delegate, Delete. We want to use few-shot prompting template to classify these emails, and want to provide a few examples that help the LLM classify them in these categories.

Let’s see how PromptSource can help us find the right template for our use case.

Setup#

  1. Ensure bodhilib is installed.

[1]:
!pip install -q bodhilib
[2]:
# import PromptSource

from bodhilib import get_prompt_source
[3]:
# Get an instance of prompt source
prompt_source = get_prompt_source('local_dir_prompt_source', publisher="bodhiext")
[4]:
# find all templates with tag 'few-shot'
templates = prompt_source.find(tags=['few-shot'])
print("\n".join([template.template for template in templates]))
role: user
source: input
inputs: examples: List[('text': str, 'sentiment': str)], text: str
text: Classify the text below in positive, negative or neutral sentiment.
{% for example in examples -%}
text: {{ example['text']}}
sentiment: {{ example['sentiment']}}
{% endfor %}
text: {text}
sentiment:

role: user
source: input
inputs: examples: List[('text': str, 'category': str)], text: str, categories: List[str]
text: Classify the text below in one of the categories: {% ",".join(categories) %}.
{% for example in examples -%}
text: {{ example['text']}}
category: {{ example['category']}}
{% endfor %}
text: {text}
category:

role: user
source: input
text: Classify the text below in positive, negative or neutral sentiment.
{% for example in examples -%}
text: {{ example['text']}}
sentiment: {{ example['sentiment']}}
{% endfor %}
text: {text}
sentiment:

role: user
source: input
text: Classify the text below in one of the categories: {% ",".join(categories) %}.
{% for example in examples -%}
text: {{ example['text']}}
category: {{ example['category']}}
{% endfor %}
text: {text}
category:

[5]:
# list all templates in prompt source
prompt_source.templates
[5]:
[PromptTemplate(id=None, template="role: user\nsource: input\ninputs: examples: List[('text': str, 'sentiment': str)], text: str\ntext: Classify the text below in positive, negative or neutral sentiment.\n{% for example in examples -%}\ntext: {{ example['text']}}\nsentiment: {{ example['sentiment']}}\n{% endfor %}\ntext: {text}\nsentiment:\n", role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-jinja2', metadata={'tags': ['few-shot', 'text-classification', 'sentiment']}, vars={}),
 PromptTemplate(id=None, template='role: user\nsource: input\ninputs: examples: List[(\'text\': str, \'category\': str)], text: str, categories: List[str]\ntext: Classify the text below in one of the categories: {% ",".join(categories) %}.\n{% for example in examples -%}\ntext: {{ example[\'text\']}}\ncategory: {{ example[\'category\']}}\n{% endfor %}\ntext: {text}\ncategory:\n', role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-jinja2', metadata={'tags': ['few-shot', 'classification', 'custom-category']}, vars={}),
 PromptTemplate(id=None, template='role: user\nsource: input\ntext: Classify the text below in positive, negative or neutral sentiment.\n{text}\n', role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-fstring', metadata={'tags': ['zero-shot', 'text-classification', 'sentiment']}, vars={}),
 PromptTemplate(id=None, template='role: user\nsource: input\ntext: Classify the text below in positive, negative or neutral sentiment.\n{text}\n', role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-fstring', metadata={'tags': ['zero-shot', 'text-classification', 'sentiment']}, vars={}),
 PromptTemplate(id=None, template="role: user\nsource: input\ntext: Classify the text below in positive, negative or neutral sentiment.\n{% for example in examples -%}\ntext: {{ example['text']}}\nsentiment: {{ example['sentiment']}}\n{% endfor %}\ntext: {text}\nsentiment:\n", role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-jinja2', metadata={'tags': ['few-shot', 'text-classification', 'sentiment']}, vars={}),
 PromptTemplate(id=None, template='role: user\nsource: input\ntext: Classify the text below in one of the categories: {% ",".join(categories) %}.\n{% for example in examples -%}\ntext: {{ example[\'text\']}}\ncategory: {{ example[\'category\']}}\n{% endfor %}\ntext: {text}\ncategory:\n', role=<Role.USER: 'user'>, source=<Source.INPUT: 'input'>, format='bodhilib-jinja2', metadata={'tags': ['few-shot', 'classification', 'custom-category']}, vars={})]

🎉 Yay! we have so many prompt templates to chose from, and are able to find the one that works for us.

Next, let’s check out how to use DataLoader.