tessl install tessl/golang-github-com-azure-azure-sdk-for-go-sdk-ai-azopenai@0.9.0Azure OpenAI extensions module for Go providing models and convenience functions to simplify integration with Azure OpenAI features.
The Azure OpenAI extensions module for Go provides models and convenience functions to simplify integration with Azure-specific OpenAI features when using the OpenAI Go client. It enables developers to leverage Azure-specific extensions such as Azure OpenAI On Your Data, content filtering, and chat completions extensions.
go get github.com/Azure/azure-sdk-for-go/sdk/ai/azopenaigithub.com/Azure/azure-sdk-for-go/sdk/ai/azopenaiimport (
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/azure"
)The azopenai package is designed to work alongside the OpenAI Go client, adding Azure-specific capabilities. Here's a basic example:
import (
"context"
"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/azure"
)
// Create Azure OpenAI client
credential, err := azidentity.NewDefaultAzureCredential(nil)
client := openai.NewClient(
azure.WithEndpoint("https://your-resource.openai.azure.com", "2024-08-01-preview"),
azure.WithTokenCredential(credential),
)
// Make a chat completion request
resp, err := client.Chat.Completions.New(context.TODO(), openai.ChatCompletionNewParams{
Model: openai.ChatModel("your-deployment-name"),
Messages: []openai.ChatCompletionMessageParamUnion{{
OfUser: &openai.ChatCompletionUserMessageParam{
Content: openai.ChatCompletionUserMessageParamContentUnion{
OfString: openai.String("Hello!"),
},
},
}},
})Configure Azure OpenAI to retrieve information from your own data sources during chat completions, enabling retrieval-augmented generation (RAG) scenarios.
func WithDataSources(dataSources ...AzureChatExtensionConfigurationClassification) option.RequestOptionThe WithDataSources function adds Azure data sources to chat completion requests. Supported data sources include:
Azure OpenAI On Your Data Documentation
Access Azure OpenAI's content filtering capabilities to detect and handle potentially harmful content in prompts and completions.
func ExtractContentFilterError(err error, contentFilterErr **ContentFilterError) boolContent filtering provides detection and severity ratings for:
Content Filtering Documentation
Configure Azure-specific enhancements for chat completions, including optical character recognition (OCR) and grounding.
func WithEnhancements(config AzureChatEnhancementConfiguration) option.RequestOption
type AzureChatEnhancementConfiguration struct {
Grounding *AzureChatGroundingEnhancementConfiguration
Ocr *AzureChatOCREnhancementConfiguration
}The WithEnhancements function adds OCR and grounding capabilities to chat requests, enabling image text extraction and bounding box detection for identified objects.
The package provides wrapper types that add Azure-specific methods to OpenAI response types:
type ChatCompletion openai.ChatCompletion
type ChatCompletionChoice openai.ChatCompletionChoice
type ChatCompletionMessage openai.ChatCompletionMessage
type ChatCompletionChunk openai.ChatCompletionChunk
type ChatCompletionChunkChoiceDelta openai.ChatCompletionChunkChoiceDelta
type Completion openai.Completion
type CompletionChoice openai.CompletionChoiceThese wrappers provide methods to access Azure-specific data like content filtering results and chat extension context.
The azopenai package supports multiple authentication methods for data sources and vectorization endpoints:
The package includes comprehensive type definitions for all Azure OpenAI extension configurations and response data.
Type Definitions Documentation
Azure OpenAI On Your Data: Enables retrieval-augmented generation by integrating your data sources (search indexes, databases, vector stores) directly into chat completions.
Content Filtering: Azure's content safety system that evaluates both input prompts and generated completions for potentially harmful content across multiple categories.
Chat Extensions: Configuration objects that specify how Azure OpenAI should integrate with external data sources during chat completions.
Vectorization Sources: Mechanisms for generating embeddings used in vector search, including internal deployment names, external endpoints, and integrated vectorizers.
Polymorphic Types: The package uses Go interfaces to represent types that can have multiple concrete implementations (e.g., different data source configurations).
The azopenai package follows these design patterns:
Wrapper Pattern: Azure-specific response types wrap OpenAI types, adding methods to extract Azure-specific data while preserving the original OpenAI structure.
Option Pattern: Functions like WithDataSources and WithEnhancements return option.RequestOption values that can be passed to OpenAI client methods.
Classification Interfaces: Polymorphic types use "Classification" interfaces (e.g., AzureChatExtensionConfigurationClassification) to provide type-safe access to different implementations.
Embedded Fields: Many Azure types are anonymously embedded in response structs, providing direct access to their fields and methods.