LangChain의 "체인(chain) 문법", 특히 |
파이프 연산자 기반의 선언형 체인이 처음엔 매우 추상적이라 감 잡기 어렵습니다.
chain = (
{
'context': retriever_from_llm | format_docs,
'question': RunnablePassthrough()
}
| prompt
| llm
| StrOutputParser()
)
이건 LangChain Runnable 체인 구성 방식이에요.
이 방식은 말 그대로 **"데이터 흐름을 파이프로 연결한 것"**이고, 이 chain
객체는 함수처럼 동작합니다.
➡️ chain
은 callable 객체예요. 즉 chain.invoke()
를 호출하면 전체 흐름이 실행됩니다.
response = chain.invoke("카카오뱅크 실적 알려줘") # or await chain.ainvoke(...)
위에서 정의한 모든 구성요소들이 순서대로 실행됩니다:
"카카오뱅크 실적 알려줘"
→ question
으로 들어감retriever_from_llm
가 관련 문서 검색format_docs
로 텍스트 변환prompt
에 넣음 ({context}
, {question}
)llm
실행StrOutputParser()
)def chain(input_question):
docs = retriever_from_llm.get_relevant_documents(input_question)
context = format_docs(docs)
prompt_text = prompt.format(context=context, question=input_question)
llm_response = llm(prompt_text)
return llm_response.content # 또는 출력 파서 적용