Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to show exception handling and more protocol method examples #42723

Merged
merged 8 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
19 changes: 18 additions & 1 deletion docs/azure/sdk/protocol-convenience-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ The preceding code demonstrates the following `System.ClientModel` convenience m

The following code uses a `ChatClient` to call the `CompleteChat` protocol method:

:::code source="snippets/protocol-convenience-methods/SCM/Protocol/Program.cs" highlight="26-31":::
:::code source="snippets/protocol-convenience-methods/SCM/Protocol/Program.cs" highlight="31-34":::

The preceding code demonstrates the following `System.ClientModel` protocol method patterns:

Expand Down Expand Up @@ -129,6 +129,23 @@ PipelineResponse response = result.GetRawResponse();

---

## Handling exceptions
alexwolfmsft marked this conversation as resolved.
Show resolved Hide resolved

When a service call fails, service clients throw an exception that exposes the HTTP status code and the details of the service response if available.
alexwolfmsft marked this conversation as resolved.
Show resolved Hide resolved

- `System.ClientModel` dependant libraries throw a [`ClientResultException`](/dotnet/api/system.clientmodel.clientresultexception).
- `Azure.Core` dependant libraries throw a [`RequestFailedException`](/dotnet/api/azure.requestfailedexception).
alexwolfmsft marked this conversation as resolved.
Show resolved Hide resolved

# [System.ClientModel exceptions](#tab/system-clientmodel)

:::code source="snippets/protocol-convenience-methods/AzureCore/ExceptionHandling/Program.cs" highlight="9-23":::
alexwolfmsft marked this conversation as resolved.
Show resolved Hide resolved

# [Azure.Core exceptions](#tab/azure-core)

:::code source="snippets/protocol-convenience-methods/SCM/ExceptionHandling/Program.cs" highlight="9-20":::
alexwolfmsft marked this conversation as resolved.
Show resolved Hide resolved

---

## Protocol and convenience method usage guidance

Although the Azure SDK for .NET client libraries provide the option to use either protocol or convenience methods, prioritize using convenience methods in most scenarios. Convenience methods are designed to improve the development experience and provide flexibility for authoring requests and handling responses. However, both method types can be used in your app as needed. Consider the following criteria when deciding which type of method to use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.ContentSafety" />
<PackageReference Include="Azure.Identity" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Azure.AI.ContentSafety;
using Azure.Identity;
using Azure;

// Create the client
ContentSafetyClient client = new(
new Uri("https://contentsafetyai.cognitiveservices.azure.com/"),
new DefaultAzureCredential());

try
{
// Call the convenience method
AnalyzeTextResult result = client.AnalyzeText("What is Microsoft Azure?");

// Display the results
foreach (TextCategoriesAnalysis item in result.CategoriesAnalysis)
{
Console.WriteLine($"{item.Category}: {item.Severity}");
}
}
catch (RequestFailedException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using OpenAI.Chat;
using System.ClientModel;

// Create the client
ChatClient client = new(
model: "gpt-4o-mini",
credential: Environment.GetEnvironmentVariable("OPENAI_API_KEY")!);

try
{
// Call the convenience method
ChatCompletion completion = client.CompleteChat("What is Microsoft Azure?");

// Display the results
Console.WriteLine($"[{completion.Role}]: {completion}");
}
catch (ClientResultException ex)
{
Console.WriteLine($"Error: {ex.Message}");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenAI" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@
"""u8.ToArray());
using BinaryContent content = BinaryContent.Create(input);

var requestOptions = new RequestOptions();

requestOptions.AddHeader("CustomHeader", "CustomHeaderValue");
requestOptions.ErrorOptions = ClientErrorBehaviors.NoThrow;

// Call the protocol method
ClientResult result = client.CompleteChat(
content,
new RequestOptions
{
ErrorOptions = ClientErrorBehaviors.NoThrow,
});
requestOptions
);

PipelineResponse response = result.GetRawResponse();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
Loading