Adventure Time - Finn 3
본문 바로가기
AI/LLM

LangChain - LCEL

by hyun9_9 2026. 5. 23.

LCEL ( LangChain Expression Language : 렝체인 표현 언어)

LCEL는 LangChain 내에서 복잡한 표현식을 처맇고 모델과 상호작용을 더 강력하고 유연하게 만드는 기능을 제공

  • 코드양을 많이 줄여줌
  • 다양한 template과 LLM 호출
  • 서로 다른 응답을 함꼐 사용케함

 

chain 생성!
'|' (pipe)연산자 사용.
LangChain 의 핵심!

 

chain = template | chat | CommaOutputParser()
print(type(chain)) # Runnable 객체
chain

# <class 'langchain_core.runnables.base.RunnableSequence'>
# ChatPromptTemplate(input_variables=['max_items', 'question'], input_types={}, partial_variables={}, messages=[SystemMessagePromptTemplate(prompt=PromptTemplate(input_variables=['max_items'], input_types={}, partial_variables={}, template='\n        You are a list generating machine.\n        Everything you are asked will be answered \n        with a comma separated list of max {max_items} in lowercase.\n        Do NOT reply with anything else.\n    '), additional_kwargs={}), HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['question'], input_types={}, partial_variables={}, template='{question}'), additional_kwargs={})])
# | ChatOpenAI(output_version=None, profile={'name': 'GPT-3.5-turbo', 'release_date': '2023-03-01', 'last_updated': '2023-11-06', 'open_weights': False, 'max_input_tokens': 16385, 'max_output_tokens': 4096, 'text_inputs': True, 'image_inputs': False, 'audio_inputs': False, 'video_inputs': False, 'text_outputs': True, 'image_outputs': False, 'audio_outputs': False, 'video_outputs': False, 'reasoning_output': False, 'tool_calling': False, 'structured_output': False, 'attachment': False, 'temperature': True, 'image_url_inputs': False, 'pdf_inputs': False, 'pdf_tool_message': False, 'image_tool_message': False, 'tool_choice': True}, client=<openai.resources.chat.completions.completions.Completions object at 0x000001C70D326180>, async_client=<openai.resources.chat.completions.completions.AsyncCompletions object at 0x000001C70D80C680>, root_client=<openai.OpenAI object at 0x000001C70D80C4D0>, root_async_client=<openai.AsyncOpenAI object at 0x000001C70D7BFBC0>, temperature=0.1, model_kwargs={}, openai_api_key=SecretStr('**********'), openai_proxy=None, stream_usage=True)
# | CommaOutputParser()

 

# chain 호출. invoke({...})
chain.invoke({
    "max_items":5,
    "question":"What are the Harry Potter Spells",
})
# chain.invoke() 리턴값은? -> chain 의 마지막요소의 리턴값


# ['expelliarmus', 'lumos', 'alohomora', 'stupify', 'wingardium leviosa']

 

 

 

 chain = template | chat | CommaOutputParser()
 chain.invoke({...})
 사실 랭체인은 내부에서
   .format_message() 호출 -> prompt 완성
   -> chat.invoke() 호출 -> AIMessage 리턴
   -> parse() 호출한다

 이러한 일련의 작업을 chain.invoke() 호출 단한번으로 끝낸다.
 이러한 chain 구문으로 정말 다양한 작업의 흐름들을 수행할수 있다.

 

 chain 끼리도 결합할수 도 있다.

 [예시]
 chain_one = template | chat | CommaOutputParser()
 chain_two = template_2 | chat | OutputParser2()
 all = chain_one | chain_two | OutputParser3()
   ↑ chain_one 의 출력을 chain_two 의 입력값으로 사용 가능.

'AI > LLM' 카테고리의 다른 글

LangChain - streaming = 과 callbacks  (0) 2026.05.25
LangChain - Chaining Chains 사용해보기  (0) 2026.05.24
LangChain OutputParser  (0) 2026.05.19
LangChain 시작  (0) 2026.05.15