🦜️🔗LangChain : モジュール : チェイン – 基礎 : LLM (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 09/06/2023
* 本ページは、LangChain の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
- 株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション
- sales-info@classcat.com ; Web: www.classcat.com ; ClassCatJP
🦜️🔗 LangChain : モジュール : チェイン – 基礎 : LLM
LLMChain は単純なチェインで言語モデル周りに幾つかの機能を追加します。それは他のチェインやエージェントを含めて、LangChain 全体で広く使用されています。
LLMChain は PromptTemplate と言語モデル (LLM またはチャットモデルのいずれか) から構成されます。それは提供された入力キー値 (そして利用可能であればメモリキー値も) を使用してプロンプトテンプレートを整形し、フォーマットされた文字列を LLM に渡して LLM 出力を返します。
Get started
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!'}
LLMChain を実行する追加の方法
すべての 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.'}]
- generate は apply に類似していますが、文字列の代わりに 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'})
- predict は、入力キーが Python 辞書の代わりにキーワード引数として指定されることを除いて、run メソッドに類似しています。
# 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 は基礎的なプロンプトオブジェクトが出力パーサーを持つ場合でさえも出力をパースしません。その出力パーサーを LLM 出力上で適用したい場合、predict の代わりに predict_and_parse を、そして apply の代わりに apply_and_parse を使用します。
With 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'
With predict_and_parse:
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.'
以上