260812

u·2026년 8월 12일

TIL

목록 보기
2/30

LLM 애플리케이션 및 에이전트

LangChain 에이전트·툴: create_agent 기반 Agent·Tool 구성, RAG 파이프라인·Tool Calling·MCP(Model Context Protocol) 서버 연동·Text-to-SQL 연계 실습

  • model.with_structured_output(스키마).invoke(문의)
    ㄴ넣은걸 스키마에 맞춰서 보여줘
    ㄴ변수에 담고, invoke,batch,stream 다 가능.
    ㄴㄴclassifier = model.with_structured_output(스키마)

  • 도구 만들기: @tool
    함수 위에 @tool을 붙임.
    ㄴ docstring -> 명세
    ㄴ 인자타입: .name .description .args

  • create_agent(model, [도구들])
    ㄴ :
    agent = create_agent(model, [도구1, 도구2])
    result = agent.invoke({'messages': '질문 문자열'})
    ㄴ역할정해주기: system_prompt
    ㄴ 결과를 정리해서 돌려줘: response_format = ProviderStrategy(스키마, strict=True) -> res['structurted_response']
    ㄴ실행궤적: agent.invoke({'messages': '질문'}), ToolMessage, [-1].text

  • messages
    ㄴ HumanMessage = 사용자의 질문
    ㄴ AIMessage (tool.call) = 모델이 도구를 어떤 인자로 부를지 결정
    ㄴ ToolMessage = 도구를 실제로 실행한 결과
    ㄴ AIMessage = 결과를 반영한 자연어 답변

실무형 도구
(1) 사내 데이터 조회
(2) 규칙 계산
ㄴ계산은 코드가 하는것. 모델은 어떤 계산이 필요한지 판단해서 금액과 조건을 넘겨주는 것.
(3) 외부 REST API


RAG를 LangChain 부품으로
(1) 표의 한 행을 Document 하나로 만들기
(2) 긴 글은 자르기
: RecursiveCharacterTextSplitter(chunk_size : 한 조각의 최대 길이, chunk_overlap : 끝에 얼마나 겹칠지)
(3) 글을 벡터로
: HuggingFaceEmbeddings(model_name)
ㄴembed_documents (글 목록) : 색인 - 여러 글을 한번에 벡터로
ㄴembed_query (글 하나) : 검색 - 질문 하나를 벡터로
(4) 벡터를 담고, 검색기로 꺼내기
: Chroma.from_documents(문서들, 임베딩)
벡터스토어를 as_retriever()로 감싸면 검색기(Retriever)가 됨

RAG 체인
(1) 프롬프트 만들기
: system, 자료 {context}, human, 질문 {question}
(2) 체인 조립
: {'context': faq_retriever | format_docs, 'question': RunnablePassthrough()} | rag_prompt | model | StrOutputParser()
(3) 실행
(4) 근거를 함께 돌려주기
: rag_with_sources = RunnableParallel(answer=체인, sources=검색기)

Text-to-SQL
자연어 질문 -> SQL로


  • invoke ( )
    모델(model), 구조화 모델(model.with_structured_output(...)) -> "질문" 가능
    에이전트(create_agent(...)) -> {"messages": [...]}

0개의 댓글