4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[翻訳] LangChainのLLM

4
Posted at

LLM | 🦜️🔗 Langchainの翻訳です。

本書は抄訳であり内容の正確性を保証するものではありません。正確な内容に関しては原文を参照ください。

LLMChainは、言語モデルに幾つかの機能を追加するシンプルなチェーンです。これは、他のチェーンやエージェントでの利用を含み、LangChainで広く使用されます。

LLMChainは、PromptTemplateと言語モデル(LLMやチャットモデル)から構成されます。指定された入力キーバリュー(利用できる場合にはメモリーキーバリューも)を用いて、プロンプトテンプレートをフォーマットし、フォーマットされた文字列をLLMに渡し、LLMのアウトプットを返却します。

使い始める

from langchain import PromptTemplate, OpenAI, LLMChain

prompt_template = "What is a good name for a company that makes {product}?"

llm = OpenAI(temperature=0)
llm_chain = LLMChain(
    llm=llm,
    prompt=PromptTemplate.from_template(prompt_template)
)
llm_chain("colorful socks")
    {'product': 'colorful socks', 'text': '\n\nSocktastic!'}

LLMチェーンを実行する別の方法

すべてのChainオブジェクトで共有されている、__call__runメソッドとは別に、LLMChainでは、チェーンのロジックを呼び出す別の方法が提供されています:

  • applyによって、入力リストに対してチェーンを実行することができます:
input_list = [
    {"product": "socks"},
    {"product": "computer"},
    {"product": "shoes"}
]

llm_chain.apply(input_list)
    [{'text': '\n\nSocktastic!'},
     {'text': '\n\nTechCore Solutions.'},
     {'text': '\n\nFootwear Factory.'}]
  • generateapplyと似ていますが、文字列ではなくLLMResultを返却します。多くの場合、LLMResultには、トークンの使用量や終了理由のような有用な生成結果が含まれます。
llm_chain.generate(input_list)
    LLMResult(generations=[[Generation(text='\n\nSocktastic!', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\n\nTechCore Solutions.', generation_info={'finish_reason': 'stop', 'logprobs': None})], [Generation(text='\n\nFootwear Factory.', generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {'prompt_tokens': 36, 'total_tokens': 55, 'completion_tokens': 19}, 'model_name': 'text-davinci-003'})
  • predictrunメソッドと似ていますが、入力のキーはPythonのdictではなく、キーワード引数として指定されます。
# Single input example
llm_chain.predict(product="colorful socks")
    '\n\nSocktastic!'
# Multiple inputs example

template = """Tell me a {adjective} joke about {subject}."""
prompt = PromptTemplate(template=template, input_variables=["adjective", "subject"])
llm_chain = LLMChain(prompt=prompt, llm=OpenAI(temperature=0))

llm_chain.predict(adjective="sad", subject="ducks")
    '\n\nQ: What did the duck say when his friend died?\nA: Quack, quack, goodbye.'

出力のパーシング

デフォルトでは、LLMChainは背後のpromptオブジェクトに出力パーサーがあったとしても、出力をパースしません。LLMの出力に出力パーサーを適用したい場合には、predictではなくpredict_and_parseapplyではなくapply_and_parseを使用してください。

predictを用いて:

from langchain.output_parsers import CommaSeparatedListOutputParser

output_parser = CommaSeparatedListOutputParser()
template = """List all the colors in a rainbow"""
prompt = PromptTemplate(template=template, input_variables=[], output_parser=output_parser)
llm_chain = LLMChain(prompt=prompt, llm=llm)

llm_chain.predict()
    '\n\nRed, orange, yellow, green, blue, indigo, violet'

predict_and_parserを用いて:

llm_chain.predict_and_parse()
    ['Red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']

文字列からの初期化

文字列のテンプレートから、直接LLMChainを構築することもできます。

template = """Tell me a {adjective} joke about {subject}."""
llm_chain = LLMChain.from_string(llm=llm, template=template)

llm_chain.predict(adjective="sad", subject="ducks")
    '\n\nQ: What did the duck say when his friend died?\nA: Quack, quack, goodbye.'
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?