@@ -67,24 +67,45 @@ class ChatWithDataPlugin:
6767 @kernel_function (name = "Greeting" , description = "Respond to any greeting or general questions" )
6868 def greeting (self , input : Annotated [str , "the question" ]) -> Annotated [str , "The output is a string" ]:
6969 query = input
70-
7170 deployment = os .environ .get ("AZURE_OPEN_AI_DEPLOYMENT_MODEL" )
72- project_connection_string = os .environ .get ("AZURE_AI_PROJECT_CONN_STRING" )
73- project = AIProjectClient .from_connection_string (
74- conn_str = project_connection_string ,
75- credential = DefaultAzureCredential ()
76- )
77- client = project .inference .get_chat_completions_client ()
71+ use_ai_project_client = os .environ .get ("USE_AI_PROJECT_CLIENT" , "False" ).lower () == "true"
7872
7973 try :
80- completion = client .complete (
81- model = deployment ,
82- messages = [
83- {"role" : "system" , "content" : "You are a helpful assistant to repond to any greeting or general questions." },
84- {"role" : "user" , "content" : query },
85- ],
86- temperature = 0 ,
87- )
74+ if use_ai_project_client :
75+ project_connection_string = os .environ .get ("AZURE_AI_PROJECT_CONN_STRING" )
76+ project = AIProjectClient .from_connection_string (
77+ conn_str = project_connection_string ,
78+ credential = DefaultAzureCredential ()
79+ )
80+ client = project .inference .get_chat_completions_client ()
81+
82+ completion = client .complete (
83+ model = deployment ,
84+ messages = [
85+ {"role" : "system" , "content" : "You are a helpful assistant to respond to any greeting or general questions." },
86+ {"role" : "user" , "content" : query },
87+ ],
88+ temperature = 0 ,
89+ )
90+ else :
91+ endpoint = os .environ .get ("AZURE_OPEN_AI_ENDPOINT" )
92+ api_key = os .environ .get ("AZURE_OPEN_AI_API_KEY" )
93+ api_version = os .environ .get ("OPENAI_API_VERSION" )
94+
95+ client = openai .AzureOpenAI (
96+ azure_endpoint = endpoint ,
97+ api_key = api_key ,
98+ api_version = api_version
99+ )
100+
101+ completion = client .chat .completions .create (
102+ model = deployment ,
103+ messages = [
104+ {"role" : "system" , "content" : "You are a helpful assistant to respond to any greeting or general questions." },
105+ {"role" : "user" , "content" : query },
106+ ],
107+ temperature = 0 ,
108+ )
88109 answer = completion .choices [0 ].message .content
89110 except Exception as e :
90111 answer = str (e ) # 'Information from database could not be retrieved. Please try again later.'
@@ -96,44 +117,65 @@ def get_SQL_Response(
96117 self ,
97118 input : Annotated [str , "the question" ]
98119 ):
99-
100120 query = input
101-
102- deployment = os .environ .get ("AZURE_OPEN_AI_DEPLOYMENT_MODEL" )
103- project_connection_string = os .environ .get ("AZURE_AI_PROJECT_CONN_STRING" )
104- project = AIProjectClient .from_connection_string (
105- conn_str = project_connection_string ,
106- credential = DefaultAzureCredential ()
107- )
108- client = project .inference .get_chat_completions_client ()
109-
121+ deployment = os .environ .get ("AZURE_OPEN_AI_DEPLOYMENT_MODEL" )
122+ use_ai_project_client = os .environ .get ("USE_AI_PROJECT_CLIENT" , "False" ).lower () == "true"
123+
110124 sql_prompt = f'''A valid T-SQL query to find { query } for tables and columns provided below:
111- 1. Table: km_processed_data
112- Columns: ConversationId,EndTime,StartTime,Content,summary,satisfied,sentiment,topic,keyphrases,complaint
113- 2. Table: processed_data_key_phrases
114- Columns: ConversationId,key_phrase,sentiment
115- Use ConversationId as the primary key as the primary key in tables for queries but not for any other operations.
116- Only return the generated sql query. do not return anything else.'''
125+ 1. Table: km_processed_data
126+ Columns: ConversationId,EndTime,StartTime,Content,summary,satisfied,sentiment,topic,keyphrases,complaint
127+ 2. Table: processed_data_key_phrases
128+ Columns: ConversationId,key_phrase,sentiment
129+ Use ConversationId as the primary key as the primary key in tables for queries but not for any other operations.
130+ Only return the generated sql query. do not return anything else.'''
131+
117132 try :
118-
119- completion = client .complete (
120- model = deployment ,
121- messages = [
122- {"role" : "system" , "content" : "You are a helpful assistant." },
123- {"role" : "user" , "content" : sql_prompt },
124- ],
125- temperature = 0 ,
126- )
127- sql_query = completion .choices [0 ].message .content
128- sql_query = sql_query .replace ("```sql" ,'' ).replace ("```" ,'' )
129-
130- conn = get_db_connection ()
131-
132- cursor = conn .cursor ()
133- cursor .execute (sql_query )
134- answer = ''
135- for row in cursor .fetchall ():
136- answer += str (row )
133+ if use_ai_project_client :
134+ project_connection_string = os .environ .get ("AZURE_AI_PROJECT_CONN_STRING" )
135+ project = AIProjectClient .from_connection_string (
136+ conn_str = project_connection_string ,
137+ credential = DefaultAzureCredential ()
138+ )
139+ client = project .inference .get_chat_completions_client ()
140+
141+ completion = client .complete (
142+ model = deployment ,
143+ messages = [
144+ {"role" : "system" , "content" : "You are a helpful assistant." },
145+ {"role" : "user" , "content" : sql_prompt },
146+ ],
147+ temperature = 0 ,
148+ )
149+ sql_query = completion .choices [0 ].message .content
150+ sql_query = sql_query .replace ("```sql" ,'' ).replace ("```" ,'' )
151+ else :
152+ endpoint = os .environ .get ("AZURE_OPEN_AI_ENDPOINT" )
153+ api_key = os .environ .get ("AZURE_OPEN_AI_API_KEY" )
154+ api_version = os .environ .get ("OPENAI_API_VERSION" )
155+
156+ client = openai .AzureOpenAI (
157+ azure_endpoint = endpoint ,
158+ api_key = api_key ,
159+ api_version = api_version
160+ )
161+
162+ completion = client .chat .completions .create (
163+ model = deployment ,
164+ messages = [
165+ {"role" : "system" , "content" : "You are a helpful assistant." },
166+ {"role" : "user" , "content" : sql_prompt },
167+ ],
168+ temperature = 0 ,
169+ )
170+ sql_query = completion .choices [0 ].message .content
171+ sql_query = sql_query .replace ("```sql" ,'' ).replace ("```" ,'' )
172+
173+ with get_db_connection () as conn :
174+ with conn .cursor () as cursor :
175+ cursor .execute (sql_query )
176+ answer = ''
177+ for row in cursor .fetchall ():
178+ answer += str (row )
137179 except Exception as e :
138180 answer = str (e ) # 'Information from database could not be retrieved. Please try again later.'
139181 return answer
0 commit comments