11import multiprocessing
22import os
3+ import sys
34from typing import Dict
45import asyncio
6+ import logging
57from azure .ai .projects .aio import AIProjectClient
68from azure .ai .projects .models import FilePurpose , FileSearchTool , AsyncToolSet
79from azure .identity import DefaultAzureCredential
1012
1113load_dotenv ()
1214
15+ # Create a central logger for the application
16+ logger = logging .getLogger ("azureaiapp" )
17+ logger .setLevel (logging .INFO )
18+
19+ # Configure the stream handler (stdout)
20+ stream_handler = logging .StreamHandler (sys .stdout )
21+ stream_handler .setLevel (logging .INFO )
22+ stream_formatter = logging .Formatter ("%(asctime)s [%(levelname)s] %(name)s: %(message)s" )
23+ stream_handler .setFormatter (stream_formatter )
24+ logger .addHandler (stream_handler )
25+
26+ # Configure logging to file, if log file name is provided
27+ log_file_name = os .getenv ("APP_LOG_FILE" , "" )
28+ if log_file_name != "" :
29+ file_handler = logging .FileHandler (log_file_name )
30+ file_handler .setLevel (logging .INFO )
31+ file_formatter = logging .Formatter ("%(asctime)s [%(levelname)s] %(name)s: %(message)s" )
32+ file_handler .setFormatter (file_formatter )
33+ logger .addHandler (file_handler )
34+
1335async def list_or_create_agent ():
1436 files : Dict [str , Dict [str , str ]] = {} # File name -> {"id": file_id, "path": file_path}
1537 vector_store = None
@@ -26,17 +48,17 @@ async def list_or_create_agent():
2648 agent = await ai_client .agents .get_agent (os .environ ["AZURE_AI_AGENT_ID" ])
2749 return
2850 except Exception as e :
29- print ( f "Error fetching agent: { e } " )
51+ logger . info ( "Error with agent ID " )
3052
3153 # Check if a previous agent created by the template exists
3254 agent_list = await ai_client .agents .list_agents ()
3355 if agent_list .data :
3456 for agent_object in agent_list .data :
35- if agent_object .name == "agent-template-assistant" :
57+ if agent_object .name == os . environ [ "AZURE_AI_AGENT_NAME" ] :
3658 return
3759
3860 # Create a new agent with the required resources
39- print ( f "Creating new agent with resources" )
61+ logger . info ( "Creating new agent with resources" )
4062 file_names = ["product_info_1.md" , "product_info_2.md" ]
4163 for file_name in file_names :
4264 file_path = os .path .abspath (os .path .join (os .path .dirname (__file__ ), 'files' , file_name ))
@@ -49,22 +71,22 @@ async def list_or_create_agent():
4971 file_ids = [info ["id" ] for info in files .values ()],
5072 name = "sample_store"
5173 )
52- print ( f "agent: file store and vector store success" )
74+ logger . info ( "agent: file store and vector store success" )
5375
5476 file_search_tool = FileSearchTool (vector_store_ids = [vector_store .id ])
5577 toolset = AsyncToolSet ()
5678 toolset .add (file_search_tool )
5779
5880 agent = await ai_client .agents .create_agent (
5981 model = os .environ ["AZURE_AI_AGENT_DEPLOYMENT_NAME" ],
60- name = "agent-template-assistant" ,
82+ name = os . environ [ "AZURE_AI_AGENT_NAME" ] ,
6183 instructions = "You are helpful assistant" ,
6284 toolset = toolset
6385 )
64- print ( f "Created agent, agent ID: { agent .id } " )
86+ logger . info ( "Created agent, agent ID: {agent.id}" )
6587
6688 except Exception as e :
67- print ( f "Error creating agent: { e } " , exc_info = True )
89+ logger . info ( "Error creating agent: {e}" , exc_info = True )
6890 raise RuntimeError (f"Failed to create the agent: { e } " )
6991
7092def on_starting (server ):
0 commit comments