AgentAI 에이전트 기초강의 6개 중 2번째난이도 중급

LangChain 기초 — 에이전트 개발 프레임워크 시작하기

발행 읽는 시간 약 2 분

  • Agent
  • LangChain
  • LLM
  • Python
  • LCEL

강의 소개

LangChain으로 LLM 체인을 구성하고, LCEL 문법으로 파이프라인을 조합하는 방법을 배웁니다. 에이전트 개발의 기반이 되는 핵심 추상화를 익힙니다.

LangChain을 쓰는 이유#

OpenAI API를 직접 써도 에이전트를 만들 수 있습니다. 그런데 LangChain을 쓰면:

  • LLM, 도구, 프롬프트, 메모리를 표준화된 인터페이스로 조합
  • 복잡한 체인을 선언적으로 구성
  • 다양한 LLM 제공사를 동일한 코드로 사용
  • 에이전트·RAG·평가 도구 등 생태계 활용
Diagram · Mermaid

설치와 설정#

Bash
pip install langchain langchain-openai langchain-community python-dotenv
Python
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI

load_dotenv()

llm = ChatOpenAI(
    model="gpt-4o-mini",
    temperature=0.7,
    # api_key 자동으로 OPENAI_API_KEY 환경변수 사용
)

첫 번째 체인: LCEL#

LCEL (LangChain Expression Language) 은 | 연산자로 컴포넌트를 연결합니다.

Python
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser

# 1. 프롬프트 템플릿
prompt = ChatPromptTemplate.from_messages([
    ("system", "당신은 친절한 한국어 어시스턴트입니다."),
    ("human", "{question}")
])

# 2. 체인 구성 (LCEL)
chain = prompt | llm | StrOutputParser()

# 3. 실행
result = chain.invoke({"question": "파이썬이란 무엇인가요?"})
print(result)
Diagram · Mermaid

프롬프트 템플릿 심화#

Python
from langchain_core.prompts import ChatPromptTemplate

# 시스템 + 사용자 메시지
prompt = ChatPromptTemplate.from_messages([
    ("system", """당신은 {role} 전문가입니다.
다음 규칙을 따르세요: {rules}"""),
    ("human", "{question}")
])

chain = prompt | llm | StrOutputParser()

result = chain.invoke({
    "role": "파이썬 튜터",
    "rules": "초보자에게 설명하듯이, 예제 코드 포함",
    "question": "리스트 컴프리헨션이란?"
})

출력 파서: 구조화된 응답#

Python
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.pydantic_v1 import BaseModel, Field
from typing import List

class BlogIdea(BaseModel):
    title: str = Field(description="블로그 제목")
    summary: str = Field(description="한 줄 요약")
    tags: List[str] = Field(description="관련 태그 3개")

parser = JsonOutputParser(pydantic_object=BlogIdea)

prompt = ChatPromptTemplate.from_messages([
    ("system", "다음 형식으로만 응답하세요.\n{format_instructions}"),
    ("human", "주제: {topic}")
]).partial(format_instructions=parser.get_format_instructions())

chain = prompt | llm | parser

result = chain.invoke({"topic": "AI 에이전트 활용법"})
print(result.title)    # AI 에이전트로 업무 자동화하기
print(result.tags)     # ['AI', '자동화', '에이전트']

체인 연결: 순차 파이프라인#

Diagram · Mermaid
Python
from langchain_core.runnables import RunnablePassthrough

# 체인 1: 개요 생성
outline_chain = (
    ChatPromptTemplate.from_template("'{topic}' 주제의 블로그 개요를 3줄로")
    | llm
    | StrOutputParser()
)

# 체인 2: 본문 작성 (체인 1의 출력을 입력으로)
draft_chain = (
    ChatPromptTemplate.from_template("이 개요로 500자 블로그 초안 작성:\n{outline}")
    | llm
    | StrOutputParser()
)

# 순차 연결
full_chain = (
    {"topic": RunnablePassthrough()}
    | {"outline": outline_chain}
    | draft_chain
)

result = full_chain.invoke("LangChain 에이전트 입문")

스트리밍#

Python
# 스트리밍 출력
for chunk in chain.stream({"question": "머신러닝이란?"}):
    print(chunk, end="", flush=True)

# 비동기 스트리밍
async def stream_answer(question: str):
    async for chunk in chain.astream({"question": question}):
        print(chunk, end="", flush=True)

메모리: 대화 기록 관리#

Python
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory

# 세션별 히스토리 저장소
store = {}

def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = ChatMessageHistory()
    return store[session_id]

# 프롬프트에 히스토리 추가
prompt_with_history = ChatPromptTemplate.from_messages([
    ("system", "당신은 친절한 어시스턴트입니다."),
    ("placeholder", "{chat_history}"),  # 대화 기록 삽입
    ("human", "{input}")
])

chain_with_history = RunnableWithMessageHistory(
    prompt_with_history | llm | StrOutputParser(),
    get_session_history,
    input_messages_key="input",
    history_messages_key="chat_history"
)

# 같은 session_id로 대화 유지
config = {"configurable": {"session_id": "user-123"}}

print(chain_with_history.invoke({"input": "내 이름은 철수야"}, config=config))
print(chain_with_history.invoke({"input": "내 이름이 뭐야?"}, config=config))
# → "철수님이라고 하셨습니다."

LCEL 핵심 메서드#

메서드설명사용 상황
.invoke()동기 단건 실행일반 호출
.stream()동기 스트리밍CLI 출력
.batch()여러 입력 일괄 처리대량 처리
.ainvoke()비동기 단건FastAPI 등
.astream()비동기 스트리밍웹 서비스

다음 단계: LangGraph#

복잡한 멀티스텝 에이전트는 LangChain 체인만으로 한계가 있습니다. LangGraph는 상태 기반 그래프로 더 복잡한 흐름을 표현합니다.

Diagram · Mermaid

다음 편에서 LangGraph로 실제 에이전트를 구현합니다.


정리#

개념내용
LCEL`
ChatPromptTemplate시스템·사용자 메시지 템플릿 구성
OutputParserLLM 응답을 원하는 형식으로 파싱
RunnableWithMessageHistory세션별 대화 기록 자동 관리
.stream() / .astream()실시간 스트리밍 출력

다음 편에서는 도구 설계와 에이전트 구현 — LangGraph로 실제 도구를 사용하는 에이전트를 만듭니다.

Follow · Ask · Work

이 글이 도움이 됐다면

짧은 작업 기록과 데모는 인스타그램에 올립니다. 만들고 싶은 것이 있다면 프로젝트 의뢰나 협업 제안도 편하게 보내 주세요.

글쓴이

파란여우

직접 만든 AI 앱·제품의 개발·배포를 공개하는 실전형 테크 크리에이터

파란여우 소개 Instagram @bluefox._.hi

이어서 보기

만들고 싶은 것이 있다면 프로젝트 의뢰로 알려 주세요.