이 페이지는 LangChain 내에서 SearchApi Google Search API를 사용하는 방법을 다룹니다. SearchApi는 쉬운 SERP 스크래핑을 위한 실시간 SERP API입니다.

Setup

  • https://www.searchapi.io/로 이동하여 무료 계정에 가입하세요
  • API key를 받아서 환경 변수(SEARCHAPI_API_KEY)로 설정하세요

Wrappers

Utility

이 API를 래핑하는 SearchApiAPIWrapper utility가 있습니다. 이 utility를 import하려면:
from langchain_community.utilities import SearchApiAPIWrapper
Self Ask chain의 일부로 사용할 수 있습니다:
from langchain_community.utilities import SearchApiAPIWrapper
from langchain_openai import OpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

import os

os.environ["SEARCHAPI_API_KEY"] = ""
os.environ['OPENAI_API_KEY'] = ""

llm = OpenAI(temperature=0)
search = SearchApiAPIWrapper()
tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search"
    )
]

self_ask_with_search = initialize_agent(tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True)
self_ask_with_search.run("Who lived longer: Plato, Socrates, or Aristotle?")

Output

> Entering new AgentExecutor chain...
 Yes.
Follow up: How old was Plato when he died?
Intermediate answer: eighty
Follow up: How old was Socrates when he died?
Intermediate answer: | Socrates |
| -------- |
| Born | c. 470 BC Deme Alopece, Athens |
| Died | 399 BC (aged approximately 71) Athens |
| Cause of death | Execution by forced suicide by poisoning |
| Spouse(s) | Xanthippe, Myrto |

Follow up: How old was Aristotle when he died?
Intermediate answer: 62 years
So the final answer is: Plato

> Finished chain.
'Plato'

Tool

이 wrapper를 Tool로 쉽게 로드할 수도 있습니다 (Agent와 함께 사용). 다음과 같이 할 수 있습니다:
from langchain_community.agent_toolkits.load_tools import load_tools
tools = load_tools(["searchapi"])
tool에 대한 자세한 내용은 이 페이지를 참조하세요.
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.
I