0% found this document useful (0 votes)
58 views47 pages

Artificial Intelligence

Uploaded by

jldtecno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views47 pages

Artificial Intelligence

Uploaded by

jldtecno
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Artificial Intelligence

AI is a field of computer science that enables computers to simulate human intelligence. At


the most basic level, an AI system ingests very large amounts of data. Similarly to how a
human would, it “learns” from this data, and then uses that knowledge to predict future data
and solve complex problems.
Today's Internet provides an unfathomable amount of data as a foundation for an AI-based
system. With this as a resource, LLMs such as ChatGPT can be used to draw from the
available wealth of information to generate helpful conversational responses.

Machine Learning
Machine learning is a subset of AI that enables computer algorithms to learn from data and
then make decisions or predictions about future data without explicit instructions from
programmers.
Without the aid of machine learning, programmers must provide a computer with all the
rules and objectives required to process data. As you can imagine, this can be a very time-
consuming and inefficient process. Integrating AI models can simplify data processing by
automating repetitive tasks to solve many of these problems at lighting speed.
Machine learning also differs from traditional programming because it uses data to produce
predictive models and then utilizes these models to make predictions. This is particularly
useful when developing rule-based programs, provided there is enough data to solve the
problem.
For example, weather forecasting relies on numerical and statistical information from past
weather data to build models that predict future weather. Similarly, machine learning uses
algorithms to craft and train models that make predictions based on data from the world
around us. In time, the computer system improves on its own by identifying which data
points are outliers and disregarding them in future predictions. The model integrates this
knowledge, leading it to make better predictions or decisions moving forward. Best of all,
programmers do not need to tell the computer how to improve; it can teach itself from the
data.
There are three main categories of machine learning models:

 Supervised learning is used to make predictions by training an algorithm on a labeled


dataset, where the correct output is already known for each input, and then using the
trained model to predict the output for new, unseen inputs.

 Unsupervised learning involves the use of an algorithm that analyzes data that isn’t
classified or labeled, and identifies trends, relationships, and other patterns within the data.

 Reinforcement learning differs in that the AI essentially learns through trial and error.
Data scientists often have to experiment to find the right model for a given situation,
keeping an open mind in the process.

The Relationship between Machine


Learning and Artificial Intelligence
As mentioned previously, AI seeks to provide solutions for problems that need human
intelligence, whereas machine learning resolves problems specifically by learning from data
and making predictions. Put another way, AI enables computer algorithms to perform
similarly to a human, while machine learning enables an algorithm to create its own
intelligence.
This is achieved using neural networks: a set of algorithms modeled on the human brain
that can be used to train computers to replicate human reasoning. These neural networks
serve a similar function to the neurons in our brains that transmit and send signals.
Similarly, using neural networks, machine learning software receives data and creates
algorithms that develop and improve performance over time. This method is a subset of
machine learning known as deep learning.
Deep learning models have the ability to manage complex, unstructured, noisy datasets
such as text and human speech. One of the stand-out applications of deep learning models
is in the field of natural language processing (NLP).

Procesamiento del lenguaje natural

Los proveedores de cursos pueden crear encabezados de nivel 2 en el


futuro.
Marcar esta página como favorita
The languages that people use to interact and communicate with each other are referred to
as natural languages—as opposed to the communication protocols and programming
languages that computers inherently "understand". As we know, computers store data as
zeros and ones, known as binary code. They’re able to interpret this data as instructions
on how to display text, sound, images, or video. If you’ve ever looked at binary code, you
probably know that it’s impossible for humans to understand data in this format. Because
humans and machines speak these different “languages”, we have developed ways to
communicate with machines, and vice versa, in a shared language.
To do so, we must combine various AI technologies and machine learning algorithms to
enable computers to understand written text and spoken words in a similar way to human
conversations.
Natural language processing (NLP) combines the rules of human linguistics with machine
learning algorithms—specifically deep learning models—not only to translate text into a
format that a computer can understand, but essentially to understand the meaning behind
the words, including the writer's or speaker’s intent and sentiment. NLP is able to make
sense of ambiguous phrases, recognize an individual’s attitude and emotion, determine
parts of speech, and convert spoken language into text data. Social media feeds, emails,
online forums, and blogs are all unstructured, text-based data. The task of NLP is to
examine this data and interpret meaning from it.
The goal of NLP is to help computers to understand not only definitions of words (as a look-
up type task) but also to interpret their meaning in a seemingly endless possibilities of
context. It may sound like a simple enough task, but in reality, there are many complicating
factors at play in order for a computer to “understand” language the same way a human
would.
For example, those proficient in English are able to distinguish between homonyms in the
English natural language. They would know that the bark of a tree and the bark of the dog
mean different things even though they sound the same. They would also understand that
the idiom "barking up the wrong tree" has nothing to do with trees or dogs. This is just one
example of the complex variety of challenges presented by NLP.
NLP relies on transformer models, which we’ll cover in more detail in the next section. In
short, “a transformer model is a neural network that learns context and thus meaning by
tracking relationships in sequential data like the words in this sentence” (Merrit, 2022).
Essentially, text or spoken words are fed into the algorithm, which extracts the data and
breaks each element into individual words or phrases through a process
called tokenization. Next, the algorithm classifies and labels each element, then uses
statistical training to propose the most likely meaning of the data.
NLP algorithms are becoming increasingly popular in part due to the availability of pre-
trained models, which help to reduce computational costs and enable advanced models.
They are frequently used to distinguish spam from legitimate emails, translate from one
language to another, analyze social media sentiment, and facilitate chatbots and virtual
agents.

The Evolution of NLP


As can be expected, the study of NLP relates closely to the field of linguistics. So much so
that earlier NLP models took a rules-based approach, using syntax and grammatical rules
to arrive at an understanding of a text's structure. Thereafter, the models used logic to
discern the text's meaning. As you might imagine, creating the rules for this approach was
no small task and models like this had limited efficacy outside of their specific use cases.
You'd only have to look at any social feed, group text thread, or online forum that you're
part of to understand why: when humans communicate, they sometimes make their own
rules, inventing vocabulary and initialisms such as LOL, BRB, or IDK. Humans are
sometimes unmindful of the rules, making unintentional grammatical errors and creating
ambiguous sounding sentences like “Sarah met Mai for coffee near where she works.”
Sometimes, humans forgo the rules of spelling and grammar entirely, resorting to
communicating with a simple "c u l8r :)". Humans are able to communicate beyond the
bounds of grammatical, syntactical, and lexical rules and still convey meaning to other
people. In order for NLPs to be counted on to reliably do the same, a new approach to
NLPs needed to be developed: a statistical one.
In the statistical approach, NLP models learn rules, each with an associated probability,
using machine learning and deep learning models. The machine learning and deep
learning models allow the NLP models to label the unstructured text datasets. Thereafter,
each possible meaning of each element in the dataset is assigned a probability.
Arguably a turning point in the development of NLP models is the introduction of attention,
which allows the model to develop an internal representation of how each word is related to
the next. This statistical representation of text is what informs the model’s inferences later
on. As you work through this course, keep in mind that, when we speak about what a
model “understands”, “learns”, or “knows”, it doesn’t mean the same as the human context.
Instead, we are referring to the model’s internal data structure that documents how words
in the dataset relate to each other and the rules of language in order to make a reasonably
good probabilistic guess about what word, phrase, or sentence should come next.

Transformadores
Transformers are powerful AI models, with the ability to process vast amounts of data,
learn from it, and make accurate predictions or generate content. They achieve this using
self-attention to weigh the relative importance of different parts of a prompt or sentence in a
given context to simulate understanding and achieve human-like natural processing.
One significant application of transformers is in generative AI in which algorithmic
techniques are used to interpret instructions given in natural language to produce new data
in various formats, such as text, images, music or code, by combining their data pool in
unique and novel ways.

Large Language Models


A language model is a type of statistical model that is trained on text data. NLP models are
deep learning models, which means that they are compiled, trained, and then used to
predict or make inferences. When we speak about what the model learns in the context of
language models, we are referring to the patterns and relationships between sentences,
words, or characters in a language. During training, the model develops an internal
representation of these patterns and relationships that it uses to predict the likelihood of a
given sequence of words or characters.
The kind of language models that are most common today are large language models
(LLMs). LLMs are AI models that have been trained on a huge corpus (or collection) of text
data with a high number of parameters. Both the size of the training data and number of
parameters it features make these language models large.
Parameters are the variables that models update as they learn in order to improve their
accuracy. Models learn parameters from any previous training data and update them as
they go. This is one of the things that makes LLMs so powerful, since evidence seems to
suggest that having more parameters enables models to solve problems that are more
intricate.
Another powerful feature of LLMs is the transformer architecture that makes up these
models. In the lessons to come, you will be introduced to tokenizers and transformers, and
we will experiment with how models that have been trained on a smaller dataset and set of
parameters perform when compared with larger training sets with more parameters.
The benefits of using a pre-trained model is that all the heavy-lifting with computed
resources has been done for you. As mentioned, when we train a model, we need a lot of
data, which could include the entire Wikipedia and Reddit corpus, the BookCorpus, and a
wide range of other corpora depending on the kind of model you want to build. The data is
trained over many days, weeks, or months using a lot of hardware accelerators, like
NVIDIA's Graphics Processing Units (GPUs) or Google's Tensor Processing Units (TPUs).
Once you have trained the data, you have a model with a very good understanding of the
data you've trained it on.
IMPORTANT

Be aware that there may be bias in the response you receive from using an LLM, because
the training data reflects the biases present in the society from which the data was
collected.

IA abierta y ChatGPT
In this course, we’ll be using ChatGPT, Open AI’s Generative pre-trained transformer
(GPT) family of large language models. GPT’s development, starting with 117 million
parameters and 4.5 GM of training data for GPT-1 to 175 billion parameters and 570 GM of
plaintext data for GPT-3 demonstrates the rapid development of the growing potential of AI
tools.
It’s important to keep in mind that the term Artificial Intelligence is surrounded by
misconceptions, especially in the media. As such, now is a good time to repeat what
generative AI actually is and what it does. The AI we have today, like ChatGPT, is a far cry
from the Artificial General Intelligences popularized by speculative science fiction. AI
characters in books and movies are often shown as conscious entities with complex inner
lives and sometimes inscrutable motivations. These portrayals tend to lean more into
the intelligence aspect of the AI concept.
In the real world, the AI we have access to, like ChatGPT and Stable Diffusion,
are artificial in the sense that they simulate, or give the appearance of, intelligence. Keep
this in mind when we discuss how text generators like ChatGPT function.
ChatGPT’s current purpose is to interact with users in a conversational way relying on user
prompts to generate text that appears natural and human-like (OpenAI, 2022). It draws on
a large database of text to associate prompt terms with particular kinds of responses and
info sets to build an appropriate response to the user’s prompts. For example, the GPT-4
model is complex enough to suggest a “proper income tax deduction after being fed reams
of tax code” and displays “human-like performance” on a large number of other tasks using
reinforcement learning (Chan, 2023).
We will be using ChatGPT in this course because it’s one of the most accessible AI
technologies available to the public right now. While there are other similar tools available,
ChatGPT adds an extra layer of reinforcement learning that makes its responses feel much
more “human”, for lack of a better word, than other technologies. In addition, ChatGPT
integrates well with another tool we’ll be using in this course: LangChain.
LangChain is a framework that helps developers build applications powered by language
models such as OpenAI. Using LangChain and OpenAI together will give you the tools you
need to build an AI-powered application later in the course.

Referencias
Chan, K. 2023. What can ChatGPT maker’s new AI model GPT-4 do? ABC. 25 April.
Available: https://abcnews.go.com/Business/wireStory/chatgpt-makers-new-ai-
model-gpt-4-97881871 [2023, May 3].
Lee, A. 2023. What Are Large Language Models Used For? [Blog, 26 January].
Available: https://blogs.nvidia.com/blog/2023/01/26/what-are-large-language-
models-used-for/ [2023, May 3].
Merritt, R. 2022. What Is a Transformer Model? [Blog, 25 March].
Available: https://blogs.nvidia.com/blog/2022/03/25/ what-is-a-transformer-model/
[2023, May 3].
Ramponi, M. 2022. How ChatGPT actually works [Blog, 23 December].
Available: https://www.assemblyai.com/blog/how-chatgpt-actually-works/ [2023,
May 3].
Rizkallah, J. 2017. The Big (Unstructured) Data Problem. Forbes. 5 June.
Available: https://www.forbes.com/sites/forbestechcouncil/2017/06/05/the-big-
unstructured-data-problem/?sh=5e2c825493a3 [2023, May 3].

Ingeniería de prontitud

What Is a Prompt?
Users interact with LLMs using natural language input. Human users type the instructions
for the model into a textual interface. The term prompt refers to the text that the user
enters into the text field. Put differently, a prompt is the text that the user gives to the AI
algorithm to tell it what to do.
The model takes the instructions from the prompt, completes the task, and then provides a
response to the user. The response from the model is frequently referred to as the output.
In this course, our focus will be on text outputs, but different types of generative AI models
provide different outputs. In the case of image generators like Stable Diffusion and
Midjourney, the output is an image file.
A prompt can be a simple, straightforward question, such as, Who was the first
president of the United States? Or it could be a vague request for the model to
generate a type of text, such as, Write a haiku about the beauty of AI. As
programmers, we can even use a prompt to generate example code, for example
using Write code that sorts an array of integers.
Prompts can also be complex, with key pieces of data and instructions to guide the model
toward an output that will be the most useful to the user. Remember, LLMs generate
responses based on an extremely high volume of data. The exact data that it uses to form
the output will be significantly impacted by the specific words that the user inputs in the
prompt.

What is Prompt Engineering?


Prompt engineering is the newly emerging field of methodically crafting the input for a
generative AI model to give users an output that best meets their needs.
In order to generate the best output from a generative AI model, it helps to understand the
opportunities and limitations of these models. This knowledge will help you phrase your
prompt in the way that best allows the model to meet your needs.
Here are a few key pieces of information to keep in mind as you develop your prompts:

 Even very minor changes in the way a user phrases a prompt can lead to significant
changes in the types of output an AI model generates. That’s why it’s important to take a
methodical approach when developing prompts.
 When approaching prompt engineering, remember that AI models are only trained to
predict the next word or “token.” Essentially, these models are text completers. The more
accurate the input you provide, the more accurate and helpful you can expect the output to
be.
 The AI model’s response is stochastic, which means randomly determined. Because the
model is pulling from large amounts of data you will get a different output even when you
enter the exact same prompt.
 Be on the lookout for AI hallucinations, the phenomenon of an AI model generating
content that “feels” like a legitimate output, but is based on unreliable data. Again, because
the model is pulling from an extremely high volume of data, not all of that data may be
factually correct. A well-engineered prompt can decrease the risk of generating an AI
hallucination.
 Sometimes a high-level of domain expertise may be required in order to develop a well-
engineered prompt. Take, for example, the case of a medical doctor using an AI algorithm
to suggest treatment options based on a patient’s medical history. The person engineering
the prompt would not only need to know the best vocabulary to use to generate the desired
output, they would also need to have an understanding of the treatment options to be able
to evaluate and validate the output.

Components of Prompt Engineering


When developing a prompt, you must include either instructions or a question. In addition,
you will likely want to include some aspect of input data and/or examples in your prompt to
guide the algorithm toward an output that will be the most useful to you.
Users can provide input data to give the model additional information about the type of
output they desire. The possibilities of the type of data that can be provided are endless,
and will depend on the type of output desired.
Users can add anything from simple audience demographic information (age, level of
education, physical location) to .csv files with many data points, and anything in between
that will help to guide the model toward the desired output.
It can also be helpful to specify the tone that you would like the algorithm to use in its
response.

In this case, instead of asking the model to tell us a joke about penguins, we specified the
tone—dad joke.
Examples help to focus the data that the model uses in its output, and are particularly
useful when prompting the model to provide recommendations.
Let’s say you’re looking for something to watch tonight and you know you’re in the mood for
a movie about a killer shark. Instead of just asking the algorithm to suggest a shark movie,
you can improve the chances of the model suggesting a movie that you will enjoy by giving
the algorithm some insight into your preferences. In your prompt you could tell the
algorithm that you like Jaws, The Shallows, and Open Water, but don’t like
the Sharknado movies or The Meg. This information makes it more likely that you’ll get an
output that matches your specific taste in shark movies.
Providing such examples after instructions appears to be more effective than before
them. Changing the order of the examples will also change the output, so you may want to
experiment with phrasing until you find the optimal result.
When you add these optional components to a prompt, you give the algorithm additional
data that personalizes the response, rather than relying on the entire breadth of the training
data.

Techniques to Improve Outputs


Now that you have a better understanding of what prompt engineering is, we’ll explore
some specific techniques that you can use to enhance your prompts.

Zero-shot, One-shot, and Few-shot Prompting


As mentioned, the number of examples and detail of input data that the user provides to the
algorithm can make a significant difference in the output. Prompt engineers use specific
terms to describe the number of data points provided.
Zero-shot prompting refers to the technique of providing the model with no additional
pieces of data to make its prediction. In our shark movie example, that would look
something like this:
Zero-shot prompting is useful when seeking a broad output. Through the use of questions
and answers with the algorithm, you can then guide it toward a more specific output if you
so choose. Zero-shot prompting can also be a great tool when seeking a creative, out-of-
the box output from the model.
One-shot prompting, as you probably guessed, refers to the practice of providing the
algorithm with a single example or data-point. In our shark movie example, it would look
something like this:

One-shot prompting can be useful when you’re seeking to narrow down the output, but still
want to give the algorithm room to come up with potentially unpredictable outputs. It’s also
clearly best utilized in cases where the user may only have a limited amount of data.
It will come as no surprise that few-shot prompting is when a user provides more than
one example or datapoint—usually between two and five. This technique allows users who
have multiple data-points to focus the model and guide it to find more specific outputs. Our
original shark movie prompt is an example of few-shot prompting:

Chain of Thought Prompting


Chain of Thought Prompting (also known as CoT) is a method used to guide the model
to providing factually correct responses by forcing it to respond to a series of steps during
the process of developing an output response.
The technique requires the model to take a “step-by-step” process in the instructions. In
addition, it is generally best practice to ask the model to explain its reasoning in the output,
and to follow a specific format.
Here is comparison of results from standard versus CoT prompting:

[Image credit: Wei et al., 2022]


CoT is an extremely helpful technique when seeking factually accurate information because
it gives the user insight into how the algorithm reached the desired output, enabling the
user to validate the algorithm’s response.
CoT also significantly reduces the risk of hallucination in the model. Remember that
“hallucinating” is the phenomenon of an AI model generating content that “feels” like a
legitimate output, but isn’t based on legitimate data. It’s one of the biggest risks of using
transformer models right now.

Prompting Citations
Forcing the model to provide citations in its response is also an effective way of reducing
the risk of hallucination. In the prompt, the user can either ask the model to use specific
sources, or take a more general approach, such as asking it to “use only credible sources.”
As with CoT, asking the model to provide sources in its response will have two benefits: it
will guide the model toward a more accurate response, and it will also provide you with a
method of verifying the response from the model.
Here is an example of prompting an algorithm to use reliable sources:

Asking the Model to Play a Role


Asking the model to play a specific role is also an effective way to improve your output and
reduce the risk of hallucination. Consider the example below:

The word “helpful” guides the model towards a factual answer, reducing the risk of
hallucination. It also provides some context of the tone that the user would like the model to
use in the output.
Question and Answer
Leading the model toward an output through a series of follow up questions is another
effective way of steering the model toward the desired output. This technique allows the
user to course correct if the model begins to go off track.
Believe it or not, some transformer models respond well to forceful language, including all
capitalization and exclamation points, as in the example below:

Templates
Developing templates is one of the most effective strategies available for prompting
thorough, accurate responses from a transformer model. Templates can use any
combination of the engineering techniques we’ve covered so far, but the benefit is that the
same well-engineered prompt can be re-used with different data points, increasing the
model’s efficiency.
In the next lesson, we'll create an AI-powered API application which makes use of a
template to compose prompts for coding related questions.

Code Generation
Another specific and powerful use for prompt engineering is code generation.
As programmers, we often leverage ideas from various resources to implement our own
coding solutions. We also use various tools to generate templated starting points for the
basic structure of newly created applications. These include:

 Textbooks which provide useful and reusable techniques to solve common coding
problems.
 Blogs which discuss at a high level architectural approaches and programming techniques.
 Question and answer web sites in which solutions are posted in response to issues posted
by the programming community.
 Integrated Development Environment (IDE) provided "wizards" which assist developers by
generating code.
 Web-based tools which provide useful starting point application structures.
In many ways, AI-powered code generation can be considered the "next generation" of the
above. In each case, we have a problem to solve and we are looking for helpful ideas. We
then evaluate and selectively integrate the information from the provided source, whether it
is a colleague, book, web site, IDE or ChatGPT.

Example Code Generation Prompts


As with any type of prompt, the specificity of a code generation response will depend on the
information we provide it.
Using the example from earlier in the lesson, we can make a general request as follows:
In this case, ChatGPT chooses to respond with Python code that makes use of the
selection sort algorithm.
We can also be more specific with our request:
This time, ChatGPT responds using the programming language (Java) and sort algorithm
(selection sort) of our choice.
We can also make requests to generate entire applications. For example, consider the
following two prompts:
In both cases, ChatGPT generates the relevant code and provides instructions for
incorporating any dependencies.

Incorporating Generated Code


As with any external source, we must use great caution when incorporating code generated
by ChatGPT. We should never assume external code is perfect and blindly incorporate it
into our application.
With this in mind, all generated code should be evaluated carefully, with the following
questions in mind:

 Does the code exactly match our requirements? Does it require any modification?
 Is the code well written, with proper error handing and security taken into consideration?
 Does the code make use of the latest versions of any dependent libraries?
 Does the code make use of any deprecated functionality?
As general guidance, LLMs such as ChatGPT should be considered viable sources of
example code. Always remember however that any externally incorporated code
immediately becomes our own, and we are solely responsible it. Proceed using thorough
evaluation accordingly.

Descargo de responsabilidad de la tarjeta de crédito


For the next lesson's activities, students are required to provide a credit card to access
OpenAI's ChatGPT API, a required part of the course.
The credit card will be charged whatever usage amounts students incur by OpenAI to
access the ChatGPT API, and this dollar charge is separate from and in addition to the fee
paid to enroll in the course.
NOTE
If students choose not to use the ChatGPT API, they can still follow along with this course,
but will not be able to complete the activities.

OpenAI API

Requesting an API Key


Before we get to building an AI-powered application, we will need to obtain an OpenAI API
key.
Head over to the OpenAI website and register for a new account. Follow the prompts to
provide your full name, email, password, and date of birth. The organization field can be left
blank. You'll then be asked to provide a mobile phone number to verify your identity. And
finally, OpenAI will send you a verification email.
After verifying your email and logging in to your account, click on your profile at the top right
and click "View API Keys". Before creating a new secret key, click on "Billing" on the panel
to the left. You can click on "Pricing" to see OpenAI's pricing models. For this course, we
will be using "Chat". It is also advisable to set usage limits and apply a monthly spending
limit to avoid unexpected charges. Finally, click on "Set up a paid account" to set up a
payment method when you are ready to proceed.
With a payment method set up, click on "API Keys" on the panel to the left and click
"Create new secret key" to generate an API key for developmental use. Give this key a
name so that it'll be easier to keep track of your keys if you decide to create additional keys
later. You can name this key anything you want!
After clicking "Create secret key" you will be able to view and copy your key. This is the key
we will use in our applications to make API calls to OpenAI. Because you won't be able to
view this key in the future, make sure you copy and store the key now and store it
somewhere safe.
If you run into any issues with OpenAI account creation or API key generation, please refer
to the OpenAI help forums.

Referencias
Kristian. 2022. ChatGPT Prompt Engineering Tips: Zero, One and Few Shot
Prompting [Blog, 14 December]. Available: https://www.allabtai.com/prompt-
engineering-tips-zero-one-and-few-shot-prompting/ [2023, May 3].
Wei, J., Wang, X., Schuurmans, D., Bosma, M., Ichter, B. Xia, F., Chi, E.H., Le, Q.V., et al.
2022. Chain-of-Thought Prompting Elicits Reasoning in Large Language
Models. Proceedings of the 36th Conference on Neural Information Processing Systems
(NeurIPS 2022). 28 November-29 December. New Orleans Convention Center.
Available: https://proceedings.neurips.cc/paper_files/paper/2022/hash/
9d5609613524ecf4f15af0f7b31abca4-Abstract-Conference.html [2023, May 4].
Weng, L. 2023. Prompt Engineering [Blog,15 March].
Available: https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/ [202
3, May 3].

Configuración del proyecto

Project Code
A downloadable version of the following demonstration code can be found
at ai_api_app_demo.zip.

Project Requirements
With the OpenAI API key in hand from the previous lesson, it's time to talk about our
project!
By the end of this course, we will create an AI-powered JS coding tutor API application.
This application will provide a POST route to allow the user to ask any question related to
coding and will respond with an explanation as well as an example code snippet!
Before getting started, you will need to download Node.js. Be sure to download
the LTS version of Node.js as it is required for LangChain to function properly.

ChatGPT Generated Code


Before we create our application, let's use ChatGPT to generate some useful code. We
want to create an Express API which includes a POST route, so we phrase our prompt
accordingly:
Use Node.js and Express.js to create an API application with a POST
route that accepts a question from the user and responds to it.

ChatGPT responds with something resembling the following:

const express = require('express');

const bodyParser = require('body-parser');

const app = express();

const port = 3000;

// Middleware to parse JSON requests

app.use(bodyParser.json());

// POST route to handle user questions

app.post('/ask', (req, res) => {


const userQuestion = req.body.question;

if (!userQuestion) {

return res.status(400).json({ error: 'Please provide a question in the


request body.' });

// You can implement logic to respond to the user's question here

const response = `You asked: ${userQuestion}. This is a simple


response.`;

res.json({ response });

});
// Start the server

app.listen(port, () => {

console.log(`Server is running on http://localhost:${port}`);

});

Carefully examine the generated code. While we can see it isn't exactly what we need, we
notice right away that there are several aspects to it which we can selectively use along the
way as we create our application. We will keep this in mind as we move forward.

Project Initialization
Start by creating a new directory that will serve as the root of our project anywhere you
wish. Change directories into the newly created folder and use the command npm init to
initialize the node package manager.
HINT
Next, we'll install all the dependencies we will need for our API application:

npm i express body-parser dotenv langchain

With the above command, we are installing "express" and "body-parser" to manage our
API, the "dotenv" package to obscure sensitive information such as our OpenAI API key,
and "langchain", an LLM library which we will learn more about in a later section.
Next, create a new file called server.js in the root directory. We can leave this one blank
for now, but this is the file that will hold all of our code!
Then, create a .env file in the root directory. We can also leave this one blank for now.
Finally, create a .gitignore file in the root directory. Then add the following inside the
file:

# dependencies

/node_modules
package-lock.json

.env

# misc

.DS_Store

The text added to the .gitignore file represents folders and files that will not be tracked
by Git when we push our code to a repository.
Our project scaffolding is now complete and should look like the following:

├── node_modules

├── .env

├── .gitignore

├── package.json

├── package-lock.json

└── server.js
If your folder structure looks like the above, it's time to move on and learn about setting up
the "dotenv" package to obscure our API keys!

Environment Variables with dotenv


The "dotenv" package helps us to obscure private information such as API keys from online
repositories like GitHub by loading environment variables from a .env file
into process.env. As such, the first thing we'll need to do is edit our .env file by adding
our OpenAI API key to it:

OPENAI_API_KEY="<PASTE-YOUR-KEY-HERE>"

The above text allows us to store our API key into a variable called "OPENAI_API_KEY"
such that we can use that variable instead of entering our API key directly within our code.
Ensure that when you paste your OpenAI API key, you remove the angled brackets.
Because we will be using our OpenAI API key within our server.js file, we'll need to
require the "dotenv" package there:

require('dotenv').config();

Finally, to test that we can access our API key from the .env file, we simply add the
following log statement:

console.log(process.env.OPENAI_API_KEY);

From the root of your application, run the command node server.js and you should see
your API key printed in your console! Verify that your API key does indeed print, then delete
the console.log() statement and proceed to the next section on "LangChain".

LangChain (Cadena de lenguas)

Introduction to LangChain
To create our AI-powered application we will leverage LangChain. LangChain is a
programming framework that helps developers build applications powered by language
models such as OpenAI.
To get a sense for LangChain take a look at the LangChain conceptual
documentation. Note that this is a link to their conceptual documentation and so it will not
go into technical implementation of the framework—that will come in later!
Now that we've learned a little about LangChain, let's make use of it to continue building
our application.

OpenAI with LangChain


We'll start by requiring the OpenAI dependency from @langchain/openai. Add the
following to the beginning of your server.js:

const { OpenAI } = require("@langchain/openai");

IMPORTANT
OpenAI has its own library, so it's important to point out that this version of OpenAI comes
directly from langchain. There's no need to install any additional packages or libraries.

Next, we'll instantiate a new object called model using the OpenAI class and log its
contents to the console:

const model = new OpenAI({

openAIApiKey: process.env.OPENAI_API_KEY,

temperature: 0,

model: 'gpt-3.5-turbo'

});

console.log(model);
Before we continue, let's take a moment to break down the properties being used when we
instantiate a new OpenAI object:

 The openAIApiKey property represents our OpenAI API key and is used to check if a
project is authorized to use the API and for collecting usage information.

 The temperature property represents variability in the words selected in a response.


Temperature ranges from 0 to 1 with 0 meaning higher precision but less creativity and 1
meaning lower precision but more variation and creativity.

 Finally, the model property represents which language model will be used. For this project,
we will use the gpt-3.5-turbo model because it’s optimized for chat and the best
available option.
Now when we run the script by using node server.js, we should see the following
output:
HINT
Before moving forward, remove the line containing console.log(model); from the
code.
At this point, your server.js should contain the following:

const { OpenAI } = require("@langchain/openai");

require('dotenv').config();

const model = new OpenAI({

openAIApiKey: process.env.OPENAI_API_KEY,

temperature: 0,

model: 'gpt-3.5-turbo'

});

Great job so far! With all the setup out of the way we can continue building the AI-powered
summary generator to accept input and produce corresponding output.

Aplicación generadora de resúmenes impulsada por IA

Calling the Model


Now that we've instantiated a new model object from the OpenAI class and verified that it is
functional, it's time to pass in prompts!
We’ll start by creating an asynchronous function named promptFunc() inside
our server.js file and test it:

const promptFunc = async (input) => {

try {

const res = await model.invoke(input);

return res;

catch (err) {

console.error(err);

throw(err);

};

// Test
console.log(promptFunc("How do you capitalize all characters of a string in
JavaScript?"));

Inside the promptFunc() function, we use a try/catch statement to help us catch any
potential errors that might arise.
Within the try portion of the try/catch we create a new variable, res, that holds the
returned value from the OpenAI .invoke() method, to which we've passed the test
question, "How do you capitalize all characters of a string in JavaScript?"
When we run the script using node server.js, it may take a moment, but the result
should be the answer to the question as well as an example!
Before moving on, remove the test call to the promptFunc() from server.js.
What if a user of our application wanted to ask a different coding question? Instead of
having the user go into the server.js and alter the question themselves, we'll need a
way to capture their input and make a call based on that input. To do this, we'll need
a POST route to help us out!

POST Route User Input


We want to use our API POST route to interact with the user. Let's begin by setting up
the express and body-parser modules in our project by requiring them as
dependencies and invoking them. At the beginning of server.js, we add the following:

const express = require('express');

const bodyParser = require('body-parser');

const app = express();

const port = 3000;


// Middleware to parse JSON requests

app.use(bodyParser.json());

Next, we define a POST route that will handle the user interaction. For ideas for this, we
refer back to the ChatGPT code we generated earlier and derive the following:

// Endpoint to handle request

app.post('/ask', async (req, res) => {

try {

const userQuestion = req.body.question;

if (!userQuestion) {

return res.status(400).json({ error: 'Please provide a question in the


request body.' });

const result = await promptFunc(text);


res.json({ result });

} catch (error) {

console.error('Error:', error.message);

res.status(500).json({ error: 'Internal Server Error' });

});

To the end of the file we also add the following, which tells Express to listen for requests at
the specified port number.

// Start the server

app.listen(port, () => {

console.log(`Server is running on http://localhost:${port}`);

});

Up to this point, your code should look like the following:

const { OpenAI } = require("@langchain/openai");


require('dotenv').config();

const express = require('express');

const bodyParser = require('body-parser');

const app = express();

const port = 3000;

// Middleware to parse JSON requests

app.use(bodyParser.json());

const model = new OpenAI({

openAIApiKey: process.env.OPENAI_API_KEY,

temperature: 0,
model: 'gpt-3.5-turbo'

});

const promptFunc = async (input) => {

try {

const res = await model.invoke(input);

return res;

catch (err) {

console.error(err);

throw(err);

};
// Endpoint to handle request

app.post('/ask', async (req, res) => {

try {

const userQuestion = req.body.question;

if (!userQuestion) {

return res.status(400).json({ error: 'Please provide a question in the


request body.' });

const result = await promptFunc(userQuestion);

console.log(result);

res.json({ result });


} catch (error) {

console.error('Error:', error.message);

res.status(500).json({ error: 'Internal Server Error' });

});

// Start the server

app.listen(port, () => {

console.log(`Server is running on http://localhost:${port}`);

});

Now when we use node server.js to run our application, we are presented with the
message "Server is running on http://localhost:3000". Use Insomnia to verify that
the POST route at http://localhost:3000/ask is working as expected.
Moving forward, we'll learn more about LangChain, including how to use prompt templates
and output parsers to make our AI-powered summary generator application more
expandable and user-friendly!
Feel free to use the LangChain JavaScript documentation to learn more about using
LangChain specifically with JavaScript!

Plantillas de solicitud de LangChain


PromptTemplate Object
The issue with our current application build is that it lacks context. Although we're asking
the user to enter a coding question, they could still ask whatever they want and our
application would provide a reasonable response. The application is essentially a general
question and answer application, so what we need to do is to find a way to establish a
coding tutor context. To do that, we'll make use of the
LangChain promptTemplate object!
The promptTemplate object takes in a combination of user input along with a fixed
template string thereby allowing developers to set hard-coded parameters but at the same
time accepting dynamic user input. Additionally, the promptTemplate object contains out-
of-the-box methods provided by LangChain including the .format() method that we will
use to add variables to our templates!

Implementing PromptTemplate
To start, we will need to require the PromptTemplate module
from @langchain/core/prompts:

const { PromptTemplate } = require("@langchain/core/prompts");

Next, we instantiate a new object using the PromptTemplate class:

// Instantiation of a new object called "prompt" using the "PromptTemplate"


class

const prompt = new PromptTemplate({

template: "You are a programming expert and will answer the user’s coding
questions as thoroughly as possible using JavaScript. If the question is unrelated
to coding, do not answer.\n{question}",

inputVariables: ["question"]

});
 During instantiation of the prompt object, we pass in a couple of properties, including
the template and inputVariables.

 The template will allow us to set the stage of our application by giving the AI context
about how it should behave and respond to user questions. In other words,
the template allows the developer to pass in instructional prompts.

 The inputVariables property allows us to inject user input directly into


our template so that whatever question the user asks will always be within the confines of
the overall context that the developer sets.

 In the template, we have statically told the AI that it is a "programming expert" and
instructed it to "answer the user’s coding questions as thoroughly as possible using
JavaScript". We further refine this by instructing the AI to respond only to coding related
questions.

 Additionally, the template property is where we inject the user input using \n immediately
followed by curly braces surrounding a variable name. The variable name is defined in the
next property, inputVariables. The inputVariables property is an array and so, if
we wanted, we could set that array to multiple variables and use all of them in
the template.
Next, we modify the promptFunc() function use the template to format the user input:

const promptFunc = async (input) => {

try {

// Format the prompt with the user input

const promptInput = await prompt.format({

question: input

});
const res = await model.invoke(promptInput);

return res;

catch (err) {

console.error(err);

throw(err);

};

 We use the .format() method on our instantiated prompt object to pass in user
input to our template. Take note that the key of the object being passed into
the format() method matches the variable name, question, and the value is
the user input captured.
With the new code in place we'll start the script using node server.js, enter a coding
question via Insomnia, and receive an output similar to the image below:
If we ask a non-coding question such as "Where is Boston?", we receive an output with an
error message similar to the image below:

And, with that, the application is now no longer a general question and answer application
as we're able to instruct OpenAI how to respond to user inquiries.
There is just one last step. So far, we've been receiving responses from OpenAI through
the API in string format. But in many cases, we'll need a more structured output to utilize.
For instance, if we wanted the language model to return data so that we could use it on a
front-end application, we'd need the data to return as JSON. LangChain provides this
functionality through an output parser object, but the capabilities of LangChain's output
parser can do much more!

Analizadores de salida de LangChain

StructuredOutputParser Object
One of LangChain's output parsers is the StructuredOutputParser class and will be
the one we use in the application. There are two main components to LangChain output
parsers: formatting instructions and the parsing method.
The formatting instructions play a pivotal role, allowing us to construct the exact JSON that
will be returned as the response. Additionally, we can use prompts to define the values of
the data that is being returned dynamically using OpenAI. For instance, not only can we
pass back the response, we can also ask OpenAI to provide additional information such as
a source, the date the source was last updated, or even the response in another language!
It's worth mentioning that the additional information is not static. You can think of it as
asking follow-up questions based on the response and passed back to the user as a more
completed dataset.
The .parse() method takes in the normal response from OpenAI as an argument and
structures it based on the formatting instructions.
Now that we have a high-level, conceptual understanding of an output parser, let's
implement it in our application!

Implementing StructuredOutputParser
To start, we'll need to require the StructuredOutputParser class:

const { StructuredOutputParser } = require("langchain/output_parsers");

Next, we’ll instantiate a new object from the StructuredOutputParser class with some
additional properties. We can define the exact structure and properties we want returned to
the user with the .fromNamesAndDescriptions() method. We're going to keep things
simple and provide a single object, and the object we return will have two properties.
Although we can make these properties anything we want, in this case we'll pass
in code and explanation. The values for each property are static prompts. This means
that we can direct OpenAI to fill in the data for us:

// With a `StructuredOutputParser` we can define a schema for the output.


const parser = StructuredOutputParser.fromNamesAndDescriptions({
code: "JavaScript code that answers the user's question",
explanation: "detailed explanation of the example code provided"
});

const formatInstructions = parser.getFormatInstructions();

// Instantiation of a new object called "prompt" using the "PromptTemplate"


class
const prompt = new PromptTemplate({
template: "You are a programming expert and will answer the user’s coding
questions as thoroughly as possible using JavaScript. If the question is unrelated
to coding, do not answer.\n{format_instructions}\n{question}",
inputVariables: ["question"],
partialVariables: { format_instructions: formatInstructions }
});

 Just below our new parser object, we create a new variable formatInstructions that
holds the value of the getFormatInstructions() method.

 The contents of the formatInstructions variable is passed to our template for how we
want the final response to be structured.

 We also make a small change to our template. First we add a new property where we
instantiate our prompt object called partialVariables, which is an object that contains
the key format_instructions. The format_instructions key holds
our formatInstructions as its value. Lastly, we add format_instructions as a
variable within the template itself.
Finally, we modify promptFunc() to incorporate the parser:

const promptFunc = async (input) => {


try {
// Format the prompt with the user input
const promptInput = await prompt.format({
question: input
});

// Call the model with the formatted prompt


const res = await model.invoke(promptInput);

// For a non-coding question, the model returns an error message, causing


parse() to throw an exception.
// In this case, simply return the error message instead of the parsed
results.
try {
const parsedResult = await parser.parse(res);
return parsedResult;
} catch (e) {
return res;
}
}
catch (err) {
console.error(err);
throw(err);
}
};

 Note that in the case that the input question is unrelated to


coding, model.invoke() will return a string error message instead of the output
formatted with code and explanation. In this case, the call
to parser.parse() will cause an exception and we simply return the error
message instead of the parsed results.
With the output parser implemented, we'll start the script using node server.js, enter a
coding question, and receive an output similar to the image below:

Now that our demo application is complete, it's your turn!


Read through the challenge.md file and try to create your own AI-powered API
application that leverages LangChain's models, prompts, templates, and output parsers!

Aplicación CLI impulsada por IA

Your Task
Throughout this course, we worked towards building a JavaScript Consultant application
where users could ask any question related to JavaScript concepts. Your task is to create
an AI-powered API application of your choice using the tools and techniques taught in this
course. Here are just a few examples of AI-powered API applications you could create:

 Translator

 Summary Generator

 News App
 Weather Forecast
The list of applications that can be built using AI is endless, and the only limitation is your
imagination! But don't fall for the trap of thinking that an AI application needs to be novel.
Adding AI to an existing application adds a level of interactivity and depth that traditional
applications simply cannot achieve.
Your AI-powered API application must use Node.js and Express to implement a POST
route which responds to user input, hide sensitive information such as API keys using
the dotenv package, and last but not least, use the gpt-3.5-turbo model through
LangChain. Additionally, you must use LangChain templates and output parsers to ensure
that data is returned as a JSON object.
The application will be invoked by using the following command:

node script.js

Use ChatGPT to generate an initial basic structure for your application. Selectively
integrate the generated code to implement your application.
Use Insomnia as necessary to verify that your API works as expected.

User Story

AS A developer

I WANT an AI-powered API application

SO THAT I can leverage modern artificial intelligence in everyday


work or casual tasks

Acceptance Criteria

GIVEN an API application that accepts user input via POST routes

WHEN the application is prompted for information or a question


THEN an AI generated response is produced and sent back in JSON
format

Hints and Getting Started


Here are some guidelines to help you get started:

 If you get stuck, consult the previous lessons in this course or utilize
the LangChain JS Docs.

Solución de aplicación API impulsada por IA

Las tareas calificadas están bloqueadas


Actualice para obtener acceso a funciones bloqueadas como esta y aproveche al
máximo su curso.

Al actualizar, puedes:

 Obtén un certificado verificado de finalización para mostrarlo en tu currículum


 Desbloquee su acceso a todas las actividades del curso, incluidas las tareas
calificadas
 Acceso completo al contenido y los materiales del curso, incluso después de
que finalice el curso
 Apoye nuestra misión en edX

Actualización para Precio original: $99, precio de descuento: $84.15 $84.15 ($99)

Conclusión
Congratulations on completing this course! Throughout the lessons, you've gained the
knowledge and skills needed to develop your own AI-powered API applications using
JavaScript. By integrating AI models, utilizing tools like Express, dotenv, and LangChain,
and incorporating prompt engineering principles, you've learned how to create intelligent
and interactive applications. This accomplishment showcases your dedication and ability to
grasp complex concepts in AI development. With your newfound expertise, you're now
well-equipped to embark on exciting projects and contribute to the ever-evolving field of
artificial intelligence.
If you're ready to keep your momentum going, explore edX courses in similar fields
that might interest you.

You might also like