azure

package
v3.7.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2025 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package azure provides configuration options so you can connect and use Azure OpenAI using the [openai.Client].

Typical usage of this package will look like this:

client := openai.NewClient(
	azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
	azure.WithTokenCredential(azureIdentityTokenCredential),
	// or azure.WithAPIKey(azureOpenAIAPIKey),
)

Or, if you want to construct a specific service:

client := openai.NewChatCompletionService(
	azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
	azure.WithTokenCredential(azureIdentityTokenCredential),
	// or azure.WithAPIKey(azureOpenAIAPIKey),
)
Example (Authentication)
// There are two ways to authenticate - using a TokenCredential (via the azidentity
// package), or using an API Key.
const azureOpenAIEndpoint = "https://<your-azureopenai-instance>.openai.azure.com"
const azureOpenAIAPIVersion = "<api version string>"

// Using a TokenCredential
{
	// For a full list of credential types look at the documentation for the Azure Identity
	// package: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity
	tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		fmt.Printf("Failed to create TokenCredential: %s\n", err)
		return
	}

	client := openai.NewClient(
		azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
		azure.WithTokenCredential(tokenCredential),
	)

	_ = client
}

// Using an API Key
{
	const azureOpenAIAPIKey = "<key from Azure portal>"

	client := openai.NewClient(
		azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
		azure.WithAPIKey(azureOpenAIAPIKey),
	)

	_ = client
}
Example (Authentication_custom_scopes)
// Custom scopes can also be passed, if needed, when using Azure OpenAI endpoints.
const azureOpenAIEndpoint = "https://<your-azureopenai-instance>.openai.azure.com"
const azureOpenAIAPIVersion = "<api version string>"

// For a full list of credential types look at the documentation for the Azure Identity
// package: https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity
tokenCredential, err := azidentity.NewDefaultAzureCredential(nil)

if err != nil {
	fmt.Printf("Failed to create TokenCredential: %s\n", err)
	return
}

client := openai.NewClient(
	azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
	azure.WithTokenCredential(tokenCredential,
		// This is an example of a custom scope. See documentation for your service
		// endpoint for the proper value to pass.
		azure.WithTokenCredentialScopes([]string{"your-custom-scope"}),
	),
)

_ = client

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithAPIKey

func WithAPIKey(apiKey string) option.RequestOption

WithAPIKey configures this client to authenticate using an API key. This function should be paired with a call to WithEndpoint to point to your Azure OpenAI instance.

func WithEndpoint

func WithEndpoint(endpoint string, apiVersion string) option.RequestOption

WithEndpoint configures this client to connect to an Azure OpenAI endpoint.

  • endpoint - the Azure OpenAI endpoint to connect to. Ex: https://<azure-openai-resource>.openai.azure.com
  • apiVersion - the Azure OpenAI API version to target (ex: 2024-06-01). See Azure OpenAI apiversions for current API versions. This value cannot be empty.

This function should be paired with a call to authenticate, like azure.WithAPIKey or azure.WithTokenCredential, similar to this:

client := openai.NewClient(
	azure.WithEndpoint(azureOpenAIEndpoint, azureOpenAIAPIVersion),
	azure.WithTokenCredential(azureIdentityTokenCredential),
	// or azure.WithAPIKey(azureOpenAIAPIKey),
)

func WithTokenCredential

func WithTokenCredential(tokenCredential azcore.TokenCredential, options ...TokenCredentialOption) option.RequestOption

WithTokenCredential configures this client to authenticate using an Azure Identity TokenCredential. This function should be paired with a call to WithEndpoint to point to your Azure OpenAI instance.

func WithTokenCredentialScopes added in v3.7.0

func WithTokenCredentialScopes(scopes []string) func(*tokenCredentialConfig) error

WithTokenCredentialScopes overrides the default scope used when requesting access tokens.

Types

type TokenCredentialOption added in v3.7.0

type TokenCredentialOption func(*tokenCredentialConfig) error

TokenCredentialOption is the type for any options that can be used to customize WithTokenCredential, including things like using custom scopes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL