Skip to content

Commit daa6f98

Browse files
committed
add arguments for scripts
1 parent de988a0 commit daa6f98

File tree

4 files changed

+59
-13
lines changed

4 files changed

+59
-13
lines changed

apps/1_call_azure_openai_chat/query_image.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
def init_args() -> argparse.Namespace:
1111
parser = argparse.ArgumentParser(
12-
prog="ProgramName",
13-
description="What the program does",
14-
epilog="Text at the bottom of help",
12+
prog="query_image",
13+
description="Query Azure OpenAI Chat with an image",
1514
)
1615
parser.add_argument("-f", "--file")
1716
parser.add_argument("-s", "--system", default="You are a professional image analyst. Describe the image.")

apps/6_call_azure_ai_search/1_create_index.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import argparse
2+
import logging
13
from os import getenv
24
from pprint import pprint
35

@@ -7,10 +9,15 @@
79
from langchain_text_splitters import RecursiveCharacterTextSplitter
810

911

10-
def load_texts() -> list:
11-
# for simplicity, hardcoding the path to the text file
12-
with open("./datasets/contoso_rules.csv") as f:
13-
return f.readlines()
12+
def init_args() -> argparse.Namespace:
13+
parser = argparse.ArgumentParser(
14+
prog="create_index",
15+
description="Create an index in Azure AI Search",
16+
)
17+
parser.add_argument("-f", "--file", default="./datasets/contoso_rules.csv")
18+
parser.add_argument("-i", "--index-name", default="contoso-rules")
19+
parser.add_argument("-v", "--verbose", action="store_true")
20+
return parser.parse_args()
1421

1522

1623
if __name__ == "__main__":
@@ -19,10 +26,21 @@ def load_texts() -> list:
1926
- Embed text with Azure OpenAI Service
2027
- Index text with Azure AI Search
2128
"""
29+
args = init_args()
30+
31+
# Set verbose mode
32+
if args.verbose:
33+
logging.basicConfig(level=logging.DEBUG)
34+
2235
load_dotenv()
2336

2437
# Get documents
25-
texts = load_texts()
38+
try:
39+
with open(args.file) as f:
40+
texts = f.readlines()
41+
except Exception as e:
42+
print(e)
43+
exit(1)
2644

2745
# Split text into chunks
2846
# https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/
@@ -51,7 +69,7 @@ def load_texts() -> list:
5169
search = AzureSearch(
5270
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
5371
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
54-
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
72+
index_name=args.index_name,
5573
embedding_function=embeddings.embed_query,
5674
additional_search_client_options={
5775
"retry_total": 4,

apps/6_call_azure_ai_search/2_search_index.py renamed to apps/6_call_azure_ai_search/2_search_docs.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,33 @@
1+
import argparse
2+
import logging
13
from os import getenv
24

35
from dotenv import load_dotenv
46
from langchain_community.vectorstores.azuresearch import AzureSearch
57
from langchain_openai import AzureOpenAIEmbeddings
68

9+
10+
def init_args() -> argparse.Namespace:
11+
parser = argparse.ArgumentParser(
12+
prog="search_docs",
13+
description="Search for documents in Azure AI Search",
14+
)
15+
parser.add_argument("-i", "--index-name", default="contoso-rules")
16+
parser.add_argument("-q", "--query", default="meetings")
17+
parser.add_argument("-v", "--verbose", action="store_true")
18+
return parser.parse_args()
19+
20+
721
if __name__ == "__main__":
822
"""
923
Search for documents in Azure AI Search
1024
"""
25+
args = init_args()
26+
27+
# Set verbose mode
28+
if args.verbose:
29+
logging.basicConfig(level=logging.DEBUG)
30+
1131
load_dotenv()
1232

1333
embeddings = AzureOpenAIEmbeddings(
@@ -20,7 +40,7 @@
2040
vector_store = AzureSearch(
2141
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
2242
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
23-
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
43+
index_name=args.index_name,
2444
embedding_function=embeddings.embed_query,
2545
additional_search_client_options={
2646
"retry_total": 4,
@@ -29,7 +49,7 @@
2949

3050
# search for documents
3151
results = vector_store.hybrid_search(
32-
query="meetings",
52+
query=args.query,
3353
k=3,
3454
)
3555
for result in results:

apps/6_call_azure_ai_search/README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,22 @@ Create an index in Azure AI Search and upload documents:
7373
> This script should be run only once to avoid creating duplicate indexes.
7474
7575
```shell
76-
$ python apps/6_call_azure_ai_search/1_create_index.py
76+
$ INDEX_NAME=yourindexname
77+
$ FILE=./datasets/yourfile.csv
78+
$ python apps/6_call_azure_ai_search/1_create_index.py \
79+
--index-name $INDEX_NAME \
80+
--file $FILE \
81+
--verbose
7782
```
7883

7984
Search documents in Azure AI Search:
8085

8186
```shell
82-
$ python apps/6_call_azure_ai_search/2_search_index.py
87+
$ INDEX_NAME=yourindexname
88+
$ python apps/6_call_azure_ai_search/2_search_docs.py \
89+
--index-name $INDEX_NAME \
90+
--query "meeting" \
91+
--verbose
8392

8493
> All meetings must include a 5-minute meditation session.
8594
> All meetings must begin with a joke.

0 commit comments

Comments
 (0)