Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,23 @@
using System.ClientModel;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using Azure.AI.OpenAI;
using OpenAI;
using DotNetEnv;

// Load environment variables from .env file
Env.Load("../../../../.env");
Env.Load();

// Get GitHub Models configuration from environment variables
var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT")
// Get Azure OpenAI configuration from environment variables
var endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT")
?? throw new InvalidOperationException("GITHUB_ENDPOINT is required");
var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
var model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
var api_key = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? throw new InvalidOperationException("GITHUB_TOKEN is required");

// Configure OpenAI client for GitHub Models
var openAIOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(github_endpoint)
};

// Create AI Agent with custom tool
AIAgent agent = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions)
.GetChatClient(github_model_id)
AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(api_key))
.GetChatClient(model_id)
.AsIChatClient()
.AsAIAgent(
name: "TravelAgent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@


<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.2.0" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.2.0-preview.1.26063.2" />
<PackageReference Include="OpenAI" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI.OpenAI/Microsoft.Agents.AI.OpenAI.csproj" />
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj" />
<PackageReference Include="Microsoft.Agents.AI" Version="1.0.0-preview.260209.1" />
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0-preview.260209.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-agent-framework-travelagent", "dotnet-agent-framework-travelagent.csproj", "{84534CF9-905D-F1F7-BE0B-AFD013A9C1AD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{84534CF9-905D-F1F7-BE0B-AFD013A9C1AD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84534CF9-905D-F1F7-BE0B-AFD013A9C1AD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84534CF9-905D-F1F7-BE0B-AFD013A9C1AD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84534CF9-905D-F1F7-BE0B-AFD013A9C1AD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1C76B464-0241-4BFB-B5D1-B18BA27355F2}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
using System.ClientModel;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using OpenAI;
using Azure;
using Azure.AI.OpenAI;
using DotNetEnv;

// Load environment variables from .env file
Expand All @@ -16,11 +17,6 @@
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? throw new InvalidOperationException("GITHUB_TOKEN is not set.");

// Configure OpenAI client for GitHub Models
var openAIOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(github_endpoint)
};

// Agent Tool: Random Destination Generator
[Description("Provides a random vacation destination.")]
Expand All @@ -45,7 +41,7 @@ static string GetRandomDestination()
}

// Create AI Agent with basic instructions and tool
AIAgent agent = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions)
AIAgent agent = new AzureOpenAIClient(new Uri(github_endpoint), new AzureKeyCredential(github_token))
.GetChatClient(github_model_id)
.AsIChatClient()
.AsAIAgent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@


<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.8.0-beta.1" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.2.0" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.2.0-preview.1.26063.2" />
<PackageReference Include="OpenAI" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI.OpenAI/Microsoft.Agents.AI.OpenAI.csproj" />
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj" />
<PackageReference Include="Microsoft.Agents.AI" Version="1.0.0-preview.260209.1" />
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0-preview.260209.1" />
</ItemGroup>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System.ClientModel;
using System.ClientModel.Primitives;
using System.IO;
using System.Reflection;
using Azure.AI.Projects;
using Azure.Identity;
using Microsoft.Agents.AI;
Expand All @@ -9,7 +12,7 @@
using DotNetEnv;


Env.Load("/Users/lokinfey/Desktop/AOAI/Foundry/Agent-Framework-Samples/.env");
Env.Load("../../../../.env");

var endpoint = Environment.GetEnvironmentVariable("AZURE_AI_PROJECT_ENDPOINT") ?? throw new InvalidOperationException("AZURE_AI_PROJECT_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_AI_MODEL_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
Expand All @@ -22,9 +25,18 @@

// Upload the file that contains the data to be used for RAG to the Foundry service.
OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient();
ClientResult<OpenAIFile> uploadResult = await fileClient.UploadFileAsync(
filePath: "../document.md",
purpose: FileUploadPurpose.Assistants);
ClientResult<OpenAIFile> uploadResult;
try
{
uploadResult = await fileClient.UploadFileAsync(
filePath: "../document.md",
purpose: FileUploadPurpose.Assistants);
}
catch (ClientResultException ex)
{
LogDetailedClientError("File upload failed", ex);
throw;
}

#pragma warning disable OPENAI001
VectorStoreClient vectorStoreClient = openAIClient.GetVectorStoreClient();
Expand All @@ -50,5 +62,56 @@ Do not answer from general knowledge or reasoning. Do not make assumptions or ge

AgentSession session = await agent.CreateSessionAsync();

Console.WriteLine(await agent.RunAsync("Can you explain Contoso's travel insurance coverage?", session));
//Console.WriteLine(await agent.RunAsync("Can you explain Contoso's travel insurance coverage?", session));
Console.WriteLine(await agent.RunAsync("What is the weather in Ohio today?", session));

static void LogDetailedClientError(string context, ClientResultException ex)
{
Console.Error.WriteLine($"[Client Error] {context}");
Console.Error.WriteLine($"Status: {ex.Status}");
Console.Error.WriteLine($"Message: {ex.Message}");
var responseBody = TryExtractResponseBody(ex);
if (!string.IsNullOrWhiteSpace(responseBody))
{
Console.Error.WriteLine("Server response body:");
Console.Error.WriteLine(responseBody);
}
}

static string? TryExtractResponseBody(ClientResultException ex)
{
try
{
var responseField = typeof(ClientResultException).GetField("_response", BindingFlags.NonPublic | BindingFlags.Instance);
if (responseField?.GetValue(ex) is PipelineResponse response)
{
var stream = response.ContentStream;
if (stream == null)
{
return null;
}

if (stream.CanSeek)
{
stream.Position = 0;
}

using var reader = new StreamReader(stream, leaveOpen: true);
var body = reader.ReadToEnd();

if (stream.CanSeek)
{
stream.Position = 0;
}

return body;
}
}
catch
{
// Ignore reflection or IO errors when extracting diagnostics.
}

return null;
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.8.0-beta.1" />
<PackageReference Include="Azure.Identity" Version="1.17.1" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageVersion Include="Azure.AI.Projects" Version="1.2.0-beta.5" />
<PackageVersion Include="Azure.AI.Projects.OpenAI" Version="1.0.0-beta.5" />
<PackageVersion Include="Azure.AI.Agents.Persistent" Version="1.2.0-beta.8" />
<PackageReference Include="Azure.AI.Projects" Version="1.2.0-beta.5" />
<PackageReference Include="Azure.AI.Projects.OpenAI" Version="1.0.0-beta.5" />
<PackageReference Include="Azure.AI.Agents.Persistent" Version="1.2.0-beta.8" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.2.0-preview.1.26063.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI.AzureAI/Microsoft.Agents.AI.AzureAI.csproj" />
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj" />
<PackageReference Include="Microsoft.Agents.AI" Version="1.0.0-preview.260209.1" />
<PackageReference Include="Microsoft.Agents.AI.AzureAI" Version="1.0.0-preview.260209.1" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.ClientModel;
using System.Text.Json;
using System.Text.Json.Serialization;
public class Plan
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System;
using System.ClientModel;
using System.Text.Json;
using System.Collections.Generic;
using System.Text.Json.Serialization;
public class TravelPlan
{
[JsonPropertyName("main_task")]
public string? Main_task { get; set; }

[JsonPropertyName("subtasks")]
public IList<Plan> Subtasks { get; set; }
public IList<Plan> Subtasks { get; set; } = new List<Plan>();
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
using System;
using System.ClientModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using OpenAI;
using Azure;
using Azure.AI.OpenAI;
using DotNetEnv;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;


// Load environment variables from .env file
Env.Load("../../../../.env");

// Get GitHub Models configuration from environment variables
var github_endpoint = Environment.GetEnvironmentVariable("GITHUB_ENDPOINT")
?? throw new InvalidOperationException("GITHUB_ENDPOINT is not set.");
var github_model_id = Environment.GetEnvironmentVariable("GITHUB_MODEL_ID") ?? "gpt-4o-mini";
var github_token = Environment.GetEnvironmentVariable("GITHUB_TOKEN")
?? throw new InvalidOperationException("GITHUB_TOKEN is not set.");
// Get Azure OpenAI configuration from environment variables
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT")
?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT is not set.");
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY")
?? throw new InvalidOperationException("AZURE_OPENAI_KEY is not set.");

// Configure OpenAI client for GitHub Models
var openAIOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(github_endpoint)
};
var openAIClient = new OpenAIClient(new ApiKeyCredential(github_token), openAIOptions);
// Configure Azure OpenAI client for the specified deployment
var azureOpenAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(apiKey));

// Planning agent configuration
const string AGENT_NAME = "TravelPlanAgent";
Expand All @@ -50,6 +45,9 @@ Your job is to decide which agents to run based on the user's request.
}
};

AIAgent agent = openAIClient.GetChatClient(github_model_id).AsIChatClient().AsAIAgent(agentOptions);
AIAgent agent = azureOpenAIClient
.GetChatClient(deploymentName)
.AsIChatClient()
.AsAIAgent(agentOptions);

Console.WriteLine(await agent.RunAsync("Create a travel plan for a family of 4, with 2 kids, from Singapore to Melbourne"));
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@


<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Microsoft.Agents.AI" Version="1.0.0-preview.260209.1" />
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0-preview.260209.1" />
<PackageReference Include="DotNetEnv" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.AI" Version="10.2.0" />
<PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="10.2.0-preview.1.26063.2" />
<PackageReference Include="OpenAI" Version="2.8.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI.OpenAI/Microsoft.Agents.AI.OpenAI.csproj" />
<ProjectReference Include="/Users/lokinfey/Desktop/AOAI/Foundry/agent-framework/dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.ClientModel;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using OpenAI;
using Azure.AI.OpenAI;
using DotNetEnv;

// Load environment variables from .env file
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET: Agent Framework",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net10.0/01-dotnet-agent-framework-aoai.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "integratedTerminal",
"stopAtEntry": false
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "process",
"command": "dotnet",
"args": [
"build",
"${workspaceFolder}/01-dotnet-agent-framework-aoai.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:Summary"
],
"problemMatcher": "$msCompile",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
Loading