Skip to content

Commit 41c5b8c

Browse files
committed
Merge branch 'm-kovalsky/rti-nltkql'
2 parents ad15bc1 + e1704a6 commit 41c5b8c

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

src/sempy_labs/eventhouse/_items.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def list_eventhouses(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
9494
"Eventhouse Name": "string",
9595
"Eventhouse Id": "string",
9696
"Description": "string",
97+
"Query Service URI": "string",
98+
"Ingestion Service URI": "string",
99+
"Database Item Ids": "list",
100+
"Minimum Consumption Units": "float",
97101
}
98102
df = _create_dataframe(columns=columns)
99103

@@ -108,11 +112,16 @@ def list_eventhouses(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
108112
rows = []
109113
for r in responses:
110114
for v in r.get("value", []):
115+
p = v.get("properties", {})
111116
rows.append(
112117
{
113118
"Eventhouse Name": v.get("displayName"),
114119
"Eventhouse Id": v.get("id"),
115120
"Description": v.get("description"),
121+
"Query Service URI": p.get("queryServiceUri"),
122+
"Ingestion Service URI": p.get("ingestionServiceUri"),
123+
"Database Item Ids": p.get("databasesItemIds"),
124+
"Minimum Consumption Units": p.get("minimumConsumptionUnits"),
116125
}
117126
)
118127

src/sempy_labs/notebook/_items.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def get_notebook_definition(
7474
7575
This is a wrapper function for the following API: `Items - Get Notebook Definition <https://learn.microsoft.com/rest/api/fabric/notebook/items/get-notebook-definition>`_.
7676
77+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
78+
7779
Parameters
7880
----------
7981
notebook_name : str
@@ -193,6 +195,10 @@ def create_notebook(
193195
"""
194196
Creates a new notebook with a definition within a workspace.
195197
198+
This is a wrapper function for the following API: `Items - Create Notebook Definition <https://learn.microsoft.com/rest/api/fabric/notebook/items/create-notebook>`_.
199+
200+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
201+
196202
Parameters
197203
----------
198204
name : str
@@ -253,6 +259,10 @@ def update_notebook_definition(
253259
"""
254260
Updates an existing notebook with a new definition.
255261
262+
This is a wrapper function for the following API: `Items - Update Notebook Definition <https://learn.microsoft.com/rest/api/fabric/notebook/items/update-notebook-definition>`_.
263+
264+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
265+
256266
Parameters
257267
----------
258268
name : str
@@ -308,6 +318,10 @@ def list_notebooks(workspace: Optional[str | UUID] = None) -> pd.DataFrame:
308318
"""
309319
Shows the notebooks within a workspace.
310320
321+
This is a wrapper function for the following API: `Items - List Notebooks <https://learn.microsoft.com/rest/api/fabric/notebook/items/list-notebooks>`_.
322+
323+
Service Principal Authentication is supported (see `here <https://github.com/microsoft/semantic-link-labs/blob/main/notebooks/Service%20Principal.ipynb>`_ for examples).
324+
311325
Parameters
312326
----------
313327
workspace : str | uuid.UUID, default=None

src/sempy_labs/rti/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from ._copilot import (
2+
nl_to_kql,
3+
)
4+
5+
__all__ = [
6+
"nl_to_kql",
7+
]

src/sempy_labs/rti/_copilot.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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

Comments
 (0)