AI Agents Fundamentals, v2

Create an Agent with OpenAI SDK

Scott Moss
Netflix
AI Agents Fundamentals, v2

Lesson Description

The "Create an Agent with OpenAI SDK" Lesson is part of the full, AI Agents Fundamentals, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Scott walks through creating a simple "Hello World" LLM, covering environment setup, basic imports, and a function to interact with the model. He demonstrates running prompts in the terminal to show how easily an LLM can generate text.

Preview

Transcript from the "Create an Agent with OpenAI SDK" Lesson

[00:00:00]
>> Scott Moss:So what we're going to do is we're just going to get started making a simple Hello World LLM. So if you've never used an LLM before, this is going to be your first time. I promise you it's super basic. So let's get started with that. I'm just going to, I have my notes on this screen here, so I'm just going to look over here. You'll see me looking over here as well. That way I can stay on par with what you see and not do something completely different.

[00:00:24]
So I'm going to go back here. The first thing you want to do, if you haven't already, seeing how this is a Node repo, you want to do an npm install to make sure you have everything installed. Everything's in the package.json for you. We probably won't need to do an npm install at any other point in this course, so you should be good there. Once you have that installed and double checking, make sure you have your OpenAI API key in your .env file.

[00:00:49]
This won't work without that. We want to head over to source, and we want to head over to agent and then run and then inside here, we're just going to go ahead and create a basic hello world call to an LLM. So we're going to import a few things. One of the packages we're going to use is called AI. This is made from Vercel, so not sure who they had to pay to get the AI package from npm, but they got it, so we're going to import that.

[00:01:23]
There's a type here called model messages. I'm using TypeScript. You don't have to use these types. You can kind of just put any everywhere. If you don't know TypeScript, just follow along with what I'm doing. But at this point, it's just a habit for me. And then we're going to import OpenAI from @ai-sdk/openai. This is an adapter module, it allows us to switch out different model providers with the same SDK.

[00:01:55]
It's very useful. So we're going to use the OpenAI provider adapter. We're going to import something called a system prompt. Don't worry, we'll be talking about that later, but it's essentially the base instructions for an LLM like, you are a blank, be helpful, talk to the user, right? Like if you go look at this, you are a helpful AI system. You provide clear, accurate and concise responses, right?

[00:02:18]
It's just a system prompt that gets passed to the LLM to prime it with its own background and instructions. We're going to make a model name here. I'm probably just going to use chat GPT or not chat GPT but GPT-4o Mini. It's a good blend between fast and good. So I'll just stick with that. You can use whatever model you want, whatever you want. It's your account, it's your money. You can just use whatever one you want from OpenAI.

[00:02:47]
I'm going to use this one because it costs like literally nothing and it's really fast. And then from there, we're going to export a function called run agent. It's an async function, and this function is going to take in 3 arguments, a user message like so. That's just a string. It's also going to take in a conversation history which is just going to be an array of these model messages like this.

[00:03:24]
And then lastly, it'll take in something called callbacks. This is not something that you need to concern yourselves with as far as the agent goes and we'll just import that from types. This has to do with the UI. This is, the callback stuff is all UI related, so it has nothing to do with the agent stuff. This is just so that as you saw in the demo, we had it in the terminal, the callbacks is what ties into that.

[00:03:45]
So we'll use that, that way we can actually see it. And then it's pretty simple. The first thing we're going to do in here is we're going to make a call to a function called generate text, and generate text does exactly what it sounds like. You give it a prompt and it generates text with the model that you want it to. So in this case, it takes in an object. We're going to say the model, and the model in this case is going to be, we're going to use the OpenAI adapter.

[00:04:12]
I'm going to pass in my model name, right? It's going to take in a prompt. That prompt is going to be the user message, OK? And then it's going to take in a system prompt and it's going to be a system prompts. It's going to return the object that we can destructure with a text property that's going to be the text. And then we're just going to console log the text. And because we're just going to run this file without having to go through the GUI and the CLI for now, so what we want to do at the top here is import something called dotenv/config that'll automatically load up our .env file, so the OpenAI or this AI SDK can pick up our OpenAI API key and load it up so we don't have to do it.

[00:05:02]
And then lastly at the bottom, let's just go ahead and call run agent. We'll give it a command and say, hello, can you hear me? All right, we'll just do that. Cool. So I'm going to go to my terminal to run this. You could say npx tsx. Then you can point this to the file. So in this case it'd be source, agent, run. You can run it, it's going to load, it's going to take a minute because we're not streaming.

[00:05:34]
And then it says, cool, I can read your message. I don't have a microphone, so I can't hear your audio, but I can process the text and images you send. So it literally thought that I was asking if it could hear me. So, you know, it's smart, right? So, there we go. That's it. That's all you have to do to talk to an LLM. You give it some text, it generates some text. At its core, that's basically it.

[00:05:58]
But how do we turn something like this to something that we saw earlier, that works on a loop, that's conversational, that can do things. Believe it or not, it's not too difficult. The art of building an agent is not hard. The art of making an agent that's good is infinitely hard. We're going to cover both.

Learn Straight from the Experts Who Shape the Modern Web

  • 250+
    In-depth Courses
  • Industry Leading Experts
  • 24
    Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now