|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import struct |
| 6 | +from datetime import date, datetime |
| 7 | +from decimal import Decimal |
| 8 | +from xmlrpc import client |
| 9 | + |
| 10 | +from agent_framework import ChatAgent, HostedFileSearchTool |
| 11 | +from agent_framework.azure import AzureAIAgentClient |
| 12 | +from azure.ai.projects.aio import AIProjectClient |
| 13 | +from azure.identity.aio import AzureCliCredential |
| 14 | +# import pyodbc |
| 15 | +from dotenv import load_dotenv |
| 16 | +from pydantic import BaseModel, ConfigDict |
| 17 | + |
| 18 | +# Load environment variables from .env file |
| 19 | +load_dotenv() |
| 20 | + |
| 21 | +# import argparse |
| 22 | + |
| 23 | +# p = argparse.ArgumentParser() |
| 24 | +# p.add_argument("--ai_project_endpoint", required=True) |
| 25 | +# p.add_argument("--solution_name", required=True) |
| 26 | +# p.add_argument("--gpt_model_name", required=True) |
| 27 | +# args = p.parse_args() |
| 28 | + |
| 29 | +ai_project_endpoint = os.environ["AZURE_AI_PROJECT_ENDPOINT"] |
| 30 | +#solutionName = os.environ["AZURE_AI_PROJECT_ENDPOINT"] |
| 31 | +gptModelName = os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"] |
| 32 | + |
| 33 | +# # fetch all required env variables |
| 34 | +# ai_project_endpoint = os.getenv("AZURE_AI_AGENT_ENDPOINT") |
| 35 | +# solution_name = os.getenv("SOLUTION_NAME") |
| 36 | +# gpt_model_name = os.getenv("AZURE_AI_AGENT_MODEL_DEPLOYMENT_NAME") |
| 37 | +# app_env = os.getenv("APP_ENV", "prod").lower() |
| 38 | + |
| 39 | +# # ai_project_endpoint = 'https://aisa-ccblgmiensv4lga.services.ai.azure.com/api/projects/aifp-ccblgmiensv4lga' |
| 40 | +# ai_project_endpoint = 'https://testmodle.services.ai.azure.com/api/projects/testModle-project' |
| 41 | +# gpt_model_name = 'gpt-4o-mini' |
| 42 | + |
| 43 | +async def create_agents(): |
| 44 | + """Create and return orchestrator, SQL, and chart agent IDs.""" |
| 45 | + |
| 46 | + async with ( |
| 47 | + AzureCliCredential() as credential, |
| 48 | + AIProjectClient( |
| 49 | + endpoint=ai_project_endpoint, |
| 50 | + credential=credential, |
| 51 | + ) as project_client, |
| 52 | + ): |
| 53 | + # Create agents |
| 54 | + agents_client = project_client.agents |
| 55 | + # print("Creating agents...") |
| 56 | + |
| 57 | + |
| 58 | + # Create the client and manually create an agent with Azure AI Search tool |
| 59 | + from azure.ai.projects.models import ConnectionType |
| 60 | + ai_search_conn_id = "" |
| 61 | + async for connection in project_client.connections.list(): |
| 62 | + if connection.type == ConnectionType.AZURE_AI_SEARCH: |
| 63 | + ai_search_conn_id = connection.id |
| 64 | + break |
| 65 | + |
| 66 | + # 1. Create Azure AI agent with the search tool |
| 67 | + product_agent_instructions = '''You are a helpful agent that searches product information using Azure AI Search. |
| 68 | + Always use the search tool and index to find product data and provide accurate information. |
| 69 | + If you can not find the answer in the search tool, respond that you can't answer the question. |
| 70 | + Do not add any other information from your general knowledge.''' |
| 71 | + product_agent = await agents_client.create_agent( |
| 72 | + model=gptModelName, |
| 73 | + name="product_agent", |
| 74 | + instructions=product_agent_instructions, |
| 75 | + tools=[{"type": "azure_ai_search"}], |
| 76 | + tool_resources={ |
| 77 | + "azure_ai_search": { |
| 78 | + "indexes": [ |
| 79 | + { |
| 80 | + "index_connection_id": ai_search_conn_id, |
| 81 | + "index_name": "products_index", |
| 82 | + "query_type": "vector_simple_hybrid", # Use vector hybrid search |
| 83 | + } |
| 84 | + ] |
| 85 | + } |
| 86 | + }, |
| 87 | + ) |
| 88 | + |
| 89 | + |
| 90 | + # 1. Create Azure AI agent with the search tool |
| 91 | + policy_agent_instructions = '''You are a helpful agent that searches policy information using Azure AI Search. |
| 92 | + Always use the search tool and index to find policy data and provide accurate information. |
| 93 | + If you can not find the answer in the search tool, respond that you can't answer the question. |
| 94 | + Do not add any other information from your general knowledge.''' |
| 95 | + policy_agent = await agents_client.create_agent( |
| 96 | + model=gptModelName, |
| 97 | + name="policy_agent", |
| 98 | + instructions=policy_agent_instructions, |
| 99 | + tools=[{"type": "azure_ai_search"}], |
| 100 | + tool_resources={ |
| 101 | + "azure_ai_search": { |
| 102 | + "indexes": [ |
| 103 | + { |
| 104 | + "index_connection_id": ai_search_conn_id, |
| 105 | + "index_name": "policies_index", |
| 106 | + "query_type": "vector_simple_hybrid", # Use vector hybrid search |
| 107 | + } |
| 108 | + ] |
| 109 | + } |
| 110 | + }, |
| 111 | + ) |
| 112 | + |
| 113 | + chat_agent_instructions = '''You are a helpful assistant that can use the product agent and policy agent to answer user questions. |
| 114 | + If you don't find any information in the knowledge source, please say no data found.''' |
| 115 | + |
| 116 | + chat_agent = await agents_client.create_agent( |
| 117 | + model=gptModelName, |
| 118 | + name=f"chat_agent", |
| 119 | + instructions=chat_agent_instructions |
| 120 | + ) |
| 121 | + |
| 122 | + # Return agent IDs |
| 123 | + return product_agent.id, policy_agent.id, chat_agent.id |
| 124 | + |
| 125 | +product_agent_id, policy_agent_id, chat_agent_id = asyncio.run(create_agents()) |
| 126 | +print(f"chatAgentId={chat_agent_id}") |
| 127 | +print(f"productAgentId={product_agent_id}") |
| 128 | +print(f"policyAgentId={policy_agent_id}") |
| 129 | + |
| 130 | +# import json |
| 131 | +# from azure.ai.projects import AIProjectClient |
| 132 | +# import sys |
| 133 | +# import os |
| 134 | +# import argparse |
| 135 | +# sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) |
| 136 | +# from azure_credential_utils import get_azure_credential |
| 137 | +# from azure.ai.projects.models import ConnectionType |
| 138 | + |
| 139 | +# p = argparse.ArgumentParser() |
| 140 | +# p.add_argument("--ai_project_endpoint", required=True) |
| 141 | +# p.add_argument("--solution_name", required=True) |
| 142 | +# p.add_argument("--gpt_model_name", required=True) |
| 143 | +# args = p.parse_args() |
| 144 | + |
| 145 | +# ai_project_endpoint = args.ai_project_endpoint |
| 146 | +# solutionName = args.solution_name |
| 147 | +# gptModelName = args.gpt_model_name |
| 148 | + |
| 149 | +# project_client = AIProjectClient( |
| 150 | +# endpoint= ai_project_endpoint, |
| 151 | +# credential=get_azure_credential(), |
| 152 | +# ) |
| 153 | + |
| 154 | + |
| 155 | +# with project_client: |
| 156 | +# # Create agents |
| 157 | +# agents_client = project_client.agents |
| 158 | +# print("Creating agents...") |
| 159 | + |
| 160 | +# # Create the client and manually create an agent with Azure AI Search tool |
| 161 | +# ai_search_conn_id = "" |
| 162 | +# for connection in project_client.connections.list(): |
| 163 | +# if connection.type == ConnectionType.AZURE_AI_SEARCH: |
| 164 | +# ai_search_conn_id = connection.id |
| 165 | +# break |
| 166 | + |
| 167 | +# # 1. Create Azure AI agent with the search tool |
| 168 | +# product_agent_instructions = '''You are a helpful agent that searches product information using Azure AI Search. |
| 169 | +# Always use the search tool and index to find product data and provide accurate information. |
| 170 | +# If you can not find the answer in the search tool, respond that you can't answer the question. |
| 171 | +# Do not add any other information from your general knowledge.''' |
| 172 | +# product_agent = agents_client.create_agent( |
| 173 | +# model=gptModelName, |
| 174 | +# name="product_agent", |
| 175 | +# instructions=product_agent_instructions, |
| 176 | +# tools=[{"type": "azure_ai_search"}], |
| 177 | +# tool_resources={ |
| 178 | +# "azure_ai_search": { |
| 179 | +# "indexes": [ |
| 180 | +# { |
| 181 | +# "index_connection_id": ai_search_conn_id, |
| 182 | +# "index_name": "products_index", |
| 183 | +# "query_type": "vector_simple_hybrid", # Use vector hybrid search |
| 184 | +# } |
| 185 | +# ] |
| 186 | +# } |
| 187 | +# }, |
| 188 | +# ) |
| 189 | + |
| 190 | + |
| 191 | +# # 1. Create Azure AI agent with the search tool |
| 192 | +# policy_agent_instructions = '''You are a helpful agent that searches policy information using Azure AI Search. |
| 193 | +# Always use the search tool and index to find policy data and provide accurate information. |
| 194 | +# If you can not find the answer in the search tool, respond that you can't answer the question. |
| 195 | +# Do not add any other information from your general knowledge.''' |
| 196 | +# policy_agent = agents_client.create_agent( |
| 197 | +# model=gptModelName, |
| 198 | +# name="policy_agent", |
| 199 | +# instructions=policy_agent_instructions, |
| 200 | +# tools=[{"type": "azure_ai_search"}], |
| 201 | +# tool_resources={ |
| 202 | +# "azure_ai_search": { |
| 203 | +# "indexes": [ |
| 204 | +# { |
| 205 | +# "index_connection_id": ai_search_conn_id, |
| 206 | +# "index_name": "policies_index", |
| 207 | +# "query_type": "vector_simple_hybrid", # Use vector hybrid search |
| 208 | +# } |
| 209 | +# ] |
| 210 | +# } |
| 211 | +# }, |
| 212 | +# ) |
| 213 | + |
| 214 | + |
| 215 | + |
| 216 | +# chat_agent_instructions = '''You are a helpful assistant that can use the product agent and policy agent to answer user questions. |
| 217 | +# If you don't find any information in the knowledge source, please say no data found.''' |
| 218 | + |
| 219 | +# chat_agent = agents_client.create_agent( |
| 220 | +# model=gptModelName, |
| 221 | +# name=f"chat_agent", |
| 222 | +# instructions=chat_agent_instructions |
| 223 | +# ) |
| 224 | + |
| 225 | + |
| 226 | +# print(f"chatAgentId={chat_agent.id}") |
| 227 | +# print(f"productAgentId={product_agent.id}") |
| 228 | +# print(f"policyAgentId={policy_agent.id}") |
| 229 | + |
| 230 | + |
| 231 | +# # agents_client = project_client.agents |
| 232 | +# # print("Creating agents...") |
| 233 | + |
| 234 | +# # product_agent_instructions = "You are a helpful assistant that uses knowledge sources to help find products. If you don't find any products in the knowledge source, please say no data found." |
| 235 | +# # product_agent = agents_client.create_agent( |
| 236 | +# # model=gptModelName, |
| 237 | +# # name=f"product_agent", |
| 238 | +# # instructions=product_agent_instructions |
| 239 | +# # ) |
| 240 | +# # print(f"Created Product Agent with ID: {product_agent.id}") |
| 241 | + |
| 242 | +# # policy_agent_instructions = "You are a helpful assistant that searches policies to answer user questions.If you don't find any information in the knowledge source, please say no data found" |
| 243 | +# # policy_agent = agents_client.create_agent( |
| 244 | +# # model=gptModelName, |
| 245 | +# # name=f"policy_agent", |
| 246 | +# # instructions=policy_agent_instructions |
| 247 | +# # ) |
| 248 | +# # print(f"Created Policy Agent with ID: {policy_agent.id}") |
| 249 | + |
| 250 | +# # chat_agent_instructions = "You are a helpful assistant that can use the product agent and policy agent to answer user questions. If you don't find any information in the knowledge source, please say no data found" |
| 251 | +# # chat_agent = agents_client.create_agent( |
| 252 | +# # model=gptModelName, |
| 253 | +# # name=f"chat_agent", |
| 254 | +# # instructions=chat_agent_instructions |
| 255 | +# # ) |
| 256 | + |
| 257 | +# # print(f"chatAgentId={chat_agent.id}") |
| 258 | +# # print(f"productAgentId={product_agent.id}") |
| 259 | +# # print(f"policyAgentId={policy_agent.id}") |
0 commit comments