|
26 | 26 | import os |
27 | 27 | from azure.identity import DefaultAzureCredential |
28 | 28 | from azure.ai.projects import AIProjectClient |
29 | | -from azure.ai.projects import AIProjectClient |
30 | | -from azure.identity import DefaultAzureCredential |
31 | 29 | from azure.ai.agents.models import AzureAISearchQueryType, AzureAISearchTool, ListSortOrder, MessageRole |
32 | | -from azure.identity import DefaultAzureCredential |
33 | 30 |
|
34 | | -# Define the Azure AI Search connection ID (replace with your actual connection ID) |
35 | | -azure_ai_conn_id = os.environ["AZURE_AI_CONNECTION_ID"] |
| 31 | +# Retrieve endpoint and model deployment name from environment variables |
| 32 | +project_endpoint = os.environ["PROJECT_ENDPOINT"] # Ensure the PROJECT_ENDPOINT environment variable is set |
| 33 | +model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] # Ensure the MODEL_DEPLOYMENT_NAME environment variable is set |
36 | 34 |
|
37 | | -# Initialize the Azure AI Search tool with the required parameters |
38 | | -ai_search = AzureAISearchTool( |
39 | | - index_connection_id=azure_ai_conn_id, # Connection ID for the Azure AI Search index |
40 | | - index_name="sample_index", # Name of the search index |
41 | | - query_type=AzureAISearchQueryType.SIMPLE, # Query type (e.g., SIMPLE, FULL) |
42 | | - top_k=3, # Number of top results to retrieve |
43 | | - filter="" # Optional filter for search results |
| 35 | +# Initialize the AIProjectClient with the endpoint and credentials |
| 36 | +project_client = AIProjectClient( |
| 37 | + endpoint=project_endpoint, |
| 38 | + credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Use Azure Default Credential for authentication |
| 39 | + api_version="latest", |
44 | 40 | ) |
45 | 41 |
|
46 | | -# Define the endpoint and model deployment name (replace with your actual values) |
47 | | -endpoint = os.environ["PROJECT_ENDPOINT"], |
48 | | -model_deployment_name = os.environ["MODEL_DEPLOYMENT_NAME"] |
49 | | - |
50 | | -# Create an AIProjectClient instance to interact with the Azure AI service |
51 | | -with AIProjectClient( |
52 | | - endpoint=endpoint, # Azure AI service endpoint |
53 | | - credential=DefaultAzureCredential(exclude_interactive_browser_credential=False), # Authentication credentials |
54 | | -) as project_client: |
55 | | - # Access the agents client from the project client |
56 | | - agents_client = project_client.agents |
| 42 | +with project_client: |
| 43 | + # Initialize the Azure AI Search tool with the required parameters |
| 44 | + ai_search = AzureAISearchTool( |
| 45 | + index_connection_id=os.environ["AZURE_AI_CONNECTION_ID"], # Connection ID for the Azure AI Search index |
| 46 | + index_name="sample_index", # Name of the search index |
| 47 | + query_type=AzureAISearchQueryType.SIMPLE, # Query type (e.g., SIMPLE, FULL) |
| 48 | + top_k=3, # Number of top results to retrieve |
| 49 | + filter="", # Optional filter for search results |
| 50 | + ) |
57 | 51 |
|
58 | 52 | # Create an agent with the specified model, name, instructions, and tools |
59 | | - agent = agents_client.create_agent( |
| 53 | + agent = project_client.agents.create_agent( |
60 | 54 | model=model_deployment_name, # Model deployment name |
61 | | - name="my-agent", # Name of the agent |
| 55 | + name="my-agent", # Name of the agent |
62 | 56 | instructions="You are a helpful agent", # Instructions for the agent |
63 | 57 | tools=ai_search.definitions, # Tools available to the agent |
64 | 58 | tool_resources=ai_search.resources, # Resources for the tools |
65 | 59 | ) |
66 | | - # [END create_agent_with_azure_ai_search_tool] |
67 | 60 | print(f"Created agent, ID: {agent.id}") |
68 | 61 |
|
69 | 62 | # Create a thread for communication with the agent |
70 | | - thread = agents_client.create_thread() |
| 63 | + thread = project_client.agents.threads.create() |
71 | 64 | print(f"Created thread, ID: {thread.id}") |
72 | 65 |
|
73 | | - # Create a message in the thread to interact with the agent |
74 | | - message = agents_client.create_message( |
| 66 | + # Send a message to the thread |
| 67 | + message = project_client.agents.messages.create( |
75 | 68 | thread_id=thread.id, # ID of the thread |
76 | | - role="user", # Role of the message sender (e.g., user) |
| 69 | + role=MessageRole.USER, # Role of the message sender (e.g., user) |
77 | 70 | content="What is the temperature rating of the cozynights sleeping bag?", # Message content |
78 | 71 | ) |
79 | | - print(f"Created message, ID: {message.id}") |
| 72 | + print(f"Created message, ID: {message['id']}") |
80 | 73 |
|
81 | 74 | # Create and process an agent run in the thread using the tools |
82 | | - run = agents_client.create_and_process_run(thread_id=thread.id, agent_id=agent.id) |
| 75 | + run = project_client.agents.runs.create_and_process(thread_id=thread.id, agent_id=agent.id) |
83 | 76 | print(f"Run finished with status: {run.status}") |
84 | 77 |
|
85 | | - # Check if the run failed and log the error if applicable |
86 | 78 | if run.status == "failed": |
| 79 | + # Log the error if the run fails |
87 | 80 | print(f"Run failed: {run.last_error}") |
88 | 81 |
|
89 | | - # Fetch and log the details of the agent run steps |
90 | | - run_steps = agents_client.list_run_steps(thread_id=thread.id, run_id=run.id) |
91 | | - for step in run_steps.data: |
92 | | - print(f"Step {step['id']} status: {step['status']}") |
93 | | - step_details = step.get("step_details", {}) |
94 | | - tool_calls = step_details.get("tool_calls", []) |
95 | | - |
96 | | - # Log details of tool calls if available |
97 | | - if tool_calls: |
98 | | - print(" Tool calls:") |
99 | | - for call in tool_calls: |
100 | | - print(f" Tool Call ID: {call.get('id')}") |
101 | | - print(f" Type: {call.get('type')}") |
102 | | - |
103 | | - azure_ai_search_details = call.get("azure_ai_search", {}) |
104 | | - if azure_ai_search_details: |
105 | | - print(f" azure_ai_search input: {azure_ai_search_details.get('input')}") |
106 | | - print(f" azure_ai_search output: {azure_ai_search_details.get('output')}") |
107 | | - print() # Add an extra newline between steps |
| 82 | + # Fetch and log all messages from the thread |
| 83 | + messages = project_client.agents.messages.list(thread_id=thread.id, order=ListSortOrder.ASCENDING) |
| 84 | + for message in messages.data: |
| 85 | + print(f"Role: {message.role}, Content: {message.content}") |
108 | 86 |
|
109 | 87 | # Delete the agent when done to clean up resources |
110 | | - agents_client.delete_agent(agent.id) |
| 88 | + project_client.agents.delete_agent(agent.id) |
111 | 89 | print("Deleted agent") |
112 | | - |
113 | | - # [START populate_references_agent_with_azure_ai_search_tool] |
114 | | - # Fetch and log all messages in the thread |
115 | | - messages = agents_client.list_messages(thread_id=thread.id, order=ListSortOrder.ASCENDING) |
116 | | - for message in messages.data: |
117 | | - # Log agent messages with URL citation annotations |
118 | | - if message.role == MessageRole.AGENT and message.url_citation_annotations: |
119 | | - placeholder_annotations = { |
120 | | - annotation.text: f" [see {annotation.url_citation.title}] ({annotation.url_citation.url})" |
121 | | - for annotation in message.url_citation_annotations |
122 | | - } |
123 | | - for message_text in message.text_messages: |
124 | | - message_str = message_text.text.value |
125 | | - for k, v in placeholder_annotations.items(): |
126 | | - message_str = message_str.replace(k, v) |
127 | | - print(f"{message.role}: {message_str}") |
128 | | - else: |
129 | | - # Log other messages without annotations |
130 | | - for message_text in message.text_messages: |
131 | | - print(f"{message.role}: {message_text.text.value}") |
132 | | - # [END populate_references_agent_with_azure_ai_search_tool] |
0 commit comments