1. InMemoryCache
데이터를 메모리에 캐시하는 데 사용되는 클래스
자주 조회되는 데이터를 저장하여, 같은 요청에 대한 반복적인 계산을 줄이고, 성능을 향상시킨다.
from langchain_core.prompts import PromptTemplate
from langchain_core.caches import InMemoryCache
from langchain_core.output_parsers import StrOutputParser
from langchain_core.globals import set_llm_cache
from langchain_ollama import OllamaLLM
llm = OllamaLLM(model="llama3.1")
template = """{country}의 수도는 어디입니까?"""
prompt = PromptTemplate.from_template(template=template)
chain = prompt | llm | StrOutputParser()
set_llm_cache(InMemoryCache())
while True:
country = input("나라를 입력하세요: ")
result = chain.invoke({"country": country})
print(result)
똑같은 계산의 경우 연산없이 바로 나온다.
나라를 입력하세요: 한국
서울입니다.
나라를 입력하세요: 한국
서울입니다.
반응형
'AI' 카테고리의 다른 글
[LangChain] HuggingFace API Key 발급, HuggingFace 모델 LangChain에서사용 (0) | 2025.01.13 |
---|---|
[LangChain] 출력 파서(Output Parsers) (0) | 2025.01.13 |
[LangChain] FewShotPromptTemplate (0) | 2025.01.11 |