작성 당시 LangChain 연결 방식을 읽은 기록이다. 명백한 shell 문법·import 오타와 타입 표기를 교정했다. provider 통합은 별도 패키지이며 설치 버전을 맞춰야 한다. 아래는 네트워크 호출을 포함한 실행 검증 가이드가 아닌 인터페이스 예시다. LangServe 예시는 당시 구성으로 구분한다.

1. langchain 구성 요소 및 사전 세팅

이 글은 작성 당시 LangChain 문서에서 모델·프롬프트·출력 파서를 연결하는 방법을 정리한 메모다. 패키지 경로와 API는 사용 버전의 문서와 대조해야 한다.

환경 설정

  1. 환경 세팅을 실시하기 위해 langchain과 openai의 SDK를 설치
`pip install langchain langchain-openai
`
  1. 환경 변수 설정
`export OPENAI_API_KEY="YOUR_API_KEY"
`

2.2) 변수로 api 세팅 2.3) Jupyter notebook 활용 시, 환경 변수 설정

  1. import os os.environ["OPENAI_API_KEY"] = "..."
  2. api_key = "sk-..." chat_model = ChatOpenAI(openai_api_key=api_key)
  3. 2.1) export를 활용한 api 세팅

2. Simple start

LangSmith

모델 호출이 여러 chain과 agent로 이어지면 각 단계의 실행을 추적할 필요가 있다. 이 예시에서는 LangSmith의 tracing 설정을 사용한다.

`export LANGCHAIN_TRACING_V2="true"
export LANGCHAIN_API_KEY=...
`

LangServe

LangServe는 chain을 REST API 경로에 연결하는 예시에서 사용한다.

`pip install "Langserve[all]"
`

LangChain 구축

LangChain의 모듈은 독립적으로 사용하거나 LCEL로 연결할 수 있다. 아래에서는 프롬프트, 모델, 출력 파서로 구성한 흐름을 정리한다.

여기서 가장 단순하고 가장 일반적인 체인에는 세 가지가 포함됩니다.

  • LLM/Chat Model: 여기서 언어 모델은 핵심 추론 엔진입니다. LangChain을 사용하려면 다양한 유형의 언어 모델과 이를 사용하는 방법을 이해해야 합니다.
  • Prompt Template: 언어 모델에 대한 지침을 제공합니다. 이는 언어 모델의 출력을 제어하므로 프롬프트와 다양한 프롬프트 전략을 구성하는 방법을 이해하는 것이 중요합니다.
  • Output Parser: 언어 모델의 원시 응답을 보다 실행 가능한 형식으로 변환하여 출력 다운스트림을 쉽게 사용할 수 있도록 합니다.

LLM / Chat Model

여기에는 LLM과 ChatModel이라는 두 가지 언어 모델을 제공해줍니다.

  • LLM : 문자열을 입력하고 문자열을 반환
  • ChatModel: 메세지의 리스트를 입력하고 메세지로 반환

기본 메시지 인터페이스는 메시지 객체로 내용을 전달합니다. 다음은 역할과 내용의 개념을 설명한 것입니다.

  • content: 문자열뿐 아니라 모델이 지원하는 콘텐츠 블록 목록을 포함할 수 있음
  • role: user·assistant·system 등 메시지 역할의 개념. Python 객체에서는 구체적인 메시지 클래스나 type으로 구분

LangChain은 서로 다른 역할을 쉽게 구별할 수 있는 여러 개체를 제공합니다.

  • HumanMessage : human/user에게 오는 BaseMessage
  • AIMessage : AI/assiatant에게 오는 BaseMessage
  • SystemMessage : system에게 오는 BaseMessage
  • FunctionMessage / ToolMessage : 함수나 tool 호출을 출력을 포함하는 BaseMessage

이러한 역할 중 어느 것도 적절하지 않은 경우 ChatMessage역할을 수동으로 지정할 수 있는 클래스도 있습니다.

Langchain은 LLM과 ChatModel을 같이 제공하는 것이 가능하지만 효과적인 프롬프트 작성을 위해서는 두 가지의 차이점을 인지하는 것이 좋습니다.

이 예시에서는 .invoke()로 LLM 또는 ChatModel을 호출한다.

  • LLM.invoke : 문자열로 입력 받아 문자열로 반환합니다.
  • ChatModel.invoke : BaseMessage의 리스트를 입력하고 BaseMessage로 반환을 해줍니다.
`from langchain_openai import OpenAI
from langchain_openai import ChatOpenAI

llm = OpenAI()
chat_model = ChatOpenAI()
`

`from langchain_core.messages import HumanMessage

text = "What would be a good company name for a company that makes colorful socks?"
messages = [HumanMessage(content=text)]

llm.invoke(text)
# >> Feetful of Fun
chat_model.invoke(messages)
# >> AIMessage(content="Socks O'Color")
`

더 알아보면 LLM.invoke와 ChatModel.invoke는 둘 다 입력으로 Union[str, List[BaseMessage], PromptValue] 제공합니다. 따라서 LLM동일한 입력을 수용 한다는 사실은 ChatModel 대부분의 chain에서 아무것도 손상 하지 않고 서로 직접 교환할 수 있다는 것을 의미합니다. 다만, 입력이 강제되는 방식과 이것이 모델 성능에 어떤 영향을 미칠 수 있는지 생각하는 것이 중요합니다.

Prompt templates

대부분의 LLM 응용 프로그램은 사용자 입력을 LLM에 직접 전달하지 않습니다. 일반적으로 현재 특정 작업에 대한 추가 Context를 제공하는 Prompt templates이라는 더 큰 텍스트에 사용자 입력을 추가합니다. LangChain에서는 Prompt template이 바로 이 작업에 도움이 되며 사용자 입력을 완전히 형식화된 Prompt로 전환하기 위한 모든 논리를 묶습니다.

`from langchain_core.prompts import PromptTemplate

prompt = PromptTemplate.from_template("What is a good name for a company that makes {product}?")
prompt.format(product="colorful socks")
`

`What is a good name for a company that makes colorful socks?
`

위와 같이 raw string format으로 질문을 하면 장점은 부분적으로 출력이 가능합니다. 예를 들어 한 번만에 일부 변수를 형식화가 가능한데 이는 다른 프롬프트와 결합이 가능하게 됩니다. PromptTemplate도 리스트의 메세지를 생성 할 수 있습니다. 이 경우 프롬프트에는 콘텐츠에 대한 정보뿐만 아니라 각 메시지(해당 역할, 목록에서의 위치 등)도 포함됩니다. 여기서 가장 자주 일어나는 일은 ChatMessageTemplates에서의 ChatPromptTemplate입니다. 각각의 ChatMessageTemplate에서는 어떻게 ChatMessageTemplate을 구성 할 지가 나와있습니다.

`from langchain_core.prompts import ChatPromptTemplate

template = "You are a helpful assistant that translate {input_language} to {output_language}."
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
			("system", template),
			("human",human_template),
])

chat_prompt.format_messages(input_language="English", output_language="Korean", text="I love programming.")
`

`[SystemMessage(content='You are a helpful assistant that translate English to Korean.'),
 HumanMessage(content='I love programming.')]
`

Output parsers

OutputParser는 원시적인 언어 출력을 downstream에 사용이 가능하도록 변환해 줍니다. 여기에 OutputParser의 몇 가지 중요한 타입이 있습니다.

  • 모델의 텍스트 응답을 리스트·JSON 등 downstream에서 사용할 구조로 파싱
  • ChatMessage를 단지 문자열로 변환
  • 메시지(예: OpenAI 함수 호출) 외에 호출에서 반환된 추가 정보를 문자열로 변환
`from langchain_core.output_parsers import BaseOutputParser

class CommaSeparatedListOutputParser(BaseOutputParser):
    """Parse the output of an LLM call to a comma-separated list."""

    def parse(self, text: str):
        """Parse the output of an LLM call."""
        return text.strip().split(", ")

CommaSeparatedListOutputParser().parse("hi, bye")
# >> ['hi', 'bye']
`

Composing with LCEL

앞에 나온 모든 것들을 하나의 Chain으로 결합할 수 있습니다. 이 Chain은 입력 변수를 가져와 Prompt Template에 전달하여 Prompt 를 생성하고, Prompt 를 언어 모델에 전달한 다음, (선택 사항) output parser를 통해 출력을 전달합니다.

`from typing import List

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import BaseOutputParser

class CommaSeparatedListOutputParser(BaseOutputParser[List[str]]):
		"""Parse the output of an LLM call to a comma-separated list."""
		def parse(self, text:str) -> List[str]:
			"""Parse the output of an LLM call."""
			return text.strip().split(",")

template = """You are a helpful assistant who generates comma separated lists.
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
ONLY return a comma separated list, and nothing more."""
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])
chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()
chain.invoke({"text": "colors"})
`

`['red', 'blue', 'green', 'yellow', 'orange']
`

Serving with LangServe

LangServe는 LCEL chain을 REST API로 연결한다. 아래 서버 예시는 FastAPI와 Pydantic을 사용한다.

Server

서버 예시는 chain 정의, FastAPI 앱, 경로 등록의 세 부분으로 구성한다.

  1. 위와 동일한 우리의 chain의 정의
  2. FastAPI app
  3. langserve.add_routes이라는 chain에 대한 경로를 정의
`#!/usr/bin/env python
from typing import List

from fastapi import FastAPI
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import BaseOutputParser
from langserve import add_routes

# 1. Chain definition

class CommaSeparatedListOutputParser(BaseOutputParser[List[str]]):
    """Parse the output of an LLM call to a comma-separated list."""

    def parse(self, text: str) -> List[str]:
        """Parse the output of an LLM call."""
        return text.strip().split(", ")

template = """You are a helpful assistant who generates comma separated lists.
A user will pass in a category, and you should generate 5 objects in that category in a comma separated list.
ONLY return a comma separated list, and nothing more."""
human_template = "{text}"

chat_prompt = ChatPromptTemplate.from_messages([
    ("system", template),
    ("human", human_template),
])
category_chain = chat_prompt | ChatOpenAI() | CommaSeparatedListOutputParser()

# 2. App definition
app = FastAPI(
  title="LangChain Server",
  version="1.0",
  description="A simple API server using LangChain's Runnable interfaces",
)

# 3. Adding chain route
add_routes(
    app,
    category_chain,
    path="/category_chain",
)

if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="localhost", port=8000)
`

`python serve.py
`

참고 자료

티스토리에 쓴 글을 이곳의 문체와 구성에 맞춰 다시 정리했습니다. 원문 보기 ↗ 원문에 포함된 일부 이미지와 서식은 원문 링크에서 확인할 수 있습니다.