0
0
LangchainHow-ToBeginner ยท 3 min read

How to Use Search Tool Langchain: Simple Guide

To use the SearchTool in Langchain, import it and create an instance with a search function that returns relevant documents. Then, call the tool with a query string to get search results. This lets you integrate search capabilities easily in your Langchain applications.
๐Ÿ“

Syntax

The SearchTool requires a search function that takes a query string and returns a list of results. You create the tool by passing this function to SearchTool.from_function(). Then, use the run() method with your query to get results.

  • search_function: Your custom function to perform search.
  • SearchTool.from_function(search_function): Creates the search tool.
  • tool.run(query): Runs the search with the query string.
python
from langchain.tools import SearchTool

def search_function(query: str) -> list[str]:
    # Your search logic here
    return ["Result 1", "Result 2"]

search_tool = SearchTool.from_function(search_function)

results = search_tool.run("example query")
print(results)
Output
['Result 1', 'Result 2']
๐Ÿ’ป

Example

This example shows how to create a simple search tool that returns matching items from a list. It demonstrates defining the search function, creating the tool, and running a query.

python
from langchain.tools import SearchTool

# Sample data to search
documents = [
    "Learn Python programming",
    "Introduction to Langchain",
    "Advanced search techniques",
    "Langchain search tool example"
]

def simple_search(query: str) -> list[str]:
    # Return documents containing the query (case-insensitive)
    return [doc for doc in documents if query.lower() in doc.lower()]

# Create the search tool
search_tool = SearchTool.from_function(simple_search)

# Run a search query
output = search_tool.run("langchain")
print(output)
Output
["Introduction to Langchain", "Langchain search tool example"]
โš ๏ธ

Common Pitfalls

Common mistakes when using the Langchain search tool include:

  • Not returning a list of strings from the search function, which causes errors.
  • Using a search function that is too slow or blocking, which delays results.
  • Forgetting to handle case sensitivity or empty queries, leading to missed results.

Always ensure your search function returns a list and handles queries gracefully.

python
from langchain.tools import SearchTool

# Wrong: returns a string instead of list
# def bad_search(query: str) -> str:
#     return "No results"

# Right: returns a list

def good_search(query: str) -> list[str]:
    return ["No results"]

search_tool = SearchTool.from_function(good_search)
print(search_tool.run("test"))
Output
['No results']
๐Ÿ“Š

Quick Reference

Remember these key points when using the Langchain search tool:

  • Define a search function that takes a string and returns a list of strings.
  • Create the tool with SearchTool.from_function().
  • Use run() to execute the search.
  • Handle empty or invalid queries inside your search function.
  • Keep the search function efficient for better performance.
โœ…

Key Takeaways

Use a function that returns a list of strings as the search logic for SearchTool.
Create the search tool with SearchTool.from_function(your_search_function).
Call run(query) on the tool to get search results.
Ensure your search function handles queries efficiently and returns consistent results.
Avoid returning single strings or non-list types from the search function.