diff --git a/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/Program.cs b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/Program.cs index 70686f0..f68acb0 100644 --- a/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/Program.cs +++ b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/Program.cs @@ -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", diff --git a/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.csproj b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.csproj index 7b3d90f..429ab2d 100644 --- a/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.csproj +++ b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.csproj @@ -10,14 +10,15 @@ + - - + + diff --git a/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.sln b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.sln new file mode 100644 index 0000000..a1a1410 --- /dev/null +++ b/00.ForBeginners/01-intro-to-ai-agents/code_samples/dotnet-agent-framework-travelagent/dotnet-agent-framework-travelagent.sln @@ -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 diff --git a/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/Program.cs b/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/Program.cs index a641bf5..9ad3fef 100644 --- a/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/Program.cs +++ b/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/Program.cs @@ -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 @@ -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.")] @@ -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( diff --git a/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/dotnet-agent-framework-basicagent.csproj b/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/dotnet-agent-framework-basicagent.csproj index fe9a539..c271625 100644 --- a/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/dotnet-agent-framework-basicagent.csproj +++ b/00.ForBeginners/02-explore-agentic-frameworks/code_samples/dotnet-agent-framework-basicagent/dotnet-agent-framework-basicagent.csproj @@ -10,14 +10,15 @@ + - - + + diff --git a/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/Program.cs b/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/Program.cs index 2ca52e2..76d26c9 100644 --- a/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/Program.cs +++ b/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/Program.cs @@ -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; @@ -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"; @@ -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 uploadResult = await fileClient.UploadFileAsync( - filePath: "../document.md", - purpose: FileUploadPurpose.Assistants); +ClientResult 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(); @@ -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; +} diff --git a/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/dotnet-agent-framework-msfoundry-file-search.csproj b/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/dotnet-agent-framework-msfoundry-file-search.csproj index 767aa18..2c5107f 100644 --- a/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/dotnet-agent-framework-msfoundry-file-search.csproj +++ b/00.ForBeginners/05-agentic-rag/code_samples/dotnet-agent-framework-msfoundry-file-search/dotnet-agent-framework-msfoundry-file-search.csproj @@ -10,18 +10,15 @@ - + - - - + + + - - - - - + + diff --git a/TempInspect/Program.cs b/TempInspect/Program.cs new file mode 100644 index 0000000..c4e6951 --- /dev/null +++ b/TempInspect/Program.cs @@ -0,0 +1,13 @@ +using System; +using System.Linq; + +var type = Type.GetType("System.ClientModel.ClientResultException, System.ClientModel"); +Console.WriteLine(type); +foreach (var prop in type.GetProperties()) +{ + Console.WriteLine($"Property: {prop.Name} - {prop.PropertyType}"); +} +foreach (var field in type.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)) +{ + Console.WriteLine($"Field: {field.Name} - {field.FieldType}"); +} diff --git a/TempInspect/TempInspect.csproj b/TempInspect/TempInspect.csproj new file mode 100644 index 0000000..60c0fb3 --- /dev/null +++ b/TempInspect/TempInspect.csproj @@ -0,0 +1,14 @@ + + + + Exe + net8.0 + enable + enable + + + + + + +