Examples of few shots learning tasks [Building AI Chatbot 2/3]
The second part of a series about building an AI chatbot. Focused on few-shots learning and how to format input strings for GPT, using examples such as sentiment analysis and keyword extraction. The article also discusses text summarization and paraphrasing and suggests how to set maximum new tokens for summary length.
Erik Hric
February 20, 2023
5 min read
As we mentioned in the first part - few-shots learning heavily relies on models ability to infer task based on few provided examples. Which means we need to provide a few tasks and solutions before we can pass a real problem and have GPT solve it for us. In this chapter we'll go through several examples and I'm sure that by the end of it you'll figure out how to force GPT into chatbot-like conversation.
Principles
The only input (besides parameters) is text. All the example tasks need be formated into one long string variable. It needs to be properly divided and contain enough various cases to successfuly infer demanded task. Let's dive into real life applications and try sentiment analysis.
Sentiment analysis
Deciding whether sentence is positive or negative might be handy for instance to analyze comments on social media. You can find many tools that do it already. How would example input string look like?
text: Today is a very beautiful day.
sentiment: positive
###
text: I feel upset.
sentiment: negative
###
text: This movie was not as good as the previous one.
sentiment: negative
###
text: It is not my cup of tea.
sentiment:
Context like this leaves GPT no other choice than to play this game with us and try to fill the missing part. We just need to set max_new_tokens to value just enough big to fit expected answer.
Keyword extraction
Keyword extraction can help us to understand the main topics or themes in a piece of text and can enable us to categorize or search for documents more effectively.
There are several techniques that can be used for keyword extraction, including:
-
Frequency-based methods: These methods analyze the frequency of words or phrases in a piece of text and select the most frequently occurring words or phrases as keywords. Examples of such methods include TF-IDF (term frequency-inverse document frequency) and TextRank.
-
Linguistic-based methods: These methods use linguistic rules to identify important words or phrases in a piece of text. For example, part-of-speech tagging can be used to identify nouns or adjectives that are likely to be important in describing the topic of the text.
-
Machine learning-based methods: These methods use machine learning algorithms to identify keywords based on features such as word frequency, context, and co-occurrence. Examples of such algorithms include Naive Bayes, Support Vector Machines (SVM), and Random Forest.
... or you can simply provide a few examples and let GPT guess its task and rely on quick and simple solution that gets the job done and works most of the time. TLDR; copy the following string, add your task and feed it to the pipeline.
Paragraph:
John works as a software engineer at a tech company. He spends his days coding, debugging, and collaborating with his team. In his free time, he enjoys playing video games and hiking in the mountains.
Keywords: John, software engineer, collaborating, team, free time, games, hiking
Paragraph:
The sun was shining brightly on a beautiful spring day. Birds were singing in the trees, and flowers were blooming in the gardens. It was a perfect day for a picnic in the park.
Keywords: sun, spring day, birds, singing, trees, flowers, gardens, picnic, park
Paragraph:
Global warming is a major environmental issue that is affecting the planet in a number of ways. Rising temperatures, melting ice caps, and more extreme weather patterns are just some of the consequences of this phenomenon. It is important that we take action to reduce our carbon footprint and mitigate the effects of global warming.
Keywords: global warming, major environmental issue,rising temperatures, melting ice caps, extreme weather patterns, carbon footprint
Paragraph:
In the world of fashion, trends come and go, but style is timeless. A well-tailored suit, a little black dress, and a classic trench coat are just some of the wardrobe staples that never go out of fashion. It's important to invest in quality pieces that can be worn for years to come, rather than constantly chasing the latest trend.
Keywords: fashion, trends, style, classic trench coat, quality pieces
Paragraph: <Your text here>
Keywords:
Since output of text summary is not static length setting maximum amount of new tokens will be a bit trickier. You might experience sentences without proper ending. So I'd suggest to add more tokens just in case. In the end its us setting the bar for summary lenght with our examples. When it is done with summarization and there are still some tokens left it will simply continue with our structure by adding new lines and ### delimiter.
Fun starts when you let it generate far more tokens than you need. I that case it will try to predict what'll be your next question/task.
Text summarization and paraphrasing
GPT-J and GPT-3 context size is only 2048 tokens (roughly 1500 words) it might not fit tasks based on larger bodies of text.
Paragraph:
The internet has revolutionized the way we communicate and access information. With just a few clicks, we can connect with people all over the world, access a wealth of knowledge, and conduct business from the comfort of our own homes. However, the internet also presents a number of risks and challenges, such as cyberbullying, identity theft, and the spread of misinformation. It is important that we are aware of these risks and take steps to protect ourselves online.
Summary:
The paragraph discusses the benefits and risks of the internet, including the ability to connect with people and access information, as well as the risks of cyberbullying, identity theft, and misinformation. It suggests that it is important to be aware of these risks and take steps to protect oneself online.
Paragraph:
The COVID-19 pandemic has had a profound impact on the world, with millions of lives lost and countless others impacted by the economic and social fallout. Despite the challenges, the pandemic has also spurred innovation and creativity, with researchers developing vaccines in record time and businesses pivoting to new models of work and service delivery. The pandemic has forced us to reevaluate our priorities and consider new ways of living and working that may continue long after the pandemic is over.
Summary:
The paragraph describes the impact of the COVID-19 pandemic, including the loss of life, economic and social consequences, and the innovation and creativity it has spurred. It suggests that the pandemic has forced people to reevaluate their priorities and consider new ways of living and working that may continue after the pandemic ends.
Paragraph:
Artificial intelligence (AI) is transforming the way we live and work, with applications ranging from self-driving cars to virtual assistants to medical diagnosis. While AI has the potential to revolutionize many industries, it also presents a number of ethical and social challenges, such as job displacement and bias in decision-making. As AI continues to advance, it is important that we carefully consider these challenges and work to develop solutions that maximize the benefits of AI while minimizing the risks.
Summary:
The paragraph discusses the applications and challenges of artificial intelligence (AI), including job displacement and bias in decision-making. It suggests that as AI continues to advance, it is important to carefully consider these challenges and work to develop solutions that maximize the benefits of AI while minimizing the risks.
Paragraph:
Climate change is one of the most pressing issues of our time, with rising temperatures and sea levels, more frequent and intense natural disasters, and the loss of biodiversity. While the challenge is daunting, there are a number of solutions that can help mitigate the effects of climate change, such as renewable energy, conservation, and sustainable agriculture. It is important that we work together as a global community to address this issue and protect the planet for future generations.
Summary:
The paragraph describes the challenges and solutions of climate change, including rising temperatures and sea levels, natural disasters, and loss of biodiversity. It suggests that it is important to work together as a global community to address this issue and protect the planet for future generations.
Paragraph:
The education system is undergoing a significant transformation, with new technologies and pedagogical approaches changing the way we teach and learn. Online learning, personalized learning, and project-based learning are just some of the new approaches being adopted in schools and universities. While these changes are exciting, they also present a number of challenges, such as ensuring equal access to education and preparing students for a rapidly changing job market. It is important that we continue to innovate and experiment with new approaches to education in order to prepare students for the challenges and opportunities of the 21st century.
Summary:
The paragraph discusses the transformation of the education system, including new technologies and pedagogical approaches such as online learning, personalized learning
I bet that by now you know where to put your input ;)
Ending words
Similar to how GPT could predict the task at hand, you should be able to generate input for few-shot learning in many scenarios by following this chapter. If you already know how to prepare inputs to have conversation with GPT, you don't need to wait for the final part of this series.
Related Articles
Text generation with GPT made easy [Building AI Chatbot 1/3]
The use of AI-powered chatbots is increasing in popularity, largely due to advancements in language models like OpenAI's GPT. In this blog series, we'll explore how to create an AI-powered chatbot with minimal coding using pre-trained text generation models from Hugging Face.
Feb 12, 2023
10 min read
Project MetaHuman: What It Takes to Build a Virtual Human
A developer's journey into creating a photorealistic digital twin using Unreal Engine's MetaHuman framework—from smartphone scanning to final render.
Dec 1, 2025
5 min read
How Augmented reality (AR) is changing the way we design digital products
Augmented Reality has emerged as a transformative force in digital product design, fundamentally altering how we approach user experience, interface design, and product development.
Mar 20, 2024
6 min read