Skip to content

Commit

Permalink
add arguments for scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Aug 29, 2024
1 parent de988a0 commit daa6f98
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
5 changes: 2 additions & 3 deletions apps/1_call_azure_openai_chat/query_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@

def init_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="ProgramName",
description="What the program does",
epilog="Text at the bottom of help",
prog="query_image",
description="Query Azure OpenAI Chat with an image",
)
parser.add_argument("-f", "--file")
parser.add_argument("-s", "--system", default="You are a professional image analyst. Describe the image.")
Expand Down
30 changes: 24 additions & 6 deletions apps/6_call_azure_ai_search/1_create_index.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import argparse
import logging
from os import getenv
from pprint import pprint

Expand All @@ -7,10 +9,15 @@
from langchain_text_splitters import RecursiveCharacterTextSplitter


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


if __name__ == "__main__":
Expand All @@ -19,10 +26,21 @@ def load_texts() -> list:
- Embed text with Azure OpenAI Service
- Index text with Azure AI Search
"""
args = init_args()

# Set verbose mode
if args.verbose:
logging.basicConfig(level=logging.DEBUG)

load_dotenv()

# Get documents
texts = load_texts()
try:
with open(args.file) as f:
texts = f.readlines()
except Exception as e:
print(e)
exit(1)

# Split text into chunks
# https://python.langchain.com/v0.2/docs/how_to/recursive_text_splitter/
Expand Down Expand Up @@ -51,7 +69,7 @@ def load_texts() -> list:
search = AzureSearch(
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
index_name=args.index_name,
embedding_function=embeddings.embed_query,
additional_search_client_options={
"retry_total": 4,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import argparse
import logging
from os import getenv

from dotenv import load_dotenv
from langchain_community.vectorstores.azuresearch import AzureSearch
from langchain_openai import AzureOpenAIEmbeddings


def init_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog="search_docs",
description="Search for documents in Azure AI Search",
)
parser.add_argument("-i", "--index-name", default="contoso-rules")
parser.add_argument("-q", "--query", default="meetings")
parser.add_argument("-v", "--verbose", action="store_true")
return parser.parse_args()


if __name__ == "__main__":
"""
Search for documents in Azure AI Search
"""
args = init_args()

# Set verbose mode
if args.verbose:
logging.basicConfig(level=logging.DEBUG)

load_dotenv()

embeddings = AzureOpenAIEmbeddings(
Expand All @@ -20,7 +40,7 @@
vector_store = AzureSearch(
azure_search_endpoint=getenv("AZURE_AI_SEARCH_ENDPOINT"),
azure_search_key=getenv("AZURE_AI_SEARCH_API_KEY"),
index_name=getenv("AZURE_AI_SEARCH_INDEX_NAME"),
index_name=args.index_name,
embedding_function=embeddings.embed_query,
additional_search_client_options={
"retry_total": 4,
Expand All @@ -29,7 +49,7 @@

# search for documents
results = vector_store.hybrid_search(
query="meetings",
query=args.query,
k=3,
)
for result in results:
Expand Down
13 changes: 11 additions & 2 deletions apps/6_call_azure_ai_search/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,22 @@ Create an index in Azure AI Search and upload documents:
> This script should be run only once to avoid creating duplicate indexes.
```shell
$ python apps/6_call_azure_ai_search/1_create_index.py
$ INDEX_NAME=yourindexname
$ FILE=./datasets/yourfile.csv
$ python apps/6_call_azure_ai_search/1_create_index.py \
--index-name $INDEX_NAME \
--file $FILE \
--verbose
```

Search documents in Azure AI Search:

```shell
$ python apps/6_call_azure_ai_search/2_search_index.py
$ INDEX_NAME=yourindexname
$ python apps/6_call_azure_ai_search/2_search_docs.py \
--index-name $INDEX_NAME \
--query "meeting" \
--verbose

> All meetings must include a 5-minute meditation session.
> All meetings must begin with a joke.
Expand Down

0 comments on commit daa6f98

Please sign in to comment.