Skip to content

Commit ceb7a38

Browse files
authored
Merge pull request #34 from ks6088ts-labs/feature/issue-30_azure-ai-search
add Azure AI Search sample codes
2 parents f18025d + b4ab99f commit ceb7a38

File tree

9 files changed

+1501
-47
lines changed

9 files changed

+1501
-47
lines changed

.env.template

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ AZURE_OPENAI_API_KEY="<YOUR_API_KEY>"
44
AZURE_OPENAI_API_VERSION="2024-06-01"
55
AZURE_OPENAI_GPT_MODEL="gpt-4o"
66
AZURE_OPENAI_STT_MODEL="whisper"
7+
AZURE_OPENAI_EMBEDDING_MODEL="text-embedding-3-large"
78

89
# Azure Cosmos DB
910
AZURE_COSMOS_DB_CONNECTION_STRING="AccountEndpoint=https://<YOUR_COSMOSDB_NAME>.documents.azure.com:443/;AccountKey=<ACCOUNT_KEY>;"
1011
AZURE_COSMOS_DB_DATABASE_NAME="workshop"
1112
AZURE_COSMOS_DB_CONTAINER_NAME="chat"
13+
14+
# Azure AI Search
15+
AZURE_AI_SEARCH_ENDPOINT="https://<YOUR_AZURE_SEARCH_NAME>.search.windows.net/"
16+
AZURE_AI_SEARCH_API_KEY="<YOUR_API_KEY>"
17+
AZURE_AI_SEARCH_INDEX_NAME="chat"
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Azure AI Search を Python から呼び出す
2+
3+
Azure AI Search を Python から呼び出す方法を説明します。
4+
5+
## 前提条件
6+
7+
- Python 3.11+ がインストールされていること
8+
- Azure AI Search が利用できること
9+
- Azure AI Search の API キーが取得できていること
10+
11+
## 手順
12+
13+
1. Azure AI Search の API キーを取得する
14+
1. [.env.template](../../.env.template) をコピーして `.env` ファイルを作成する
15+
1. `.env` ファイルに API キーを設定する
16+
17+
```shell
18+
# 仮想環境を作成してライブラリをインストールする
19+
python -m venv .venv
20+
21+
# 仮想環境を有効化する
22+
source .venv/bin/activate
23+
24+
# ライブラリをインストールする
25+
pip install -r requirements.txt
26+
```
27+
28+
### 実行例
29+
30+
```shell
31+
# Azure AI Search にインデックスを作成して、ドキュメントを追加する
32+
python apps/6_call_azure_ai_search/create_index.py
33+
34+
# Azure AI Search にクエリを発行して、検索結果を取得する
35+
python apps/6_call_azure_ai_search/search_index.py
36+
```
37+
38+
## 参考資料
39+
40+
- [How to recursively split text by characters](https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/)
41+
- [青空文庫 > 吾輩は猫である](https://www.aozora.gr.jp/cards/000148/files/789_14547.html)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
from os import getenv
2+
from pprint import pprint
3+
4+
from datasets import load_texts
5+
from dotenv import load_dotenv
6+
from langchain_community.vectorstores.azuresearch import AzureSearch
7+
from langchain_openai import AzureOpenAIEmbeddings
8+
from langchain_text_splitters import RecursiveCharacterTextSplitter
9+
10+
if __name__ == "__main__":
11+
"""
12+
テキストを Azure OpenAI Service で埋め込み化し、Azure AI Search にインデックス化します。
13+
"""
14+
load_dotenv()
15+
16+
# ドキュメントを取得
17+
texts = load_texts(
18+
file_path="./apps/6_call_azure_ai_search/data/test.txt",
19+
)
20+
21+
# テキストを分割
22+
# https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/
23+
text_splitter = RecursiveCharacterTextSplitter(
24+
chunk_size=400,
25+
chunk_overlap=20,
26+
length_function=len,
27+
is_separator_regex=False,
28+
)
29+
30+
documents = text_splitter.create_documents(
31+
texts=texts,
32+
)
33+
pprint(documents)
34+
35+
embeddings = AzureOpenAIEmbeddings(
36+
api_key=getenv("AZURE_OPENAI_API_KEY"),
37+
api_version=getenv("AZURE_OPENAI_API_VERSION"),
38+
azure_endpoint=getenv("AZURE_OPENAI_ENDPOINT"),
39+
model=getenv("AZURE_OPENAI_EMBEDDING_MODEL"),
40+
)
41+
42+
# Azure AI Search でのインデックス化
43+
# https://python.langchain.com/v0.2/docs/integrations/vectorstores/azuresearch/
44+
# create index
45+
search = AzureSearch(
46+
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
47+
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
48+
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
49+
embedding_function=embeddings.embed_query,
50+
additional_search_client_options={
51+
"retry_total": 4,
52+
},
53+
)
54+
55+
# add documents
56+
docs_ids = search.add_documents(documents=documents)
57+
pprint(docs_ids)

apps/6_call_azure_ai_search/data/test.txt

Lines changed: 41 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def load_texts(file_path: str) -> list:
2+
with open(file_path) as f:
3+
return f.readlines()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from os import getenv
2+
from pprint import pprint
3+
4+
from dotenv import load_dotenv
5+
from langchain_community.vectorstores.azuresearch import AzureSearch
6+
from langchain_openai import AzureOpenAIEmbeddings
7+
8+
if __name__ == "__main__":
9+
"""
10+
Azure AI Search で検索を行う
11+
"""
12+
load_dotenv()
13+
14+
embeddings = AzureOpenAIEmbeddings(
15+
api_key=getenv("AZURE_OPENAI_API_KEY"),
16+
api_version=getenv("AZURE_OPENAI_API_VERSION"),
17+
azure_endpoint=getenv("AZURE_OPENAI_ENDPOINT"),
18+
model=getenv("AZURE_OPENAI_EMBEDDING_MODEL"),
19+
)
20+
21+
vector_store = AzureSearch(
22+
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
23+
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
24+
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
25+
embedding_function=embeddings.embed_query,
26+
additional_search_client_options={
27+
"retry_total": 4,
28+
},
29+
)
30+
31+
# search for documents
32+
results = vector_store.hybrid_search(
33+
query="吾輩は猫である。名前はまだない",
34+
k=5,
35+
)
36+
pprint(results)

0 commit comments

Comments
 (0)