LangChain 통합(Integration) 구현하기
LangChain 통합(Integration) 구현하기
LangChain의 컴포넌트는 langchain-core의 기본 클래스를 상속한 서브클래스로 만들어요. 여기서는 채팅 모델, 툴, 미들웨어, 샌드박스처럼 실제로 통합을 구현할 때 어떤 기본 클래스를 쓰고 어떤 메서드를 채워야 하는지 차근차근 살펴볼게요.
출처: 공식문서
채팅 모델 (Chat Models)
채팅 모델은 BaseChatModel 클래스의 서브클래스예요. 채팅 완성 생성, 메시지 포맷 처리, 모델 파라미터 관리 같은 메서드를 구현해야 하죠.
주의할 점이 하나 있어요. 채팅 모델의 툴 호출(tool calling) 기능에 텍스트 요청과 함께 넘길 "입력 스키마(input schema)" 또는 "args schema"를 정의해야 해요. 그래야 채팅 모델이 "툴 호출(tool call)"이나 그 툴을 호출할 파라미터를 생성할 수 있어요.
툴 (Tools)
툴 클래스는 BaseTool 기본 클래스를 상속해야 해요. 이 인터페이스는 서브클래스에서 구현해야 하는 속성(property) 3개와 메서드(method) 2개를 요구해요.
미들웨어 (Middleware)
미들웨어는 모델 호출, 툴 호출, 에이전트 생명주기 이벤트에 훅을 걸어 에이전트 동작을 커스터마이즈하게 해줘요. 미들웨어 클래스는 AgentMiddleware 기본 클래스를 상속해요.
통합을 만들기 전에 커스텀 미들웨어 가이드를 읽고 훅, 상태 업데이트, 미들웨어 패턴을 먼저 이해해 두는 게 좋아요. 미들웨어 통합은 크게 두 갈래로 나뉘어요. 체크포인터(checkpointer) 통합 예시는 LangGraph 저장소에서 구현 사례를 찾아볼 수 있어요.
샌드박스 (Sandboxes)
샌드박스 통합은 Deep Agents가 코드를 격리된 환경에서 실행할 수 있게 해줘요. Deep Agents의 SandboxBackendProtocol을 구현하면 되는데, 이 프로토콜에는 execute(), async 변형, 그리고 ls, read, write, edit, glob, grep 같은 파일시스템 툴 메서드가 포함돼 있어요.
실제로는 샌드박스 환경이 셸 명령을 실행할 수 있고 python3이 있다면, 보통 BaseSandbox를 서브클래스로 두는 게 일반적이에요. BaseSandbox가 python3을 통해 파일시스템 연산을 제공하니까, 주로 execute(), upload_files(), download_files(), id만 구현하면 돼요.
from __future__ import annotations
from deepagents.backends.protocol import (
ExecuteResponse,
FileDownloadResponse,
FileUploadResponse,
)
from deepagents.backends.sandbox import BaseSandbox # [!code highlight]
class MySandbox(BaseSandbox):
def __init__(self, client: MySandboxSdkClient) -> None:
self._client = client
@property
# ... (execute, upload_files, download_files, id 구현)
통합을 검증하려면 샌드박스 표준 테스트 스위트를 이용해요. Python 쪽은 langchain_tests.integration_tests의 SandboxIntegrationTests를 사용하는데, 서브클래스를 만들고 깨끗한 샌드박스를 반환하는 sandbox 픽스처를 제공하면 돼요.
from deepagents.backends.protocol import SandboxBackendProtocol
from langchain_tests.integration_tests import SandboxIntegrationTests
from langchain_myprovider import MySandbox
from myprovider_sdk import MySandboxSdkClient
class TestMySandboxStandard(SandboxIntegrationTests):
@pytest.fixture(scope="class")
def sandbox(self) -> Iterator[SandboxBackendProtocol]:
client = MySandboxSdkClient()
backend = MySandbox(client=client)
try:
yield backend
finally:
# Replace this with your provider's cleanup logic.
...
참고할 구현으로 Daytona 파트너 통합이 있어요. BaseSandbox를 서브클래스로 두고 execute(), upload_files(), download_files(), id를 구현한 예시예요.
더 알아보기 (Learn more)
- LangChain contributors 문서 — 통합 기여 방법 전반
- Middleware 커스텀 가이드 — 훅·상태 업데이트·패턴 전체 레퍼런스