🦜️🔗LangChain : ユースケース : チャットボット (翻訳/解説)
翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 09/06/2023
* 本ページは、LangChain の以下のドキュメントを翻訳した上で適宜、補足説明したものです:
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
🦜️🔗 LangChain : ユースケース : チャットボット
ユースケース
チャットボットは LLM の中心的なユースケースの一つです。チャットボットの中核の機能は、長く続く会話を持てることと、ユーザが知りたい情報へのアクセスを持てることです。
基本的なプロンプティングと LLM を別にすれば、メモリと検索取得 (retrieval) がチャットボットの中核コンポーネントです。メモリはチャットボットが過去の相互作用を記憶することを可能にし、検索取得はチャットボットに最新の、ドメイン固有な情報を提供します。
概要
チャットモデルのインターフェイスは raw テキストではなくメッセージに基づいています。幾つかのコンポーネントがチャットについて考慮するために重要です :
- chat model: チャットモデル統合のリストについては ここ を、そして LangChain のチャットモデル・インターフェイスのドキュメントについては ここ をご覧ください。チャットボットのために LLM を (ここ を参照) 使用することもできますが、チャットモデルはより会話的なトーンを持ち、メッセージインターフェイスをネイティブでサポートしています。
- prompt template: プロンプトテンプレートは、デフォルトメッセージ、ユーザ入力、チャット履歴、そして (オプションで) 追加の検索取得されたコンテキストを組み合わせたプロンプトを編集する (assemble) ことを簡単にします。
- memory: メモリの種類の詳細なドキュメントについては こちらをご覧ください。
- retriever (オプション): retrieval システムの詳細なドキュメントについては こちらをご覧ください。ドメイン固有の知識を持つチャットボットを構築したい場合にこれらは役立ちます。
クイックスタート
ここにチャットボット・インターフェイスを作成する方法の素早いプレビューがあります。最初に幾つかの依存関係をインストールして必要なクレデンシャルを設定します :
pip install langchain openai
# Set env var OPENAI_API_KEY or load from a .env file:
# import dotenv
# dotenv.load_dotenv()
プレーンなチャットモデルを使用して、一つまたはそれ以上のメッセージをモデルに渡すことによりチャット補完を取得できます。
チャットモデルはメッセージで応答します。
from langchain.schema import (
AIMessage,
HumanMessage,
SystemMessage
)
from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI()
chat([HumanMessage(content="Translate this sentence from English to French: I love programming.")])
API リファレンス :
AIMessage(content="J'adore la programmation.", additional_kwargs={}, example=False)
And if we pass in a list of messages:
messages = [
SystemMessage(content="You are a helpful assistant that translates English to French."),
HumanMessage(content="I love programming.")
]
chat(messages)
AIMessage(content="J'adore la programmation.", additional_kwargs={}, example=False)
そしてチャットモデルを ConversationChain でラップすることができます、これは過去のユーザ入力とモデル出力を記憶するための組み込みメモリを持ちます。
from langchain.chains import ConversationChain
conversation = ConversationChain(llm=chat)
conversation.run("Translate this sentence from English to French: I love programming.")
API リファレンス :
'Je adore la programmation.'
conversation.run("Translate it to German.")
'Ich liebe Programmieren.'
メモリ
上述のように、チャットボットの中核コンポーネントはメモリシステムです。メモリの最も単純で最も一般に使用される形式の一つは ConversationBufferMemory です :
- このメモリはメッセージのバッファ内のストアを可能にします。
- チェインで呼び出されるとき、それはストアしたメッセージのすべてを返します。
LangChain はまた多くの他の種類のメモリも装備しています。メモリの種類の詳細なドキュメントについては ここをご覧ください。
取り敢えずは ConversationBufferMemory を素早く見てみましょう。このように幾つかのチャットメッセージをメモリに手動で追加できます :
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.chat_memory.add_user_message("hi!")
memory.chat_memory.add_ai_message("whats up?")
API リファレンス :
そしてメモリからロードできるようになります。すべての Memory クラスにより公開される主要メソッドは load_memory_variables です。これは初期チェイン入力を受け取り、メモリ変数のリストを返します、これはチェイン入力に追加されます。
この単純なメモリタイプはメモリをロードするときチェイン入力を実際には考慮していませんので、取り敢えずは空の入力を渡すことができます :
memory.load_memory_variables({})
{'history': 'Human: hi!\nAI: whats up?'}
ConversationBufferWindowMemory を使用して最も最近の k 個の相互作用のスライディングウィンドウを保持することもできます。
from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=1)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})
memory.load_memory_variables({})
API リファレンス :
{'history': 'Human: not much you\nAI: not much'}
ConversationSummaryMemory はこのテーマの拡張です。
時間とともに会話の要約を作成します。
このメモリは長い会話に対して最も有用です、そこでは完全なメッセージ履歴は多くのトークンを消費します。
from langchain.llms import OpenAI
from langchain.memory import ConversationSummaryMemory
llm = OpenAI(temperature=0)
memory = ConversationSummaryMemory(llm=llm)
memory.save_context({"input": "hi"},{"output": "whats up"})
memory.save_context({"input": "im working on better docs for chatbots"},{"output": "oh, that sounds like a lot of work"})
memory.save_context({"input": "yes, but it's worth the effort"},{"output": "agreed, good docs are important!"})
API リファレンス :
memory.load_memory_variables({})
{'history': '\nThe human greets the AI, to which the AI responds. The human then mentions they are working on better docs for chatbots, to which the AI responds that it sounds like a lot of work. The human agrees that it is worth the effort, and the AI agrees that good docs are important.'}
ConversationSummaryBufferMemory はこれを少し更に拡張しています:
それは相互作用をいつフラッシュするか決定するために相互作用の数ではなくトークン長を使用します。
from langchain.memory import ConversationSummaryBufferMemory
memory = ConversationSummaryBufferMemory(llm=llm, max_token_limit=10)
memory.save_context({"input": "hi"}, {"output": "whats up"})
memory.save_context({"input": "not much you"}, {"output": "not much"})
API リファレンス :
会話
ConversationChain で内部的に在るものをアンパックできます。
メモリ, ConversationSummaryMemory を指定してプロンプトを指定することができます。
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain
# LLM
llm = ChatOpenAI()
# Prompt
prompt = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template(
"You are a nice chatbot having a conversation with a human."
),
# The `variable_name` here is what must align with memory
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{question}")
]
)
# Notice that we `return_messages=True` to fit into the MessagesPlaceholder
# Notice that `"chat_history"` aligns with the MessagesPlaceholder name
memory = ConversationBufferMemory(memory_key="chat_history",return_messages=True)
conversation = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=memory
)
# Notice that we just pass in the `question` variables - `chat_history` gets populated by memory
conversation({"question": "hi"})
API リファレンス:
- ChatPromptTemplate
- MessagesPlaceholder
- SystemMessagePromptTemplate
- HumanMessagePromptTemplate
- LLMChain
> Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi > Finished chain. {'question': 'hi', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False)], 'text': 'Hello! How can I assist you today?'}
conversation({"question": "Translate this sentence from English to French: I love programming."})
> Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi AI: Hello! How can I assist you today? Human: Translate this sentence from English to French: I love programming. > Finished chain. {'question': 'Translate this sentence from English to French: I love programming.', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False), HumanMessage(content='Translate this sentence from English to French: I love programming.', additional_kwargs={}, example=False), AIMessage(content='Sure! The translation of "I love programming" from English to French is "J\'adore programmer."', additional_kwargs={}, example=False)], 'text': 'Sure! The translation of "I love programming" from English to French is "J\'adore programmer."'}
conversation({"question": "Now translate the sentence to German."})
> Entering new LLMChain chain... Prompt after formatting: System: You are a nice chatbot having a conversation with a human. Human: hi AI: Hello! How can I assist you today? Human: Translate this sentence from English to French: I love programming. AI: Sure! The translation of "I love programming" from English to French is "J'adore programmer." Human: Now translate the sentence to German. > Finished chain. {'question': 'Now translate the sentence to German.', 'chat_history': [HumanMessage(content='hi', additional_kwargs={}, example=False), AIMessage(content='Hello! How can I assist you today?', additional_kwargs={}, example=False), HumanMessage(content='Translate this sentence from English to French: I love programming.', additional_kwargs={}, example=False), AIMessage(content='Sure! The translation of "I love programming" from English to French is "J\'adore programmer."', additional_kwargs={}, example=False), HumanMessage(content='Now translate the sentence to German.', additional_kwargs={}, example=False), AIMessage(content='Certainly! The translation of "I love programming" from English to German is "Ich liebe das Programmieren."', additional_kwargs={}, example=False)], 'text': 'Certainly! The translation of "I love programming" from English to German is "Ich liebe das Programmieren."'}
LangSmith トレースを使用してプロンプトに保存されていたチャット履歴を見ることができます。
チャット検索
さて、他の知識のソースや ドキュメントを使用してチャット したいと仮定します。
これはポピュラーなユースケースで、チャットを ドキュメント検索 と組み合わせます。
それはモデルがその上で訓練されていない特定の情報でチャットすることを可能にします。
pip install tiktoken chromadb
ブログ記事をロードします。
from langchain.document_loaders import WebBaseLoader
loader = WebBaseLoader("https://lilianweng.github.io/posts/2023-06-23-agent/")
data = loader.load()
API リファレンス :
これを分割してベクトルにストアします。
from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=0)
all_splits = text_splitter.split_documents(data)
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import Chroma
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
API リファレンス :
前のようにメモリを作成しますが、ConversationSummaryMemory を使用しましょう。
memory = ConversationSummaryMemory(llm=llm,memory_key="chat_history",return_messages=True)
from langchain.chat_models import ChatOpenAI
from langchain.chains import ConversationalRetrievalChain
llm = ChatOpenAI()
retriever = vectorstore.as_retriever()
qa = ConversationalRetrievalChain.from_llm(llm, retriever=retriever, memory=memory)
API リファレンス :
qa("How do agents use Task decomposition?")
{'question': 'How do agents use Task decomposition?', 'chat_history': [SystemMessage(content='', additional_kwargs={})], 'answer': 'Agents can use task decomposition in several ways:\n\n1. Simple prompting: Agents can use Language Model based prompting to break down tasks into subgoals. For example, by providing prompts like "Steps for XYZ" or "What are the subgoals for achieving XYZ?", the agent can generate a sequence of smaller steps that lead to the completion of the overall task.\n\n2. Task-specific instructions: Agents can be given task-specific instructions to guide their planning process. For example, if the task is to write a novel, the agent can be instructed to "Write a story outline." This provides a high-level structure for the task and helps in breaking it down into smaller components.\n\n3. Human inputs: Agents can also take inputs from humans to decompose tasks. This can be done through direct communication or by leveraging human expertise. Humans can provide guidance and insights to help the agent break down complex tasks into manageable subgoals.\n\nOverall, task decomposition allows agents to break down large tasks into smaller, more manageable subgoals, enabling them to plan and execute complex tasks efficiently.'}
qa("What are the various ways to implemet memory to support it?")
{'question': 'What are the various ways to implemet memory to support it?', 'chat_history': [SystemMessage(content='The human asks how agents use task decomposition. The AI explains that agents can use task decomposition in several ways, including simple prompting, task-specific instructions, and human inputs. Task decomposition allows agents to break down large tasks into smaller, more manageable subgoals, enabling them to plan and execute complex tasks efficiently.', additional_kwargs={})], 'answer': 'There are several ways to implement memory to support task decomposition:\n\n1. Long-Term Memory Management: This involves storing and organizing information in a long-term memory system. The agent can retrieve past experiences, knowledge, and learned strategies to guide the task decomposition process.\n\n2. Internet Access: The agent can use internet access to search for relevant information and gather resources to aid in task decomposition. This allows the agent to access a vast amount of information and utilize it in the decomposition process.\n\n3. GPT-3.5 Powered Agents: The agent can delegate simple tasks to GPT-3.5 powered agents. These agents can perform specific tasks or provide assistance in task decomposition, allowing the main agent to focus on higher-level planning and decision-making.\n\n4. File Output: The agent can store the results of task decomposition in files or documents. This allows for easy retrieval and reference during the execution of the task.\n\nThese memory resources help the agent in organizing and managing information, making informed decisions, and effectively decomposing complex tasks into smaller, manageable subgoals.'}
Again, we can use the LangSmith trace to explore the prompt structure.
以上