LoginSignup
0
0

LLMChain からLCELへ

Posted at

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!"
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0