はじめに
この記事は株式会社ナレッジコミュニケーションが運営する クラウド AI by ナレコム Advent Calendar 2023 の6日目にあたる記事になります!
Azure AI Searchを使って、Pythonでベクトル検索を実行してみました。Azure AI Searchを使ったベクトル検索を試したい方におすすめです。
開発環境
- Azure サブスクリプション
- Azure AI Search
- Python 3.11
実装
Azure AI Searchリソースの作成
Azure Portalから「AI Search」のページに移動し、「作成」を選択します。
入力項目を埋め、「確認および作成」を選択します。
検証が完了したら、「作成」を選択します。
デプロイが完了したら、「リソースに移動」を選択します。
「設定」の「キー」タブを選択し、プライマリー管理者キーをメモします。
ライブラリのインポート、環境変数、リクエストヘッダーの設定
ここからはPythonファイルに記述していきます。まず、必要なライブラリをインポートし、環境変数、リクエストヘッダーを設定します。サンプルデータを使用するため、index_nameは「hotels-vector-quickstart」に指定してください。
1 2 3 4 5 6 7 8 9 10 11 |
<span class="kn">import</span> <span class="n">requests</span> <span class="kn">import</span> <span class="n">json</span> <span class="n">service_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s"><AI Searchのサービス名></span><span class="sh">"</span> <span class="n">index_name</span> <span class="o">=</span> <span class="sh">"</span><span class="s">hotels-vector-quickstart</span><span class="sh">"</span> <span class="n">api_key</span> <span class="o">=</span> <span class="sh">"</span><span class="s"><AI Searchのプライマリー管理者APIキー></span><span class="sh">"</span> <span class="n">headers</span> <span class="o">=</span> <span class="p">{</span> <span class="sh">"</span><span class="s">Content-Type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">application/json</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">api-key</span><span class="sh">"</span><span class="p">:</span> <span class="n">api_key</span> <span class="p">}</span> |
インデックスの作成
インデックスを作成するを参考に、Pythonに書き換えた以下のコードでインデックスを作成します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
<span class="n">url</span> <span class="o">=</span> <span class="sa">f</span><span class="sh">"</span><span class="s">https://</span><span class="si">{</span><span class="n">service_name</span><span class="si">}</span><span class="s">.search.windows.net/indexes/</span><span class="si">{</span><span class="n">index_name</span><span class="si">}</span><span class="s">?api-version=2023-11-01</span><span class="sh">"</span> <span class="n">body</span> <span class="o">=</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">hotels-vector-quickstart</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">fields</span><span class="sh">"</span><span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">key</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Collection(Edm.Single)</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">dimensions</span><span class="sh">"</span><span class="p">:</span> <span class="mi">1536</span><span class="p">,</span> <span class="sh">"</span><span class="s">vectorSearchProfile</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-vector-profile</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Collection(Edm.Single)</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">dimensions</span><span class="sh">"</span><span class="p">:</span> <span class="mi">1536</span><span class="p">,</span> <span class="sh">"</span><span class="s">vectorSearchProfile</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-vector-profile</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Address</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.ComplexType</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">fields</span><span class="sh">"</span><span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">City</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">StateProvince</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.String</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span> <span class="p">}</span> <span class="p">]</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Location</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">type</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Edm.GeographyPoint</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">searchable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span><span class="p">,</span> <span class="sh">"</span><span class="s">filterable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">retrievable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">sortable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">facetable</span><span class="sh">"</span><span class="p">:</span> <span class="bp">False</span> <span class="p">}</span> <span class="p">],</span> <span class="sh">"</span><span class="s">vectorSearch</span><span class="sh">"</span><span class="p">:</span> <span class="p">{</span> <span class="sh">"</span><span class="s">algorithms</span><span class="sh">"</span><span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-hnsw-vector-config-1</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">kind</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">hnsw</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">hnswParameters</span><span class="sh">"</span><span class="p">:</span> <span class="p">{</span> <span class="sh">"</span><span class="s">m</span><span class="sh">"</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="sh">"</span><span class="s">efConstruction</span><span class="sh">"</span><span class="p">:</span> <span class="mi">400</span><span class="p">,</span> <span class="sh">"</span><span class="s">efSearch</span><span class="sh">"</span><span class="p">:</span> <span class="mi">500</span><span class="p">,</span> <span class="sh">"</span><span class="s">metric</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">cosine</span><span class="sh">"</span> <span class="p">}</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-hnsw-vector-config-2</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">kind</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">hnsw</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">hnswParameters</span><span class="sh">"</span><span class="p">:</span> <span class="p">{</span> <span class="sh">"</span><span class="s">m</span><span class="sh">"</span><span class="p">:</span> <span class="mi">4</span><span class="p">,</span> <span class="sh">"</span><span class="s">metric</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">euclidean</span><span class="sh">"</span> <span class="p">}</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-eknn-vector-config</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">kind</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">exhaustiveKnn</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">exhaustiveKnnParameters</span><span class="sh">"</span><span class="p">:</span> <span class="p">{</span> <span class="sh">"</span><span class="s">metric</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">cosine</span><span class="sh">"</span> <span class="p">}</span> <span class="p">}</span> <span class="p">],</span> <span class="sh">"</span><span class="s">profiles</span><span class="sh">"</span><span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="sh">"</span><span class="s">name</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-vector-profile</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">algorithm</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">my-hnsw-vector-config-1</span><span class="sh">"</span> <span class="p">}</span> <span class="p">]</span> <span class="p">}</span> <span class="p">}</span> <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="p">.</span><span class="nf">put</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">body</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">status_code</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">text</span><span class="p">)</span> |
ここまで終えたら、Pythonファイルを実行してください。
ステータスコード201が返され、以下のようにhotels-vector-quickstartが作成されれば成功です。
次のステップへ進む前に、コードが重複して実行されるのを防ぐため、url、body、responseの各変数を削除するか、コメントアウトしてください。
ドキュメントのアップロード
ドキュメントのアップロードを参考に、Pythonに書き換えた以下のコードでドキュメントをアップロードします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
<span class="n">url</span> <span class="o">=</span> <span class="sa">f</span><span class="sh">"</span><span class="s">https://</span><span class="si">{</span><span class="n">service_name</span><span class="si">}</span><span class="s">.search.windows.net/indexes/</span><span class="si">{</span><span class="n">index_name</span><span class="si">}</span><span class="s">/docs/index?api-version=2023-11-01</span><span class="sh">"</span> <span class="n">body</span> <span class="o">=</span> <span class="p">{</span> <span class="sh">"</span><span class="s">value</span><span class="sh">"</span><span class="p">:</span> <span class="p">[</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">1</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Secret Point Motel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">The hotel is ideally located on the main commercial artery of the city in the heart of New York.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Boutique</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">2</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Twin Dome Hotel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">The hotel is situated in a nineteenth century plaza, which has been expanded and renovated to the highest architectural standards to create a modern, functional and first-class hotel in which art and unique historical elements coexist with the most modern comforts.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Boutique</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">3</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Triple Landscape Hotel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">The Hotel stands out for its gastronomic excellence under the management of William Dough, who advises on and oversees all of the Hotel’s restaurant services.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Resort and Spa</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">4</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Sublime Cliff Hotel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Boutique</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">13</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Historic Lion Resort</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Unmatched Luxury. Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Resort and Spa</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">48</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Nordick</span><span class="sh">'</span><span class="s">s Hotel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Only 90 miles (about 2 hours) from the nation</span><span class="sh">'</span><span class="s">s capital and nearby most everything the historic valley has to offer. Hiking? Wine Tasting? Exploring the caverns? It</span><span class="sh">'</span><span class="s">s all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Boutique</span><span class="sh">"</span> <span class="p">},</span> <span class="p">{</span> <span class="sh">"</span><span class="s">@search.action</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">mergeOrUpload</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelId</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">49</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelName</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Old Carrabelle Hotel</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">HotelNameVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Description</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Spacious rooms, glamorous suites and residences, rooftop pool, walking access to shopping, dining, entertainment and the city center.</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">DescriptionVector</span><span class="sh">"</span><span class="p">:</span> <span class="p">[],</span> <span class="sh">"</span><span class="s">Category</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">Luxury</span><span class="sh">"</span> <span class="p">}</span> <span class="p">]</span> <span class="p">}</span> <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="p">.</span><span class="nf">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">body</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">status_code</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">text</span><span class="p">)</span> |
ここまで終えたら、Pythonファイルを実行してください。
ステータスコード200が返され、以下のようにドキュメント数が増えていれば成功です。
先ほどと同様、次のステップへ進む前に、コードが重複して実行されるのを防ぐため、url、body、responseの各変数を削除するか、コメントアウトしてください。
クエリの実行
クエリを実行するを参考に、Pythonに書き換えた以下のコードでクエリを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<span class="n">url</span> <span class="o">=</span> <span class="sa">f</span><span class="sh">"</span><span class="s">https://</span><span class="si">{</span><span class="n">service_name</span><span class="si">}</span><span class="s">.search.windows.net/indexes/</span><span class="si">{</span><span class="n">index_name</span><span class="si">}</span><span class="s">/docs/search?api-version=2023-11-01</span><span class="sh">"</span> <span class="n">body</span> <span class="o">=</span> <span class="p">{</span> <span class="sh">"</span><span class="s">count</span><span class="sh">"</span><span class="p">:</span> <span class="bp">True</span><span class="p">,</span> <span class="sh">"</span><span class="s">search</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">historic hotel walk to restaurants and shopping</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">select</span><span class="sh">"</span><span class="p">:</span> <span class="sh">"</span><span class="s">HotelName, Description</span><span class="sh">"</span><span class="p">,</span> <span class="sh">"</span><span class="s">top</span><span class="sh">"</span><span class="p">:</span> <span class="mi">3</span> <span class="p">}</span> <span class="n">response</span> <span class="o">=</span> <span class="n">requests</span><span class="p">.</span><span class="nf">post</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">,</span> <span class="n">json</span><span class="o">=</span><span class="n">body</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">status_code</span><span class="p">)</span> <span class="nf">print</span><span class="p">(</span><span class="n">response</span><span class="p">.</span><span class="n">text</span><span class="p">)</span> |
"count": True
は、検索結果の総数を取得するかどうかを指定します。
"search": "historic hotel walk to restaurants and shopping"
は、検索クエリです。この例では、「歴史的なホテルで、レストランやショッピングエリアに徒歩で行ける」という条件を指定しています。
"select": "HotelName, Description"
は、取得したいフィールドを指定します。ここではホテルの名前と説明を取得します。
"top": 3
は、最上位の3つの結果のみを取得することを指定します。
ここまで終えたら、Pythonファイルを実行してください。
ステータスコード200が返され、以下のように条件との関連度を示す@search.scoreの順に3つのホテルが検索結果として返ってくれば成功です。
value”:[{“@search.score”:2.1902227,”HotelName”:”Nordick’s Hotel”,”Description”:”Only 90 miles (about 2 hours) from the nation’s capital and nearby most everything the historic valley has to offer. Hiking? Wine Tasting? Exploring the caverns? It’s all nearby and we have specially priced packages to help make our B&B your home base for fun while visiting the valley.”},{“@search.score”:2.0014017,”HotelName”:”Historic Lion Resort”,”Description”:”Unmatched Luxury. Visit our downtown hotel to indulge in luxury accommodations. Moments from the stadium, we feature the best in comfort”},{“@search.score”:1.3935266,”HotelName”:”Sublime Cliff Hotel”,”Description”:”Sublime Cliff Hotel is located in the heart of the historic center of Sublime in an extremely vibrant and lively area within short walking distance to the sites and landmarks of the city and is surrounded by the extraordinary beauty of churches, buildings, shops and monuments. Sublime Cliff is part of a lovingly restored 1800 palace.”}]
おわりに
Azure AI Searchを使って、Pythonでベクトル検索を実行してみました。Azure AI Searchはクエリベースの検索ニーズに対して、柔軟かつ効率的な解決策を提供します。ぜひ、自分のプロジェクトで試してみてください。私ももっと深めていきたいと思います。
最後までお読みいただき、ありがとうございました! 記事に関する質問等ございましたら、コメントもしくは以下のDMにてよろしくお願いします。
ナレッジコミュニケーションでは「Musubite」というエンジニア同士のカジュアルトークサービスを利用しています。
生成 AI 技術を使ったプロジェクトに携わるメンバーと直接話せるサービスですので興味がある方は是非利用を検討してください!
参考文献