---
title: 'Build an AI Agent From Scratch in Python - Tutorial for Beginners'
source: 'https://youtube.com/watch?v=bTMPwUgLZf0'
video_id: 'bTMPwUgLZf0'
date: 2026-07-28
duration_sec: 2059
---

# Build an AI Agent From Scratch in Python - Tutorial for Beginners

> Source: [Build an AI Agent From Scratch in Python - Tutorial for Beginners](https://youtube.com/watch?v=bTMPwUgLZf0)

## Summary

This tutorial teaches beginners how to build an AI agent in Python using LangChain. It covers setting up an LLM (OpenAI or Anthropic), creating a structured output parser, and giving the agent tools like Wikipedia search, web search, and a custom file-saving function. The final agent acts as a research assistant that can answer queries and save results to a file.

### Key Points

- **Project Overview** [0:00] — Build an AI agent from scratch in Python using LangChain, with access to LLMs like Claude or GPT and tools like Wikipedia and Google Search.
- **Prerequisites** [1:38] — Need Python 3.10+, a code editor (VS Code recommended), and a virtual environment to install dependencies.
- **Installing Dependencies** [2:21] — Create a requirements.txt file with packages like langchain, langchain-community, openai, anthropic, pydantic, python-dotenv, wikipedia, duckduckgo-search.
- **Setting Up Virtual Environment** [2:53] — Create a virtual environment with 'python -m venv venv' and activate it (source venv/bin/activate on Mac/Linux, .\venv\Scripts\activate on Windows).
- **Creating Project Files** [6:52] — Create main.py, tools.py, and .env files. The .env file stores API keys for OpenAI or Anthropic.
- **Setting Up LLM and Prompt** [7:34] — Import necessary modules, load environment variables, and initialize the LLM (ChatOpenAI or ChatAnthropic). Create a prompt template with system message and format instructions.
- **Structured Output with Pydantic** [13:55] — Define a Pydantic model (ResearchResponse) with fields like topic, summary, sources, tools_used. Use PydanticOutputParser to parse LLM output into this model.
- **Creating the Agent** [17:35] — Use create_tool_calling_agent with LLM, prompt, and tools. Then create an AgentExecutor to run the agent with verbose mode.
- **Adding Tools** [23:41] — Add DuckDuckGo search tool, Wikipedia tool (with API wrapper), and a custom save_to_txt tool. Tools are defined in tools.py and imported into main.py.
- **Custom Tool Example** [30:47] — Create a Python function save_to_txt that writes data to a file with a timestamp. Wrap it as a Tool with name and description.
- **Final Demo** [33:35] — The agent can research a topic (e.g., Southeast Asia population) and save the structured output to a text file using the custom tool.

### Conclusion

This tutorial provides a solid foundation for building AI agents in Python. By combining LLMs, structured output, and custom tools, you can create powerful research assistants and other applications.

## Transcript

In this video, you'll learn how to build an AI agent
from scratch in Python in just a few minutes.
I'll walk you through everything step by step.
This will be very beginner friendly,
and you'll learn how to make something
quite interesting in Python
with some popular frameworks like Lang Chain.
I'll show you how to use various
LMS like Claude or GPT,
how you can give the agent access to various tools,
and how you can structure the output of the agent
so you can use it in your code.
So let me show you
a quick demo of the finished project,
and then we'll go through exactly how to build that.
So you can see that I built
what's known as a research assistant.
You can make this anything that you want.
This is just a quick demo for the tutorial.
And it asks me what it wants to research.
So I'm going to paste here.
Tell me about Lang Chain and its applications.
And I'm going to tell it to save it to a file.
So it can use the tool that I created
to save the contents to a text file.
Now this has access to some tools
like Wikipedia and Google Search.
So you can see here
it says that it's actually searching on Wikipedia
with this query gives us some kind of output here.
And then if we scroll through
we can kind of see the entire thought process.
And we can disable that as well if we want.
Anyways, you see that we get some output here.
We have a topic.
We have a summary.
We have the sources that it used
and then the various tools that are used as well.
And if we go here to the left hand side
you can see that actually generate a text file for me
that contains the research output as well as a
timestamp and all of the content that it gave us.
Of course, we can make this much more advanced,
we can give it access to many more tools,
and we can get it to do some really cool things
with that tool access.
But this will really get us started
and show you how you can write these types of agents.
So I hope you're excited.
Let's go ahead and get into the video.
So let's begin with a few prerequisites
in order to follow along with this video.
First of all,
you will need Python installed on your system
and ideally Python version 3.10 or above,
but the version won't be too important.
You'll also need some kind of code editor.
In this case, I'll recommend using something
like Visual Studio Code,
which is what I currently have open on my computer.
You'll then need to open a new folder
so you can go to file.
If you're in VS code, for example, open folder
and you can just make a new folder.
In this case, I made one on my desktop by clicking
new folder, gave it a name and then I opened it.
That folder is called AI Agent Tutorial.
So I just opened it here and you can see
this is where I'll be working from here.
We're then going to need to install some different
Python dependencies and get access to some API keys.
So let's start with the Python dependencies.
What I'm going to do is make a new file.
I'm doing that by pressing this button up here
that says new file.
And I'm going to call this requirements.txt.
I suggest that you do the same here.
But there's other ways
that you can follow along with this.
Now inside of this file
I'm going to paste the following seven lines of code.
You can manually write these out, or you can copy
this code from the link in the description.
There'll be a GitHub link
that has all of the code for this video.
And you can find this file in there.
Now these are the packages that we'll need in our
Python environment in order to work with this agent.
So what we're going to do now is we're going
to create something known as a virtual environment.
A virtual environment is an isolated place
where we can install these
dependencies, and then we can work
in this environment for the project.
If you're not familiar with virtual environments
or you don't want to set one up,
then you can simply run the command pip install dash
r requirements.txt from your terminal,
ideally in VS code in the same directory
where your file exists.
So in this case,
we're inside of the AI agent tutorial.
And then I'm running this command pip install dash r
and then pointing it
to requirements.txt
which is again in this same directory.
Which is why this will work.
Otherwise you can do pip three, install dash r
requirements.txt
and that will install the dependencies globally.
However, I'm
going to suggest you create a virtual environment.
And to do that you're going to type
a different command which is going to be Python
dash m v env and then v env.
This is going to use
venv to make a new virtual environment called venv.
In the current directory, if you're on Mac or Linux,
you can adjust this to Python three.
On windows it should simply be Python.
When you run this command,
you should see that it takes a second
and then you get a new directory here called V env.
Now what we need to do
is activate the virtual environment.
And then we can start using it.
And everything will be the same as it normally would.
So in order to activate the virtual environment,
if you're on Mac or Linux, you're going to type
source dot slash the name of the virtual environment
which is V and v.
And then this is going to be slash
bin slash activate.
Okay let me just type this correctly for you guys.
And then you can hit enter.
This is if you're on Mac or Linux.
If you're on windows the command is different
and it's simply dot slash Venv
slash scripts slash activate okay this is windows.
The other one is Mac or Linux.
You hit enter and then you should see that
you get this venv as a prefix in your terminal.
Now we can install the dependencies
like I said before.
So pip install dash requirements.txt
or pip three install.
And this should install
all of the dependencies into this virtual environment
and we'll be good to start working.
So the dependencies have now been installed
in this virtual environment.
And I just want to make you aware
that if you see that I have like this autocomplete
popping up in my editor, and you're wondering what
that's from, that's actually from GitHub Copilot.
So if you want to use that, you can do that for free
by going to extensions.
And then type in GitHub and you can see copilot.
So pop up right here.
I've just installed this in my editor.
So that's why you're seeing the autocomplete
and speaking of Microsoft's GitHub copilot.
Yeah.
You know that insane
AI tool that replaces 95% of your manual typing?
Well, that's
just one example of how generative AI coding tools
are transforming
how US developers tackle software engineering.
Now, personally,
my workflow has changed massively over the past
two years and to be honest with you,
I'd say it's for the better.
Whether it's generating unit tests, stubbing classes,
or even writing entire features.
I'm sure that you guys have come up
with some pretty creative ways to use these AI tools.
Now that's actually something
that Microsoft is interested in empowering developers
to learn new skills
and showcase the creative and useful ways
that they've used GitHub Copilot to solve everyday
problems.
Now, Microsoft is currently running hashtag
coding with Copilot, where they're highlighting
and celebrating the standout ways that developers
have used GitHub Copilot to make their jobs easier.
Now, you can share your own GitHub
Copilot story for a chance
to be featured on the Microsoft
Developer social channels.
If you have a cool story on how you've used GitHub,
Copilot, then you can share it anywhere, including
platforms like Instagram X, YouTube, or LinkedIn
with the hashtag coding with Copilot
all personally be reviewing
all of the submissions for a shoutout
in a future video, so make sure to tag me.
I'd love to check them out.
Now we all know the AI is reshaping the industry,
and devs are able to leverage this tool
to tackle and solve problems
more easily and creatively than ever before.
Now, a massive shoutout to GitHub Copilot
and Microsoft for sponsoring today's video,
and I look forward to reviewing some of your
submissions in a future video, so stay tuned.
Okay, so now that everything is set
up, it's time to start writing some code.
So what we're going to do
is make a new file in this folder called Main.py.
This will be the file
where we're going to write most of our code.
We're
also going to make another file called tools.py.
We're just going to separate things out
so that we put the tools in this file.
And the main logic here so that things are
a little bit easier to read and more organized.
We're also going to make one more file called dot
and then env.
This is an environment variable file
where we're going to store some credentials
for things like our GPT API key
or our entropic API key.
Because I'm going to show you two ways
to use while two different lines within this agent.
Okay. So we're going to go to Main.py.
And we're going to start
by setting up a very simple agent.
We're going to run the agent.
And then we'll start adding
all of the tooling functionality.
So to begin we're going to start
importing some things that we need.
So we're first going to say from dot env import
local env.
We're then going to say from pedantic import.
If we can spell this correctly the base model
we're then going to say from Lang train.
And this is going to be underscore open AI import.
And this is going to be chat open AI.
And then additionally and this is optional,
I'm going to say from Lang Train and Tropic Imports.
And this is going to be Chat and Tropic
because I'm going to show you how you can
choose between either
open AI or cloud or GPT or Claude for this video.
Beneath that I am going to type this line
which says load env.
What this is going to do is load
the environment variable file that we created here,
which we'll fill in in just a minute.
So we have all of the credentials
that we need to continue with the tutorial.
Next what we want to do is we want to set up in Lem.
So our agent will begin with some type of Lem.
We can just use the yellow Lem normally.
But what we're going to do is give it to an agent
and then give the agent some tools
and some various other functional, like being able
to generate output in a specific format.
So we're going to start by saying Lem is equal to.
And here's where you're going to have a choice.
You can choose to use chat OpenAI, which means you're
going to be using something like an OpenAI API key.
Or you can use something like Chat and Tropic.
So you can see here
it's giving me the autocomplete for chat and Tropic.
Now for both of these, what you need to do
is specify the model that you want to use.
So if you're using something like OpenAI
then you can say model is equal to.
And then again something like GPT five turbo.
Or you could use GPT for mini or for mini.
There's all kinds of different models.
You can just go with something like GPT for,
I believe also
for mini is another option as well.
And as for the API key, don't worry,
I'll show you how you load that in one second.
Now if you're using entropic
then you want to load in probably the Claude model.
So I'm just going to paste
in the one that I'm using here.
So Claude 3-5 sonnet.
And then this is the version that I picked.
But you can pick a different version.
And when this video is out, there may be
a more recent version that you can select here.
Either way you're going to select a model and you're
going to choose what type of LM you're using.
In my case, I'm using Chat Tropic
because I'm currently rate limited by OpenAI.
Okay. So now we've selected the yellow line.
However, we need to provide an API key
in order to be able to use this element.
So regardless of if you're using OpenAI or in Tropic
or really any other provider,
you're going to need to go into your environment
variable file this dot env file,
and you're going to need to write
one of the following variables.
The first is if you're using OpenAI,
you're going to have to say OpenAI
underscore API underscore key is equal to.
And then you're going to have to paste the API key
here.
Lastly, if you're using entropic
then how do you spell entropic.
Let me make sure I do this correctly.
You're going to have to do anthropic
if we can type this correctly.
Underscore
API underscore key is equal to an empty string.
So again if you're using OpenAI I use this.
If you're using anthropic
or Claude you're going to use this.
I'm going to show you
how to get both of the API keys.
So hang tight for one second.
Okay.
So in order to get the API keys, I believe
you do need to have credit card information on file.
Don't worry, this will cost you like $0.01 at most.
If anything, it should probably just be free.
Regardless,
you can go to platform.openai.com/api keys.
I will leave this link in the description
and simply press Generate new key.
Give it a name, create the secret key and copy it.
If you're working with OpenAI and for Entropic
or Claude, you can go to this page right here.
Console dot dot entropic.com/settings/keys.
Again link in the description.
Press create key.
Same thing.
Give it a name and then copy that key.
Obviously do not share this with anyone.
So now what I'm going to do is I'm going to close
that and I'm going to paste in my key.
And then I'm going to close this file
so that I don't leak the API key to you.
So my API key is now loaded in order to test
if this is working
we can invoke the lemon
run it locally from our computer.
So in order to do that
we can say response is equal to LM dot invoke.
And then I believe we can simply just pass a query.
So something like what is the meaning of life? Great
I love that.
That's giving me the autocomplete from the AI
and we can print the response.
It's possible that we need to pass, something else,
but I think this is totally fine for right now.
So now that we have that,
we can simply run our Python code.
Make sure that when you run the Python code, you're
running it from within your virtual environment.
And the easiest way to do that is, again,
just to open up the terminal you created in VS code
and then type Python and then the name of your file,
which in this case
is main.py or Python three main.py.
And then hit enter
and you should see that we get a response.
It might just take one second. So let's see.
And we get some content and kind of some
other metadata information from this lab.
You can see it says that's one of humanity's oldest
most profound questions, blah blah blah, blah, blah.
And we get the response.
Okay, so that's how you use the LM very simply.
Like we're just using L alone.
We haven't added any agent functionality.
But next what I want to do
is obviously add some more content
and make this a little bit more advanced.
So after we have our LM, the next thing I'm going
to set up is something known as a prompt template.
This is something that will kind of act as a template
for any of the queries that we give to the LM,
so that we can give it more information
on what we actually want it to do.
So for our prompt template,
we're going to do the following.
We're going to say from line chain.
And then this is going to be underscore underscore.
And then this is going to be dot prompts.
And we're going to import the chat prompt template.
Okay.
While we're here
we're also going to say from length chain core.
And then this is going to be dot output underscore
parsers.
We're going to import the pedantic output parser.
Now basically what we're going to do
is we're going to define a simple Python class
which will specify the type of content
that we want our LM to generate.
We're then going to give the LM a prompt and we're
going to tell it hey answer the user's question.
And as a part of your response
generate it using this schema or using this model.
So it will give us output in a format that we can
then know and kind of use predictably.
You'll see what I mean in one second, but
just bear with me while I write some of this code.
So I'm going to create a class here.
And this is going to be my response or my research.
Sorry response.
Okay.
And this is going to inherit from my base model.
Now you can make this class anything that you want.
I'm just giving you a simple example here with like
a response that we're expecting from the lab.
So the response that I want is
I want it to generate for me a topic.
And I want that topic to be of type string.
So I'm going to specify that right here.
I then want to have a summary.
And I want that summary to be of type string.
So I specify the type right.
Then I want to have some sources.
And I want these sources to be a list of strings.
So I'm going to type list
and then in square brackets string.
Then I'm going to have some tools used.
And I'm going to type list and string.
Now here is where you can just specify
all of the fields
that you want as output from your LM call.
You can make this as complicated as you want.
You can have nested objects, so long as all of your
classes inherit from the base model from pedantic.
Okay.
So you just need to make sure
you inherit from base model, and then you can specify
all of the fields
that you want to have in your response model.
And we can eventually pass that to the LM.
Now that we have this, what we're going to do
is we're going to create a parser.
So we're going to say
parser is equal to the pedantic output parser.
And we're simply going to parse
the research response.
But we're going to do this as the pedantic object.
Okay.
So we're gonna say pedantic object is equal to this.
There's other ways that you can set up this parser
like using Json for example,
or using other types of kind of
what do you call it, schemas, I guess.
But in this case we're using pedantic,
which is very popular within Python.
So this parser will now allow us to essentially take
the output of the LM and parse it into this model.
And then we can use it
like a normal Python object inside of our code.
Okay. So next we need to set up a prompt.
Now I'm just going to copy this in
because I don't wanna spend a ton of time writing it.
And I'll walk through it line by line.
So we're going to say our prompt is equal to our chat
prompt template
dot from messages okay.
Then inside of here the first thing we're going to
specify is a system message.
The system message is information to the LM.
So it knows what it's supposed to be doing.
So we tell it you are a research assistant
that will help generate a research paper,
answer the user query and use the necessary tools.
And then the important part
is that I tell it to wrap the output in this format
and provide
no other text, and I provide the format instructions.
Okay. That's very important.
Make sure you have this part.
Lastly,
there's a few things that you do need to add here.
So you need to add the chat history.
The query this is coming from the user.
And the placeholder which is the agent scratchpad.
Don't worry
too much about these three fields right here.
The only important one is the query.
This is just necessary for the type of agent
that we're going to create.
And you can find all this information from the link
chain documentation.
Okay. Then we say partial.
And what this means is we're partially going to fill
in this prompt by passing the format instructions.
So what this now does
is it uses our parser that we created here.
And it just takes this pedantic model
and turns it into a string
that we can then give to the prompt.
So we're pretty much just taking this model
that we want to have our output
and converting it to a string,
giving it to the other as part of the system prompt.
So now it knows when it generates a response,
it's got to do it in this format.
So notice that format instructions here
matches up with format instructions here.
They're the same variable. That's important.
You could call this anything that you want
so long as you adjust both of the values here.
Okay.
And then these other values will be
automatically filled in for us
when we start actually running our agent.
Okay. So now we have our prompt.
We have our parser.
We have our LM.
And it's time to create a simple agent.
So we're going to say agent is equal to.
And then we're going to bring in a function
called create tool calling agent.
So from the top of our code
we're going to say from link chain.
And then this is going to be dot agent imports.
And then create underscore.
And notice
there's all types of agents that you can create.
But we we want to create tool calling agent okay.
Now this is one we'll use again
there's a bunch of ones that you can use here.
So I'm going to use this function.
And inside of this function
what we need to pass is the following are alarm
which will simply be equal to the alarm
that we've already defined up here.
And then we can pass it our prompt.
So we can say prompt is equal to prompt.
And then lastly we're going to say tools.
And for right now
we're just going to make this equal to an empty list.
We'll specify some tools in a second.
But for now I just want to test the agent
and make sure that the agent will work.
Okay.
So we have our agent create
two calling agent alarm prompt tools.
And now we want to test the agent.
So in order to test the agent
we need to import one more thing.
So from link change agents we're going to import
what's called the agent Executer.
The agent Executer is just a way
to actually execute the agent.
Right. So that's why we need to bring that in.
And then down here
we're going to say that the agent underscore
executer is equal to an agent Executer.
For the agent Executer,
we're going to say the agent is equal to our agent.
The tools is equal to again,
just an empty list for right now.
And we're going to say verbose
if we specify this is equal to true.
So we can see the thought process of the agent.
If you don't care about the thought process
and you don't want to see that,
then you can just mark this as false
or not include it.
So now we have an agent Executer, and we can use
the agent executer to generate some kind of response.
So we can say our raw underscore response is equal
to the agent Executer dot invoke.
And then when we invoke this we need to pass
in this prompt variable which is query.
Now you can have multiple prompt variables.
And notice
that they're all specified inside of braces.
So we have the format instruction prompt variable
that we filled in here.
The chat history and the agent scratchpad will
automatically be filled in by our agent Executer.
And then we're left with one more prompt variable
which is the query.
And if you wanted to add multiple variables here,
you could just do another one.
And you can say you know, name or something.
And then here when you invoke this
you're going to pass two things.
So you'd pass the query which is you know,
what is the capital France or something.
And the name of Alice.
So you can pass multiple prompt variables.
Again in this case we just need query.
But I just wanted to show you
that you can pass more if you want to.
Okay.
So we're going to invoke the agent Executer.
We're going to get a raw response.
And for now we can just print out the raw response.
And I believe that that should be it
in this code should be working.
So let me zoom out a little bit.
You can see our imports again.
You can find all of this code from the link
in the description.
Generate the Elm.
Make the parser create our prompt
with the prompt template.
Then we have our agent and we create the agent
executer and generate the raw response.
So let's try this out and see if we get something.
Give this a second.
You can see it's going into the agent executor chain.
And it should generate a response for me.
And you can see that we get that.
What is the capital of France.
And then it gives us kind of output text topic.
And it gives us the output in this format.
Now if we just want to see the response
that we're looking for.
So like this kind of model, what we need to do
is use the parser to parse this content.
So what we're going to do is we're going to say
our structured response is equal to parser dot parse.
And then rather than just parsing the raw response
we're going to say raw response dot get
we're going to get the output key because it gives us
kind of like a dictionary with multiple values.
Like if you look here,
I know it's a little bit messed up.
You can see we have output and then we have an array
and then we have another thing inside of here.
So we just need to go inside of that.
So we're going to say output
at index zero and then text.
What this
is going to do is it's going to get the output.
It's going to get the text from like the first value
of output, which is all we're going to have.
And it's going to parse that
with our parser into a Python object.
So then if I go here and I print the structured
response, let's run this again
and I'll show you what we get.
And don't worry,
I'll slow down and go through all of the code again.
Okay.
So we're entering the agent executer chain
and you can see that we get this right.
So query output our text.
And then when we print out the Python object
it gives us all of the fields
that are in our pedantic bottle.
So we have the topic.
If we keep going through here
we should get like a summary a summary is right here
we have the tools used and then we have the.
If we keep going here, the sources. Right.
And it tells us where these sources came from.
Cool. Okay. So that is that.
And that allows us now
to get it in the correct output.
And the interesting thing, right, is that
now that this is in the Python model,
what I can do is something like structured
response dot and then topic.
And I can just access this topic
which is typed as a string.
And I can just use that.
So this is the really kind of neat part
about using these structured output models
is that now I can get specific components
from their response and actually use them predictably
in my code, rather than just having this plain text,
which is usually what the models give you.
So we've already got that part.
We have our structured response,
and just one thing we want to do is we just want to
add a simple try catch here or try accept block
because it's possible the model can mess up.
And then this will give us an error.
So if it doesn't give us the correct response type,
which is possible, we can get an error.
So what we're going to do is just say accept
exception as E and we're going to say print,
and we're just going to go, what do you call it here?
Error parsing response.
Then we're going to print out E.
And then we're going to say the raw response
like this.
And we'll actually just do another one
and we'll say wrong response like that okay.
Can make this look better if you want,
but I'm just doing a simple error message
so we can see kind of what's going wrong.
Okay. So there we go. We have our try accept.
And now we need to do the cool part
which is adding tools.
So of course, you know this is interesting, but
we want to have the ability to call various tools.
Tools are things that the LM can use
or the agent can use
that we can either write ourself or we can bring in
from things like the Lang Chain Community Hub.
So what I'm going to do is go to my tools.py file,
and I'm going to show you how to add three
different tools one for looking up Wikipedia,
one for going to DuckDuckGo and searching something.
These are all free. You don't need any API keys.
And then one custom tool that we write ourself
which can be any Python function.
So we're going to go to the top of our code.
We're going to say from Lang Chain.
And this is going to be underscore community import.
And then the Wikipedia or sorry
this is going to be dot tools.
And then we're going to import the Wikipedia
query run.
Then we're going to say from Lang chain community
dot tools.
And actually this is going to be dot utilities
getting carried away with the copilot autocomplete.
Here we're going to say import the Wikipedia
API wrapper.
We're then going to say from Lang Chain
and actually can do this and want to import up here
we're going to bring in the DuckDuckGo
search run okay.
So a tool that we can use.
And then we are going to have two last imports.
So we're going to say from
Lang chain dot tools.
And we're going to import the tool.
This will allow us to kind of wrap
or create our own custom tool.
And then we're going to say from date time
imports date time okay.
Sorry I know that was me messing around and trying
to remember what these imports actually are.
So you can see we have some stuff
related to Wikipedia, the DuckDuckGo, search run.
Again, all of these are free, but you will get rate
limited if you use them too much.
And then we'll be able to create our own custom tool.
So let's start by simply creating the tool
that can access DuckDuckGo or kind of like search.
Google search is what I'm calling it,
even though it's DuckDuckGo search.
So to do that, I'm simply going to say
search is equal to DuckDuckGo search run.
I'm just going to call that.
I'm then going to say my search underscore tool
is equal to a tool.
For my tool, I need to give this a name.
So I'm just going to say
that this tool is called search okay.
So we can say name is equal to this.
The function is going to be equal to search dot run.
So this provides a function called run.
So that's what we're passing here. And then
what do we want to have here.
We need to have a description for the tool.
And this is just going to be search the web
for information okay.
So this is our tool.
That's literally all you need to do.
We've now created a tool
that we can pass to our agent.
The key thing is that you need to have some name.
This can name
this names right. Can not have any spaces.
So just make sure if you want to have something like
you know
search web you do with an underscore
or you do it like with camel case.
And then you just need to have a description so that
the agent knows when it should be using this tool.
This is a basic description,
but you could give a more detailed description
if you wanted it to only use the tool
in a specific scenario.
Okay, so that's our first tool.
Now in order to use that, we're going to go back
into our Main.py file and we're going to import it.
So we're going to say from tools imports.
And we're just going to import the search tool okay.
We are then going to go to our agent.
And before here
we're going to make a list called tools.
And this is going to be equal
to our search tool inside of a list.
We're then going to pass our tools now to our agent
as well as to our agent Executer okay.
So now we'll have access to this list of tools.
Right now we just have one tool the search tool.
But if we gave it access to multiple tools
and it can pick and use all of them
or just the ones that are relevant.
Last thing here, rather than just having the query
be manually typed
in, I'm going to just get it from the user.
So I'm going to say query is equal to input.
You know what can I help you research question mark.
And then we're just going to pass query here.
So now the user will just type
in the query themselves okay.
And rather than printing the raw response
I'm going to print
the structured output okay.
So that we can use that as we see fit.
All right. So let's come up here.
Let's clear the screen and let's run
and let's see what we get.
And there was an issue here.
Could not import DuckDuckGo search package okay.
So we just need to install DuckDuckGo
search my bad guys.
So I'm going to go to requirements.txt.
And I put this inside of my requirements
so that I don't forget in that you guys will have it
when you look at the code in the video.
And I'm just going to run the command pip install
and then DuckDuckGo search.
So we're able to use that okay.
So give that a second to run. All good.
And now we can run the code again.
And hopefully this will work.
What can I help you research
I want to know about sharks okay.
And let's see what it gives us okay.
So you can see that it's invoking search.
So it's using this tool shark biology
habit behavior research okay.
That's an interesting search string.
And you can see that it gives us back
this research paper.
And obviously if we improve the prompt,
we'd get a better response.
But you know this is what we're looking for. Perfect.
So that is the search tool.
Next I will show you
how do you set up the Wikipedia tool.
And then our own custom tool.
So the Wikipedia tool is pretty straightforward.
What we can do is below here
we can say our API wrapper
is equal to the Wikipedia API wrapper.
Inside of here we can pass a few pieces of content
or few parameters.
So we can say top k results.
In this case, just make it equal to one.
But if we want it to return,
say five different types of results from Wikipedia,
we could go with five, right?
We can change this to be whatever we want.
Then we can say the doc content character is Max.
I'll just make this equal to 100
because this is a quick demo, but if you want it
to get a lot more content from the Wikipedia page,
then you would put 1000 or 10,000.
Again, it will take longer to run,
and you may get rate limited faster
because you don't even need an API key for this.
But again, just showing you a quick example.
So these are two parameters.
And I believe there's a few more
that you can pass here
like the language load
all available mate metadata etc..
Okay.
Now that we have the API wrapper,
we need to convert this to a tool.
So we're going to say the wiki
tool is equal to the Wikipedia query run.
And then all we're going to do is just pass our API
wrapper equal to our API wrapper.
Now that's actually all we need for the tool.
We don't need to wrap it in a custom tool.
We can just pass this as a tool
directly to link chain.
So what I'm going to do now is import the wiki tool.
So say wiki underscore tool.
And then I'm going to go here to my tools list.
Bring in the wiki tool.
And now we can run this and let's see what we get.
And if it's able to use Wikipedia.
So I'm gonna say same thing.
Yeah.
Shark.
So let's go hammerhead sharks
okay.
Let's see what we get.
So you can see it's using Wikipedia
looking up the hammerhead shark.
And then it's using search.
Yes. Search. Hammerhead shark.
Research latest findings. Okay.
And then it gives us the response,
and it tells us that it used these two tools.
Suite. Okay. Last thing.
I'm going to show you how we make our own custom tool
so we can save this to a file.
So in order to save this to a file
we can actually just write our own Python function.
So actually let's go up to the top here.
And this function or any function for that
matter can be wrapped as a tool.
So we're just going to make a function called save.
And actually I'm going to save some time because I
don't think you guys need to watch me write this out.
I'm just going to copy it in called save to txt.
We'll take in some data
and we'll take in a file name.
Now it's important that you give the parameters
a type here
so that the model knows how to call this function.
So make sure that you type them.
In this case I've typed it as a string.
If it was a more advanced type
you'd want to include that as well.
So what I'm doing is I'm just writing
at the top of the paper in a research output,
the timestamp, and then I'm going to write the data.
And that data is going to be that pedantic model
which you'll see in just one second.
Okay, so that's my function.
Now once we have the function
we just need to wrap it as a tool.
So to do that
we can say save underscore tool is equal to tool.
And then we just do
the exact same thing that we had here.
So I'm just going to copy this paste it
I'm going to change the function to be save to txt.
Notice I didn't call the function,
I just wrote the name of it.
And then for the name
we're just going to say save text to file.
Again make sure we don't have any spaces.
And then I will
just kind of bring in this description.
You know, save structured research data
to a text file so it knows what this is doing.
That's as easy as it is to make your own custom tool.
So if you want a tool that calls an API for example,
you can do that.
Just write a Python function, wrap it as a tool,
and you can pass as many of these
to your agent that you want
and really get some advanced functionality here.
So now we're going to bring in the tool.
So same thing got bring in. We save to TXT.
That's what I called it right.
No sorry. Save tool okay.
And then we are going to put that in the list
save tool.
And then we can start using the save tool.
Now the only thing is we just need
to instruct the model to save this to a file.
So we need a Python Main.py I'm going to say
let's research
I don't know what do we want to research.
You know self
East Asia population or something.
And so save to a file okay.
And then it should use kind of all of the tools
and save this to a file.
And let's see if it does that.
And just give this a second to run.
So you can see it just used Wikipedia.
It's using what else.
Southeast Asia.
Okay.
I think it's just my terminals
cutting off a little bit here.
And it used the saved text file and then finished
the chain gave us the output.
And now you can see
we have our research output. TXT file.
And inside of here we get our time stamp.
We get our topic and we get all of this information.
You can see the region has relatively
young population blah blah blah
blah blah and goes through
kind of all of the details.
So there you go.
We have just completed the project
and built an agent from scratch
in Python that has access to various tools.
This is super cool.
We are really just scratching the surface
with what is possible here.
I just wanted to give you a video
that kind of overview, the main components
and main topics
that make up like 80% of the Asian applications.
And this really does get you quite far
and allows you to build some really cool stuff.
So I will leave the code link here in the description
in case you want to check out the GitHub
which will have all of this content you can just copy
it and do whatever you want with it.
If you enjoyed the video, make sure leave a like.
Subscribe to the channel
and I will see you in the next one.
