|
1 | 1 | import multiprocessing |
2 | 2 | import os |
| 3 | +from typing import Dict |
| 4 | +import asyncio |
| 5 | +from azure.ai.projects.aio import AIProjectClient |
| 6 | +from azure.ai.projects.models import FilePurpose, FileSearchTool, AsyncToolSet |
| 7 | +from azure.identity import DefaultAzureCredential |
3 | 8 |
|
4 | 9 | from dotenv import load_dotenv |
5 | 10 |
|
6 | 11 | load_dotenv() |
7 | 12 |
|
| 13 | +async def list_or_create_agent(): |
| 14 | + files: Dict[str, Dict[str, str]] = {} # File name -> {"id": file_id, "path": file_path} |
| 15 | + vector_store = None |
| 16 | + agent = None |
| 17 | + |
| 18 | + try: |
| 19 | + ai_client = AIProjectClient.from_connection_string( |
| 20 | + credential=DefaultAzureCredential(exclude_shared_token_cache_credential=True), |
| 21 | + conn_str=os.environ["AZURE_AIPROJECT_CONNECTION_STRING"], |
| 22 | + ) |
| 23 | + |
| 24 | + if os.environ.get("AZURE_AI_AGENT_ID"): |
| 25 | + try: |
| 26 | + agent = await ai_client.agents.get_agent(os.environ["AZURE_AI_AGENT_ID"]) |
| 27 | + return |
| 28 | + except Exception as e: |
| 29 | + print(f"Error fetching agent: {e}") |
| 30 | + |
| 31 | + # Check if a previous agent created by the template exists |
| 32 | + agent_list = await ai_client.agents.list_agents() |
| 33 | + if agent_list.data: |
| 34 | + for agent_object in agent_list.data: |
| 35 | + if agent_object.name == "agent-template-assistant": |
| 36 | + return |
| 37 | + |
| 38 | + # Create a new agent with the required resources |
| 39 | + print(f"Creating new agent with resources") |
| 40 | + file_names = ["product_info_1.md", "product_info_2.md"] |
| 41 | + for file_name in file_names: |
| 42 | + file_path = os.path.abspath(os.path.join(os.path.dirname(__file__), 'files', file_name)) |
| 43 | + file = await ai_client.agents.upload_file_and_poll(file_path=file_path, purpose=FilePurpose.AGENTS) |
| 44 | + # Store both file id and the file path using the file name as key. |
| 45 | + files[file_name] = {"id": file.id, "path": file_path} |
| 46 | + |
| 47 | + # Create the vector store using the file IDs. |
| 48 | + vector_store = await ai_client.agents.create_vector_store_and_poll( |
| 49 | + file_ids=[info["id"] for info in files.values()], |
| 50 | + name="sample_store" |
| 51 | + ) |
| 52 | + print(f"agent: file store and vector store success") |
| 53 | + |
| 54 | + file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id]) |
| 55 | + toolset = AsyncToolSet() |
| 56 | + toolset.add(file_search_tool) |
| 57 | + |
| 58 | + agent = await ai_client.agents.create_agent( |
| 59 | + model=os.environ["AZURE_AI_AGENT_DEPLOYMENT_NAME"], |
| 60 | + name="agent-template-assistant", |
| 61 | + instructions="You are helpful assistant", |
| 62 | + toolset=toolset |
| 63 | + ) |
| 64 | + print(f"Created agent, agent ID: {agent.id}") |
| 65 | + |
| 66 | + except Exception as e: |
| 67 | + print(f"Error creating agent: {e}", exc_info=True) |
| 68 | + raise RuntimeError(f"Failed to create the agent: {e}") |
| 69 | + |
| 70 | +def on_starting(server): |
| 71 | + """This code runs once before the workers will start.""" |
| 72 | + asyncio.get_event_loop().run_until_complete(list_or_create_agent()) |
| 73 | + |
8 | 74 | max_requests = 1000 |
9 | 75 | max_requests_jitter = 50 |
10 | 76 | log_file = "-" |
|
13 | 79 | if not os.getenv("RUNNING_IN_PRODUCTION"): |
14 | 80 | reload = True |
15 | 81 |
|
| 82 | +preload_app = True |
16 | 83 | num_cpus = multiprocessing.cpu_count() |
17 | | -workers = 1 #(num_cpus * 2) + 1 |
| 84 | +workers = (num_cpus * 2) + 1 |
18 | 85 | worker_class = "uvicorn.workers.UvicornWorker" |
19 | 86 |
|
20 | 87 | timeout = 120 |
0 commit comments