01. Tools

Tools (Tool) are interfaces for agents, chains or LLMs to interact with the outside world.

You can easily take advantage of the tools using the tools provided by LangChain, and it is also possible to easily build custom tools.

The tool list integrated into LangChain can be found in the link below.

Copy

# Configuration file for managing API keys as environment variables
from dotenv import load_dotenv

# Load API key information
load_dotenv()

Copy

 True 

Copy

# LangSmith set up tracking. https://smith.langchain.com
# !pip install -qU langchain-teddynote
from langchain_teddynote import logging

# Enter a project name.
logging.langsmith("CH15-Tools")

Copy

Copy

built-in tools (built-in tools)

You can use predefined tools (tool) and toolkits provided by Langchain.

tool means a single tool, toolkit can tie multiple tools and use them as one tool.

Related tools can be referenced at the link below.

Reference

Python REPL tool

This tool provides two main classes for running Python code in a Read-Eval-Print Loop (REPL) environment. -PythonREPLTool

Explanation

  • Python shell environment.

  • Execute valid Python instructions as input.

  • Print to see results (...) You need to use functions.

Main features

  • sanitize_input: option to purify input (default: True)

  • python_repl: PythonREPL instance (default: run across range)

How to use

  • PythonREPLTool instance creation

  • Run Python code using run or arun, invoke method

Input tablets

  • Removes unnecessary spaces, back ticks,'python' keywords, etc. from the input string.

Copy

Copy

Copy

Below is an example that asks LLM to write a Python code and returns the result.

Flow cleanup

  1. Ask the LLM model to write a Python code that does a specific job.

  2. Run the written code to get results.

  3. Output results.

Copy

Copy

Copy

Search API tools

A tool that implements search capabilities by utilizing the Tavily search API. This tool offers two main classes: TavilySearchResults Wow TavilyAnswer .

API key issuance address

  • https://app.tavily.com/

Set the API key issued to the environment variable.

.env Set to the file as below.

Copy

TavilySearchResults

Explanation

  • Query the Tavily search API and return results in JSON format.

  • It is a search engine optimized for comprehensive, accurate and reliable results.

  • Useful when answering questions about current events.

Main parameters

  • max_results (int): Maximum number of search results to return (default: 5)

  • search_depth (str): Search depth ("basic" or "advanced")

  • include_domains (List[str]): List of domains to include in search results

  • exclude_domains (List[str]): List of domains to exclude from search results

  • include_answer (bool): Whether to include a short answer to the original query

  • include_raw_content (bool): Whether each site contains refined HTML content

  • include_images (bool): Query-related image list included

Return value

  • String in JSON format containing search results (url, content) Or uncomment below and enter the API key that was issued.

Copy

Copy

Copy

Copy

Added TavilySearch tool (custom implementation)

Description of the main parameters used in the Tavily search API

Default search settings

  • query (str): keywords or sentences you want to search for

  • search_depth (str): detail of the search. Select between "basic" (basic) or "advanced" (advanced)

  • topic (str): Search subject area. Select between "general" (general) or "news" (news)

  • days (int): The latest in search results. Returns only results within the specified number of days

  • max_results (int): Maximum number of search results to be returned

Domain filtering

  • include_domains (list): List of domains that must be included in the search results

  • exclude_domains (list): List of domains to exclude from search results

Result detail setting

  • include_answer (bool): Whether the API generated answers are included

  • include_raw_content (bool): Whether the webpage contains the original HTML content

  • include_images (bool): Whether relevant image information is included

  • format_output (bool): Whether the search results are formatted

Etc

  • **kwargs : Additional keyword factors. Can be used for future updates or special features of the API

Copy

Copy

Image creation tool (DALL-E)

  • DallEAPIWrapper 클래스 : Wrapper for OpenAI's DALL-E image generator.

With this tool, you can easily integrate the DALL-E API to implement text-based image creation capabilities. Various setup options allow you to leverage it as a flexible and powerful image creation tool.

Main properties

  • model : DALL-E model name to use (default: "dall-e-2", "dall-e-3")

  • n : Number of images to create (default: 1)

  • size : Image size to create

  • "dall-e-2": "1024x1024", "512x512", "256x256"

  • "dall-e-3": "1024x1024", "1792x1024", "1024x1792"

  • style : Style of the image to be created (default: "natural", "vivid")

  • quality : Quality of the image to be created (default: "standard", "hd")

  • max_retries : Maximum number of retries at creation

Main features

  • Create images based on text descriptions using the DALL-E API

Flow cleanup

Here is an example of creating an image using DALL-E Image Generator.

this time DallEAPIWrapper Let's create an image using.

At this time, the input prompt asks the LLM model to create a prompt that creates an image.

Copy

Copy

So, the image prompt you created earlier DallEAPIWrapper Let's create an image by typing in.

Copy

Custom Tool

In addition to the built-in tools provided by LangChain, users can define and use the tools themselves.

To do this langchain.tools Provided by the module tool Convert functions to tools using decorators.

@tool decorator

This decorator provides the ability to convert functions into tools. Various options allow you to customize the behavior of the tool.

How to use

  1. Above function @tool Decorator application

  2. Set decorator parameters as needed

With this decorator, you can easily convert ordinary Python functions into powerful tools, and create automated documentation and flexible interfaces.

Copy

Copy

Copy

Copy

Copy

Google News Agency Search Tool

langchain-teddynote Packaged GoogleNews A tool that uses tools to search for Google news agencies.

Reference

  • No API key required. (Because I use the RRSS feed)

A tool to search for news articles provided by news.google.com.

Explanation

  • Search for the latest news using the Google News Search API.

  • You can search for news based on keywords.

  • Search for the latest news.

Main parameters

  • k (int): Maximum number of search results to return (default: 5)

Please update the package before use.

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Copy

Last updated