Skip to content

Commit c2c9655

Browse files
tomaadadityatomar
and
adityatomar
authored
Integrate changes for using OPen AI (#2)
Co-authored-by: adityatomar <[email protected]>
1 parent 116279a commit c2c9655

File tree

7 files changed

+577
-214
lines changed

7 files changed

+577
-214
lines changed

apis.py

+548
Large diffs are not rendered by default.

app.py

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import kustoQuery
3-
import utils
3+
import apis
44
from azure.kusto.data import KustoClient
55
from azure.kusto.data.exceptions import KustoServiceError
66
from azure.kusto.data.helpers import dataframe_from_result_table
@@ -21,35 +21,37 @@ def favicon():
2121
return send_from_directory(os.path.join(app.root_path, 'static'),
2222
'favicon.ico', mimetype='image/vnd.microsoft.icon')
2323

24-
@app.route('/hello', methods=['POST'])
25-
def hello():
26-
name = request.form.get('name')
24+
@app.route('/execute_prompt', methods=['POST'])
25+
def execute_prompt():
26+
user_input = request.form.get('prompt')
2727

28-
if name:
29-
print('Request for hello page received with name=%s' % name)
30-
31-
print("Kusto Query App is starting...")
28+
if user_input:
29+
print('Prompt=%s' % user_input)
30+
response = apis.call_openai(apis.system_prompt, user_input)
31+
# Printing the result
32+
print(response)
33+
# print("Kusto Query App is starting...")
3234

3335
app = kustoQuery.KustoQueryApp()
3436
app.load_configs(app.CONFIG_FILE_NAME)
3537

36-
if app.config.authentication_mode == "UserPrompt":
37-
app.wait_for_user_to_proceed("You will be prompted for credentials during this script. Please return to the console after authenticating.")
38+
# if app.config.authentication_mode == "UserPrompt":
39+
# app.wait_for_user_to_proceed("You will be prompted for credentials during this script. Please return to the console after authenticating.")
3840

39-
kusto_connection_string = utils.Utils.Authentication.generate_connection_string(app.config.kusto_uri, app.config.authentication_mode)
41+
kusto_connection_string = apis.Utils.Authentication.generate_connection_string(app.config.kusto_uri, app.config.authentication_mode)
4042
print(f"Using cluster URI: {app.config.kusto_uri}")
4143

4244
if not kusto_connection_string:
43-
utils.Utils.error_handler("Connection String error. Please validate your configuration file.")
45+
apis.Utils.error_handler("Connection String error. Please validate your configuration file.")
4446
else:
4547
with KustoClient(kusto_connection_string) as kusto_client:
46-
app.query_table(kusto_client, app.config.database_name, app.config.table_name)
48+
df = app.query_table(kusto_client, app.config.database_name, app.config.table_name, response)
4749

4850
print("\nKusto Query App done")
4951

50-
return render_template('hello.html', name = name)
52+
return render_template('response.html', output = df.to_string(index=False))
5153
else:
52-
print('Request for hello page received with no name or blank name -- redirecting')
54+
print('Request received without any prompt from the user or blank prompt -- redirecting')
5355
return redirect(url_for('index'))
5456

5557
if __name__ == '__main__':

kustoQuery.py

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import json
2+
import pandas as pd
23
from dataclasses import dataclass
34

45
from azure.kusto.data import KustoClient
56
from azure.kusto.data.exceptions import KustoServiceError
67
from azure.kusto.data.helpers import dataframe_from_result_table
7-
from utils import AuthenticationModeOptions, Utils
8+
from apis import AuthenticationModeOptions, Utils
89

910
@dataclass
1011
class ConfigJson:
@@ -47,28 +48,15 @@ def load_configs(cls, config_file_name: str) -> None:
4748
Utils.error_handler(f"Couldn't read config file '{config_file_name}'", ex)
4849

4950
@classmethod
50-
def query_table(cls, kusto_client: KustoClient, database_name: str, table_name: str) -> None:
51+
def query_table(cls, kusto_client: KustoClient, database_name: str, table_name: str, command: str) -> pd.DataFrame:
5152
"""
5253
Execute queries on the Kusto table.
5354
:param kusto_client: Client to run queries
5455
:param database_name: DB Name
5556
:param table_name: Table Name
5657
"""
58+
df = pd.DataFrame()
5759
try:
58-
escaped_table = f"['{table_name}']"
59-
60-
# Get row count
61-
# cls.wait_for_user_to_proceed(f"Get row count for '{database_name}.{table_name}':")
62-
command = f"{escaped_table} | count"
63-
print(f"Executing command: {command}")
64-
response = kusto_client.execute_query(database_name, command)
65-
if response and response.primary_results:
66-
count = response.primary_results[0][0][0]
67-
print(f"Row count: {count}")
68-
69-
# Get sample rows
70-
# cls.wait_for_user_to_proceed(f"Get first two rows from '{database_name}.{table_name}':")
71-
command = f"{escaped_table} | take 2"
7260
print(f"Executing command: {command}")
7361
response = kusto_client.execute_query(database_name, command)
7462
if response and response.primary_results:
@@ -84,6 +72,7 @@ def query_table(cls, kusto_client: KustoClient, database_name: str, table_name:
8472
print(f"Error: {ex}")
8573
print(f"Failed command: {command}")
8674
raise ex
75+
return df
8776

8877
@classmethod
8978
def wait_for_user_to_proceed(cls, prompt_msg: str) -> None:

requirements.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Flask==3.1.0
22
gunicorn
33
azure-kusto-data>=0.0.15
4-
pandas>=1.0.0
4+
pandas>=1.0.0
5+
utils
6+
openai

templates/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ <h1 class="display-6 fw-bold text-primary">XInsights</h1>
5151
<div class="row">
5252
<!-- Query Input Section -->
5353
<div class="col-md-8 mx-auto mb-4">
54-
<form method="post" action="{{url_for('hello')}}">
54+
<form method="post" action="{{url_for('execute_prompt')}}">
5555
<div class="card">
5656
<div class="card-body">
5757
<h5 class="card-title mb-3">Enter Your Query</h5>
58-
<textarea class="form-control mb-3" id="name" name="name" rows="3" placeholder="Type or select a query..."></textarea>
58+
<textarea class="form-control mb-3" id="prompt" name="prompt" rows="3" placeholder="Type or select a query..."></textarea>
5959
<button type="submit" class="btn btn-primary">Submit Query</button>
6060
</div>
6161
</div>

templates/hello.html renamed to templates/response.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@
4848
<div class="col-md-10 mx-auto">
4949
<div class="card mb-4">
5050
<div class="card-header bg-primary text-white">
51-
<h5 class="card-title mb-0">Your Query</h5>
51+
<h5 class="card-title mb-0">Response</h5>
5252
</div>
5353
<div class="card-body">
54-
<p class="card-text">{{name}}</p>
54+
{{output}}
5555
</div>
5656
</div>
5757

utils.py

-178
This file was deleted.

0 commit comments

Comments
 (0)