🦜️🔗LangChain : ユースケース : 要約 (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 09/08/2023
* 本ページは、LangChain の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
- 人工知能研究開発支援
- 人工知能研修サービス(経営者層向けオンサイト研修)
- テクニカルコンサルティングサービス
- 実証実験(プロトタイプ構築)
- アプリケーションへの実装
- 人工知能研修サービス
- PoC(概念実証)を失敗させないための支援
- お住まいの地域に関係なく Web ブラウザからご参加頂けます。事前登録 が必要ですのでご注意ください。
◆ お問合せ : 本件に関するお問い合わせ先は下記までお願いいたします。
- 株式会社クラスキャット セールス・マーケティング本部 セールス・インフォメーション
- sales-info@classcat.com ; Web: www.classcat.com ; ClassCatJP
🦜️🔗 LangChain : ユースケース : 要約
ユースケース
貴方がドキュメント (PDF, Notion ページ, 顧客の質問等) のセットを持ち、その内容を要約したいとします。
LLM は、テキストの理解と合成に習熟している点を考慮すると、これに対して優れたツールです。
このウォークスルーでは、LLM を使用してドキュメント要約を実行する方法を調べます。
概要
summarizer を構築するための中心的な問題はドキュメントを LLM のコンテキスト・ウィンドウに渡す方法です。このための 2 つの一般的なアプローチは :
- Stuff: すべてのドキュメントを単一のプロンプトに単純に「詰め込み」ます。これは最も単純なアプローチです (StuffDocumentsChains の詳細は ここ をご覧ください、これはこの手法のために使用されます)。
- Map-reduce: “map” ステップで各ドキュメントをそれ自身の上で要約してから、それらの要約は最終的な要約に “reduce” されます (MapReduceDocumentsChain の詳細は ここ をご覧ください、これはこの手法のために使用されます)。
クイックスタート
こっそりと (sneak) プレビューを与えると、いずれのパイプラインも単一オブジェクトでラップできます : load_summarize_chain です。
ブログ投稿を要約したいと仮定します。これを数行のコードで作成できます。
最初に環境変数を設定してパッケージをインストールします :
pip install openai tiktoken chromadb langchain
# Set env var OPENAI_API_KEY or load from a .env file
# import dotenv
# dotenv.load_dotenv()
chain_type=”stuff” を使用できます、特に以下のような大きなコンテキストウィンドウ・モデルを使用する場合 :
- 16k トークン OpenAI gpt-3.5-turbo-16k
- 100k トーク Anthropic Claude-2
chain_type=”map_reduce” or chain_type=”refine” を供給することもできます (ここ で詳細を読んでください) :
from langchain.chat_models import ChatOpenAI
from langchain.document_loaders import WebBaseLoader
from langchain.chains.summarize import load_summarize_chain
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
docs = loader.load()
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
chain = load_summarize_chain(llm, chain_type="stuff")
chain.run(docs)
API リファレンス :
- ChatOpenAI
- WebBaseLoader
- load_summarize_chain (訳注: リンク切れ)
'The article discusses the concept of building autonomous agents powered by large language models (LLMs). It explores the components of such agents, including planning, memory, and tool use. The article provides case studies and proof-of-concept examples of LLM-powered agents in various domains. It also highlights the challenges and limitations of using LLMs in agent systems.'
オプション 1. Stuff
chain_type=”stuff” で load_summarize_chain を使用するとき、StuffDocumentsChain を使用します。
チェインはドキュメントのリストを取り、それらすべてをプロンプトに挿入して、そしてそのプロンプトを LLM に渡します :
from langchain.chains.llm import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
# Define prompt
prompt_template = """Write a concise summary of the following:
"{text}"
CONCISE SUMMARY:"""
prompt = PromptTemplate.from_template(prompt_template)
# Define LLM chain
llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
llm_chain = LLMChain(llm=llm, prompt=prompt)
# Define StuffDocumentsChain
stuff_chain = StuffDocumentsChain(
llm_chain=llm_chain, document_variable_name="text"
)
docs = loader.load()
print(stuff_chain.run(docs))
API リファレンス :
The article discusses the concept of building autonomous agents powered by large language models (LLMs). It explores the components of such agents, including planning, memory, and tool use. The article provides case studies and examples of proof-of-concept demos, highlighting the challenges and limitations of LLM-powered agents. It also includes references to related research papers and provides a citation for the article.
Great! load_summarize_chain を使用して先の結果を再現することが分かります。
更に詳しく
- プロンプトは簡単にカスタマイズできます。
- llm パラメータを通して 別の LLM (e.g., Claude) を簡単に試すことができます。
オプション 2. Map-Reduce
map reduce アプローチを説明しましょう。このため、最初に LLMChain を使用して各ドキュメントを個別の要約にマップします。それから ReduceDocumentsChain を使用してそれらの要約を単一のグローバルな要約に連結します。
まず、各ドキュメントを個別の要約にマップするために使用する LLMChain を指定します。
from langchain.chains.mapreduce import MapReduceChain
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains import ReduceDocumentsChain, MapReduceDocumentsChain
llm = ChatOpenAI(temperature=0)
# Map
map_template = """The following is a set of documents
{docs}
Based on this list of docs, please identify the main themes
Helpful Answer:"""
map_prompt = PromptTemplate.from_template(map_template)
map_chain = LLMChain(llm=llm, prompt=map_prompt)
API リファレンス :
プロンプト・ハブを使用してプロンプトをストアして取得することもできます。
これは LangSmith API キー で動作します。
例えば、ここ で map プロンプトをご覧ください。
from langchain import hub
map_prompt = hub.pull("rlm/map-prompt")
map_chain = LLMChain(llm=llm, prompt=map_prompt)
ReduceDocumentsChain はドキュメントのマッピング結果を取得してそれらを単一出力に reduce する処理をします。それは汎用の CombineDocumentsChain をラップしますが、累積サイズが token_max を超えた場合にはそれを CombineDocumentsChain に渡す前にドキュメントを collapse する機能を追加しています。この例では、チェインを実際に再利用してドキュメントを連結して collapse することもできます。
そしてマップされたドキュメントで累積トークン数が 400 トークンを超えた場合、4000 トークン未満のバッチでドキュメントを StuffDocumentsChain に再帰的に渡して、バッチ処理された要約を作成します。そしてそれらのバッチ処理された要約が累計で 4000 トークン未満であれば、それらをすべて最後に StuffDocumentsChain に渡して最終的な要約を作成します。
# Reduce
reduce_template = """The following is set of summaries:
{doc_summaries}
Take these and distill it into a final, consolidated summary of the main themes.
Helpful Answer:"""
reduce_prompt = PromptTemplate.from_template(reduce_template)
# Note we can also get this from the prompt hub, as noted above
reduce_prompt = hub.pull("rlm/map-prompt")
# Run chain
reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt)
# Takes a list of documents, combines them into a single string, and passes this to an LLMChain
combine_documents_chain = StuffDocumentsChain(
llm_chain=reduce_chain, document_variable_name="doc_summaries"
)
# Combines and iteravely reduces the mapped documents
reduce_documents_chain = ReduceDocumentsChain(
# This is final chain that is called.
combine_documents_chain=combine_documents_chain,
# If documents exceed context for `StuffDocumentsChain`
collapse_documents_chain=combine_documents_chain,
# The maximum number of tokens to group documents into.
token_max=4000,
)
map & reduce チェインを一つに連結します :
# Combining documents by mapping a chain over them, then combining results
map_reduce_chain = MapReduceDocumentsChain(
# Map chain
llm_chain=map_chain,
# Reduce chain
reduce_documents_chain=reduce_documents_chain,
# The variable name in the llm_chain to put the documents in
document_variable_name="docs",
# Return the results of the map steps in the output
return_intermediate_steps=False,
)
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=1000, chunk_overlap=0
)
split_docs = text_splitter.split_documents(docs)
Created a chunk of size 1003, which is longer than the specified 1000
print(map_reduce_chain.run(split_docs))
The main themes identified in the provided set of documents are: 1. LLM-powered autonomous agent systems: The documents discuss the concept of building autonomous agents with large language models (LLMs) as the core controller. They explore the potential of LLMs beyond content generation and present them as powerful problem solvers. 2. Components of the agent system: The documents outline the key components of LLM-powered agent systems, including planning, memory, and tool use. Each component is described in detail, highlighting its role in enhancing the agent's capabilities. 3. Planning and task decomposition: The planning component focuses on task decomposition and self-reflection. The agent breaks down complex tasks into smaller subgoals and learns from past actions to improve future results. 4. Memory and learning: The memory component includes short-term memory for in-context learning and long-term memory for retaining and recalling information over extended periods. The use of external vector stores for fast retrieval is also mentioned. 5. Tool use and external APIs: The agent learns to utilize external APIs for accessing additional information, code execution, and proprietary sources. This enhances the agent's knowledge and problem-solving abilities. 6. Case studies and proof-of-concept examples: The documents provide case studies and examples to demonstrate the application of LLM-powered agents in scientific discovery, generative simulations, and other domains. These examples serve as proof-of-concept for the effectiveness of the agent system. 7. Challenges and limitations: The documents mention challenges associated with building LLM-powered autonomous agents, such as the limitations of finite context length, difficulties in long-term planning, and reliability issues with natural language interfaces. 8. Citation and references: The documents include a citation and reference section for acknowledging the sources and inspirations for the concepts discussed. Overall, the main themes revolve around the development and capabilities of LLM-powered autonomous agent systems, including their components, planning and task decomposition, memory and learning mechanisms, tool use and external APIs, case studies and proof-of-concept examples, challenges and limitations, and the importance of proper citation and references.
更に詳しく
カスタマイズ
上記で示されたように、map & reduce ステージのために LLM とプロンプトをカスタマイズできます。
現実世界のユースケース
- ユーザ相互作用 (LangChain ドキュメントに関する質問) に関する このブログ投稿 のケーススタディをご覧ください!
- ブログ投稿と関連する レポジトリ はまた要約の手段としてクラスタリングを紹介しています。
- これはstuff or map-reduce のアプローチを超えた 3 番目のパスを開きます、これは考慮するに値します。
オプション 3. Refine
Refine は map-reduce に類似しています :
refine ドキュメント・チェインは、入力ドキュメントに渡りループしてその答えを反復的に更新することによりレスポンスを構築します。各ドキュメントについて、それはすべての非ドキュメント入力、現在のドキュメント、そして最新の中間的な答えを LLM チェインに渡して新しい答えを取得します。
This can be easily run with the chain_type=”refine” specified.
chain = load_summarize_chain(llm, chain_type="refine")
chain.run(split_docs)
'The GPT-Engineer project aims to create a repository of code for specific tasks specified in natural language. It involves breaking down tasks into smaller components and seeking clarification from the user when needed. The project emphasizes the importance of implementing every detail of the architecture as code and provides guidelines for file organization, code structure, and dependencies. However, there are challenges in long-term planning and task decomposition, as well as the reliability of the natural language interface. The system has limited communication bandwidth and struggles to adjust plans when faced with unexpected errors. The reliability of model outputs is questionable, as formatting errors and rebellious behavior can occur. The conversation also includes instructions for writing the code, including laying out the core classes, functions, and methods, and providing the code in a markdown code block format. The user is reminded to ensure that the code is fully functional and follows best practices for file naming, imports, and types. The project is powered by LLM (Large Language Models) and incorporates prompting techniques from various research papers.'
プロンプトを供給して中間的なステップを返すことも可能です。
prompt_template = """Write a concise summary of the following:
{text}
CONCISE SUMMARY:"""
prompt = PromptTemplate.from_template(prompt_template)
refine_template = (
"Your job is to produce a final summary\n"
"We have provided an existing summary up to a certain point: {existing_answer}\n"
"We have the opportunity to refine the existing summary"
"(only if needed) with some more context below.\n"
"------------\n"
"{text}\n"
"------------\n"
"Given the new context, refine the original summary in Italian"
"If the context isn't useful, return the original summary."
)
refine_prompt = PromptTemplate.from_template(refine_template)
chain = load_summarize_chain(
llm=llm,
chain_type="refine",
question_prompt=prompt,
refine_prompt=refine_prompt,
return_intermediate_steps=True,
input_key="input_documents",
output_key="output_text",
)
result = chain({"input_documents": split_docs}, return_only_outputs=True)
print(result["output_text"])
L'articolo discute il concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. Esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso di strumenti. Dimostrazioni di concetto come AutoGPT mostrano la possibilità di creare agenti autonomi con LLM come controller principale. Approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Tuttavia, ci sono sfide legate alla lunghezza del contesto, alla pianificazione a lungo termine e alla decomposizione delle attività. Inoltre, l'affidabilità dell'interfaccia di linguaggio naturale tra LLM e componenti esterni come la memoria e gli strumenti è incerta. Nonostante ciò, l'uso di LLM come router per indirizzare le richieste ai moduli esperti più adatti è stato proposto come architettura neuro-simbolica per agenti autonomi nel sistema MRKL. L'articolo fa riferimento a diverse pubblicazioni che approfondiscono l'argomento, tra cui Chain of Thought, Tree of Thoughts, LLM+P, ReAct, Reflexion, e MRKL Systems.
print("\n\n".join(result["intermediate_steps"][:3]))
This article discusses the concept of building autonomous agents using LLM (large language model) as the core controller. The article explores the different components of an LLM-powered agent system, including planning, memory, and tool use. It also provides examples of proof-of-concept demos and highlights the potential of LLM as a general problem solver. Questo articolo discute del concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. L'articolo esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso degli strumenti. Vengono anche forniti esempi di dimostrazioni di proof-of-concept e si evidenzia il potenziale di LLM come risolutore generale di problemi. Inoltre, vengono presentati approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion che consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Questo articolo discute del concetto di costruire agenti autonomi utilizzando LLM (large language model) come controller principale. L'articolo esplora i diversi componenti di un sistema di agenti alimentato da LLM, inclusa la pianificazione, la memoria e l'uso degli strumenti. Vengono anche forniti esempi di dimostrazioni di proof-of-concept e si evidenzia il potenziale di LLM come risolutore generale di problemi. Inoltre, vengono presentati approcci come Chain of Thought, Tree of Thoughts, LLM+P, ReAct e Reflexion che consentono agli agenti autonomi di pianificare, riflettere su se stessi e migliorare iterativamente. Il nuovo contesto riguarda l'approccio Chain of Hindsight (CoH) che permette al modello di migliorare autonomamente i propri output attraverso un processo di apprendimento supervisionato. Viene anche presentato l'approccio Algorithm Distillation (AD) che applica lo stesso concetto alle traiettorie di apprendimento per compiti di reinforcement learning.
以上