-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
47 lines (36 loc) · 1.66 KB
/
Copy pathProgram.cs
File metadata and controls
47 lines (36 loc) · 1.66 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
using System.ComponentModel;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("Set OPENAI_API_KEY (function calling needs a tool-capable provider).");
IChatClient chat = new OpenAIClient(apiKey).GetChatClient("gpt-4o-mini").AsIChatClient();
var tools = new List<AITool>
{
AIFunctionFactory.Create(GetCustomer),
AIFunctionFactory.Create(DeleteCustomerProtected),
};
ChatClientAgent agent = new(chat, new ChatClientAgentOptions
{
Name = "AdminBot",
ChatOptions = new ChatOptions
{
Instructions = "You manage customers. Use the tools provided.",
Tools = tools,
},
});
AgentSession session = await agent.CreateSessionAsync();
AgentResponse response = await agent.RunAsync("Look up customer 42, then delete them.", session);
Console.WriteLine($"\n[{agent.Name}] {response}");
[Description("Look up a customer by ID. Safe -- read only.")]
static string GetCustomer([Description("Customer ID")] int id)
=> $"Customer {id}: Anna Lee, premium tier, joined 2024-02-09.";
[Description("Delete a customer permanently. PROTECTED -- requires explicit human approval before execution.")]
[RequiresApproval]
static string DeleteCustomerProtected([Description("Customer ID")] int id)
=> $"Customer {id} permanently deleted.";
// Marker attribute. A real production app would have a function-invocation
// middleware that checks for this and emits FunctionApprovalRequest events
// before invoking the underlying delegate.
[AttributeUsage(AttributeTargets.Method)]
internal sealed class RequiresApprovalAttribute : Attribute { }