mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
69225b4aa2
* change namespaces for agents and extension methods of the Microsoft.Agents.AI.OpenAI package * remove unnecessary namespace * remove unused namespaces * fix compilation issues and rrolled back removed run methods * sort usings * add extension methods for AIAgent to work with OpenAI Responses primitives * Move OpenAIChatClientAgent and OpenAIResponseClientAgent to samples * sort usings * sort usings
49 lines
2.0 KiB
C#
49 lines
2.0 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using Azure;
|
|
using Azure.AI.OpenAI;
|
|
using Azure.Identity;
|
|
using Microsoft.Agents.AI;
|
|
using Microsoft.Agents.AI.Hosting.AzureFunctions;
|
|
using Microsoft.Azure.Functions.Worker.Builder;
|
|
using Microsoft.Extensions.Hosting;
|
|
using OpenAI.Chat;
|
|
|
|
// Get the Azure OpenAI endpoint and deployment name from environment variables.
|
|
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
|
|
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
|
|
string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT")
|
|
?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT is not set.");
|
|
|
|
// Use Azure Key Credential if provided, otherwise use Azure CLI Credential.
|
|
string? azureOpenAiKey = System.Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
|
|
AzureOpenAIClient client = !string.IsNullOrEmpty(azureOpenAiKey)
|
|
? new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(azureOpenAiKey))
|
|
: new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential());
|
|
|
|
// Two agents used by the orchestration to demonstrate conditional logic.
|
|
const string SpamDetectionName = "SpamDetectionAgent";
|
|
const string SpamDetectionInstructions = "You are a spam detection assistant that identifies spam emails.";
|
|
|
|
const string EmailAssistantName = "EmailAssistantAgent";
|
|
const string EmailAssistantInstructions = "You are an email assistant that helps users draft responses to emails with professionalism.";
|
|
|
|
AIAgent spamDetectionAgent = client.GetChatClient(deploymentName)
|
|
.CreateAIAgent(SpamDetectionInstructions, SpamDetectionName);
|
|
|
|
AIAgent emailAssistantAgent = client.GetChatClient(deploymentName)
|
|
.CreateAIAgent(EmailAssistantInstructions, EmailAssistantName);
|
|
|
|
using IHost app = FunctionsApplication
|
|
.CreateBuilder(args)
|
|
.ConfigureFunctionsWebApplication()
|
|
.ConfigureDurableAgents(options =>
|
|
{
|
|
options
|
|
.AddAIAgent(spamDetectionAgent)
|
|
.AddAIAgent(emailAssistantAgent);
|
|
})
|
|
.Build();
|
|
|
|
app.Run();
|