LLMChain
from langchain.chains import LLMChain
from langchain_community.llms import OpenAI
from langchain_core.prompts import PromptTemplate
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"], template=prompt_template
)
llm = LLMChain(llm=OpenAI(), prompt=prompt)
llm.run("beautiful")
'\n\nWhy did the tomato turn red? Because it saw the salad dressing!'
以下の様なWarningが出るので、llm.invoke
を使うとwarningを回避できる。runはChainに定義されているが、Deprecatedとなっている。
LangChainDeprecationWarning: The function `run` was deprecated in LangChain 0.1.0 and will be removed in 0.2.0. Use invoke instead.
>>> llm.invoke("beautiful")
{'adjective': 'beautiful', 'text': "\n\nWhy couldn't the bicycle stand up by itself?\n\nBecause it was two-tired."}
invokeはChainに定義されている。
llm.predict(adjective="beautiful")
"\n\nWhy don't scientists trust atoms? \n\nBecause they make up everything."
LCEL
from langchain_community.llms import OpenAI
from langchain_core.prompts import PromptTemplate
prompt_template = "Tell me a {adjective} joke"
prompt = PromptTemplate(
input_variables=["adjective"], template=prompt_template
)
llm = prompt | OpenAI()
llm.invoke({"adjective": "beautiful"})
"\n\nWhy don't scientists trust atoms? \n\nBecause they make up everything!"