|
| 1 | +# Azure AI Discovery client library for Python |
| 2 | + |
| 3 | +The Azure AI Discovery client library for Python provides two clients for interacting with Azure AI Discovery services: |
| 4 | + |
| 5 | +- **WorkspaceClient** — manage investigations, conversations, tasks, and tools in a Discovery workspace. |
| 6 | +- **BookshelfClient** — manage knowledge bases and knowledge base versions. |
| 7 | + |
| 8 | +[Source code][source_code] | [Package (PyPI)][pypi] | [Samples][samples] |
| 9 | + |
| 10 | +## Getting started |
| 11 | + |
| 12 | +### Install the Package |
| 13 | + |
| 14 | +```bash |
| 15 | +python -m pip install azure-ai-discovery |
| 16 | +``` |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +- Python 3.9 or later is required to use this package. |
| 21 | +- You need an [Azure subscription][azure_sub] to use this package. |
| 22 | +- An existing Azure AI Discovery workspace or bookshelf instance. |
| 23 | + |
| 24 | +### Authenticate the Client |
| 25 | + |
| 26 | +Both clients use Azure Active Directory (AAD) token authentication. Use the [azure-identity][azure_identity] library to obtain credentials: |
| 27 | + |
| 28 | +```bash |
| 29 | +pip install azure-identity |
| 30 | +``` |
| 31 | + |
| 32 | +```python |
| 33 | +from azure.ai.discovery import WorkspaceClient, BookshelfClient |
| 34 | +from azure.identity import DefaultAzureCredential |
| 35 | + |
| 36 | +workspace_client = WorkspaceClient( |
| 37 | + endpoint="https://<workspaceName>.workspace.discovery.azure.com", |
| 38 | + credential=DefaultAzureCredential(), |
| 39 | +) |
| 40 | + |
| 41 | +bookshelf_client = BookshelfClient( |
| 42 | + endpoint="https://<bookshelfName>.bookshelf.discovery.azure.com", |
| 43 | + credential=DefaultAzureCredential(), |
| 44 | +) |
| 45 | +``` |
| 46 | + |
| 47 | +## Key concepts |
| 48 | + |
| 49 | +### WorkspaceClient |
| 50 | + |
| 51 | +The `WorkspaceClient` provides access to Discovery workspace operations, organized into four operation groups: |
| 52 | + |
| 53 | +- **Investigations** — create and manage research investigations within a project. Each investigation can have a Discovery Engine that autonomously explores data and generates insights. |
| 54 | +- **Conversations** — interact with the Discovery Engine through conversational sessions tied to an investigation. |
| 55 | +- **Tasks** — create, assign, and track units of work within an investigation, such as research steps or follow-up actions. |
| 56 | +- **Tools** — run compute jobs on supercomputer node pools and monitor their status and resource usage. |
| 57 | + |
| 58 | +### BookshelfClient |
| 59 | + |
| 60 | +The `BookshelfClient` provides access to knowledge base management: |
| 61 | + |
| 62 | +- **Knowledge Bases** — list available knowledge bases. |
| 63 | +- **Knowledge Base Versions** — create, update, index, and manage versions of knowledge bases backed by storage assets. |
| 64 | + |
| 65 | +## Examples |
| 66 | + |
| 67 | +The following sections provide code snippets covering common scenarios. For complete runnable samples, see the [Samples][samples] directory. |
| 68 | + |
| 69 | +### Create and Manage an Investigation |
| 70 | + |
| 71 | +```python |
| 72 | +from azure.ai.discovery import WorkspaceClient |
| 73 | +from azure.ai.discovery.models import Investigation |
| 74 | +from azure.identity import DefaultAzureCredential |
| 75 | + |
| 76 | +client = WorkspaceClient( |
| 77 | + endpoint="https://<workspaceName>.workspace.discovery.azure.com", |
| 78 | + credential=DefaultAzureCredential(), |
| 79 | +) |
| 80 | + |
| 81 | +# Create an investigation |
| 82 | +investigation = client.investigations.create_or_replace( |
| 83 | + project_name="my-project", |
| 84 | + investigation_name="sample-investigation", |
| 85 | + resource=Investigation( |
| 86 | + description="Investigating anomalies in dataset X", |
| 87 | + display_name="Sample Investigation", |
| 88 | + ), |
| 89 | +) |
| 90 | +print(f"Created investigation: {investigation.name}") |
| 91 | + |
| 92 | +# Start the Discovery Engine |
| 93 | +engine = client.investigations.start_discovery_engine( |
| 94 | + project_name="my-project", |
| 95 | + investigation_name="sample-investigation", |
| 96 | +) |
| 97 | +print(f"Discovery Engine status: {engine.discovery_engine_status}") |
| 98 | +``` |
| 99 | + |
| 100 | +### Create and Manage Tasks |
| 101 | + |
| 102 | +```python |
| 103 | +from azure.ai.discovery import WorkspaceClient |
| 104 | +from azure.ai.discovery.models import Task, TaskAssignee, TaskComment |
| 105 | +from azure.identity import DefaultAzureCredential |
| 106 | + |
| 107 | +client = WorkspaceClient( |
| 108 | + endpoint="https://<workspaceName>.workspace.discovery.azure.com", |
| 109 | + credential=DefaultAzureCredential(), |
| 110 | +) |
| 111 | + |
| 112 | +# Create a task |
| 113 | +task = client.tasks.create( |
| 114 | + project_name="my-project", |
| 115 | + investigation_name="sample-investigation", |
| 116 | + body=Task( |
| 117 | + title="Analyze compound interactions", |
| 118 | + priority="High", |
| 119 | + description="Review the interaction data for compounds A and B", |
| 120 | + assigned_to=TaskAssignee(id="researcher-agent", type="Application"), |
| 121 | + investigation_id="/projects/my-project/investigations/sample-investigation", |
| 122 | + ), |
| 123 | +) |
| 124 | +print(f"Created task: {task.title} ({task.status})") |
| 125 | + |
| 126 | +# Add a comment |
| 127 | +client.tasks.add_comment( |
| 128 | + project_name="my-project", |
| 129 | + investigation_name="sample-investigation", |
| 130 | + task_name=task.name, |
| 131 | + body=TaskComment( |
| 132 | + created_by="sample-user", |
| 133 | + created_by_type="User", |
| 134 | + text="Initial analysis shows promising results.", |
| 135 | + ), |
| 136 | +) |
| 137 | +``` |
| 138 | + |
| 139 | +### Run a Tool on Compute |
| 140 | + |
| 141 | +```python |
| 142 | +from azure.ai.discovery import WorkspaceClient |
| 143 | +from azure.identity import DefaultAzureCredential |
| 144 | + |
| 145 | +client = WorkspaceClient( |
| 146 | + endpoint="https://<workspaceName>.workspace.discovery.azure.com", |
| 147 | + credential=DefaultAzureCredential(), |
| 148 | +) |
| 149 | + |
| 150 | +poller = client.tools.begin_run( |
| 151 | + project_name="my-project", |
| 152 | + tool_id="/subscriptions/.../tools/my-tool", |
| 153 | + node_pool_ids=["/subscriptions/.../nodePools/my-pool"], |
| 154 | + command='echo "Hello from Discovery"', |
| 155 | +) |
| 156 | +result = poller.result() |
| 157 | +print(f"Run completed: {result.status}") |
| 158 | +``` |
| 159 | + |
| 160 | +### Manage Knowledge Bases |
| 161 | + |
| 162 | +```python |
| 163 | +from azure.ai.discovery import BookshelfClient |
| 164 | +from azure.ai.discovery.models import KnowledgeBaseVersion, StorageAssetReference |
| 165 | +from azure.identity import DefaultAzureCredential |
| 166 | + |
| 167 | +client = BookshelfClient( |
| 168 | + endpoint="https://<bookshelfName>.bookshelf.discovery.azure.com", |
| 169 | + credential=DefaultAzureCredential(), |
| 170 | +) |
| 171 | + |
| 172 | +# List knowledge bases |
| 173 | +for kb in client.knowledge_bases.list(): |
| 174 | + print(f"Knowledge base: {kb.name}") |
| 175 | + |
| 176 | +# Create a knowledge base version |
| 177 | +version = client.knowledge_base_versions.create_or_update( |
| 178 | + knowledge_base_name="my-kb", |
| 179 | + version_name="v1", |
| 180 | + resource=KnowledgeBaseVersion( |
| 181 | + description="Research data for compound analysis", |
| 182 | + copilot_instruction="Use this to query information about compound interactions.", |
| 183 | + storage_asset_references=[ |
| 184 | + StorageAssetReference( |
| 185 | + id="/subscriptions/.../storageAssets/my-asset", |
| 186 | + user_assigned_identity="/subscriptions/.../userAssignedIdentities/my-id", |
| 187 | + ), |
| 188 | + ], |
| 189 | + ), |
| 190 | +) |
| 191 | +print(f"Created version: {version.name}") |
| 192 | +``` |
| 193 | + |
| 194 | +## Troubleshooting |
| 195 | + |
| 196 | +### Logging |
| 197 | + |
| 198 | +This library uses the standard [logging][python_logging] library for logging. HTTP session information (URLs, headers, etc.) is logged at the `DEBUG` level. |
| 199 | + |
| 200 | +Detailed `DEBUG` level logging, including request/response bodies and unredacted headers, can be enabled on a client with the `logging_enable` argument: |
| 201 | + |
| 202 | +```python |
| 203 | +client = WorkspaceClient( |
| 204 | + endpoint="https://<workspaceName>.workspace.discovery.azure.com", |
| 205 | + credential=DefaultAzureCredential(), |
| 206 | + logging_enable=True, |
| 207 | +) |
| 208 | +``` |
| 209 | + |
| 210 | +Or on a single operation: |
| 211 | + |
| 212 | +```python |
| 213 | +investigation = client.investigations.get( |
| 214 | + project_name="my-project", |
| 215 | + investigation_name="my-investigation", |
| 216 | + logging_enable=True, |
| 217 | +) |
| 218 | +``` |
| 219 | + |
| 220 | +### General |
| 221 | + |
| 222 | +Azure AI Discovery clients raise exceptions defined in [azure-core][azure_core_exceptions]. For example, if you try to get an investigation that does not exist, `ResourceNotFoundError` is raised: |
| 223 | + |
| 224 | +```python |
| 225 | +from azure.core.exceptions import ResourceNotFoundError |
| 226 | + |
| 227 | +try: |
| 228 | + client.investigations.get( |
| 229 | + project_name="my-project", |
| 230 | + investigation_name="nonexistent", |
| 231 | + ) |
| 232 | +except ResourceNotFoundError as e: |
| 233 | + print(f"Investigation not found: {e.message}") |
| 234 | +``` |
| 235 | + |
| 236 | +## Next steps |
| 237 | + |
| 238 | +- [Samples][samples] — runnable code examples for common scenarios. |
| 239 | +- [Azure AI Discovery documentation][product_docs] |
| 240 | + |
| 241 | +## Contributing |
| 242 | + |
| 243 | +This project welcomes contributions and suggestions. Most contributions require |
| 244 | +you to agree to a Contributor License Agreement (CLA) declaring that you have |
| 245 | +the right to, and actually do, grant us the rights to use your contribution. |
| 246 | +For details, visit https://cla.microsoft.com. |
| 247 | + |
| 248 | +When you submit a pull request, a CLA-bot will automatically determine whether |
| 249 | +you need to provide a CLA and decorate the PR appropriately (e.g., label, |
| 250 | +comment). Simply follow the instructions provided by the bot. You will only |
| 251 | +need to do this once across all repos using our CLA. |
| 252 | + |
| 253 | +This project has adopted the |
| 254 | +[Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). |
| 255 | +For more information, see the |
| 256 | +[Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) |
| 257 | +or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) |
| 258 | +with any additional questions or comments. |
| 259 | + |
| 260 | +<!-- LINKS --> |
| 261 | +[azure_sub]: https://azure.microsoft.com/free/ |
| 262 | +[azure_identity]: https://learn.microsoft.com/python/api/overview/azure/identity-readme |
| 263 | +[azure_core_exceptions]: https://learn.microsoft.com/python/api/azure-core/azure.core.exceptions |
| 264 | +[python_logging]: https://docs.python.org/3/library/logging.html |
| 265 | +[source_code]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/discovery/azure-ai-discovery |
| 266 | +[pypi]: https://pypi.org/project/azure-ai-discovery/ |
| 267 | +[samples]: https://github.com/Azure/azure-sdk-for-python/tree/main/sdk/discovery/azure-ai-discovery/samples |
| 268 | +[product_docs]: https://learn.microsoft.com/azure/microsoft-discovery/ |
0 commit comments