chain = prompt | model | output_parser 에서 "|" 문법에 해당<br> 왜 LCEL인가? 복잡한 함수 호출식 문법에서 간단한 형식으로 변경! -> 많게는 한 두줄의 코드로도 모든 파이프라인 실행 가능<br> LLM의 프로세스 : 질의 -> 프롬프트 -> LLM -> output<br> LLM의 프로세스를 아주 직관적으로 표현한 것이 LCEL이다.
각 component에 해당하는 객체들은 chain 내에서 인풋과 아웃풋 형식이 정해져있다.
from langchain_core.runnables import RunnableParallel, RunnablePassthrough, RunnableLambda
전체 프로세스
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
)
chain = prompt | model
# chain.invoke() # [x] : RunnableSequence.invoke() missing 1 required positional argument: 'input'
chain.invoke({"country":"대한민국"}).content
# 질의(RunnablePassthrough) -> 프롬프트 -> LLM 의 chain을 완성
chain = {"country":RunnablePassthrough()} | prompt | model
chain.invoke("대한민국").content
RAG에서의 RunnablePassthrough예시
chain = {"content":retriever, "question":RunnablePassthrough()} | prompt | model