|
| 1 | +from functools import lru_cache |
| 2 | + |
| 3 | +from azure.cosmos import CosmosClient, PartitionKey |
| 4 | +from langchain_community.vectorstores.azure_cosmos_db_no_sql import ( |
| 5 | + AzureCosmosDBNoSqlVectorSearch, |
| 6 | +) |
| 7 | +from langchain_core.documents import Document |
| 8 | +from langchain_core.tools import tool |
| 9 | +from pydantic import BaseModel, Field |
| 10 | +from pydantic_settings import BaseSettings, SettingsConfigDict |
| 11 | + |
| 12 | +from template_langgraph.llms.azure_openais import AzureOpenAiWrapper |
| 13 | + |
| 14 | + |
| 15 | +class Settings(BaseSettings): |
| 16 | + cosmosdb_host: str = "<AZURE_COSMOS_DB_ENDPOINT>" |
| 17 | + cosmosdb_key: str = "<AZURE_COSMOS_DB_KEY>" |
| 18 | + cosmosdb_database_name: str = "template_langgraph" |
| 19 | + cosmosdb_container_name: str = "kabuto" |
| 20 | + cosmosdb_partition_key: str = "/id" |
| 21 | + |
| 22 | + model_config = SettingsConfigDict( |
| 23 | + env_file=".env", |
| 24 | + env_ignore_empty=True, |
| 25 | + extra="ignore", |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +@lru_cache |
| 30 | +def get_cosmosdb_settings() -> Settings: |
| 31 | + """Get Cosmos DB settings.""" |
| 32 | + return Settings() |
| 33 | + |
| 34 | + |
| 35 | +class CosmosdbClientWrapper: |
| 36 | + def __init__( |
| 37 | + self, |
| 38 | + settings: Settings = None, |
| 39 | + ): |
| 40 | + if settings is None: |
| 41 | + settings = get_cosmosdb_settings() |
| 42 | + self.vector_store = AzureCosmosDBNoSqlVectorSearch( |
| 43 | + cosmos_client=CosmosClient( |
| 44 | + url=settings.cosmosdb_host, |
| 45 | + credential=settings.cosmosdb_key, |
| 46 | + ), |
| 47 | + embedding=AzureOpenAiWrapper().embedding_model, |
| 48 | + vector_embedding_policy={ |
| 49 | + "vectorEmbeddings": [ |
| 50 | + { |
| 51 | + "path": "/embedding", |
| 52 | + "dataType": "float32", |
| 53 | + "distanceFunction": "cosine", |
| 54 | + "dimensions": 1536, |
| 55 | + } |
| 56 | + ] |
| 57 | + }, |
| 58 | + indexing_policy={ |
| 59 | + "indexingMode": "consistent", |
| 60 | + "includedPaths": [ |
| 61 | + {"path": "/*"}, |
| 62 | + ], |
| 63 | + "excludedPaths": [ |
| 64 | + {"path": '/"_etag"/?'}, |
| 65 | + ], |
| 66 | + "vectorIndexes": [ |
| 67 | + {"path": "/embedding", "type": "diskANN"}, |
| 68 | + ], |
| 69 | + "fullTextIndexes": [ |
| 70 | + {"path": "/text"}, |
| 71 | + ], |
| 72 | + }, |
| 73 | + cosmos_container_properties={ |
| 74 | + "partition_key": PartitionKey(path=settings.cosmosdb_partition_key), |
| 75 | + }, |
| 76 | + cosmos_database_properties={}, |
| 77 | + full_text_policy={ |
| 78 | + "defaultLanguage": "en-US", |
| 79 | + "fullTextPaths": [ |
| 80 | + { |
| 81 | + "path": "/text", |
| 82 | + "language": "en-US", |
| 83 | + } |
| 84 | + ], |
| 85 | + }, |
| 86 | + database_name=settings.cosmosdb_database_name, |
| 87 | + container_name=settings.cosmosdb_container_name, |
| 88 | + ) |
| 89 | + |
| 90 | + def add_documents( |
| 91 | + self, |
| 92 | + documents: list[Document], |
| 93 | + ) -> list[str]: |
| 94 | + """Add documents to a Cosmos DB container.""" |
| 95 | + return self.vector_store.add_documents( |
| 96 | + documents=documents, |
| 97 | + ) |
| 98 | + |
| 99 | + def delete_documents( |
| 100 | + self, |
| 101 | + ids: list[str], |
| 102 | + ) -> bool | None: |
| 103 | + """Delete documents from a Cosmos DB container.""" |
| 104 | + return self.vector_store.delete( |
| 105 | + ids=ids, |
| 106 | + ) |
| 107 | + |
| 108 | + def similarity_search( |
| 109 | + self, |
| 110 | + query: str, |
| 111 | + k: int = 5, |
| 112 | + ) -> list[Document]: |
| 113 | + """Perform a similarity search in the Cosmos DB index.""" |
| 114 | + return self.vector_store.similarity_search( |
| 115 | + query=query, |
| 116 | + k=k, # Number of results to return |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +class CosmosdbInput(BaseModel): |
| 121 | + query: str = Field( |
| 122 | + default="禅モード", |
| 123 | + description="Query to search in the Cosmos DB index", |
| 124 | + ) |
| 125 | + k: int = Field( |
| 126 | + default=5, |
| 127 | + description="Number of results to return from the similarity search", |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +class CosmosdbOutput(BaseModel): |
| 132 | + content: str = Field(description="Content of the document") |
| 133 | + id: str = Field(description="ID of the document") |
| 134 | + |
| 135 | + |
| 136 | +@tool(args_schema=CosmosdbInput) |
| 137 | +def search_cosmosdb(query: str, k: int = 5) -> list[CosmosdbOutput]: |
| 138 | + """Search for similar documents in CosmosDB vector store. |
| 139 | +
|
| 140 | + Args: |
| 141 | + query: The search query string |
| 142 | + k: Number of results to return (default: 5) |
| 143 | +
|
| 144 | + Returns: |
| 145 | + CosmosdbOutput: A Pydantic model containing the search results |
| 146 | + """ |
| 147 | + wrapper = CosmosdbClientWrapper() |
| 148 | + documents = wrapper.similarity_search( |
| 149 | + query=query, |
| 150 | + k=k, |
| 151 | + ) |
| 152 | + outputs = [] |
| 153 | + for document in documents: |
| 154 | + outputs.append( |
| 155 | + { |
| 156 | + "content": document.page_content, |
| 157 | + "id": document.id, |
| 158 | + } |
| 159 | + ) |
| 160 | + return outputs |
0 commit comments