Home / File/ tools.py — langchain Source File

tools.py — langchain Source File

Architecture documentation for tools.py, a python file in the langchain codebase. 5 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e["tools.py"]
  8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"]
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3
  f3bc7443_c889_119d_0744_aacc3620d8d2["langchain_core.callbacks"]
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e --> f3bc7443_c889_119d_0744_aacc3620d8d2
  43d88577_548b_2248_b01b_7987bae85dcc["langchain_core.tools"]
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e --> 43d88577_548b_2248_b01b_7987bae85dcc
  6e58aaea_f08e_c099_3cc7_f9567bfb1ae7["pydantic"]
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e --> 6e58aaea_f08e_c099_3cc7_f9567bfb1ae7
  706126c9_5306_8b22_9189_e911bde5257c["langchain_perplexity._utils"]
  1a67c21d_2ec3_4b16_a500_a33a7c94b43e --> 706126c9_5306_8b22_9189_e911bde5257c
  style 1a67c21d_2ec3_4b16_a500_a33a7c94b43e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from __future__ import annotations

from typing import Any, Literal

from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import Field, SecretStr, model_validator

from langchain_perplexity._utils import initialize_client


class PerplexitySearchResults(BaseTool):
    """Perplexity Search tool."""

    name: str = "perplexity_search_results_json"
    description: str = (
        "A wrapper around Perplexity Search. "
        "Input should be a search query. "
        "Output is a JSON array of the query results"
    )
    client: Any = Field(default=None, exclude=True)
    pplx_api_key: SecretStr = Field(default=SecretStr(""))

    @model_validator(mode="before")
    @classmethod
    def validate_environment(cls, values: dict) -> Any:
        """Validate the environment."""
        return initialize_client(values)

    def _run(
        self,
        query: str | list[str],
        max_results: int = 10,
        country: str | None = None,
        search_domain_filter: list[str] | None = None,
        search_recency_filter: Literal["day", "week", "month", "year"] | None = None,
        search_after_date: str | None = None,
        search_before_date: str | None = None,
        run_manager: CallbackManagerForToolRun | None = None,
    ) -> list[dict] | str:
        """Use the tool."""
        try:
            params = {
                "query": query,
                "max_results": max_results,
                "country": country,
                "search_domain_filter": search_domain_filter,
                "search_recency_filter": search_recency_filter,
                "search_after_date": search_after_date,
                "search_before_date": search_before_date,
            }
            params = {k: v for k, v in params.items() if v is not None}
            response = self.client.search.create(**params)
            return [
                {
                    "title": result.title,
                    "url": result.url,
                    "snippet": result.snippet,
                    "date": result.date,
                    "last_updated": result.last_updated,
                }
                for result in response.results
            ]
        except Exception as e:
            msg = f"Perplexity search failed: {type(e).__name__}"
            return msg

Subdomains

Dependencies

  • langchain_core.callbacks
  • langchain_core.tools
  • langchain_perplexity._utils
  • pydantic
  • typing

Frequently Asked Questions

What does tools.py do?
tools.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, MessageSchema subdomain.
What does tools.py depend on?
tools.py imports 5 module(s): langchain_core.callbacks, langchain_core.tools, langchain_perplexity._utils, pydantic, typing.
Where is tools.py in the architecture?
tools.py is located at libs/partners/perplexity/langchain_perplexity/tools.py (domain: CoreAbstractions, subdomain: MessageSchema, directory: libs/partners/perplexity/langchain_perplexity).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free