Home / Articles / Blog

Understanding Prompt Engineering from Basics to Advance
Prompt EngineeringGPTLLMAIVercel AI

Understanding Prompt Engineering from Basics to Advance

Where to learn, what to learn and How to learn

AIShrey Vijayvargiya

Under the Hood

Well I was reading about Vercel AI SDK because it was trending in market that Vercel AI SDK helps to develop AI-based apps easily and fast in less time.

Vercel AI SDK is the wrapper by Vercel that helps to use GPT models provided by multiple LLM companies such as GPT-4 by OpenAI, Claude by Anthropic, Gemini by Google.

To develop apps in frontend and backend, developers need to create an endpoint service using the above-mentioned GPT models. 

One can create endpoints and use GPT models within the endpoint or one can prefer using the direct GPT models API provided by each of the GPT LLM providers such as OpenAI, and Google Gemini and all provide APIs to develop GPT-based apps.

Vercel did the same under the hood making easy for developers to create Next.js app + AI-powered apps.

Read introduction - https://sdk.vercel.ai/docs/introduction

Basic Introduction

I am not getting into the details because one can easily read and understand how to use AI SDK by Vercel using docs or Youtube video.

Here is the Youtube video by vercel itself

https://www.youtube.com/results?search_query=vercel+ai+sdk

Embedded Content

Now Vercel AI SDK is a wrapper for using GPT models API directly in client/server side

text
import { generateText } from "ai"
import { openai } from "@ai-sdk/openai"

const { text } = await generateText({
 model: openai("gpt-4-turbo"),
 prompt: "What is love?"
})

// OpenAI + Vercel AI SDK

3 Core Vercel SDKs

  • AI SDK Core: A unified API for generating text, structured objects, and tool calls with LLMs.
  • AI SDK UI: A set of framework-agnostic hooks for quickly building chat and generative user interface.
  • AI SDK RSC: A library to stream generative user interfaces with React Server Components (RSC).

While reading about Vercel AI SDK and core SDKs I have found this link for Prompt Engineering in GPT models.

https://github.com/brexhq/prompt-engineering?tab=readme-ov-file

Prompt engineering is important for all AI-based apps as we will take user inputs and pass is as the prompt or so-called input to the AI or GPT model to then give us the response or output.

Another thing to understand with prompt engineering is there are more than 10+ ways to provide prompt to GPT models or simple OpenAI API

Some of the prompts techniques are as follows

  1. Zero shot prompts
  2. Few shot prompts
  3. Chain of prompts
  4. Tree of thoughts
  5. Self consistency 

and more, read more at https://www.promptingguide.ai/techniques

This website https://www.promptingguide.ai/techniques basically give the in-depth knowledge about AI and prompt engineering a more about it later

Coming back to Prompt Engineering by Brexhq, this github repository explains few important concept in details

Such as 

What is LLM?

What is Prompt?

Need of prompt engineering?

Strategies for prompt engineering

But this not where the picture ends, after reading this repository I've found few more good concepts that I would love to share over here.

Tokens

If you thought tokens were 🔥 in 2022, tokens in 2023 are on a whole different plane of existence. The atomic unit of consumption for a language model is not a “word”, but rather a “token”. You can kind of think of tokens as syllables, and on average they work out to about 750 words per 1,000 tokens. They represent many concepts beyond just alphabetical characters – such as punctuation, sentence boundaries, and the end of a document.

Tokenisation

This is the broad and important concept in GPT and AI domain. Without tokenisation AI is nothing or LLM model can do anything in the response.

Tokenisation is quite easy to understand but actually a lot of things are happening under the hood to tokenise the single sentence.

Tokenisation is a way to convert each occurring word in a prompt into a vector or mathematical expression that is easy for a machine to understand and that is called tokenisation.

Basically each GPT model or LLM under the hood convert each prompt into tokens or each input into tokens to understand and store and predict and analyse faster in milliseconds. Understanding and storing english words directly will take more memory and hinder the performance so in easier words we have to convert those english word prompts into a mathematical expression making easier for machine to learn and understand.

Read more about tokens over here by brexhq

Each LLM models either by OpenAI or Google use there respective tokenisation methods or third-party libraries. OpenAI uses BRE(Bytes Reverse Encoding)

Learn more about OpenAI tokenizer method and way https://platform.openai.com/tokenizer

Remember using GPT models in your app will come up with a token limit meaning a user can use a limited amount of tokens within a GPT model conversation. For example, the GPT4 model allows approximately 8K tokens in total including input + output tokens, this number becomes important in putting cost for your AI-powered client or business apps.

Working with OpenAI APIs might include minimal cost but that cost depends on the number of tokens used so be very careful while providing prompts to the GPT models.

Token Models by Each GPTs

As I've told each LLM models use their own respective tokenizer or tokenization methods. 

This article by hugging face provides an in-depth tokenisation concept understanding along with which tokenisation method each LLM used.

Some of them use BPE(Bypes Pair Encoding), and some prefer WordPiece, and SentencePiece.

It depends on which LLM model you are working which tokenizer method that LLM is using under the hood, it will directly impact your costing of the GPT models as well.

The above shared article by Hugging Face explains the concept of tokenisation with examples as well. spaCy and Moses are two popular rule-based tokenizers. Applying them to our example, spaCy and Moses would output something like this:

"Don't you love 🤗 Transformers? We sure do."

Let's say above is the token we want to tokenise, so using SpaCy and Moses following is the output after tokenisation

text
["Don", "'", "t", "you", "love", "🤗", "Transformers", "?", "We", "sure", "do", "."]

This also explains what is going on and How GPT models get trained and learn.

In this way, each model understand and lean fast and easily predict the next word or token to give the accurate answers.

OpenAI uses the following tokenisation library https://github.com/openai/tiktoken. tiktoken is a fast BPE tokeniser for use with OpenAI's models.

Brexhq Github repository includes various other concepts explanations such as 

Prompt Hacking

GPT 3 vs GPT 4

Prefer reading OpenAI Github cookbook for more LLM usage examples

Prompt Engineering Guide

The story wasn't over yet, after reading the brexhq github repository about prompt engineering, the doc shared the another guide to learn prompt engineeing in detail.

https://www.promptingguide.ai/

This makes things easy now to understand everything about prompt engineering.

This website provides an introduction along with concepts that a developer needs to learn to understand everything related to AI and LLM- based apps.

Conclusion

Highly recommend to learn more about Techniques, Models, Tools, Papers

This whole reading gives me so much understanding about GPT models and how they work under the hood.

Check each of the links I've attached to get a better understanding to work easily with LLM models.

I will soon share more stories on building apps and understanding LLM and GPT models for that free to subscribe to our newsletter

That's it, see you in the next one

Shrey

Summary

Thissummary explains the Vercel AI SDK and its functionalities. The SDK offers aunified API, UI hooks, and RSC libraries for building AI apps.It uses various LLMs like GPT-4, Claude, and Gemini. Prompt engineering, crucial for effective AI app development, involves techniques such as zero-shot and few-shot prompting. Tokenization, converting words into machine-readable vectors, is a key process impacting cost and performance. Understanding these conceptsis essential for developing efficient AI applications.Thissummary explains the Vercel AI SDK and its functionalities. The SDK offers aunified API, UI hooks, and RSC libraries for building AI apps.It uses various LLMs like GPT-4, Claude, and Gemini. Prompt engineering, crucial for effective AI app development, involves techniques such as zero-shot and few-shot prompting. Tokenization, converting words into machine-readable vectors, is a key process impacting cost and performance. Understanding these conceptsis essential for developing efficient AI applications.

External Links (31)

Explore topics

More in

Weekly letter

Subscribe to iHateReading

Our once-a-week newsletter on programming, jobs, AI, and building products online.

Our once a week newsletter on Programming, Jobs, AI, and Business