Skip to content

ClasCat® AI Research

クラスキャット – 生成 AI, AI エージェント, MCP

Menu
  • ホーム
    • ClassCat® AI Research ホーム
    • クラスキャット・ホーム
  • OpenAI API
    • OpenAI Python ライブラリ 1.x : 概要
    • OpenAI ブログ
      • GPT の紹介
      • GPT ストアの紹介
      • ChatGPT Team の紹介
    • OpenAI platform 1.x
      • Get Started : イントロダクション
      • Get Started : クイックスタート (Python)
      • Get Started : クイックスタート (Node.js)
      • Get Started : モデル
      • 機能 : 埋め込み
      • 機能 : 埋め込み (ユースケース)
      • ChatGPT : アクション – イントロダクション
      • ChatGPT : アクション – Getting started
      • ChatGPT : アクション – アクション認証
    • OpenAI ヘルプ : ChatGPT
      • ChatGPTとは何ですか?
      • ChatGPT は真実を語っていますか?
      • GPT の作成
      • GPT FAQ
      • GPT vs アシスタント
      • GPT ビルダー
    • OpenAI ヘルプ : ChatGPT > メモリ
      • FAQ
    • OpenAI ヘルプ : GPT ストア
      • 貴方の GPT をフィーチャーする
    • OpenAI Python ライブラリ 0.27 : 概要
    • OpenAI platform
      • Get Started : イントロダクション
      • Get Started : クイックスタート
      • Get Started : モデル
      • ガイド : GPT モデル
      • ガイド : 画像生成 (DALL·E)
      • ガイド : GPT-3.5 Turbo 対応 微調整
      • ガイド : 微調整 1.イントロダクション
      • ガイド : 微調整 2. データセットの準備 / ケーススタディ
      • ガイド : 埋め込み
      • ガイド : 音声テキスト変換
      • ガイド : モデレーション
      • ChatGPT プラグイン : イントロダクション
    • OpenAI Cookbook
      • 概要
      • API 使用方法 : レート制限の操作
      • API 使用方法 : tiktoken でトークンを数える方法
      • GPT : ChatGPT モデルへの入力をフォーマットする方法
      • GPT : 補完をストリームする方法
      • GPT : 大規模言語モデルを扱う方法
      • 埋め込み : 埋め込みの取得
      • GPT-3 の微調整 : 分類サンプルの微調整
      • DALL-E : DALL·E で 画像を生成して編集する方法
      • DALL·E と Segment Anything で動的マスクを作成する方法
      • Whisper プロンプティング・ガイド
  • Gemini API
    • Tutorials : クイックスタート with Python (1) テキスト-to-テキスト生成
    • (2) マルチモーダル入力 / 日本語チャット
    • (3) 埋め込みの使用
    • (4) 高度なユースケース
    • クイックスタート with Node.js
    • クイックスタート with Dart or Flutter (1) 日本語動作確認
    • Gemma
      • 概要 (README)
      • Tutorials : サンプリング
      • Tutorials : KerasNLP による Getting Started
  • Keras 3
    • 新しいマルチバックエンド Keras
    • Keras 3 について
    • Getting Started : エンジニアのための Keras 入門
    • Google Colab 上のインストールと Stable Diffusion デモ
    • コンピュータビジョン – ゼロからの画像分類
    • コンピュータビジョン – 単純な MNIST convnet
    • コンピュータビジョン – EfficientNet を使用した微調整による画像分類
    • コンピュータビジョン – Vision Transformer による画像分類
    • コンピュータビジョン – 最新の MLPモデルによる画像分類
    • コンピュータビジョン – コンパクトな畳込み Transformer
    • Keras Core
      • Keras Core 0.1
        • 新しいマルチバックエンド Keras (README)
        • Keras for TensorFlow, JAX, & PyTorch
        • 開発者ガイド : Getting started with Keras Core
        • 開発者ガイド : 関数型 API
        • 開発者ガイド : シーケンシャル・モデル
        • 開発者ガイド : サブクラス化で新しい層とモデルを作成する
        • 開発者ガイド : 独自のコールバックを書く
      • Keras Core 0.1.1 & 0.1.2 : リリースノート
      • 開発者ガイド
      • Code examples
      • Keras Stable Diffusion
        • 概要
        • 基本的な使い方 (テキスト-to-画像 / 画像-to-画像変換)
        • 混合精度のパフォーマンス
        • インペインティングの簡易アプリケーション
        • (参考) KerasCV – Stable Diffusion を使用した高性能画像生成
  • TensorFlow
    • TF 2 : 初級チュートリアル
    • TF 2 : 上級チュートリアル
    • TF 2 : ガイド
    • TF 1 : チュートリアル
    • TF 1 : ガイド
  • その他
    • 🦜️🔗 LangChain ドキュメント / ユースケース
    • Stable Diffusion WebUI
      • Google Colab で Stable Diffusion WebUI 入門
      • HuggingFace モデル / VAE の導入
      • LoRA の利用
    • Diffusion Models / 拡散モデル
  • クラスキャット
    • 会社案内
    • お問合せ
    • Facebook
    • ClassCat® Blog
Menu

🦜️🔗 LangChain : ユースケース : チャットボット

Posted on 09/07/2023 by Sales Information

🦜️🔗LangChain : ユースケース : チャットボット (翻訳/解説)

翻訳 : (株)クラスキャット セールスインフォメーション
作成日時 : 09/06/2023

* 本ページは、LangChain の以下のドキュメントを翻訳した上で適宜、補足説明したものです:

  • Use cases : Chatbots

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、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
  • HumanMessage
  • SystemMessage
  • ChatOpenAI
    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 リファレンス :

  • ConversationChain
    '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 リファレンス :

  • ConversationBufferMemory

そしてメモリからロードできるようになります。すべての 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 リファレンス :

  • ConversationBufferWindowMemory
    {'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 リファレンス :

  • OpenAI
  • ConversationSummaryMemory
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 リファレンス :

  • ConversationSummaryBufferMemory

 

会話

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 リファレンス :

  • WebBaseLoader

これを分割してベクトルにストアします。

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 リファレンス :

  • RecursiveCharacterTextSplitter
  • OpenAIEmbeddings
  • Chroma

前のようにメモリを作成しますが、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 リファレンス :

  • ChatOpenAI
  • ConversationalRetrievalChain
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.

 

以上



クラスキャット

最近の投稿

  • LangGraph Platform : Get started : クイックスタート
  • LangGraph Platform : 概要
  • LangGraph : Prebuilt エージェント : ユーザインターフェイス
  • LangGraph : Prebuilt エージェント : 配備
  • LangGraph : Prebuilt エージェント : マルチエージェント

タグ

AutoGen (13) ClassCat Press Release (20) ClassCat TF/ONNX Hub (11) DGL 0.5 (14) Eager Execution (7) Edward (17) FLUX.1 (16) Gemini (20) HuggingFace Transformers 4.5 (10) HuggingFace Transformers 4.6 (7) HuggingFace Transformers 4.29 (9) Keras 2 Examples (98) Keras 2 Guide (16) Keras 3 (10) Keras Release Note (17) Kubeflow 1.0 (10) LangChain (45) LangGraph (20) MediaPipe 0.8 (11) Model Context Protocol (16) NNI 1.5 (16) OpenAI Agents SDK (8) OpenAI Cookbook (13) OpenAI platform (10) OpenAI platform 1.x (10) OpenAI ヘルプ (8) TensorFlow 2.0 Advanced Tutorials (33) TensorFlow 2.0 Advanced Tutorials (Alpha) (15) TensorFlow 2.0 Advanced Tutorials (Beta) (16) TensorFlow 2.0 Guide (10) TensorFlow 2.0 Guide (Alpha) (16) TensorFlow 2.0 Guide (Beta) (9) TensorFlow 2.0 Release Note (12) TensorFlow 2.0 Tutorials (20) TensorFlow 2.0 Tutorials (Alpha) (14) TensorFlow 2.0 Tutorials (Beta) (12) TensorFlow 2.4 Guide (24) TensorFlow Deploy (8) TensorFlow Get Started (7) TensorFlow Graphics (7) TensorFlow Probability (9) TensorFlow Programmer's Guide (22) TensorFlow Release Note (18) TensorFlow Tutorials (33) TF-Agents 0.4 (11)
2023年9月
月 火 水 木 金 土 日
 123
45678910
11121314151617
18192021222324
252627282930  
« 8月   10月 »
© 2025 ClasCat® AI Research | Powered by Minimalist Blog WordPress Theme