TubeSum ← Transcribe a video

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

Transcribed Jun 14, 2026 Watch on YouTube ↗
Beginner 8 min read For: Beginner Python developers interested in building AI agents with LangChain.
716.3K
Views
15.3K
Likes
503
Comments
210
Dislikes
2.2%
📈 Moderate

AI 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.

[0:00]
Project Overview

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.

[1:38]
Prerequisites

Need Python 3.10+, a code editor (VS Code recommended), and a virtual environment to install dependencies.

[2:21]
Installing Dependencies

Create a requirements.txt file with packages like langchain, langchain-community, openai, anthropic, pydantic, python-dotenv, wikipedia, duckduckgo-search.

[2:53]
Setting Up Virtual Environment

Create a virtual environment with 'python -m venv venv' and activate it (source venv/bin/activate on Mac/Linux, .\venv\Scripts\activate on Windows).

[6:52]
Creating Project Files

Create main.py, tools.py, and .env files. The .env file stores API keys for OpenAI or Anthropic.

[7:34]
Setting Up LLM and Prompt

Import necessary modules, load environment variables, and initialize the LLM (ChatOpenAI or ChatAnthropic). Create a prompt template with system message and format instructions.

[13:55]
Structured Output with Pydantic

Define a Pydantic model (ResearchResponse) with fields like topic, summary, sources, tools_used. Use PydanticOutputParser to parse LLM output into this model.

[17:35]
Creating the Agent

Use create_tool_calling_agent with LLM, prompt, and tools. Then create an AgentExecutor to run the agent with verbose mode.

[23:41]
Adding Tools

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.

[30:47]
Custom Tool Example

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.

[33:35]
Final Demo

The agent can research a topic (e.g., Southeast Asia population) and save the structured output to a text file using the custom tool.

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.

Clickbait Check

95% Legit

"Title accurately describes the tutorial: building an AI agent from scratch in Python, beginner-friendly."

Mentioned in this Video

Tutorial Checklist

1 1:38 Ensure Python 3.10+ is installed and have a code editor (e.g., VS Code).
2 2:21 Create a requirements.txt file with: langchain, langchain-community, langchain-openai, langchain-anthropic, openai, anthropic, pydantic, python-dotenv, wikipedia, duckduckgo-search.
3 2:53 Create a virtual environment: python -m venv venv. Activate it: source venv/bin/activate (Mac/Linux) or .\venv\Scripts\activate (Windows).
4 4:47 Install dependencies: pip install -r requirements.txt.
5 6:52 Create main.py, tools.py, and .env files.
6 7:34 In main.py, import modules: from dotenv import load_dotenv; from pydantic import BaseModel; from langchain_openai import ChatOpenAI; from langchain_anthropic import ChatAnthropic; from langchain.prompts import ChatPromptTemplate; from langchain_core.output_parsers import PydanticOutputParser; from langchain.agents import create_tool_calling_agent, AgentExecutor.
7 8:25 Load environment variables: load_dotenv().
8 8:51 Initialize LLM: llm = ChatOpenAI(model='gpt-4-turbo') or llm = ChatAnthropic(model='claude-3-5-sonnet-20240620').
9 9:59 Add API keys to .env file: OPENAI_API_KEY=your_key or ANTHROPIC_API_KEY=your_key.
10 13:55 Define a Pydantic model for structured output (e.g., ResearchResponse with topic, summary, sources, tools_used).
11 15:03 Create parser: parser = PydanticOutputParser(pydantic_object=ResearchResponse).
12 15:43 Create a ChatPromptTemplate with system message, human message, and placeholders for chat_history, query, agent_scratchpad. Use partial to inject format_instructions.
13 17:35 Create agent: agent = create_tool_calling_agent(llm, tools, prompt).
14 18:46 Create agent executor: agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True).
15 19:21 Invoke agent: raw_response = agent_executor.invoke({'query': user_input}).
16 21:03 Parse response: structured_response = parser.parse(raw_response['output'][0]['text']).
17 23:41 In tools.py, create search tool: from langchain_community.tools import DuckDuckGoSearchRun; search = DuckDuckGoSearchRun(); search_tool = Tool(name='search', func=search.run, description='Search the web for information').
18 28:53 Create Wikipedia tool: from langchain_community.tools import WikipediaQueryRun; from langchain_community.utilities import WikipediaAPIWrapper; api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=100); wiki_tool = WikipediaQueryRun(api_wrapper=api_wrapper).
19 30:47 Create custom save tool: define function save_to_txt(data: str, filename: str) -> str; wrap as Tool(name='save_text_to_file', func=save_to_txt, description='Save structured research data to a text file').
20 31:35 Import tools into main.py and add them to the tools list. Pass tools list to both create_tool_calling_agent and AgentExecutor.

Study Flashcards (10)

What is the purpose of a virtual environment in Python?

easy Click to reveal answer

An isolated place to install dependencies for a project.

2:57

What command creates a virtual environment named 'venv'?

easy Click to reveal answer

python -m venv venv

3:42

How do you activate a virtual environment on Mac/Linux?

easy Click to reveal answer

source venv/bin/activate

4:13

How do you activate a virtual environment on Windows?

easy Click to reveal answer

.\venv\Scripts\activate

4:34

What is the PydanticOutputParser used for?

medium Click to reveal answer

To parse LLM output into a Pydantic model (structured Python object).

15:03

What are the three required parameters for create_tool_calling_agent?

medium Click to reveal answer

llm, tools, prompt

18:03

What does the AgentExecutor do?

medium Click to reveal answer

It executes the agent, handling the loop of calling the LLM and tools.

18:41

What are the three components needed to define a custom tool using the Tool class?

medium Click to reveal answer

name, func (the function), and description

25:48

What is the purpose of the 'format_instructions' in the prompt?

hard Click to reveal answer

To tell the LLM the exact format (schema) it should use for its output.

16:14

What does the 'partial' method do on a ChatPromptTemplate?

hard Click to reveal answer

It pre-fills some prompt variables (like format_instructions) before the prompt is used.

16:44

💡 Key Takeaways

💡

Build an AI Agent from Scratch

Core promise of the video: a beginner-friendly tutorial to create an AI agent using Python and LangChain.

🔧

Structured Output with Pydantic

Teaches how to enforce a specific output schema from the LLM, making responses predictable and usable in code.

13:55
🔧

Adding Tools to the Agent

Demonstrates how to give the agent access to external tools (search, Wikipedia) and custom Python functions.

23:41
🔧

Creating a Custom Tool

Shows how to wrap any Python function as a tool, enabling the agent to perform arbitrary actions like saving files.

30:47
💡

Final Working Agent Demo

Validates the entire tutorial by showing a functional research assistant that uses multiple tools and saves output.

33:35

✂️ Creator Tools: Viral Hooks

AI-generated clip ideas for Shorts based on the transcript

Build an AI Agent in Minutes!

45s

Shows a quick demo of a working AI research assistant, hooking viewers with immediate results.

▶ Play Clip

Install Python Dependencies Fast

45s

Provides a clear, step-by-step setup guide that beginners need to follow along.

▶ Play Clip

Create Your First AI Agent

45s

Demonstrates the core code to create an agent, making complex concepts accessible.

▶ Play Clip

Add Tools to Your AI Agent

45s

Shows how to integrate Wikipedia and search tools, dramatically expanding the agent's capabilities.

▶ Play Clip

Custom Tool: Save to File

45s

Teaches viewers to create their own Python function as a tool, unlocking endless customization.

▶ Play Clip

[00:00] In this video, you'll learn how to build an AI agent

[00:06] I'll walk you through everything step by step.

[00:08] This will be very beginner friendly,

[00:11] quite interesting in Python

[00:15] I'll show you how to use various

[00:19] how you can give the agent access to various tools,

[00:22] and how you can structure the output of the agent

[00:26] So let me show you

[00:27] a quick demo of the finished project,

[00:30] So you can see that I built

[00:33] You can make this anything that you want.

[00:35] This is just a quick demo for the tutorial.

[00:37] And it asks me what it wants to research.

[00:39] So I'm going to paste here.

[00:41] Tell me about Lang Chain and its applications.

[00:43] And I'm going to tell it to save it to a file.

[00:45] So it can use the tool that I created

[00:49] Now this has access to some tools

[00:53] So you can see here

[00:56] with this query gives us some kind of output here.

[00:59] And then if we scroll through

[01:02] And we can disable that as well if we want.

[01:04] Anyways, you see that we get some output here.

[01:06] We have a topic.

[01:07] We have a summary.

[01:08] We have the sources that it used

[01:12] And if we go here to the left hand side

[01:17] that contains the research output as well as a

[01:22] Of course, we can make this much more advanced,

[01:26] and we can get it to do some really cool things

[01:30] But this will really get us started

[01:34] So I hope you're excited.

[01:35] Let's go ahead and get into the video.

[01:38] So let's begin with a few prerequisites

[01:42] First of all,

[01:45] and ideally Python version 3.10 or above,

[01:50] You'll also need some kind of code editor.

[01:52] In this case, I'll recommend using something

[01:54] like Visual Studio Code,

[01:58] You'll then need to open a new folder

[02:01] If you're in VS code, for example, open folder

[02:05] In this case, I made one on my desktop by clicking

[02:10] That folder is called AI Agent Tutorial.

[02:12] So I just opened it here and you can see

[02:16] We're then going to need to install some different

[02:21] So let's start with the Python dependencies.

[02:23] What I'm going to do is make a new file.

[02:25] I'm doing that by pressing this button up here

[02:28] And I'm going to call this requirements.txt.

[02:31] I suggest that you do the same here.

[02:32] But there's other ways

[02:35] Now inside of this file

[02:39] You can manually write these out, or you can copy

[02:43] There'll be a GitHub link

[02:46] And you can find this file in there.

[02:48] Now these are the packages that we'll need in our

[02:53] So what we're going to do now is we're going

[02:57] A virtual environment is an isolated place

[03:01] dependencies, and then we can work

[03:04] If you're not familiar with virtual environments

[03:08] then you can simply run the command pip install dash

[03:11] r requirements.txt from your terminal,

[03:14] ideally in VS code in the same directory

[03:18] So in this case,

[03:21] And then I'm running this command pip install dash r

[03:25] to requirements.txt

[03:28] Which is why this will work.

[03:30] Otherwise you can do pip three, install dash r

[03:32] requirements.txt

[03:36] However, I'm

[03:39] And to do that you're going to type

[03:42] dash m v env and then v env.

[03:46] This is going to use

[03:50] In the current directory, if you're on Mac or Linux,

[03:55] On windows it should simply be Python.

[03:57] When you run this command,

[03:59] and then you get a new directory here called V env.

[04:03] Now what we need to do

[04:05] And then we can start using it.

[04:07] And everything will be the same as it normally would.

[04:09] So in order to activate the virtual environment,

[04:13] source dot slash the name of the virtual environment

[04:17] And then this is going to be slash

[04:22] Okay let me just type this correctly for you guys.

[04:26] And then you can hit enter.

[04:27] This is if you're on Mac or Linux.

[04:29] If you're on windows the command is different

[04:34] slash scripts slash activate okay this is windows.

[04:37] The other one is Mac or Linux.

[04:39] You hit enter and then you should see that

[04:44] Now we can install the dependencies

[04:47] So pip install dash requirements.txt

[04:51] And this should install

[04:52] all of the dependencies into this virtual environment

[04:56] So the dependencies have now been installed

[05:00] And I just want to make you aware

[05:03] popping up in my editor, and you're wondering what

[05:08] So if you want to use that, you can do that for free

[05:11] And then type in GitHub and you can see copilot.

[05:15] So pop up right here.

[05:16] I've just installed this in my editor.

[05:18] So that's why you're seeing the autocomplete

[05:21] Yeah.

[05:22] You know that insane

[05:26] Well, that's

[05:30] are transforming

[05:33] Now, personally,

[05:36] two years and to be honest with you,

[05:39] Whether it's generating unit tests, stubbing classes,

[05:44] I'm sure that you guys have come up

[05:48] Now that's actually something

[05:52] to learn new skills

[05:56] that they've used GitHub Copilot to solve everyday

[05:59] Now, Microsoft is currently running hashtag

[06:04] and celebrating the standout ways that developers

[06:10] Now, you can share your own GitHub

[06:13] to be featured on the Microsoft

[06:16] If you have a cool story on how you've used GitHub,

[06:21] platforms like Instagram X, YouTube, or LinkedIn

[06:24] with the hashtag coding with Copilot

[06:28] all of the submissions for a shoutout

[06:32] I'd love to check them out.

[06:34] Now we all know the AI is reshaping the industry,

[06:39] to tackle and solve problems

[06:43] Now, a massive shoutout to GitHub Copilot

[06:48] and I look forward to reviewing some of your

[06:52] Okay, so now that everything is set

[06:55] So what we're going to do

[07:00] This will be the file

[07:02] We're

[07:05] We're just going to separate things out

[07:08] And the main logic here so that things are

[07:13] We're also going to make one more file called dot

[07:16] This is an environment variable file

[07:20] for things like our GPT API key

[07:25] Because I'm going to show you two ways

[07:31] Okay. So we're going to go to Main.py.

[07:34] And we're going to start

[07:37] We're going to run the agent.

[07:38] And then we'll start adding

[07:41] So to begin we're going to start

[07:43] So we're first going to say from dot env import

[07:49] We're then going to say from pedantic import.

[07:53] If we can spell this correctly the base model

[07:57] we're then going to say from Lang train.

[08:00] And this is going to be underscore open AI import.

[08:04] And this is going to be chat open AI.

[08:06] And then additionally and this is optional,

[08:12] And this is going to be Chat and Tropic

[08:16] choose between either

[08:21] Beneath that I am going to type this line

[08:25] What this is going to do is load

[08:29] which we'll fill in in just a minute.

[08:30] So we have all of the credentials

[08:34] Next what we want to do is we want to set up in Lem.

[08:37] So our agent will begin with some type of Lem.

[08:40] We can just use the yellow Lem normally.

[08:42] But what we're going to do is give it to an agent

[08:46] and some various other functional, like being able

[08:51] So we're going to start by saying Lem is equal to.

[08:54] And here's where you're going to have a choice.

[08:55] You can choose to use chat OpenAI, which means you're

[09:01] Or you can use something like Chat and Tropic.

[09:04] So you can see here

[09:08] Now for both of these, what you need to do

[09:11] So if you're using something like OpenAI

[09:16] And then again something like GPT five turbo.

[09:18] Or you could use GPT for mini or for mini.

[09:21] There's all kinds of different models.

[09:22] You can just go with something like GPT for,

[09:25] for mini is another option as well.

[09:27] And as for the API key, don't worry,

[09:31] Now if you're using entropic

[09:35] So I'm just going to paste

[09:38] So Claude 3-5 sonnet.

[09:39] And then this is the version that I picked.

[09:41] But you can pick a different version.

[09:43] And when this video is out, there may be

[09:47] Either way you're going to select a model and you're

[09:52] In my case, I'm using Chat Tropic

[09:57] Okay. So now we've selected the yellow line.

[09:59] However, we need to provide an API key

[10:03] So regardless of if you're using OpenAI or in Tropic

[10:07] you're going to need to go into your environment

[10:12] and you're going to need to write

[10:15] The first is if you're using OpenAI,

[10:18] underscore API underscore key is equal to.

[10:21] And then you're going to have to paste the API key

[10:24] Lastly, if you're using entropic

[10:28] Let me make sure I do this correctly.

[10:30] You're going to have to do anthropic

[10:33] if we can type this correctly.

[10:35] Underscore

[10:40] So again if you're using OpenAI I use this.

[10:42] If you're using anthropic

[10:45] I'm going to show you

[10:47] So hang tight for one second.

[10:48] Okay.

[10:48] So in order to get the API keys, I believe

[10:53] Don't worry, this will cost you like $0.01 at most.

[10:55] If anything, it should probably just be free.

[10:58] Regardless,

[11:02] I will leave this link in the description

[11:05] Give it a name, create the secret key and copy it.

[11:08] If you're working with OpenAI and for Entropic

[11:13] Console dot dot entropic.com/settings/keys.

[11:17] Again link in the description.

[11:18] Press create key.

[11:19] Same thing.

[11:20] Give it a name and then copy that key.

[11:22] Obviously do not share this with anyone.

[11:24] So now what I'm going to do is I'm going to close

[11:28] And then I'm going to close this file

[11:31] So my API key is now loaded in order to test

[11:35] we can invoke the lemon

[11:38] So in order to do that

[11:43] And then I believe we can simply just pass a query.

[11:45] So something like what is the meaning of life? Great

[11:48] That's giving me the autocomplete from the AI

[11:52] It's possible that we need to pass, something else,

[11:57] So now that we have that,

[12:00] Make sure that when you run the Python code, you're

[12:04] And the easiest way to do that is, again,

[12:09] and then type Python and then the name of your file,

[12:12] is main.py or Python three main.py.

[12:16] And then hit enter

[12:18] It might just take one second. So let's see.

[12:20] And we get some content and kind of some

[12:26] You can see it says that's one of humanity's oldest

[12:30] And we get the response.

[12:32] Okay, so that's how you use the LM very simply.

[12:34] Like we're just using L alone.

[12:35] We haven't added any agent functionality.

[12:37] But next what I want to do

[12:41] and make this a little bit more advanced.

[12:43] So after we have our LM, the next thing I'm going

[12:48] This is something that will kind of act as a template

[12:53] so that we can give it more information

[12:57] So for our prompt template,

[13:00] We're going to say from line chain.

[13:03] And then this is going to be underscore underscore.

[13:08] And then this is going to be dot prompts.

[13:11] And we're going to import the chat prompt template.

[13:15] Okay.

[13:15] While we're here

[13:19] And then this is going to be dot output underscore

[13:23] We're going to import the pedantic output parser.

[13:28] Now basically what we're going to do

[13:32] which will specify the type of content

[13:36] We're then going to give the LM a prompt and we're

[13:41] And as a part of your response

[13:47] So it will give us output in a format that we can

[13:52] You'll see what I mean in one second, but

[13:55] So I'm going to create a class here.

[13:57] And this is going to be my response or my research.

[14:00] Sorry response.

[14:02] Okay.

[14:05] Now you can make this class anything that you want.

[14:07] I'm just giving you a simple example here with like

[14:12] So the response that I want is

[14:15] And I want that topic to be of type string.

[14:17] So I'm going to specify that right here.

[14:19] I then want to have a summary.

[14:21] And I want that summary to be of type string.

[14:23] So I specify the type right.

[14:25] Then I want to have some sources.

[14:27] And I want these sources to be a list of strings.

[14:30] So I'm going to type list

[14:33] Then I'm going to have some tools used.

[14:35] And I'm going to type list and string.

[14:38] Now here is where you can just specify

[14:41] that you want as output from your LM call.

[14:44] You can make this as complicated as you want.

[14:46] You can have nested objects, so long as all of your

[14:52] Okay.

[14:53] So you just need to make sure

[14:56] all of the fields

[15:00] And we can eventually pass that to the LM.

[15:03] Now that we have this, what we're going to do

[15:06] So we're going to say

[15:11] And we're simply going to parse

[15:14] But we're going to do this as the pedantic object.

[15:17] Okay.

[15:19] There's other ways that you can set up this parser

[15:24] or using other types of kind of

[15:27] But in this case we're using pedantic,

[15:31] So this parser will now allow us to essentially take

[15:37] And then we can use it

[15:41] Okay. So next we need to set up a prompt.

[15:43] Now I'm just going to copy this in

[15:46] And I'll walk through it line by line.

[15:48] So we're going to say our prompt is equal to our chat

[15:52] dot from messages okay.

[15:55] Then inside of here the first thing we're going to

[15:59] The system message is information to the LM.

[16:01] So it knows what it's supposed to be doing.

[16:03] So we tell it you are a research assistant

[16:07] answer the user query and use the necessary tools.

[16:10] And then the important part

[16:14] and provide

[16:18] Okay. That's very important.

[16:19] Make sure you have this part.

[16:21] Lastly,

[16:24] So you need to add the chat history.

[16:26] The query this is coming from the user.

[16:28] And the placeholder which is the agent scratchpad.

[16:31] Don't worry

[16:33] The only important one is the query.

[16:35] This is just necessary for the type of agent

[16:38] And you can find all this information from the link

[16:41] Okay. Then we say partial.

[16:44] And what this means is we're partially going to fill

[16:50] So what this now does

[16:54] And it just takes this pedantic model

[16:59] that we can then give to the prompt.

[17:02] So we're pretty much just taking this model

[17:04] and converting it to a string,

[17:09] So now it knows when it generates a response,

[17:13] So notice that format instructions here

[17:17] They're the same variable. That's important.

[17:19] You could call this anything that you want

[17:23] Okay.

[17:23] And then these other values will be

[17:24] automatically filled in for us

[17:29] Okay. So now we have our prompt.

[17:31] We have our parser.

[17:32] We have our LM.

[17:35] So we're going to say agent is equal to.

[17:38] And then we're going to bring in a function

[17:41] So from the top of our code

[17:46] And then this is going to be dot agent imports.

[17:50] And then create underscore.

[17:51] And notice

[17:54] But we we want to create tool calling agent okay.

[17:59] Now this is one we'll use again

[18:02] So I'm going to use this function.

[18:03] And inside of this function

[18:08] which will simply be equal to the alarm

[18:12] And then we can pass it our prompt.

[18:14] So we can say prompt is equal to prompt.

[18:17] And then lastly we're going to say tools.

[18:19] And for right now

[18:22] We'll specify some tools in a second.

[18:23] But for now I just want to test the agent

[18:27] Okay.

[18:27] So we have our agent create

[18:31] And now we want to test the agent.

[18:33] So in order to test the agent

[18:36] So from link change agents we're going to import

[18:41] The agent Executer is just a way

[18:44] Right. So that's why we need to bring that in.

[18:46] And then down here

[18:51] executer is equal to an agent Executer.

[18:55] For the agent Executer,

[18:59] The tools is equal to again,

[19:02] And we're going to say verbose

[19:07] So we can see the thought process of the agent.

[19:10] If you don't care about the thought process

[19:12] then you can just mark this as false

[19:15] So now we have an agent Executer, and we can use

[19:21] So we can say our raw underscore response is equal

[19:24] to the agent Executer dot invoke.

[19:28] And then when we invoke this we need to pass

[19:32] Now you can have multiple prompt variables.

[19:34] And notice

[19:37] So we have the format instruction prompt variable

[19:41] The chat history and the agent scratchpad will

[19:46] And then we're left with one more prompt variable

[19:49] And if you wanted to add multiple variables here,

[19:52] And you can say you know, name or something.

[19:54] And then here when you invoke this

[19:58] So you'd pass the query which is you know,

[20:02] And the name of Alice.

[20:03] So you can pass multiple prompt variables.

[20:05] Again in this case we just need query.

[20:08] But I just wanted to show you

[20:11] Okay.

[20:12] So we're going to invoke the agent Executer.

[20:14] We're going to get a raw response.

[20:16] And for now we can just print out the raw response.

[20:18] And I believe that that should be it

[20:21] So let me zoom out a little bit.

[20:23] You can see our imports again.

[20:24] You can find all of this code from the link

[20:27] Generate the Elm.

[20:28] Make the parser create our prompt

[20:31] Then we have our agent and we create the agent

[20:36] So let's try this out and see if we get something.

[20:40] Give this a second.

[20:41] You can see it's going into the agent executor chain.

[20:44] And it should generate a response for me.

[20:46] And you can see that we get that.

[20:48] And then it gives us kind of output text topic.

[20:51] And it gives us the output in this format.

[20:53] Now if we just want to see the response

[20:57] So like this kind of model, what we need to do

[21:00] is use the parser to parse this content.

[21:03] So what we're going to do is we're going to say

[21:06] our structured response is equal to parser dot parse.

[21:09] And then rather than just parsing the raw response

[21:15] we're going to get the output key because it gives us

[21:19] Like if you look here,

[21:21] You can see we have output and then we have an array

[21:26] So we just need to go inside of that.

[21:27] So we're going to say output

[21:32] What this

[21:34] It's going to get the text from like the first value

[21:39] And it's going to parse that

[21:43] So then if I go here and I print the structured

[21:46] response, let's run this again

[21:49] And don't worry,

[21:52] Okay.

[21:52] So we're entering the agent executer chain

[21:58] So query output our text.

[21:59] And then when we print out the Python object

[22:03] that are in our pedantic bottle.

[22:06] If we keep going through here

[22:09] we have the tools used and then we have the.

[22:13] If we keep going here, the sources. Right.

[22:15] And it tells us where these sources came from.

[22:17] Cool. Okay. So that is that.

[22:20] And that allows us now

[22:22] And the interesting thing, right, is that

[22:26] what I can do is something like structured

[22:31] And I can just access this topic

[22:35] And I can just use that.

[22:36] So this is the really kind of neat part

[22:40] is that now I can get specific components

[22:45] in my code, rather than just having this plain text,

[22:51] So we've already got that part.

[22:52] We have our structured response,

[22:56] add a simple try catch here or try accept block

[23:01] And then this will give us an error.

[23:03] So if it doesn't give us the correct response type,

[23:07] So what we're going to do is just say accept

[23:11] and we're just going to go, what do you call it here?

[23:15] Error parsing response.

[23:19] Then we're going to print out E.

[23:21] And then we're going to say the raw response

[23:24] And we'll actually just do another one

[23:29] Can make this look better if you want,

[23:30] but I'm just doing a simple error message

[23:34] Okay. So there we go. We have our try accept.

[23:37] And now we need to do the cool part

[23:41] So of course, you know this is interesting, but

[23:46] Tools are things that the LM can use

[23:50] that we can either write ourself or we can bring in

[23:55] So what I'm going to do is go to my tools.py file,

[23:59] different tools one for looking up Wikipedia,

[24:02] one for going to DuckDuckGo and searching something.

[24:05] These are all free. You don't need any API keys.

[24:08] And then one custom tool that we write ourself

[24:12] So we're going to go to the top of our code.

[24:13] We're going to say from Lang Chain.

[24:15] And this is going to be underscore community import.

[24:19] And then the Wikipedia or sorry

[24:25] And then we're going to import the Wikipedia

[24:29] Then we're going to say from Lang chain community

[24:32] And actually this is going to be dot utilities

[24:36] getting carried away with the copilot autocomplete.

[24:39] Here we're going to say import the Wikipedia

[24:43] We're then going to say from Lang Chain

[24:48] we're going to bring in the DuckDuckGo

[24:53] So a tool that we can use.

[24:55] And then we are going to have two last imports.

[24:57] So we're going to say from

[25:01] Lang chain dot tools.

[25:04] And we're going to import the tool.

[25:07] This will allow us to kind of wrap

[25:10] And then we're going to say from date time

[25:13] imports date time okay.

[25:16] Sorry I know that was me messing around and trying

[25:20] So you can see we have some stuff

[25:24] Again, all of these are free, but you will get rate

[25:28] And then we'll be able to create our own custom tool.

[25:30] So let's start by simply creating the tool

[25:35] Google search is what I'm calling it,

[25:38] So to do that, I'm simply going to say

[25:42] I'm just going to call that.

[25:43] I'm then going to say my search underscore tool

[25:48] For my tool, I need to give this a name.

[25:50] So I'm just going to say

[25:53] So we can say name is equal to this.

[25:56] The function is going to be equal to search dot run.

[26:00] So this provides a function called run.

[26:02] So that's what we're passing here. And then

[26:05] what do we want to have here.

[26:06] We need to have a description for the tool.

[26:09] And this is just going to be search the web

[26:13] So this is our tool.

[26:15] That's literally all you need to do.

[26:17] We've now created a tool

[26:20] The key thing is that you need to have some name.

[26:22] This can name

[26:24] So just make sure if you want to have something like

[26:26] search web you do with an underscore

[26:30] And then you just need to have a description so that

[26:34] This is a basic description,

[26:37] if you wanted it to only use the tool

[26:40] Okay, so that's our first tool.

[26:42] Now in order to use that, we're going to go back

[26:47] So we're going to say from tools imports.

[26:50] And we're just going to import the search tool okay.

[26:54] We are then going to go to our agent.

[26:57] And before here

[27:00] And this is going to be equal

[27:04] We're then going to pass our tools now to our agent

[27:07] as well as to our agent Executer okay.

[27:11] So now we'll have access to this list of tools.

[27:13] Right now we just have one tool the search tool.

[27:15] But if we gave it access to multiple tools

[27:19] or just the ones that are relevant.

[27:21] Last thing here, rather than just having the query

[27:24] be manually typed

[27:27] So I'm going to say query is equal to input.

[27:30] You know what can I help you research question mark.

[27:36] And then we're just going to pass query here.

[27:38] So now the user will just type

[27:42] And rather than printing the raw response

[27:45] the structured output okay.

[27:48] So that we can use that as we see fit.

[27:50] All right. So let's come up here.

[27:52] Let's clear the screen and let's run

[27:57] And there was an issue here.

[27:58] Could not import DuckDuckGo search package okay.

[28:01] So we just need to install DuckDuckGo

[28:04] So I'm going to go to requirements.txt.

[28:07] And I put this inside of my requirements

[28:10] when you look at the code in the video.

[28:12] And I'm just going to run the command pip install

[28:16] So we're able to use that okay.

[28:18] So give that a second to run. All good.

[28:21] And now we can run the code again.

[28:23] And hopefully this will work.

[28:24] What can I help you research

[28:31] And let's see what it gives us okay.

[28:34] So you can see that it's invoking search.

[28:36] So it's using this tool shark biology

[28:39] That's an interesting search string.

[28:41] And you can see that it gives us back

[28:44] And obviously if we improve the prompt,

[28:46] But you know this is what we're looking for. Perfect.

[28:49] So that is the search tool.

[28:51] Next I will show you

[28:53] And then our own custom tool.

[28:55] So the Wikipedia tool is pretty straightforward.

[28:57] What we can do is below here

[29:01] is equal to the Wikipedia API wrapper.

[29:04] Inside of here we can pass a few pieces of content

[29:07] So we can say top k results.

[29:09] In this case, just make it equal to one.

[29:11] But if we want it to return,

[29:15] we could go with five, right?

[29:16] We can change this to be whatever we want.

[29:18] Then we can say the doc content character is Max.

[29:22] I'll just make this equal to 100

[29:26] to get a lot more content from the Wikipedia page,

[29:30] Again, it will take longer to run,

[29:32] and you may get rate limited faster

[29:36] But again, just showing you a quick example.

[29:38] So these are two parameters.

[29:39] And I believe there's a few more

[29:41] like the language load

[29:45] Okay.

[29:45] Now that we have the API wrapper,

[29:48] So we're going to say the wiki

[29:53] And then all we're going to do is just pass our API

[29:58] Now that's actually all we need for the tool.

[30:00] We don't need to wrap it in a custom tool.

[30:02] We can just pass this as a tool

[30:05] So what I'm going to do now is import the wiki tool.

[30:08] So say wiki underscore tool.

[30:10] And then I'm going to go here to my tools list.

[30:12] Bring in the wiki tool.

[30:15] And now we can run this and let's see what we get.

[30:18] And if it's able to use Wikipedia.

[30:20] So I'm gonna say same thing.

[30:22] Yeah.

[30:23] Shark.

[30:23] So let's go hammerhead sharks

[30:29] okay.

[30:29] Let's see what we get.

[30:30] So you can see it's using Wikipedia

[30:34] And then it's using search.

[30:36] Yes. Search. Hammerhead shark.

[30:38] Research latest findings. Okay.

[30:40] And then it gives us the response,

[30:45] Suite. Okay. Last thing.

[30:47] I'm going to show you how we make our own custom tool

[30:51] So in order to save this to a file

[30:55] So actually let's go up to the top here.

[30:57] And this function or any function for that

[31:01] So we're just going to make a function called save.

[31:03] And actually I'm going to save some time because I

[31:06] I'm just going to copy it in called save to txt.

[31:09] We'll take in some data

[31:12] Now it's important that you give the parameters

[31:16] so that the model knows how to call this function.

[31:19] So make sure that you type them.

[31:20] In this case I've typed it as a string.

[31:22] If it was a more advanced type

[31:25] So what I'm doing is I'm just writing

[31:28] the timestamp, and then I'm going to write the data.

[31:30] And that data is going to be that pedantic model

[31:35] Okay, so that's my function.

[31:36] Now once we have the function

[31:39] So to do that

[31:43] And then we just do

[31:46] So I'm just going to copy this paste it

[31:51] Notice I didn't call the function,

[31:54] And then for the name

[32:00] Again make sure we don't have any spaces.

[32:01] And then I will

[32:04] You know, save structured research data

[32:08] That's as easy as it is to make your own custom tool.

[32:10] So if you want a tool that calls an API for example,

[32:14] Just write a Python function, wrap it as a tool,

[32:18] to your agent that you want

[32:22] So now we're going to bring in the tool.

[32:23] So same thing got bring in. We save to TXT.

[32:26] That's what I called it right.

[32:27] No sorry. Save tool okay.

[32:31] And then we are going to put that in the list

[32:36] And then we can start using the save tool.

[32:38] Now the only thing is we just need

[32:42] So we need a Python Main.py I'm going to say

[32:45] let's research

[32:49] You know self

[32:52] East Asia population or something.

[32:57] And so save to a file okay.

[33:00] And then it should use kind of all of the tools

[33:03] And let's see if it does that.

[33:05] And just give this a second to run.

[33:06] So you can see it just used Wikipedia.

[33:09] It's using what else.

[33:10] Southeast Asia.

[33:12] Okay.

[33:12] I think it's just my terminals

[33:16] And it used the saved text file and then finished

[33:21] And now you can see

[33:24] And inside of here we get our time stamp.

[33:26] We get our topic and we get all of this information.

[33:29] You can see the region has relatively

[33:32] blah blah and goes through

[33:35] So there you go.

[33:35] We have just completed the project

[33:39] in Python that has access to various tools.

[33:42] This is super cool.

[33:43] We are really just scratching the surface

[33:47] I just wanted to give you a video

[33:51] and main topics

[33:54] And this really does get you quite far

[33:59] So I will leave the code link here in the description

[34:02] which will have all of this content you can just copy

[34:06] If you enjoyed the video, make sure leave a like.

[34:08] Subscribe to the channel

⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.