-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunction_app.py
More file actions
48 lines (37 loc) · 1.5 KB
/
function_app.py
File metadata and controls
48 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import azure.functions as func
from copilot import CopilotClient, PermissionHandler
app = func.FunctionApp()
client = CopilotClient()
instructions = """
1. A robot may not injure a human being...
2. A robot must obey orders given it by human beings...
3. A robot must protect its own existence...
Objective: Give me the TLDR in exactly 5 words.
"""
def _session_config():
"""Build session config, optionally using Azure Foundry as provider."""
config = {
"system_message": {"content": instructions},
"on_permission_request": PermissionHandler.approve_all,
}
base_url = os.environ.get("AZURE_OPENAI_ENDPOINT")
api_key = os.environ.get("AZURE_OPENAI_API_KEY")
model = os.environ.get("AZURE_OPENAI_MODEL", "gpt-5-mini")
if base_url and api_key:
config["model"] = model
config["provider"] = {
"type": "azure",
"base_url": base_url,
"api_key": api_key,
}
return config
@app.route(route="ask", methods=["POST"])
async def ask(req: func.HttpRequest) -> func.HttpResponse:
"""HTTP trigger that sends a message to the Copilot SDK agent."""
prompt = req.get_body().decode("utf-8") or "What are the laws?"
session = await client.create_session(_session_config())
reply = await session.send_and_wait({"prompt": prompt})
response_text = reply.data.content if reply else "No response"
await session.destroy()
return func.HttpResponse(response_text, mimetype="text/plain")