> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-fbfa8bee.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 2,800万件超の Hacker News の投稿とそのベクトル埋め込みを含むデータセット

# Hacker News ベクトル検索データセット

<div id="introduction">
  ## はじめに
</div>

[Hacker News dataset](https://news.ycombinator.com/) には、2,874 万件の投稿とそのベクトル埋め込みが含まれています。これらの埋め込みは、[SentenceTransformers](https://sbert.net/) のモデル [all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2) を使用して生成されています。各埋め込みベクトルの次元数は `384` です。

このデータセットは、ユーザー生成のテキストデータをもとに構築された大規模な実運用のベクトル検索アプリケーションについて、設計、サイジング、性能の各側面を確認するのに役立ちます。

<div id="dataset-details">
  ## データセットの詳細
</div>

ベクトル埋め込みを含む完全なデータセットは、ClickHouse により、単一の `Parquet` ファイルとして [S3バケット](https://clickhouse-datasets.s3.amazonaws.com/hackernews-miniLM/hackernews_part_1_of_1.parquet) で提供されています。

このデータセットに必要なストレージ容量とメモリ要件を見積もるため、まず [ドキュメント](/ja/reference/engines/table-engines/mergetree-family/annindexes) を参照してサイジングを実施することを推奨します。

<div id="steps">
  ## 手順
</div>

<Steps>
  <Step>
    ### テーブルを作成

    投稿、その埋め込み、関連する属性を保存するための `hackernews` テーブルを作成します。

    ```sql theme={null}
    CREATE TABLE hackernews
    (
        `id` Int32,
        `doc_id` Int32,
        `text` String,
        `vector` Array(Float32),
        `node_info` Tuple(
            start Nullable(UInt64),
            end Nullable(UInt64)),
        `metadata` String,
        `type` Enum8('story' = 1, 'comment' = 2, 'poll' = 3, 'pollopt' = 4, 'job' = 5),
        `by` LowCardinality(String),
        `time` DateTime,
        `title` String,
        `post_score` Int32,
        `dead` UInt8,
        `deleted` UInt8,
        `length` UInt32
    )
    ENGINE = MergeTree
    ORDER BY id;
    ```

    `id` は単なる連番の整数です。追加の属性は、[ドキュメント](/ja/reference/engines/table-engines/mergetree-family/annindexes) で説明しているように、
    後段フィルタリング/前段フィルタリングを組み合わせたベクトル類似度検索を理解するための述語に使用できます
  </Step>

  <Step>
    ### データの読み込み

    `Parquet`ファイルからデータセットを読み込むには、次のSQLステートメントを実行します。

    ```sql theme={null}
    INSERT INTO hackernews SELECT * FROM s3('https://clickhouse-datasets.s3.amazonaws.com/hackernews-miniLM/hackernews_part_1_of_1.parquet');
    ```

    テーブルに2,874万行を挿入するには、数分かかります。
  </Step>

  <Step>
    ### ベクトル類似度索引を構築する

    次のSQLを実行して、`hackernews`テーブルの`vector`カラムにベクトル類似度索引を定義し、構築します。

    ```sql theme={null}
    ALTER TABLE hackernews ADD INDEX vector_index vector TYPE vector_similarity('hnsw', 'cosineDistance', 384, 'bf16', 64, 512);

    ALTER TABLE hackernews MATERIALIZE INDEX vector_index SETTINGS mutations_sync = 2;
    ```

    索引の作成と検索に関するパラメータおよびパフォーマンス上の考慮事項については、[ドキュメント](/ja/reference/engines/table-engines/mergetree-family/annindexes)で説明しています。
    上記のステートメントでは、HNSW ハイパーパラメータ `M` と `ef_construction` に、それぞれ 64 と 512 を使用しています。
    これらのパラメータについては、索引のビルド時間と検索結果の品質を評価しながら、
    選択した値に応じて最適な値を慎重に見極める必要があります。

    2,874 万件のデータセット全体では、利用可能な CPU コア数とストレージ帯域幅によっては、索引の構築と保存に数分から 1 時間程度かかることもあります。
  </Step>

  <Step>
    ### ANN検索を実行する

    ベクトル類似度索引が構築されると、ベクトル検索クエリでは自動的にこの索引が使用されます。

    ```sql title="Query" theme={null}
    SELECT id, title, text
    FROM hackernews
    ORDER BY cosineDistance( vector, <search vector>)
    LIMIT 10

    ```

    ベクトル索引を初めてメモリに読み込む際には、数秒から数分かかることがあります。
  </Step>

  <Step>
    ### 検索クエリの埋め込みを生成する

    [Sentence Transformers](https://www.sbert.net/) は、文や段落のセマンティックな意味を捉えるための、ローカルで手軽に使える埋め込みモデルを提供します。

    このHackerNewsデータセットには、[all-MiniLM-L6-v2](https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2)モデルによって生成されたベクトル埋め込みが含まれています。

    以下に、`sentence_transformers` Pythonパッケージを使用して埋め込みベクトルをプログラム的に生成する方法を示すPythonスクリプトの例を示します。検索用の埋め込みベクトルは、`SELECT` クエリ内の [`cosineDistance()`](/ja/reference/functions/regular-functions/distance-functions#cosineDistance) 関数に引数として渡されます。

    ```python theme={null}
    from sentence_transformers import SentenceTransformer
    import sys

    import clickhouse_connect

    print("Initializing...")

    model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

    chclient = clickhouse_connect.get_client() # ClickHouse credentials here

    while True:
        # ユーザーから検索クエリを受け取る
        print("Enter a search query :")
        input_query = sys.stdin.readline();
        texts = [input_query]

        # モデルを実行して検索ベクトルを取得する
        print("Generating the embedding for ", input_query);
        embeddings = model.encode(texts)

        print("Querying ClickHouse...")
        params = {'v1':list(embeddings[0]), 'v2':20}
        result = chclient.query("SELECT id, title, text FROM hackernews ORDER BY cosineDistance(vector, %(v1)s) LIMIT %(v2)s", parameters=params)
        print("Results :")
        for row in result.result_rows:
            print(row[0], row[2][:100])
            print("---------")
    ```

    上記の Python スクリプトの実行例と類似検索の結果を以下に示します
    (上位20件の投稿はそれぞれ先頭100文字のみを表示しています) ：

    ```text theme={null}
    初期化中...

    検索クエリを入力してください：
    Are OLAP cubes useful

    「Are OLAP cubes useful」の埋め込みを生成中

    ClickHouseにクエリ中...

    結果：

    27742647 smartmic:
    slt2021: OLAP Cube is not dead, as long as you use some form of:<p>1. GROUP BY multiple fi
    ---------
    27744260 georgewfraser:A data mart is a logical organization of data to help humans understand the schema. Wh
    ---------
    27761434 mwexler:&quot;We model data according to rigorous frameworks like Kimball or Inmon because we must r
    ---------
    28401230 chotmat:
    erosenbe0: OLAP database is just a copy, replica, or archive of data with a schema designe
    ---------
    22198879 Merick:+1 for Apache Kylin, it&#x27;s a great project and awesome open source community. If anyone i
    ---------
    27741776 crazydoggers:I always felt the value of an OLAP cube was uncovering questions you may not know to as
    ---------
    22189480 shadowsun7:
    _Codemonkeyism: After maintaining an OLAP cube system for some years, I&#x27;m not that
    ---------
    27742029 smartmic:
    gengstrand: My first exposure to OLAP was on a team developing a front end to Essbase that
    ---------
    22364133 irfansharif:
    simo7: I&#x27;m wondering how this technology could work for OLAP cubes.<p>An OLAP cube
    ---------
    23292746 scoresmoke:When I was developing my pet project for Web analytics (<a href="https:&#x2F;&#x2F;github
    ---------
    22198891 js8:It seems that the article makes a categorical error, arguing that OLAP cubes were replaced by co
    ---------
    28421602 chotmat:
    7thaccount: Is there any advantage to OLAP cube over plain SQL (large historical database r
    ---------
    22195444 shadowsun7:
    lkcubing: Thanks for sharing. Interesting write up.<p>While this article accurately capt
    ---------
    22198040 lkcubing:Thanks for sharing. Interesting write up.<p>While this article accurately captures the issu
    ---------
    3973185 stefanu:
    sgt: Interesting idea. Ofcourse, OLAP isn't just about the underlying cubes and dimensions,
    ---------
    22190903 shadowsun7:
    js8: It seems that the article makes a categorical error, arguing that OLAP cubes were r
    ---------
    28422241 sradman:OLAP Cubes have been disrupted by Column Stores. Unless you are interested in the history of
    ---------
    28421480 chotmat:
    sradman: OLAP Cubes have been disrupted by Column Stores. Unless you are interested in the
    ---------
    27742515 BadInformatics:
    quantified: OP posts with inverted condition: “OLAP != OLAP Cube” is the actual titl
    ---------
    28422935 chotmat:
    rstuart4133: I remember hearing about OLAP cubes donkey&#x27;s years ago (probably not far
    ---------
    ```

    ## 要約デモアプリケーション

    上記の例では、ClickHouse を使用したセマンティック検索とドキュメントの取得を実演しました。

    非常にシンプルながら高いポテンシャルを持つGenerative AIのサンプルアプリケーションを次に紹介します。

    アプリケーションは以下の手順を実行します：

    1. ユーザーから*topic*の入力を受け付けます
    2. `SentenceTransformers` とモデル `all-MiniLM-L6-v2` を使用して、*topic* の埋め込みベクトルを生成します
    3. `hackernews` テーブルでベクトル類似度検索を使用して、関連性の高い投稿やコメントを取得します
    4. `LangChain` と OpenAI `gpt-3.5-turbo` Chat API を使用して、ステップ #3 で取得した内容を**要約**します。
       ステップ #3 で取得した投稿/コメントは、Chat API に *コンテキスト* として渡され、Generative AI における重要なつながりとなります。

    要約アプリケーションの実行例を以下に示し、その後に要約アプリケーションのコードを掲載します。アプリケーションを実行するには、環境変数 `OPENAI_API_KEY` にOpenAI APIキーを設定する必要があります。OpenAI APIキーは、[https://platform.openai.com](https://platform.openai.com) に登録後に取得できます。

    このアプリケーションは、顧客感情分析、テクニカルサポートの自動化、ユーザー会話のマイニング、法的文書、医療記録、会議の議事録、財務諸表など、複数のエンタープライズ領域に適用できるGenerative AIのユースケースを示しています。

    ```shell theme={null}
    $ python3 summarize.py

    Enter a search topic :
    ClickHouse performance experiences

    Generating the embedding for ---->  ClickHouse performance experiences

    Querying ClickHouse to retrieve relevant articles...

    Initializing chatgpt-3.5-turbo model...

    Summarizing search results retrieved from ClickHouse...

    Summary from chatgpt-3.5:
    The discussion focuses on comparing ClickHouse with various databases like TimescaleDB, Apache Spark,
    AWS Redshift, and QuestDB, highlighting ClickHouse's cost-efficient high performance and suitability
    for analytical applications. Users praise ClickHouse for its simplicity, speed, and resource efficiency
    in handling large-scale analytics workloads, although some challenges like DMLs and difficulty in backups
    are mentioned. ClickHouse is recognized for its real-time aggregate computation capabilities and solid
    engineering, with comparisons made to other databases like Druid and MemSQL. Overall, ClickHouse is seen
    as a powerful tool for real-time data processing, analytics, and handling large volumes of data
    efficiently, gaining popularity for its impressive performance and cost-effectiveness.
    ```

    上記アプリケーションのコード：

    ```python theme={null}
    print("Initializing...")

    import sys
    import json
    import time
    from sentence_transformers import SentenceTransformer

    import clickhouse_connect

    from langchain.docstore.document import Document
    from langchain.text_splitter import CharacterTextSplitter
    from langchain.chat_models import ChatOpenAI
    from langchain.prompts import PromptTemplate
    from langchain.chains.summarize import load_summarize_chain
    import textwrap
    import tiktoken

    def num_tokens_from_string(string: str, encoding_name: str) -> int:
        encoding = tiktoken.encoding_for_model(encoding_name)
        num_tokens = len(encoding.encode(string))
        return num_tokens

    model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

    chclient = clickhouse_connect.get_client(compress=False) # ここに ClickHouse の認証情報を設定

    while True:
        # ユーザーから検索クエリを取得する
        print("Enter a search topic :")
        input_query = sys.stdin.readline();
        texts = [input_query]

        # モデルを実行して、検索ベクトルまたは参照ベクトルを取得する
        print("Generating the embedding for ----> ", input_query);
        embeddings = model.encode(texts)

        print("Querying ClickHouse...")
        params = {'v1':list(embeddings[0]), 'v2':100}
        result = chclient.query("SELECT id,title,text FROM hackernews ORDER BY cosineDistance(vector, %(v1)s) LIMIT %(v2)s", parameters=params)

        # 検索結果をすべて連結する
        doc_results = ""
        for row in result.result_rows:
            doc_results = doc_results + "\n" + row[2]

        print("Initializing chatgpt-3.5-turbo model")
        model_name = "gpt-3.5-turbo"

        text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
            model_name=model_name
        )

        texts = text_splitter.split_text(doc_results)

        docs = [Document(page_content=t) for t in texts]

        llm = ChatOpenAI(temperature=0, model_name=model_name)

        prompt_template = """
    Write a concise summary of the following in not more than 10 sentences:

    {text}

    CONSCISE SUMMARY :
    """

        prompt = PromptTemplate(template=prompt_template, input_variables=["text"])

        num_tokens = num_tokens_from_string(doc_results, model_name)

        gpt_35_turbo_max_tokens = 4096
        verbose = False

        print("Summarizing search results retrieved from ClickHouse...")

        if num_tokens <= gpt_35_turbo_max_tokens:
            chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt, verbose=verbose)
        else:
            chain = load_summarize_chain(llm, chain_type="map_reduce", map_prompt=prompt, combine_prompt=prompt, verbose=verbose)

        summary = chain.run(docs)

        print(f"Summary from chatgpt-3.5: {summary}")
    ```
  </Step>
</Steps>
