|
| 1 | +from functools import lru_cache |
| 2 | + |
| 3 | +from langchain_community.vectorstores.azuresearch import AzureSearch |
| 4 | +from langchain_core.documents import Document |
| 5 | +from langchain_core.tools import tool |
| 6 | +from pydantic import BaseModel, Field |
| 7 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 8 | + |
| 9 | +from template_langgraph.llms.azure_openais import AzureOpenAiWrapper |
| 10 | + |
| 11 | + |
| 12 | +class Settings(BaseSettings): |
| 13 | + ai_search_key: str = "<your-ai-search-key>" |
| 14 | + ai_search_endpoint: str = "<your-ai-search-endpoint>" |
| 15 | + ai_search_index_name: str = "<your-ai-index-name>" |
| 16 | + |
| 17 | + model_config = SettingsConfigDict( |
| 18 | + env_file=".env", |
| 19 | + env_ignore_empty=True, |
| 20 | + extra="ignore", |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +@lru_cache |
| 25 | +def get_ai_search_settings() -> Settings: |
| 26 | + """Get AI Search settings.""" |
| 27 | + return Settings() |
| 28 | + |
| 29 | + |
| 30 | +class AiSearchClientWrapper: |
| 31 | + def __init__( |
| 32 | + self, |
| 33 | + settings: Settings = None, |
| 34 | + ): |
| 35 | + if settings is None: |
| 36 | + settings = get_ai_search_settings() |
| 37 | + self.vector_store: AzureSearch = AzureSearch( |
| 38 | + azure_search_endpoint=settings.ai_search_endpoint, |
| 39 | + azure_search_key=settings.ai_search_key, |
| 40 | + index_name=settings.ai_search_index_name, |
| 41 | + embedding_function=AzureOpenAiWrapper().embedding_model.embed_query, |
| 42 | + ) |
| 43 | + |
| 44 | + def add_documents( |
| 45 | + self, |
| 46 | + documents: list[Document], |
| 47 | + ) -> list[str]: |
| 48 | + """Add documents to a Cosmos DB container.""" |
| 49 | + return self.vector_store.add_documents( |
| 50 | + documents=documents, |
| 51 | + ) |
| 52 | + |
| 53 | + def similarity_search( |
| 54 | + self, |
| 55 | + query: str, |
| 56 | + k: int = 5, |
| 57 | + ) -> list[Document]: |
| 58 | + """Perform a similarity search in the Cosmos DB index.""" |
| 59 | + return self.vector_store.similarity_search( |
| 60 | + query=query, |
| 61 | + k=k, # Number of results to return |
| 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