Artificial Intelligence
Artificial Intelligence
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:
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.
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.
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.
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.
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.
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:
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:
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.
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.
OpenAI API
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].
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.
app.use(bodyParser.json());
if (!userQuestion) {
});
// Start the server
app.listen(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:
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!
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".
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.
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:
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.
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:
require('dotenv').config();
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.
try {
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!
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:
try {
if (!userQuestion) {
} catch (error) {
console.error('Error:', error.message);
});
To the end of the file we also add the following, which tells Express to listen for requests at
the specified port number.
app.listen(port, () => {
});
app.use(bodyParser.json());
openAIApiKey: process.env.OPENAI_API_KEY,
temperature: 0,
model: 'gpt-3.5-turbo'
});
try {
return res;
catch (err) {
console.error(err);
throw(err);
};
// Endpoint to handle request
try {
if (!userQuestion) {
console.log(result);
console.error('Error:', error.message);
});
app.listen(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!
Implementing PromptTemplate
To start, we will need to require the PromptTemplate module
from @langchain/core/prompts:
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.
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:
try {
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!
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:
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:
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:
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
Acceptance Criteria
GIVEN an API application that accepts user input via POST routes
If you get stuck, consult the previous lessons in this course or utilize
the LangChain JS Docs.
Al actualizar, puedes:
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.