LCEL : LangChain Expression Language

chain = prompt | model | output_parser 에서 "|" 문법에 해당<br> 왜 LCEL인가? 복잡한 함수 호출식 문법에서 간단한 형식으로 변경! -> 많게는 한 두줄의 코드로도 모든 파이프라인 실행 가능<br> LLM의 프로세스 : 질의 -> 프롬프트 -> LLM -> output<br> LLM의 프로세스를 아주 직관적으로 표현한 것이 LCEL이다.

from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda

전체 프로세스

  1. 질문을 입력받는다.
  2. 프롬프트로 질문이 들어간다. (format string)
  3. 프롬프트를 모델에 전달한다.
  4. 결과를 출력한다.
from langchain.prompts import PromptTemplate

prompt = PromptTemplate.from_template("{country}의 수도는 어디야?") # {}에 들어가는 부분이 input_variable로 자동으로 추가됨.
prompt

from langchain_ollama.chat_models import ChatOllama
model = ChatOllama(
    model = "llama3.1:8B",
    temperature = 0.8, # 모델의 창의성 계수 (d=0.8)
    num_gpu = 1
)

LCEL

chain = prompt | model
# chain.invoke() # [x] : RunnableSequence.invoke() missing 1 required positional argument: 'input'
chain.invoke({"country":"대한민국"}).content

RunnablePassthrough

# 질의(RunnablePassthrough) -> 프롬프트 -> LLM 의 chain을 완성
chain = {"country":RunnablePassthrough()} | prompt | model
chain.invoke("대한민국").content