Skip to content

Commit 88a107d

Browse files
committed
add AI Search tool
1 parent 9695a14 commit 88a107d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

docs/references.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [CSVLoader](https://python.langchain.com/docs/how_to/document_loader_csv/)
2222
- [Qdrant](https://github.com/qdrant/qdrant)
2323
- [Azure Cosmos DB No SQL](https://python.langchain.com/docs/integrations/vectorstores/azure_cosmos_db_no_sql/)
24+
- [Azure AI Search](https://python.langchain.com/docs/integrations/vectorstores/azuresearch/)
2425

2526
### Services
2627

template_langgraph/tools/ai_search_tool.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from langchain_community.vectorstores.azuresearch import AzureSearch
44
from langchain_core.documents import Document
5+
from langchain_core.tools import tool
6+
from pydantic import BaseModel, Field
57
from pydantic_settings import BaseSettings, SettingsConfigDict
68

79
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
@@ -58,3 +60,46 @@ def similarity_search(
5860
query=query,
5961
k=k, # Number of results to return
6062
)
63+
64+
65+
class AiSearchInput(BaseModel):
66+
query: str = Field(
67+
default="禅モード",
68+
description="Query to search in the AI Search index",
69+
)
70+
k: int = Field(
71+
default=5,
72+
description="Number of results to return from the similarity search",
73+
)
74+
75+
76+
class AiSearchOutput(BaseModel):
77+
content: str = Field(description="Content of the document")
78+
id: str = Field(description="ID of the document")
79+
80+
81+
@tool(args_schema=AiSearchInput)
82+
def search_ai_search(query: str, k: int = 5) -> list[AiSearchOutput]:
83+
"""Search for similar documents in AI Search index.
84+
85+
Args:
86+
query: The search query string
87+
k: Number of results to return (default: 5)
88+
89+
Returns:
90+
AiSearchOutput: A Pydantic model containing the search results
91+
"""
92+
wrapper = AiSearchClientWrapper()
93+
documents = wrapper.similarity_search(
94+
query=query,
95+
k=k,
96+
)
97+
outputs = []
98+
for document in documents:
99+
outputs.append(
100+
{
101+
"content": document.page_content,
102+
"id": document.id,
103+
}
104+
)
105+
return outputs

template_langgraph/tools/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
22
from template_langgraph.loggers import get_logger
3+
from template_langgraph.tools.ai_search_tool import search_ai_search
34
from template_langgraph.tools.cosmosdb_tool import search_cosmosdb
45
from template_langgraph.tools.dify_tool import run_dify_workflow
56
from template_langgraph.tools.elasticsearch_tool import search_elasticsearch
@@ -18,6 +19,7 @@ def get_default_tools():
1819
logger.error(f"Error occurred while getting SQL database tools: {e}")
1920
sql_database_tools = []
2021
return [
22+
search_ai_search,
2123
search_cosmosdb,
2224
run_dify_workflow,
2325
search_qdrant,

0 commit comments

Comments
 (0)