|
| 1 | +from typing import Literal, Optional, List |
| 2 | +from sempy_labs._helper_functions import ( |
| 3 | + resolve_item_id, |
| 4 | + resolve_workspace_id, |
| 5 | + _base_api, |
| 6 | +) |
| 7 | +from uuid import UUID |
| 8 | +from sempy._utils._log import log |
| 9 | +import sempy_labs._icons as icons |
| 10 | +from sempy_labs.kql_database import resolve_cluster_uri |
| 11 | + |
| 12 | + |
| 13 | +@log |
| 14 | +def nl_to_kql( |
| 15 | + kql_database: str | UUID, |
| 16 | + billing_item: str | UUID, |
| 17 | + billing_item_type: Literal["KQLQueryset", "KQLDashboard", "Eventhouse"], |
| 18 | + prompt: str, |
| 19 | + chat_messages: Optional[dict | List[dict]] = None, |
| 20 | + user_shots: Optional[List[dict]] = None, |
| 21 | + workspace: Optional[str | UUID] = None, |
| 22 | +) -> str: |
| 23 | + """ |
| 24 | + Returns a KQL query generated from natural language. |
| 25 | +
|
| 26 | + This is a wrapper function for the following API: `Copilot - NL To KQL <https://learn.microsoft.com/rest/api/fabric/realtimeintelligence/copilot/nl-to-kql(beta)>`_. |
| 27 | +
|
| 28 | + Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples). |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + kql_database : str | uuid.UUID |
| 33 | + The name or UUID of the KQL database. |
| 34 | + billing_item : str | uuid.UUID |
| 35 | + The name or ID of the item for the request. This can be a KQLQueryset, KQLDashboard, or Eventhouse item. This item is used for billing the request. |
| 36 | + billing_item_type : typing.Literal["KQLQueryset", "KQLDashboard", "Eventhouse"] |
| 37 | + The type of the billing item. |
| 38 | + prompt : str |
| 39 | + The natural language to generate the KQL query from. |
| 40 | + chat_messages : Optional[str | List[str]], default=None |
| 41 | + The `chat messages <https://learn.microsoft.com/rest/api/fabric/realtimeintelligence/copilot/nl-to-kql(beta)?tabs=HTTP#chatmessage>`_ for the request. The chat messages provide additional context for generating the KQL query if necessary. |
| 42 | +
|
| 43 | + Example: |
| 44 | + chat_messages = [ |
| 45 | + { |
| 46 | + "content": "Content....", |
| 47 | + "role": "User" |
| 48 | + }, |
| 49 | + { |
| 50 | + "content": "Content...", |
| 51 | + "role": "Assistant" |
| 52 | + } |
| 53 | + ] |
| 54 | + user_shots : Optional[List[str]], default=None |
| 55 | + The `user shots <https://learn.microsoft.com/rest/api/fabric/realtimeintelligence/copilot/nl-to-kql(beta)?tabs=HTTP#usershot>`_ for the request. This consists of user provided pairs of natural language and KQL queries in order to help in generating the current requested KQL query. |
| 56 | +
|
| 57 | + Example: |
| 58 | + user_shots = [ |
| 59 | + { |
| 60 | + "kqlQuery": "KQL Query....", |
| 61 | + "naturalLanguage": "Natural language...." |
| 62 | + }, |
| 63 | + { |
| 64 | + "kqlQuery": "KQL Query...", |
| 65 | + "naturalLanguage": "Natural language..." |
| 66 | + } |
| 67 | + ] |
| 68 | + workspace : str | uuid.UUID, default=None |
| 69 | + The Fabric workspace name or ID. |
| 70 | + Defaults to None which resolves to the workspace of the attached lakehouse |
| 71 | + or if no lakehouse attached, resolves to the workspace of the notebook. |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + str |
| 76 | + The generated KQL query. |
| 77 | + """ |
| 78 | + |
| 79 | + workspace_id = resolve_workspace_id(workspace=workspace) |
| 80 | + db_id = resolve_item_id( |
| 81 | + item=kql_database, type="KQLDatabase", workspace=workspace_id |
| 82 | + ) |
| 83 | + cluster_uri = resolve_cluster_uri(kql_database=kql_database, workspace=workspace_id) |
| 84 | + |
| 85 | + valid_types = ["KQLQueryset", "KQLDashboard", "Eventhouse"] |
| 86 | + if billing_item_type not in valid_types: |
| 87 | + raise ValueError( |
| 88 | + f"{icons.red_dot} The billing_item_type must be either 'KQLQueryset', 'KQLDashboard', or 'Eventhouse'." |
| 89 | + ) |
| 90 | + |
| 91 | + billing_item_id = resolve_item_id( |
| 92 | + item=billing_item, type=billing_item_type, workspace=workspace_id |
| 93 | + ) |
| 94 | + |
| 95 | + payload = { |
| 96 | + "clusterUrl": cluster_uri, |
| 97 | + "databaseName": db_id, |
| 98 | + "itemIdForBilling": billing_item_id, |
| 99 | + "naturalLanguage": prompt, |
| 100 | + } |
| 101 | + |
| 102 | + if chat_messages: |
| 103 | + if isinstance(chat_messages, str): |
| 104 | + chat_messages = [chat_messages] |
| 105 | + payload["chatMessages"] = chat_messages |
| 106 | + if user_shots: |
| 107 | + if isinstance(user_shots, str): |
| 108 | + user_shots = [user_shots] |
| 109 | + payload["userShots"] = user_shots |
| 110 | + |
| 111 | + response = _base_api( |
| 112 | + request=f"/v1/workspaces/{workspace_id}/realtimeintelligence/nltokql?beta=True", |
| 113 | + payload=payload, |
| 114 | + method="post", |
| 115 | + client="fabric_sp", |
| 116 | + ) |
| 117 | + return response.get("kqlQuery") |
0 commit comments