1
1
import multiprocessing
2
2
import os
3
+ import sys
3
4
from typing import Dict
4
5
import asyncio
6
+ import logging
5
7
from azure .ai .projects .aio import AIProjectClient
6
8
from azure .ai .projects .models import FilePurpose , FileSearchTool , AsyncToolSet
7
9
from azure .identity import DefaultAzureCredential
10
12
11
13
load_dotenv ()
12
14
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
+
13
35
async def list_or_create_agent ():
14
36
files : Dict [str , Dict [str , str ]] = {} # File name -> {"id": file_id, "path": file_path}
15
37
vector_store = None
@@ -26,17 +48,17 @@ async def list_or_create_agent():
26
48
agent = await ai_client .agents .get_agent (os .environ ["AZURE_AI_AGENT_ID" ])
27
49
return
28
50
except Exception as e :
29
- print ( f "Error fetching agent: { e } " )
51
+ logger . info ( "Error with agent ID " )
30
52
31
53
# Check if a previous agent created by the template exists
32
54
agent_list = await ai_client .agents .list_agents ()
33
55
if agent_list .data :
34
56
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" ] :
36
58
return
37
59
38
60
# Create a new agent with the required resources
39
- print ( f "Creating new agent with resources" )
61
+ logger . info ( "Creating new agent with resources" )
40
62
file_names = ["product_info_1.md" , "product_info_2.md" ]
41
63
for file_name in file_names :
42
64
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():
49
71
file_ids = [info ["id" ] for info in files .values ()],
50
72
name = "sample_store"
51
73
)
52
- print ( f "agent: file store and vector store success" )
74
+ logger . info ( "agent: file store and vector store success" )
53
75
54
76
file_search_tool = FileSearchTool (vector_store_ids = [vector_store .id ])
55
77
toolset = AsyncToolSet ()
56
78
toolset .add (file_search_tool )
57
79
58
80
agent = await ai_client .agents .create_agent (
59
81
model = os .environ ["AZURE_AI_AGENT_DEPLOYMENT_NAME" ],
60
- name = "agent-template-assistant" ,
82
+ name = os . environ [ "AZURE_AI_AGENT_NAME" ] ,
61
83
instructions = "You are helpful assistant" ,
62
84
toolset = toolset
63
85
)
64
- print ( f "Created agent, agent ID: { agent .id } " )
86
+ logger . info ( "Created agent, agent ID: {agent.id}" )
65
87
66
88
except Exception as e :
67
- print ( f "Error creating agent: { e } " , exc_info = True )
89
+ logger . info ( "Error creating agent: {e}" , exc_info = True )
68
90
raise RuntimeError (f"Failed to create the agent: { e } " )
69
91
70
92
def on_starting (server ):
0 commit comments