すべてのプロダクト
Search
ドキュメントセンター

Hologres:基盤モジュールと Hologres に基づいて専用のチャットボットを無料でカスタマイズする

最終更新日:Apr 09, 2025

このトピックでは、モデルのトレーニングを必要とせずに、Hologres のベクトル計算機能に基づいて専用のチャットボットを無料でカスタマイズする方法について説明します。

背景情報

基盤モデルは、ほとんどのシナリオで使用できます。基盤モデルをトレーニングするか、プロンプトチューニング方式を使用することで、基盤モデルベースのチャットボットをカスタマイズできます。プロンプトチューニング方式を使用して特定の業界専用のチャットボットをカスタマイズする場合、チャットボットは、業界固有のコーパスと基盤モデルの読解力と情報統合機能に基づいて、業界関連の質問に対する正確な回答を提供できます。

Hologres は、オールインワンのリアルタイムデータウェアハウスエンジンです。 Hologres は Proxima と緊密に統合されており、効率的で使いやすいベクトル計算機能を提供します。 Proxima は、Alibaba DAMO Academy によって開発された高性能ベクトル処理ソフトウェアライブラリです。 詳細については、「Proxima に基づくベクトル処理」をご参照ください。

Hologres をリアルタイムのベクトルストレージおよび検索エンジンとして使用できます。 Hologres はカスタマイズされたコーパスをベクトルとして保存し、基盤モデルにベクトルを提供します。 このようにして、チャットボットをすばやくカスタマイズできます。

前提条件

  • 基盤モデルが利用可能です。 すべてのタイプの基盤モデルがサポートされています。

  • Hologres インスタンスが購入されています。

    説明

    数百万のベクトルを処理できる 8 コンピュートユニット(CU)の仕様の Hologres インスタンスを使用することをお勧めします。 数百万を超えるベクトルを処理する場合は、8 CU を超える Hologres インスタンスを使用します。

  • Anaconda がインストールされています。 詳細については、「Anaconda」をご参照ください。

手順

  1. 環境を構成します。

    • Anaconda で次のコマンドを実行して、依存関係をインストールします。

      conda create --name chatbot python=3.8
      conda activate chatbot
      
      pip install langchain psycopg2-binary torch transformers modelscope bottle
      pip install <Foundation model dependencies>  // 基盤モデルの依存関係
    • 基盤モデルを準備します。 この例では、API オペレーションを呼び出すことでアクセスできる基盤モデルが使用されています。 API オペレーションを呼び出すための AccessKey ペアを取得する必要があります。

    • コードとサンプルデータ をクリックしてコードとサンプルデータをダウンロードするか、次のコマンドを実行してコードとサンプルデータを複製します。

      説明
      • ダウンロードリンクからパッケージをダウンロードする場合は、パッケージを解凍してから、パッケージ内のコードとサンプルデータを使用する必要があります。

      • 次のコマンドを使用してコードとサンプルデータを複製する場合は、Git をダウンロードしてから使用する必要があります。

      git clone https://github.com/aliyun/alibabacloud-hologres-connectors.git
  2. 基盤モデルをテストします。

    Hologres 関連の質問を使用して、ネイティブの基盤モデルをテストします。

    1. holo-chatbot/chatbot.py コードの main 関数(107 行目)で、chat_model_name を使用する基盤モデルの名前に置き換えます。 サンプルコード:

      説明

      LangChain でサポートされている基盤モデルを直接使用できます。 詳細については、「チャットモデル」をご参照ください。 基盤モデルに必要なインターフェースを実装した後、別の基盤モデルを使用することもできます。

      # Select a foundation model. // 基盤モデルを選択します。
      from langchain.chat_models import <chat_model_name>
      
      chat_model = <chat_model_name>(<api_key>='xxx', <api_base>='xxx', temperature=0.1)
      bot = Chatbot(chat_model, args.clear, args.no_vector_store)
    2. 次のコマンドを実行して、基盤モデルにアクセスします。

      説明

      基盤モデルを初めて使用すると、サイズが 400 MB の埋め込みモデルが自動的にダウンロードされます。これは時間がかかります。 システムは埋め込みモデルを一度だけダウンロードします。

      # Switch to the directory where the code is stored. // コードが保存されているディレクトリに切り替えます。
      cd alibabacloud-hologres-connectors/holo-chatbot
      # Run the following command to open the configuration wizard. Press Enter to skip each configuration item. // 次のコマンドを実行して構成ウィザードを開きます。 各構成項目をスキップするには、Enter キーを押します。
      python generate_config.py
      
      # -n --no-vector-store: Hologres-based vector search is not used. Only the foundation model is used to provide answers. // -n --no-vector-store:Hologres ベースのベクトル検索は使用されません。 基盤モデルのみを使用して回答が提供されます。
      python chatbot.py -n
    3. 質問を送信し、基盤モデルから回答を取得します。

      • Question: What is Hologres? // 質問:Hologres とは何ですか?
        Chatbot: Hologres is an analytical cloud-native data warehouse that is provided by Alibaba Cloud. Hologres can handle petabytes of data and provide high-performance query and analytics capabilities. Hologres supports the SQL and PostgreSQL protocols and is deeply integrated with the Alibaba Cloud ecosystem, such as MaxCompute and DataWorks. // チャットボット:Hologres は、Alibaba Cloud によって提供される分析クラウドネイティブデータウェアハウスです。 Hologres はペタバイトのデータを処理し、高性能のクエリおよび分析機能を提供できます。 Hologres は SQL および PostgreSQL プロトコルをサポートし、MaxCompute や DataWorks などの Alibaba Cloud エコシステムと緊密に統合されています。

      • Question: What methods are available in Hologres to accelerate queries on JSONB-formatted data? // 質問:Hologres では、JSONB 形式のデータに対するクエリを高速化するためにどのような方法が利用できますか?
        Chatbot: Hologres provides Gin indexes to accelerate queries on JSONB-formatted data. Gin indexes are inverted indexes and can help quickly locate rows that contain specific JSONB-formatted key-value pairs. In Hologres, you can execute the CREATE INDEX statement to create a Gin index. Sample statement: // チャットボット:Hologres は、JSONB 形式のデータに対するクエリを高速化するために Gin インデックスを提供します。 Gin インデックスは転置インデックスであり、特定の JSONB 形式のキーと値のペアを含む行をすばやく見つけるのに役立ちます。 Hologres では、CREATE INDEX ステートメントを実行して Gin インデックスを作成できます。 ステートメントの例:
        CREATE INDEX idx_gin ON my_table USING gin(my_jsonb_column);
        In this example, a Gin index is created for the column my_jsonb_column in the my_table table. You can use query operators of JSONB-formatted data, such as ->, ->>, #>, and #>>, to query data in the column my_jsonb_column. The Gin index is used to accelerate the query. // この例では、my_table テーブルの my_jsonb_column 列に Gin インデックスが作成されます。 ->、->>、#>、#>> などの JSONB 形式のデータのクエリ演算子を使用して、my_jsonb_column 列のデータをクエリできます。 Gin インデックスは、クエリを高速化するために使用されます。
      • Question: What is column-oriented storage for JSONB-formatted data in Hologres? // 質問:Hologres における JSONB 形式のデータの列指向ストレージとは何ですか?
        Chatbot: JSONB is a data type in Hologres and supports column-oriented storage. Hologres can store JSON data based on this data type. JSON is a lightweight data interchange format that is commonly used in web applications. JSONB is a binary format of JSON, which can provide faster query speed and requires smaller storage space than JSON. In Hologres, you can store JSON data as JSONB-formatted data in column-oriented storage mode. // チャットボット:JSONB は Hologres のデータ型であり、列指向ストレージをサポートしています。 Hologres は、このデータ型に基づいて JSON データを格納できます。 JSON は、Web アプリケーションで一般的に使用される軽量のデータ交換形式です。 JSONB は JSON のバイナリ形式であり、JSON よりも高速なクエリ速度を提供し、必要なストレージ容量が少なくなります。 Hologres では、JSON データを JSONB 形式のデータとして列指向ストレージモードで格納できます。

  3. Hologres でベクトルデータを準備します。

    1. Hologres でデータベースを作成します。 詳細については、「データベースの作成」をご参照ください。

    2. 接続情報を構成します。

      次のコマンドを実行して構成ウィザードを開き、接続情報を構成します。 後で config フォルダー内の config.yaml ファイルを変更して、接続情報を更新できます。

      python generate_config.py

      次の表に、接続情報のパラメーターを示します。

      パラメーター

      説明

      HOLO_ENDPOINT

      Hologres インスタンスのエンドポイント。

      Hologres コンソール[インスタンスの詳細] ページで、Hologres インスタンスのエンドポイントを取得できます。

      HOLO_PORT

      Hologres インスタンスのポート番号。

      Hologres コンソール[インスタンスの詳細] ページで、Hologres インスタンスのポート番号を取得できます。

      HOLO_USER

      Hologres インスタンスへの接続に使用する Alibaba Cloud アカウントの AccessKey ID。

      セキュリティ管理 ページから AccessKey ID を取得できます。

      HOLO_PASSWORD

      Hologres インスタンスへの接続に使用する Alibaba Cloud アカウントの AccessKey シークレット。

      セキュリティ管理 ページから AccessKey シークレットを取得できます。

      HOLO_DATABASE

      接続するデータベースの名前。

    3. コーパスを準備します。

      この例では、ダウンロードしたコードとサンプルデータの data ディレクトリにある example_data.csv ファイルが使用されています。

      • コーパス内のファイルは CSV 形式であり、title 列と content 列が含まれている必要があります。

      • この例では、コーパスで Hologres のユーザーマニュアルが使用されています。 次の表にサンプルデータを示します。 サンプルファイルの形式に基づいてコーパスをカスタマイズできます。

        title

        content

        Who are you? // あなたは誰?

        I am Holo chatbot, an intelligent Q&A chatbot. I will do my best to answer your questions. // 私は Holo チャットボット、インテリジェントな Q&A チャットボットです。 私はあなたの質問に答えるために最善を尽くします。

        How do you work? // どのように動作しますか?

        I am developed based on the LangChain framework. LangChain is a simple and easy-to-use framework that is used to develop foundation models in Python. LangChain integrates and encapsulates a set of practical tools. My backend uses Hologres for vector storage. Hologres can store large amounts of texts and vectors and supports quick and approximate vector queries to provide related content. This way, I can provide you answers by calling API operations of the foundation model. // 私は LangChain フレームワークに基づいて開発されています。 LangChain は、Python で基盤モデルを開発するために使用されるシンプルで使いやすいフレームワークです。 LangChain は一連の実用的なツールを統合およびカプセル化します。 私のバックエンドはベクトルストレージに Hologres を使用しています。 Hologres は大量のテキストとベクトルを格納でき、関連コンテンツを提供するための迅速かつ近似的なベクトルクエリをサポートしています。 このようにして、基盤モデルの API オペレーションを呼び出すことで回答を提供できます。

        What is Hologres? // Hologres とは何ですか?

        Hologres is an all-in-one real-time data warehouse service developed by Alibaba Cloud and allows you to write, update, process, and analyze large amounts of data in real time. // Hologres は、Alibaba Cloud によって開発されたオールインワンのリアルタイムデータウェアハウスサービスであり、大量のデータをリアルタイムで書き込み、更新、処理、分析できます。

      • コーパス内のデータをベクトル化し、Hologres にインポートします。

        次のコマンドを実行して、テキスト埋め込みモデルを使用してコーパス内のデータをベクトル化し、ベクトルデータを Hologres に書き込みます。

        説明

        このトピックでは、Alibaba DAMO Academy によって開発された ModelScope プラットフォームの中国語オープンソース coRom の埋め込みモデルが使用されています。 詳細については、「coROM」をご参照ください。

        python chatbot.py -l

        変換されたベクトルは、Hologres の langchain_embedding テーブルでクエリできます。

        SELECT * FROM langchain_embedding limit 1;
        
        -- The following result is returned: // 次の結果が返されます。
        id|embedding|metadata|document
        2419815e-1407-11 ee-bee5-acde48001122 |{0.395261,0.123794,0.761932,0.413286,...}|{"source": "data/example_data.csv", "row": 1}| title: How do you work? // タイトル:どのように動作しますか? content: I am developed based on the LangChain framework. LangChain is a simple and easy-to-use framework that is used to develop foundation models in Python. LangChain integrates and encapsulates a set of practical tools. My backend uses Hologres for vector storage. Hologres can store large amounts of texts and vectors and supports quick and approximate vector queries to provide related content. This way, I can provide you answers by calling API operations of the foundation model. // コンテンツ:私は LangChain フレームワークに基づいて開発されています。 LangChain は、Python で基盤モデルを開発するために使用されるシンプルで使いやすいフレームワークです。 LangChain は一連の実用的なツールを統合およびカプセル化します。 私のバックエンドはベクトルストレージに Hologres を使用しています。 Hologres は大量のテキストとベクトルを格納でき、関連コンテンツを提供するための迅速かつ近似的なベクトルクエリをサポートしています。 このようにして、基盤モデルの API オペレーションを呼び出すことで回答を提供できます。

  4. 基盤モデルとコーパスを検証します。

    プロンプトチューニングのために、コーパス内のベクトルを基盤モデルにインポートします。 次のコマンドを実行して基盤モデルにアクセスし、同じ質問に対する回答を取得して、コーパスのインポート後の基盤モデルのパフォーマンスを検証します。

    python chatbot.py
    • 改善点 1:最適化された基盤モデルによって提供される回答は、ネイティブの基盤モデルによって提供される回答よりも正確で包括的です。

      Question: What is Hologres? // 質問:Hologres とは何ですか?
      Chatbot: Hologres is an all-in-one real-time data warehousing service developed by Alibaba Cloud. You can use Hologres to write, update, process, and analyze large amounts of data in real time. Hologres supports standard SQL syntax, is compatible with PostgreSQL, and supports most PostgreSQL functions. Hologres supports online analytical processing (OLAP) and ad hoc analysis for up to petabytes of data, and provides high-concurrency and low-latency online data services. Hologres supports fine-grained isolation of multiple workloads and enterprise-level security capabilities. Hologres is deeply integrated with MaxCompute, Realtime Compute for Apache Flink, and DataWorks, and provides full-stack online and offline data warehousing solutions for enterprises. Hologres is designed to provide a real-time data warehouse engine that delivers high performance, high reliability, cost efficiency, and high scalability. Hologres provides real-time data warehousing solutions that help manage large amounts of data and interactive query services that can respond in sub-seconds. In most cases, Hologres is used in scenarios such as construction of real-time data mid-ends, fine-grained analysis, self-service analysis, marketing profiling, audience grouping, and real-time risk control. // チャットボット:Hologres は、Alibaba Cloud によって開発されたオールインワンのリアルタイムデータウェアハウジングサービスです。 Hologres を使用すると、大量のデータをリアルタイムで書き込み、更新、処理、分析できます。 Hologres は標準 SQL 構文をサポートし、PostgreSQL と互換性があり、ほとんどの PostgreSQL 関数をサポートしています。 Hologres は、最大ペタバイトのデータのオンライン分析処理(OLAP)とアドホック分析をサポートし、高並行性と低レイテンシのオンラインデータサービスを提供します。 Hologres は、複数のワークロードのきめ細かい分離とエンタープライズレベルのセキュリティ機能をサポートしています。 Hologres は、MaxCompute、Realtime Compute for Apache Flink、および DataWorks と緊密に統合されており、企業向けのフルスタックのオンラインおよびオフラインデータウェアハウジングソリューションを提供します。 Hologres は、高性能、高信頼性、費用対効果、および高スケーラビリティを提供するリアルタイムデータウェアハウスエンジンを提供するように設計されています。 Hologres は、大量のデータの管理に役立つリアルタイムデータウェアハウジングソリューションと、サブ秒で応答できるインタラクティブクエリサービスを提供します。 ほとんどの場合、Hologres は、リアルタイムデータミッドエンドの構築、きめ細かい分析、セルフサービス分析、マーケティングプロファイリング、オーディエンスグループ化、リアルタイムリスク制御などのシナリオで使用されます。

    • 改善点 2:最適化された基盤モデルは、Hologres における JSONB 形式のデータに対する高速クエリの核心情報を理解し、Hologres の完全なバージョン情報と原則を要約できます。

      Question: What methods are available in Hologres to accelerate queries on JSONB-formatted data? // 質問:Hologres では、JSONB 形式のデータに対するクエリを高速化するためにどのような方法が利用できますか?
      Chatbot: Hologres V1.3 and later support column-oriented storage for the JSONB data type. The column-oriented storage method reduces the storage space of JSONB-formatted data and helps accelerate queries on JSONB-formatted data. After column-oriented storage is enabled for the JSONB data type, the system converts the column of the JSONB data type into multiple columns with strong schemas at the underlying layer. This way, the column in which the value that you want to query resides can be directly located to improve query performance. Column-oriented storage enables JSONB-formatted data to be stored and compressed with the same efficiency as structured data at the storage layer because the values of different keys are stored in different columns. This helps improve efficiency and reduces costs. // チャットボット:Hologres V1.3 以降では、JSONB データ型の列指向ストレージがサポートされています。 列指向ストレージ方式は、JSONB 形式のデータのストレージ容量を削減し、JSONB 形式のデータに対するクエリを高速化するのに役立ちます。 JSONB データ型に対して列指向ストレージが有効になると、システムは JSONB データ型の列を基盤レイヤーの強力なスキーマを持つ複数の列に変換します。 このようにして、クエリする値が存在する列を直接特定して、クエリのパフォーマンスを向上させることができます。 列指向ストレージでは、異なるキーの値が異なる列に格納されるため、JSONB 形式のデータをストレージレイヤーで構造化データと同じ効率で格納および圧縮できます。 これは効率の向上とコストの削減に役立ちます。

    • 改善点 3:最適化された基盤モデルは、JSONB 形式のデータ列指向ストレージ をよりよく理解し、Hologres の原則を完全に要約できます。

      Question: What is column-oriented storage for JSONB-formatted data in Hologres? // 質問:Hologres における JSONB 形式のデータの列指向ストレージとは何ですか?
      Chatbot: In column-oriented storage for the JSONB data type, JSONB-formatted data is not stored as a whole. Instead, Hologres splits key-value pairs of JSONB-formatted data and stores keys and values in different columns. This storage mode can improve query efficiency because only the required columns need to be scanned during queries. Keys that have the same values are stored in different rows. This way, data can be compressed, and less storage space is required. Hologres V1.3 and later support column-oriented storage for the JSONB data type. // チャットボット:JSONB データ型の列指向ストレージでは、JSONB 形式のデータは全体として格納されません。 代わりに、Hologres は JSONB 形式のデータのキーと値のペアを分割し、キーと値を異なる列に格納します。 このストレージモードでは、クエリ中に必要な列のみをスキャンする必要があるため、クエリ効率を向上させることができます。 同じ値を持つキーは異なる行に格納されます。 このようにして、データを圧縮でき、必要なストレージ容量が少なくなります。 Hologres V1.3 以降では、JSONB データ型の列指向ストレージがサポートされています。

    検証結果は、最適化された基盤モデルが Hologres 関連の質問に対する正確な回答を提供することを示しています。

  5. オプション。 チャットボットを DingTalk グループに接続します。

    チャットボットを DingTalk グループに接続できます。 dingding_server.py と同様のスクリプトを実行することで、チャットボットを他のサービスに接続することもできます。

    1. カスタマイズされたチャットボットを追加するには、DingTalk グループを開きます。

      セキュリティ設定追加署名 を選択し、発信メカニズム を開き、チャットボットをデプロイするサーバーの IP アドレスとトークンを入力します。 config/config.yaml ファイルで、環境変数 DINGDING_TOKENDINGDING_SECRETWebhook アドレスの access_token フィールドと secret フィールドの値に設定します。

    2. 次のコマンドを実行して、サーバーに HTTP サービスをデプロイします。

      # The script is used to process HTTP requests from the DingTalk chatbot. The URL is http://localhost:8889/chat. // このスクリプトは、DingTalk チャットボットからの HTTP リクエストを処理するために使用されます。 URL は http://localhost:8889/chat です。
      python dingding_server.py > log.txt 2>&1 &
    3. DingTalk グループに参加してチャットを開始します。

      DingTalk グループでチャットボットに @ を付けてチャットを開始できます。