Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,8 +4,10 @@ import json
|
|
4 |
import uuid
|
5 |
from pathlib import Path
|
6 |
|
7 |
-
# Azure
|
8 |
-
from azure.
|
|
|
|
|
9 |
from azure.ai.agents import AgentsClient
|
10 |
from azure.ai.agents.models import (
|
11 |
FunctionTool,
|
@@ -14,7 +16,7 @@ from azure.ai.agents.models import (
|
|
14 |
MessageRole,
|
15 |
)
|
16 |
|
17 |
-
# ---------- Inline custom function(s)
|
18 |
|
19 |
def submit_support_ticket(email_address: str, description: str) -> str:
|
20 |
"""
|
@@ -33,30 +35,40 @@ def submit_support_ticket(email_address: str, description: str) -> str:
|
|
33 |
)
|
34 |
file_path.write_text(text, encoding="utf-8")
|
35 |
|
36 |
-
|
37 |
"message": f"Support ticket {ticket_number} submitted. The ticket file is saved as {file_name}"
|
38 |
})
|
39 |
-
return message_json
|
40 |
|
41 |
-
#
|
42 |
user_functions: Set[Callable[..., Any]] = { submit_support_ticket }
|
43 |
|
44 |
# ---------- Core Agent Helpers ----------
|
45 |
|
46 |
-
def init_agent(
|
47 |
"""
|
48 |
-
Initialize an Azure AI Agent
|
49 |
Returns a session dict containing client, agent_id, thread_id, etc.
|
50 |
"""
|
51 |
-
if not
|
52 |
-
raise ValueError("Please provide
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
client = AgentsClient(
|
55 |
-
endpoint=
|
56 |
-
credential=
|
57 |
)
|
58 |
|
59 |
-
# Build toolset
|
60 |
functions_tool = FunctionTool(user_functions)
|
61 |
toolset = ToolSet()
|
62 |
toolset.add(functions_tool)
|
@@ -68,8 +80,8 @@ def init_agent(endpoint: str, api_key: str, model_deployment: str) -> dict:
|
|
68 |
name="support-agent",
|
69 |
instructions=(
|
70 |
"You are a technical support agent. "
|
71 |
-
"
|
72 |
-
"
|
73 |
"If a file is saved, tell the user the file name."
|
74 |
),
|
75 |
toolset=toolset,
|
@@ -80,8 +92,7 @@ def init_agent(endpoint: str, api_key: str, model_deployment: str) -> dict:
|
|
80 |
|
81 |
# Session we keep in Gradio state
|
82 |
return {
|
83 |
-
"endpoint":
|
84 |
-
"api_key": api_key.strip(),
|
85 |
"model": model_deployment.strip(),
|
86 |
"client": client,
|
87 |
"agent_id": agent.id,
|
@@ -159,15 +170,13 @@ def teardown(session: dict) -> str:
|
|
159 |
with gr.Blocks(title="Azure AI Support Agent (Functions) β Gradio") as demo:
|
160 |
gr.Markdown(
|
161 |
"## Azure AI Support Agent (Custom Function Tool)\n"
|
162 |
-
"
|
163 |
-
"
|
|
|
164 |
)
|
165 |
|
166 |
with gr.Row():
|
167 |
endpoint = gr.Textbox(label="Project Endpoint", placeholder="https://<your-project-endpoint>")
|
168 |
-
api_key = gr.Textbox(label="Project Key", placeholder="paste your key", type="password")
|
169 |
-
|
170 |
-
with gr.Row():
|
171 |
model = gr.Textbox(label="Model Deployment Name", value="gpt-4o")
|
172 |
|
173 |
session_state = gr.State(value=None)
|
@@ -191,16 +200,16 @@ with gr.Blocks(title="Azure AI Support Agent (Functions) β Gradio") as demo:
|
|
191 |
|
192 |
# ----- Callbacks -----
|
193 |
|
194 |
-
def on_connect(ep,
|
195 |
try:
|
196 |
-
sess = init_agent(ep,
|
197 |
-
return sess, "β
Connected. Support agent and thread are ready."
|
198 |
except Exception as e:
|
199 |
return None, f"β Connection error: {e}"
|
200 |
|
201 |
connect_btn.click(
|
202 |
fn=on_connect,
|
203 |
-
inputs=[endpoint,
|
204 |
outputs=[session_state, connect_status],
|
205 |
)
|
206 |
|
|
|
4 |
import uuid
|
5 |
from pathlib import Path
|
6 |
|
7 |
+
# Azure SDK auth (Entra ID)
|
8 |
+
from azure.identity import DefaultAzureCredential
|
9 |
+
|
10 |
+
# Azure AI Agents SDK
|
11 |
from azure.ai.agents import AgentsClient
|
12 |
from azure.ai.agents.models import (
|
13 |
FunctionTool,
|
|
|
16 |
MessageRole,
|
17 |
)
|
18 |
|
19 |
+
# ---------- Inline custom function(s) ----------
|
20 |
|
21 |
def submit_support_ticket(email_address: str, description: str) -> str:
|
22 |
"""
|
|
|
35 |
)
|
36 |
file_path.write_text(text, encoding="utf-8")
|
37 |
|
38 |
+
return json.dumps({
|
39 |
"message": f"Support ticket {ticket_number} submitted. The ticket file is saved as {file_name}"
|
40 |
})
|
|
|
41 |
|
42 |
+
# Register callable tools
|
43 |
user_functions: Set[Callable[..., Any]] = { submit_support_ticket }
|
44 |
|
45 |
# ---------- Core Agent Helpers ----------
|
46 |
|
47 |
+
def init_agent(project_endpoint: str, model_deployment: str) -> dict:
|
48 |
"""
|
49 |
+
Initialize an Azure AI Agent (Entra ID auth via DefaultAzureCredential).
|
50 |
Returns a session dict containing client, agent_id, thread_id, etc.
|
51 |
"""
|
52 |
+
if not project_endpoint or not model_deployment:
|
53 |
+
raise ValueError("Please provide a Project Endpoint and Model Deployment name (e.g., gpt-4o).")
|
54 |
+
|
55 |
+
# Entra ID token credential (requires `az login` or other supported auth in your environment)
|
56 |
+
credential = DefaultAzureCredential(
|
57 |
+
exclude_environment_credential=False,
|
58 |
+
exclude_managed_identity_credential=False,
|
59 |
+
exclude_shared_token_cache_credential=False,
|
60 |
+
exclude_visual_studio_code_credential=False,
|
61 |
+
exclude_powershell_credential=False,
|
62 |
+
exclude_cli_credential=False, # allows Azure CLI token if you've run `az login`
|
63 |
+
exclude_interactive_browser_credential=True, # set True for server environments
|
64 |
+
)
|
65 |
|
66 |
client = AgentsClient(
|
67 |
+
endpoint=project_endpoint.strip(),
|
68 |
+
credential=credential,
|
69 |
)
|
70 |
|
71 |
+
# Build toolset and enable auto function calls
|
72 |
functions_tool = FunctionTool(user_functions)
|
73 |
toolset = ToolSet()
|
74 |
toolset.add(functions_tool)
|
|
|
80 |
name="support-agent",
|
81 |
instructions=(
|
82 |
"You are a technical support agent. "
|
83 |
+
"Ask for the user's email address and a description of the issue. "
|
84 |
+
"Then submit a support ticket using the provided function. "
|
85 |
"If a file is saved, tell the user the file name."
|
86 |
),
|
87 |
toolset=toolset,
|
|
|
92 |
|
93 |
# Session we keep in Gradio state
|
94 |
return {
|
95 |
+
"endpoint": project_endpoint.strip(),
|
|
|
96 |
"model": model_deployment.strip(),
|
97 |
"client": client,
|
98 |
"agent_id": agent.id,
|
|
|
170 |
with gr.Blocks(title="Azure AI Support Agent (Functions) β Gradio") as demo:
|
171 |
gr.Markdown(
|
172 |
"## Azure AI Support Agent (Custom Function Tool)\n"
|
173 |
+
"1) **Run `az login`** in the same environment. \n"
|
174 |
+
"2) Paste your **Project Endpoint** and **Model Deployment** (e.g., `gpt-4o`). \n"
|
175 |
+
"Then chat β the agent can auto-call a function to submit a support ticket."
|
176 |
)
|
177 |
|
178 |
with gr.Row():
|
179 |
endpoint = gr.Textbox(label="Project Endpoint", placeholder="https://<your-project-endpoint>")
|
|
|
|
|
|
|
180 |
model = gr.Textbox(label="Model Deployment Name", value="gpt-4o")
|
181 |
|
182 |
session_state = gr.State(value=None)
|
|
|
200 |
|
201 |
# ----- Callbacks -----
|
202 |
|
203 |
+
def on_connect(ep, mdl):
|
204 |
try:
|
205 |
+
sess = init_agent(ep, mdl)
|
206 |
+
return sess, "β
Connected. Support agent and thread are ready. (Auth via DefaultAzureCredential)"
|
207 |
except Exception as e:
|
208 |
return None, f"β Connection error: {e}"
|
209 |
|
210 |
connect_btn.click(
|
211 |
fn=on_connect,
|
212 |
+
inputs=[endpoint, model],
|
213 |
outputs=[session_state, connect_status],
|
214 |
)
|
215 |
|