|
7 | 7 | # - Returns a comprehensive Markdown style guide based on code examples |
8 | 8 | import os |
9 | 9 | import logging |
10 | | -from azure.ai.projects.aio import AIProjectClient |
11 | | -from azure.ai.projects.models import AsyncFunctionTool |
12 | | -from azure.identity.aio import DefaultAzureCredential |
| 10 | +from azure.ai.projects import AIProjectClient |
| 11 | +from azure.ai.agents.models import FunctionTool, ToolSet |
| 12 | +from azure.identity import DefaultAzureCredential |
13 | 13 | from agents.tools import vector_search |
14 | 14 |
|
15 | 15 | # Configure logging for this module |
@@ -85,99 +85,88 @@ async def generate_code_style(chat_history: str = "", user_query: str = "") -> s |
85 | 85 |
|
86 | 86 | # Create an Azure credential for authentication |
87 | 87 | logger.info("Initializing Azure authentication") |
88 | | - async with DefaultAzureCredential() as credential: |
89 | | - # Connect to the Azure AI Project that hosts our agent |
90 | | - logger.info("Connecting to Azure AI Project") |
91 | | - async with AIProjectClient( |
92 | | - credential=credential, |
93 | | - endpoint=os.environ["PROJECT_ENDPOINT"] |
94 | | - ) as project_client: |
95 | | - # Create the vector search tool that the agent will use |
96 | | - logger.info("Setting up vector search tool") |
97 | | - functions = AsyncFunctionTool(functions=[vector_search.vector_search]) |
98 | | - |
99 | | - # Create the agent with its personality and tools |
100 | | - logger.info("Creating CodeStyleSynthesizer agent with model: %s", os.environ["AGENTS_MODEL_DEPLOYMENT_NAME"]) |
101 | | - agent = await project_client.agents.create_agent( |
102 | | - name="CodeStyleSynthesizer", |
103 | | - description="An agent that produces code style guides", |
104 | | - instructions=_CODE_STYLE_SYSTEM_PROMPT, |
105 | | - tools=functions.definitions, |
106 | | - model=os.environ["AGENTS_MODEL_DEPLOYMENT_NAME"] |
107 | | - ) |
108 | | - logger.info("Created agent: %s with tool: vector_search", agent.name) |
109 | | - |
110 | | - # Create a conversation thread for the agent |
111 | | - logger.info("Creating conversation thread") |
112 | | - thread = await project_client.agents.create_thread() |
113 | | - |
114 | | - # Add chat history if provided |
115 | | - if chat_history: |
116 | | - logger.info("Adding chat history to thread") |
117 | | - await project_client.agents.create_message( |
118 | | - thread_id=thread.id, |
119 | | - role="user", |
120 | | - content=chat_history |
121 | | - ) |
122 | | - |
123 | | - # Add the user's query or default message |
124 | | - final_query = user_query if user_query else "Generate a code style guide." |
125 | | - logger.info("Adding user query to thread: %s", final_query) |
126 | | - await project_client.agents.create_message( |
| 88 | + credential = DefaultAzureCredential() |
| 89 | + |
| 90 | + # Connect to the Azure AI Project that hosts our agent |
| 91 | + logger.info("Connecting to Azure AI Project") |
| 92 | + project_client = AIProjectClient( |
| 93 | + endpoint=os.environ["PROJECT_ENDPOINT"], |
| 94 | + credential=credential |
| 95 | + ) |
| 96 | + |
| 97 | + with project_client: |
| 98 | + # Create the vector search tool that the agent will use |
| 99 | + logger.info("Setting up vector search tool") |
| 100 | + functions = FunctionTool({vector_search.vector_search}) |
| 101 | + toolset = ToolSet() |
| 102 | + toolset.add(functions) |
| 103 | + |
| 104 | + # Enable automatic function calls |
| 105 | + project_client.agents.enable_auto_function_calls(toolset) |
| 106 | + |
| 107 | + # Create the agent with its personality and tools |
| 108 | + logger.info("Creating CodeStyleSynthesizer agent with model: %s", os.environ["AGENTS_MODEL_DEPLOYMENT_NAME"]) |
| 109 | + agent = project_client.agents.create_agent( |
| 110 | + name="CodeStyleSynthesizer", |
| 111 | + instructions=_CODE_STYLE_SYSTEM_PROMPT, |
| 112 | + toolset=toolset, |
| 113 | + model=os.environ["AGENTS_MODEL_DEPLOYMENT_NAME"] |
| 114 | + ) |
| 115 | + logger.info("Created agent: %s with tool: vector_search", agent.name) |
| 116 | + |
| 117 | + # Create a conversation thread for the agent |
| 118 | + logger.info("Creating conversation thread") |
| 119 | + thread = project_client.agents.threads.create() |
| 120 | + |
| 121 | + # Add chat history if provided |
| 122 | + if chat_history: |
| 123 | + logger.info("Adding chat history to thread") |
| 124 | + project_client.agents.messages.create( |
127 | 125 | thread_id=thread.id, |
128 | 126 | role="user", |
129 | | - content=final_query |
130 | | - ) |
131 | | - |
132 | | - # Start the agent's execution |
133 | | - logger.info("Starting agent execution") |
134 | | - run = await project_client.agents.create_run( |
135 | | - thread_id=thread.id, |
136 | | - agent_id=agent.id |
| 127 | + content=chat_history |
137 | 128 | ) |
138 | | - |
139 | | - # Monitor the agent's progress and handle tool calls |
140 | | - tool_call_count = 0 |
141 | | - while True: |
142 | | - run = await project_client.agents.get_run(thread_id=thread.id, run_id=run.id) |
143 | | - logger.info("Agent run status: %s", run.status) |
144 | | - |
145 | | - if run.status == "completed": |
146 | | - logger.info("Agent run completed successfully") |
| 129 | + |
| 130 | + # Add the user's query or default message |
| 131 | + final_query = user_query if user_query else "Generate a code style guide." |
| 132 | + logger.info("Adding user query to thread: %s", final_query) |
| 133 | + project_client.agents.messages.create( |
| 134 | + thread_id=thread.id, |
| 135 | + role="user", |
| 136 | + content=final_query |
| 137 | + ) |
| 138 | + |
| 139 | + # Start the agent's execution and process it automatically |
| 140 | + logger.info("Starting agent execution") |
| 141 | + run = project_client.agents.runs.create_and_process( |
| 142 | + thread_id=thread.id, |
| 143 | + agent_id=agent.id |
| 144 | + ) |
| 145 | + logger.info("Agent run completed with status: %s", run.status) |
| 146 | + |
| 147 | + # Check for failure |
| 148 | + if run.status == "failed": |
| 149 | + logger.error("Agent run failed with error: %s", run.last_error) |
| 150 | + raise Exception(f"Agent run failed: {run.last_error}") |
| 151 | + |
| 152 | + # Get the final response from the agent |
| 153 | + logger.info("Retrieving final response from agent") |
| 154 | + messages = project_client.agents.messages.list(thread_id=thread.id) |
| 155 | + |
| 156 | + # Find the latest assistant message |
| 157 | + response = None |
| 158 | + for message in messages: |
| 159 | + if message.role == "assistant": |
| 160 | + # Get the text content from the message |
| 161 | + if message.content and len(message.content) > 0: |
| 162 | + response = str(message.content[0].text.value) |
147 | 163 | break |
148 | | - elif run.status == "failed": |
149 | | - logger.error("Agent run failed with : %s", run) |
150 | | - raise Exception("Agent run failed") |
151 | | - elif run.status == "requires_action": |
152 | | - # Handle tool calls from the agent |
153 | | - tool_calls = run.required_action.submit_tool_outputs.tool_calls |
154 | | - logger.info("Agent requires action with %d tool calls", len(tool_calls)) |
155 | | - tool_outputs = [] |
156 | | - for tool_call in tool_calls: |
157 | | - logger.info("Agent %s calling tool: %s with arguments: %s", |
158 | | - agent.name, |
159 | | - tool_call.function.name, |
160 | | - tool_call.function.arguments) |
161 | | - output = await functions.execute(tool_call) |
162 | | - logger.info("Tool call completed with output length: %d", len(str(output))) |
163 | | - tool_outputs.append({ |
164 | | - "tool_call_id": tool_call.id, |
165 | | - "output": output |
166 | | - }) |
167 | | - tool_call_count += 1 |
168 | | - await project_client.agents.submit_tool_outputs_to_run( |
169 | | - thread_id=thread.id, |
170 | | - run_id=run.id, |
171 | | - tool_outputs=tool_outputs |
172 | | - ) |
173 | | - |
174 | | - # Get the final response from the agent |
175 | | - logger.info("Retrieving final response from agent") |
176 | | - messages = await project_client.agents.list_messages(thread_id=thread.id) |
177 | | - response = str(messages.data[0].content[0].text.value) |
178 | | - logger.info("Code style guide generated by %s (%d tool calls). Response length: %d", |
179 | | - agent.name, tool_call_count, len(response)) |
180 | | - return response |
| 164 | + |
| 165 | + if not response: |
| 166 | + raise Exception("No response generated by the agent") |
| 167 | + |
| 168 | + logger.info("Code style guide generated by %s. Response length: %d", agent.name, len(response)) |
| 169 | + return response |
181 | 170 |
|
182 | 171 | except Exception as e: |
183 | 172 | logger.error("Code style generation failed with error: %s", str(e), exc_info=True) |
|
0 commit comments