|
2 | 2 |
|
3 | 3 | from langchain_community.vectorstores.azuresearch import AzureSearch
|
4 | 4 | from langchain_core.documents import Document
|
| 5 | +from langchain_core.tools import tool |
| 6 | +from pydantic import BaseModel, Field |
5 | 7 | from pydantic_settings import BaseSettings, SettingsConfigDict
|
6 | 8 |
|
7 | 9 | from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
|
@@ -58,3 +60,46 @@ def similarity_search(
|
58 | 60 | query=query,
|
59 | 61 | k=k, # Number of results to return
|
60 | 62 | )
|
| 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 |
0 commit comments