@@ -235,6 +235,7 @@ async def stream_openai_text(conversation_id: str, query: str) -> StreamingRespo
235235 logger .info ("No response received from OpenAI." )
236236 yield "I cannot answer this question with the current data. Please rephrase or add more details."
237237
238+
238239async def stream_openai_text_workshop (conversation_id : str , query : str ) -> StreamingResponse :
239240 """
240241 Get a streaming text response from OpenAI with workshop mode using responses.create().
@@ -256,55 +257,45 @@ async def stream_openai_text_workshop(conversation_id: str, query: str) -> Strea
256257 credential = credential
257258 ) as project_client :
258259
259- cache = get_thread_cache ()
260- conv_id = cache .get (conversation_id , None )
261-
262260 # Get database connection based on AZURE_ENV_ONLY flag
263261 from history_sql import SqlQueryTool , get_azure_sql_connection , get_fabric_db_connection
264-
262+
265263 if AZURE_ENV_ONLY :
266264 logger .info ("Workshop mode: Using Azure SQL Database" )
267265 db_connection = await get_azure_sql_connection ()
268266 else :
269267 logger .info ("Workshop mode: Using Fabric Lakehouse SQL" )
270268 db_connection = await get_fabric_db_connection ()
271-
269+
272270 if not db_connection :
273271 logger .error ("Failed to establish database connection" )
274272 raise Exception ("Database connection failed" )
275-
273+
276274 custom_tool = SqlQueryTool (pyodbc_conn = db_connection )
277275
278276 openai_client = project_client .get_openai_client ()
279-
280- # Create or retrieve conversation
281- if not conv_id :
282- conv = await openai_client .conversations .create ()
283- conv_id = conv .id
284- cache [conversation_id ] = conv_id
285277
286278 # Initial request to the agent
287279 response = await openai_client .responses .create (
288- conversation = conv_id ,
289280 input = query ,
290281 extra_body = {"agent" : {"name" : os .getenv ("AGENT_NAME_CHAT" ), "type" : "agent_reference" }}
291282 )
292-
283+
293284 # Process response - handle function calls and search
294285 max_iterations = 10
295286 iteration = 0
296-
287+
297288 while iteration < max_iterations :
298289 iteration += 1
299-
290+
300291 # Check for function calls and tool uses in output
301292 function_calls = []
302293 text_output = ""
303294 search_results = []
304-
295+
305296 for item in response .output :
306297 item_type = getattr (item , 'type' , None )
307-
298+
308299 if item_type == 'function_call' :
309300 function_calls .append (item )
310301 elif item_type == 'message' :
@@ -332,22 +323,22 @@ async def stream_openai_text_workshop(conversation_id: str, query: str) -> Strea
332323 # Handle search tool output (result)
333324 elif item_type == 'azure_ai_search_call_output' :
334325 logger .info ("AI Search completed" )
335-
326+
336327 # If no function calls, we're done
337328 if not function_calls :
338329 if text_output :
339330 complete_response += text_output
340331 yield text_output
341332 break
342-
333+
343334 # Handle function calls
344335 tool_outputs = []
345336 for fc in function_calls :
346337 func_name = fc .name
347338 func_args = json .loads (fc .arguments )
348-
339+
349340 logger .info ("Calling function: %s" , func_name )
350-
341+
351342 if func_name == "execute_sql" :
352343 sql_query = func_args .get ("sql_query" , "" )
353344 logger .info ("Executing SQL query: %s" , sql_query [:100 ])
@@ -363,13 +354,13 @@ async def stream_openai_text_workshop(conversation_id: str, query: str) -> Strea
363354 else :
364355 result_str = f"Unknown function: { func_name } "
365356 logger .warning ("Unknown function called: %s" , func_name )
366-
357+
367358 tool_outputs .append ({
368359 "type" : "function_call_output" ,
369360 "call_id" : fc .call_id ,
370361 "output" : result_str
371362 })
372-
363+
373364 # Submit tool outputs and get next response
374365 # Note: Don't include 'conversation' when using 'previous_response_id'
375366 response = await openai_client .responses .create (
@@ -379,19 +370,14 @@ async def stream_openai_text_workshop(conversation_id: str, query: str) -> Strea
379370 "previous_response_id" : response .id
380371 }
381372 )
382-
373+
383374 if iteration >= max_iterations :
384375 logger .warning ("Max iterations reached in workshop mode" )
385376 yield "\n \n (Response processing reached maximum iterations)"
386377
387378 except Exception as e :
388379 complete_response = str (e )
389380 logger .error ("Error in stream_openai_text_workshop: %s" , e )
390- cache = get_thread_cache ()
391- conv_id = cache .pop (conversation_id , None )
392- if conv_id is not None :
393- corrupt_key = f"{ conversation_id } _corrupt_{ random .randint (1000 , 9999 )} "
394- cache [corrupt_key ] = conv_id
395381 raise HTTPException (status_code = status .HTTP_500_INTERNAL_SERVER_ERROR , detail = "Error streaming OpenAI text" ) from e
396382
397383 finally :
0 commit comments