Beat-by-Wire
search
⌘Ctrlk
Beat-by-Wire
  • sparklesBeat by Wire (BTW)
  • AI Master Book
    • 이상치 탐지 with Python
    • 베이지안 뉴럴네트워크 (BNN) with Python
    • 그래프 뉴럴네트워크 (GNN) with Python
    • 데이터 마케팅 분석 with Python
    • CAIO 플레이북: 최고 AI 책임자 완벽 가이드
    • 강화 학습 1: Classic Reinforcement Learning
  • LLM MASTER BOOK
    • OpenAI API 쿡북 with Python
    • 기초부터 심화까지 RAG 쿡북 with Python
    • MCP 에이전트 쿡북 with Python
    • ACP 에이전트 쿡북 with Python
    • 로컬 LLM Serving with Python
    • LLM 파인튜닝-얼라이먼트 쿡북 with Python
  • AI AGENT BOOK
    • Function Calling과 Tool로 똑똑한 AI 에이전트 만들기
    • 에이전트 워크플로우 설계로 똑똑한 AI 에이전트 만들기
    • LLM Router 적용으로 똑똑한 AI 에이전트 만들기
    • Agent Skills 활용으로 똑똑한 AI 에이전트 만들기
    • 핸즈온 Google ADK with Python
    • 핸즈온 OpenAI Agents SDK with Python
    • Enable Agent로 똑똑한 AI 에이전트 만들기
  • LLMs(버전 업데이트 요망)
    • OpenAI API
    • LangChain
      • LangChain Basic
        • 1️⃣Basic Modules
        • 2️⃣Model I/O
        • 3️⃣Prompts
        • 4️⃣Chains
        • 5️⃣Agents
        • 6️⃣Tools
        • 7️⃣Memory
      • LangChain Intermediate
      • LangChain Advanced
    • LlamaIndex
    • Hugging Face
    • Agentic LLM
    • Multimodal
    • Building LLM
gitbookPowered by GitBook
block-quoteOn this pagechevron-down
  1. LLMs(버전 업데이트 요망)chevron-right
  2. LangChainchevron-right
  3. LangChain Basic

5️⃣Agents

hashtag
LangChain: Agents

LogoLangChain overview - Docs by LangChainDocs by LangChainchevron-right
LogoGoogle Search API - SerpApiSerpApichevron-right
PreviousChainschevron-leftNextToolschevron-right

Last updated 1 year ago

import os
from dotenv import load_dotenv  

load_dotenv()
api_key = os.getenv("<OPENAI_API_KEY>")
os.environ["SERPAPI_API_KEY"] = "<Serp_API_KEY>" # Serp API 가입 후 key 발급
# 패키지 설치
#%pip install google-search-results
from langchain.agents import load_tools
from langchain.chat_models import ChatOpenAI

# 도구 준비
tools = load_tools(
    tool_names=["serpapi", "llm-math"], # 도구 이름
    llm=ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0
    ) # 도구의 초기화에 사용할 LLM
)
from langchain.chains.conversation.memory import ConversationBufferMemory

# 메모리 생성
memory = ConversationBufferMemory(
    memory_key="chat_history", 
    return_messages=True
)
from langchain.agents import initialize_agent

# 에이전트 생성
agent = initialize_agent(
    agent="chat-conversational-react-description", # 에이전트 유형 설정
    llm=ChatOpenAI(
        model="gpt-3.5-turbo",
        temperature=0
    ), # 에이전트 초기화에 사용할 LLM
    tools=tools, # 도구
    memory=memory, # 메모리
    verbose=True # 상세 정보 출력
)
# 에이전트 실행
agent.run("좋은 아침입니다.")
> Entering new AgentExecutor chain...
```json
{
    "action": "Final Answer",
    "action_input": "Good morning!"
}
```

> Finished chain.





'Good morning!'
# 에이전트 실행
agent.run("우리집 반려견 이름은 보리입니다.")
> Entering new AgentExecutor chain...
```json
{
    "action": "Final Answer",
    "action_input": "우리집 반려견 이름은 보리입니다."
}
```

> Finished chain.





'우리집 반려견 이름은 보리입니다.'
# 에이전트 실행
agent.run("우리집 반려견 이름을 불러주세요.")
> Entering new AgentExecutor chain...
```json
{
    "action": "Final Answer",
    "action_input": "우리집 반려견 이름은 보리입니다."
}
```

> Finished chain.





'우리집 반려견 이름은 보리입니다.'
# 에이전트 실행
agent.run("123*4를 계산기로 계산해 주세요")
> Entering new AgentExecutor chain...
```json
{
    "action": "Calculator",
    "action_input": "123*4"
}
```
Observation: Answer: 492
Thought:```json
{
    "action": "Final Answer",
    "action_input": "492"
}
```

> Finished chain.





'492'
# 에이전트 실행
agent.run("오늘 서울의 날씨를 웹에서 검색해 주세요.")
> Entering new AgentExecutor chain...
```json
{
    "action": "Search",
    "action_input": "Today's weather in Seoul"
}
```
Observation: {'type': 'weather_result', 'temperature': '53', 'unit': 'Fahrenheit', 'precipitation': '0%', 'humidity': '91%', 'wind': '6 mph', 'location': 'Seoul, South Korea', 'date': 'Monday', 'weather': 'Cloudy'}
Thought:```json
{
    "action": "Final Answer",
    "action_input": "The weather in Seoul, South Korea on Monday is cloudy with a temperature of 53°F, 0% precipitation, 91% humidity, and wind speed of 6 mph."
}
```

> Finished chain.





'The weather in Seoul, South Korea on Monday is cloudy with a temperature of 53°F, 0% precipitation, 91% humidity, and wind speed of 6 mph.'