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
42 changes: 42 additions & 0 deletions generated-snippets/csharp/chat-completion/entra/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Azure.Identity;
using OpenAI;
using OpenAI.Chat;
using System.ClientModel.Primitives;

#pragma warning disable OPENAI001

var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");

BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default");

ChatClient client = new(
authenticationPolicy: tokenPolicy,
model: deploymentName,
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
});

ChatCompletionOptions options = new ChatCompletionOptions{
Temperature=(float)0.7,
};

ChatCompletion completion = client.CompleteChat(
[
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
new UserChatMessage("Hi, can you help me?"),
new AssistantChatMessage("Arrr! Of course, me hearty! What can I do for ye?"),
new UserChatMessage("What's the best way to train a parrot?"),
], options);

Console.WriteLine($"Model={completion.Model}");
foreach (ChatMessageContentPart contentPart in completion.Content)
{
string message = contentPart.Text;
Console.WriteLine($"Chat Role: {completion.Role}");
Console.WriteLine("Message:");
Console.WriteLine(message);
}
15 changes: 15 additions & 0 deletions generated-snippets/csharp/chat-completion/entra/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.3.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions generated-snippets/csharp/chat-completion/keyAuth/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using OpenAI;
using OpenAI.Chat;
using System.ClientModel;

#pragma warning disable OPENAI001

var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY environment variable is not set.");


ChatClient client = new(
credential: new ApiKeyCredential(apiKey),
model: deploymentName,
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
});


ChatCompletion completion = client.CompleteChat(
[
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
new UserChatMessage("Hi, can you help me?"),
new AssistantChatMessage("Arrr! Of course, me hearty! What can I do for ye?"),
new UserChatMessage("What's the best way to train a parrot?"),
]);

Console.WriteLine($"Model={completion.Model}");
foreach (ChatMessageContentPart contentPart in completion.Content)
{
string message = contentPart.Text;
Console.WriteLine($"Chat Role: {completion.Role}");
Console.WriteLine("Message:");
Console.WriteLine(message);
}
15 changes: 15 additions & 0 deletions generated-snippets/csharp/chat-completion/keyAuth/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.3.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
32 changes: 32 additions & 0 deletions generated-snippets/csharp/chat-responses/entra/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Azure.Identity;
using OpenAI;
using OpenAI.Responses;
using System.ClientModel.Primitives;

#pragma warning disable OPENAI001

var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");

BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default");

OpenAIResponseClient client = new(
model: deploymentName,
authenticationPolicy: tokenPolicy,
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
});

ResponseCreationOptions options = new ResponseCreationOptions{
Temperature=(float)0.7,
};

OpenAIResponse response = client.CreateResponse(
[
ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?"),
], options);

Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");
15 changes: 15 additions & 0 deletions generated-snippets/csharp/chat-responses/entra/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.3.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions generated-snippets/csharp/chat-responses/keyAuth/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using OpenAI;
using OpenAI.Responses;
using System.ClientModel;

#pragma warning disable OPENAI001

var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY environment variable is not set.");

OpenAIResponseClient client = new(
model: deploymentName,
credential: new ApiKeyCredential(apiKey),
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
});

ResponseCreationOptions options = new ResponseCreationOptions{
Temperature=(float)0.7,
};

OpenAIResponse response = client.CreateResponse(
[
ResponseItem.CreateUserMessageItem("What's the weather like today for my current location?"),
], options);

Console.WriteLine($"[ASSISTANT]: {response.GetOutputText()}");
15 changes: 15 additions & 0 deletions generated-snippets/csharp/chat-responses/keyAuth/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.3.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions generated-snippets/csharp/images/entra/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Azure.Identity;
using OpenAI;
using OpenAI.Images;
using System;
using System.IO;
using System.ClientModel.Primitives;

#pragma warning disable OPENAI001

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
BearerTokenPolicy tokenPolicy = new(
new DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default");

ImageClient client = new(
authenticationPolicy: tokenPolicy,
model: deploymentName,
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
});

string prompt = "A cute baby polar bear";

ImageGenerationOptions options = new()
{
Size = GeneratedImageSize.W1024xH1024,
};

GeneratedImage image = client.GenerateImage(prompt, options);
BinaryData bytes = image.ImageBytes;

using FileStream stream = File.OpenWrite("output.png");
bytes.ToStream().CopyTo(stream);
15 changes: 15 additions & 0 deletions generated-snippets/csharp/images/entra/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.5.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions generated-snippets/csharp/images/keyAuth/Sample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Azure;
using OpenAI;
using OpenAI.Images;
using System;
using System.IO;
using System.ClientModel;

#pragma warning disable OPENAI001

var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT environment variable is not set.");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? throw new InvalidOperationException("AZURE_OPENAI_DEPLOYMENT_NAME environment variable is not set.");
var apiKey = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? throw new InvalidOperationException("AZURE_OPENAI_API_KEY environment variable is not set.");;

ImageClient client = new(
credential: new ApiKeyCredential(apiKey),
model: deploymentName,
options: new OpenAIClientOptions()
{
Endpoint = new($"{endpoint}"),
}
);

string prompt = "A cute baby polar bear";

ImageGenerationOptions options = new()
{
Size = GeneratedImageSize.W1024xH1024,
};

GeneratedImage image = client.GenerateImage(prompt, options);
BinaryData bytes = image.ImageBytes;

using FileStream stream = File.OpenWrite("output.png");
bytes.ToStream().CopyTo(stream);
15 changes: 15 additions & 0 deletions generated-snippets/csharp/images/keyAuth/Sample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<PackageReference Include="OpenAI" Version="2.5.0" />
<PackageReference Include="Azure.Identity" Version="1.15.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions generated-snippets/go/chat-completion/entra/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module sample

go 1.24.3

require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.11.0
github.com/openai/openai-go/v3 v3.0.0
)
67 changes: 67 additions & 0 deletions generated-snippets/go/chat-completion/entra/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"context"
"fmt"
"log"
"os"

"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go/v2"
"github.com/openai/openai-go/v2/azure"
"github.com/openai/openai-go/v2/option"
)

func main() {
endpoint := os.Getenv("AZURE_OPENAI_ENDPOINT")
if len(endpoint) == 0 {
fmt.Println("Please set the AZURE_OPENAI_ENDPOINT environment variable.")
os.Exit(1)
}
deploymentName := os.Getenv("AZURE_OPENAI_DEPLOYMENT")
if len(deploymentName) == 0 {
fmt.Println("Please set the AZURE_OPENAI_DEPLOYMENT environment variable.")
os.Exit(1)
}

token_credential, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
fmt.Println("Error creating credential:", err)
os.Exit(1)
}
client := openai.NewClient(
option.WithBaseURL(endpoint),
azure.WithTokenCredential(token_credential),
)

resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: openai.ChatModel(deploymentName),
Messages: []openai.ChatCompletionMessageParamUnion{
{
OfSystem: &openai.ChatCompletionSystemMessageParam{
Content: openai.ChatCompletionSystemMessageParamContentUnion{
OfString: openai.String("You are a helpful assistant. You will talk like a pirate."),
},
},
},
{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("What's the best way to train a parrot?"),
},
},
},
},
})

if err != nil {
log.Printf("ERROR: %s", err)
return
}

for _, choice := range resp.Choices {
if choice.Message.Content != "" {
fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", choice.Index, choice.Message.Content)
}
}
}
8 changes: 8 additions & 0 deletions generated-snippets/go/chat-completion/keyAuth/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module sample

go 1.24.3

require (
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.11.0
github.com/openai/openai-go/v3 v3.0.0
)
Loading