Introduction to Using Tools in LangChain

LangChain is a powerful open-source framework designed to help developers more efficiently build and deploy applications based on Large Language Models (LLMs). It provides a complete set of tools and components that enable developers to easily integrate language models with other data sources and tools.

Core Functions

  1. Tool Integration:

    • Supports connecting to various external APIs and tools
    • Includes pre-built tools like Google Search, Calculator, and Python REPL
    • Allows custom tool development to meet specific business needs
  2. Chained Calls:

    • Allows chaining multiple LLM calls and tool operations
    • Supports conditional logic and loop control
    • Enables complex workflow automation
  3. Memory Management:

    • Provides short-term and long-term memory storage
    • Supports conversation history maintenance
    • Enables context-aware interactions

Typical Application Scenarios

  • Intelligent assistant development
  • Data analysis
  • Automated workflows

Background Description

LangChain’s community provides many encapsulated tools that can be used directly (some require applying for and configuring API keys).


Installing Dependencies

pip install --upgrade --quiet  langchain-core langchain langchain-openai

Writing Code

To more effectively retrieve the latest information from the internet, we can use DuckDuckGoSearchRun, a powerful search tool. This tool allows us to directly call the DuckDuckGo search engine API to achieve fast, privacy-preserving web search.

DuckDuckGoSearchRun offers the following advantages:

  1. Privacy Protection: Unlike mainstream search engines, DuckDuckGo doesn’t track user search history
  2. Instant Results: Direct access to the latest search results through API calls
  3. Easy Integration: Can be easily integrated into various applications and workflows

Usage scenario examples:

  • Quickly获取 the latest public data in research projects
  • Real-time web search functionality when developing intelligent assistants
  • Regular monitoring of certain keywords’ online dynamics

Basic usage:

  1. Install required Python package: pip install duckduckgo-search
  2. Import module: from duckduckgo_search import ddg
  3. Execute search: results = ddg("search keyword", max_results=5)
  4. Process returned JSON format results

Advanced features also include:

  • Specifying number of results to return
  • Setting search region (country/area)
  • Filtering results from specific time periods
  • Getting image, video, and other multimedia search results

Code Example

search = DuckDuckGoSearchRun()

template = """turn the following user input into a search query for a search engine:

{input}"""
prompt = ChatPromptTemplate.from_template(template)

# Using GPT-4-Turbo 3.5 doesn't work well
model = ChatOpenAI(
    model="gpt-4-0125-preview"
)

chain = prompt | model | StrOutputParser() | search
message1 = chain.invoke({"input": "I'd like to figure out what games are tonight"})
print(f"message1: {message1}")

Tool List

The article also lists many tools encapsulated in the package:

  • DuckDuckGoSearchRun / DuckDuckGoSearchResults
  • GoogleSearchRun / GoogleSearchResults
  • WikipediaQueryRun
  • WolframAlphaQueryRun
  • PythonREPLTool / PythonAstREPLTool
  • Various Gmail tools
  • Various file operation tools

Running Results

message1: How to live stream NBA games tonight. Pelicans vs Thunder and Clippers vs Lakers will air on ESPN. Viewers can also stream NBA games on Sling TV...