azopenai

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2023 License: MIT Imports: 14 Imported by: 36

README

Azure OpenAI client module for Go

NOTE: this client can be used with Azure OpenAI and OpenAI.

Azure OpenAI Service provides access to OpenAI's powerful language models including the GPT-4, GPT-35-Turbo, and Embeddings model series, as well as image generation using DALL-E.

Source code | Package (pkg.go.dev) | REST API documentation | Product documentation

Getting started

Prerequisites
Install the packages

Install the azopenai and azidentity modules with go get:

go get github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai

# optional
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity

The azidentity module is used for Azure Active Directory authentication with Azure OpenAI.

Authentication
Azure OpenAI

Azure OpenAI clients can authenticate using Azure Active Directory or with an API key:

  • Using Azure Active Directory, with a TokenCredential: example
  • Using an API key: example
OpenAI

OpenAI supports connecting using an API key: example

Key concepts

See Key concepts in the product documentation for more details about general concepts.

Examples

Examples for various scenarios can be found on pkg.go.dev or in the example*_test.go files in our GitHub repo for azopenai.

Troubleshooting

Error Handling

All methods that send HTTP requests return *azcore.ResponseError when these requests fail. ResponseError has error details and the raw response from the service.

Logging

This module uses the logging implementation in azcore. To turn on logging for all Azure SDK modules, set AZURE_SDK_GO_LOGGING to all. By default, the logger writes to stderr. Use the azcore/log package to control log output. For example, logging only HTTP request and response events, and printing them to stdout:

import azlog "github.com/Azure/azure-sdk-for-go/sdk/azcore/log"

// Print log events to stdout
azlog.SetListener(func(cls azlog.Event, msg string) {
	fmt.Println(msg)
})

// Includes only requests and responses in credential logs
azlog.SetEvents(azlog.EventRequest, azlog.EventResponse)

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Documentation

Overview

Package azopenai Azure OpenAI Service provides access to OpenAI's powerful language models including the GPT-4, GPT-35-Turbo, and Embeddings model series, as well as image generation using DALL-E.

The Client in this package can be used with Azure OpenAI or OpenAI.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AzureChatExtensionConfiguration added in v0.2.0

type AzureChatExtensionConfiguration struct {

	// REQUIRED; The label for the type of an Azure chat extension. This typically corresponds to a matching Azure resource. Azure
	// chat extensions are only compatible with Azure OpenAI.
	Type *AzureChatExtensionType

	// REQUIRED; The configuration payload used for the Azure chat extension. The structure payload details are specific to the
	// extension being configured. Azure chat extensions are only compatible with Azure OpenAI.
	Parameters any
}

AzureChatExtensionConfiguration - A representation of configuration data for a single Azure OpenAI chat extension. This will be used by a chat completions request that should use Azure OpenAI chat extensions to augment the response behavior. The use of this configuration is compatible only with Azure OpenAI.

func (AzureChatExtensionConfiguration) MarshalJSON added in v0.2.0

func (a AzureChatExtensionConfiguration) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type AzureChatExtensionConfiguration.

func (*AzureChatExtensionConfiguration) UnmarshalJSON added in v0.2.0

func (a *AzureChatExtensionConfiguration) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type AzureChatExtensionConfiguration.

type AzureChatExtensionOptions added in v0.2.0

type AzureChatExtensionOptions struct {
	// Extensions is a slice of extensions to the chat completions endpoint, like Azure Cognitive Search.
	Extensions []AzureChatExtensionConfiguration
}

AzureChatExtensionOptions provides Azure specific options to extend ChatCompletions.

type AzureChatExtensionType added in v0.2.0

type AzureChatExtensionType string

AzureChatExtensionType - A representation of configuration data for a single Azure OpenAI chat extension. This will be used by a chat completions request that should use Azure OpenAI chat extensions to augment the response behavior. The use of this configuration is compatible only with Azure OpenAI.

const (
	// AzureChatExtensionTypeAzureCognitiveSearch enables the use of an Azure Cognitive Search index with chat completions.
	// [AzureChatExtensionConfiguration.Parameter] should be of type [AzureCognitiveSearchChatExtensionConfiguration].
	AzureChatExtensionTypeAzureCognitiveSearch AzureChatExtensionType = "AzureCognitiveSearch"
)

func PossibleAzureChatExtensionTypeValues added in v0.2.0

func PossibleAzureChatExtensionTypeValues() []AzureChatExtensionType

PossibleAzureChatExtensionTypeValues returns the possible values for the AzureChatExtensionType const type.

type AzureChatExtensionsMessageContext added in v0.2.0

type AzureChatExtensionsMessageContext struct {
	// The contextual message payload associated with the Azure chat extensions used for a chat completions request. These messages
	// describe the data source retrievals, plugin invocations, and other
	// intermediate steps taken in the course of generating a chat completions response that was augmented by capabilities from
	// Azure OpenAI chat extensions.
	Messages []ChatMessage
}

AzureChatExtensionsMessageContext - A representation of the additional context information available when Azure OpenAI chat extensions are involved in the generation of a corresponding chat completions response. This context information is only populated when using an Azure OpenAI request configured to use a matching extension.

func (AzureChatExtensionsMessageContext) MarshalJSON added in v0.2.0

func (a AzureChatExtensionsMessageContext) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type AzureChatExtensionsMessageContext.

func (*AzureChatExtensionsMessageContext) UnmarshalJSON added in v0.2.0

func (a *AzureChatExtensionsMessageContext) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type AzureChatExtensionsMessageContext.

type AzureCognitiveSearchChatExtensionConfiguration added in v0.2.0

type AzureCognitiveSearchChatExtensionConfiguration struct {
	// REQUIRED; The absolute endpoint path for the Azure Cognitive Search resource to use.
	Endpoint *string

	// REQUIRED; The name of the index to use as available in the referenced Azure Cognitive Search resource.
	IndexName *string

	// REQUIRED; The API admin key to use with the specified Azure Cognitive Search endpoint.
	Key *string

	// REQUIRED; The type label to use when configuring Azure OpenAI chat extensions. This should typically not be changed from
	// its default value for Azure Cognitive Search.
	Type *AzureCognitiveSearchChatExtensionConfigurationType

	// When using embeddings for search, specifies the resource URL from which embeddings should be retrieved.
	EmbeddingEndpoint *string

	// When using embeddings, specifies the API key to use with the provided embeddings endpoint.
	EmbeddingKey *string

	// Customized field mapping behavior to use when interacting with the search index.
	FieldsMapping *AzureCognitiveSearchChatExtensionConfigurationFieldsMapping

	// Whether queries should be restricted to use of indexed data.
	InScope *bool

	// The query type to use with Azure Cognitive Search.
	QueryType *AzureCognitiveSearchQueryType

	// The additional semantic configuration for the query.
	SemanticConfiguration *string

	// The configured top number of documents to feature for the configured query.
	TopNDocuments *int32
}

AzureCognitiveSearchChatExtensionConfiguration - A specific representation of configurable options for Azure Cognitive Search when using it as an Azure OpenAI chat extension.

func (AzureCognitiveSearchChatExtensionConfiguration) MarshalJSON added in v0.2.0

MarshalJSON implements the json.Marshaller interface for type AzureCognitiveSearchChatExtensionConfiguration.

func (*AzureCognitiveSearchChatExtensionConfiguration) UnmarshalJSON added in v0.2.0

UnmarshalJSON implements the json.Unmarshaller interface for type AzureCognitiveSearchChatExtensionConfiguration.

type AzureCognitiveSearchChatExtensionConfigurationFieldsMapping added in v0.2.0

type AzureCognitiveSearchChatExtensionConfigurationFieldsMapping struct {
	// The names of index fields that should be treated as content.
	ContentFieldNames []string

	// The separator pattern that content fields should use.
	ContentFieldSeparator *string

	// The name of the index field to use as a filepath.
	FilepathField *string

	// The name of the index field to use as a title.
	TitleField *string

	// The name of the index field to use as a URL.
	URLField *string

	// The names of fields that represent vector data.
	VectorFields []string
}

AzureCognitiveSearchChatExtensionConfigurationFieldsMapping - Customized field mapping behavior to use when interacting with the search index.

func (AzureCognitiveSearchChatExtensionConfigurationFieldsMapping) MarshalJSON added in v0.2.0

MarshalJSON implements the json.Marshaller interface for type AzureCognitiveSearchChatExtensionConfigurationFieldsMapping.

func (*AzureCognitiveSearchChatExtensionConfigurationFieldsMapping) UnmarshalJSON added in v0.2.0

UnmarshalJSON implements the json.Unmarshaller interface for type AzureCognitiveSearchChatExtensionConfigurationFieldsMapping.

type AzureCognitiveSearchChatExtensionConfigurationType added in v0.2.0

type AzureCognitiveSearchChatExtensionConfigurationType string

AzureCognitiveSearchChatExtensionConfigurationType - The type label to use when configuring Azure OpenAI chat extensions. This should typically not be changed from its default value for Azure Cognitive Search.

const (
	AzureCognitiveSearchChatExtensionConfigurationTypeAzureCognitiveSearch AzureCognitiveSearchChatExtensionConfigurationType = "AzureCognitiveSearch"
)

func PossibleAzureCognitiveSearchChatExtensionConfigurationTypeValues added in v0.2.0

func PossibleAzureCognitiveSearchChatExtensionConfigurationTypeValues() []AzureCognitiveSearchChatExtensionConfigurationType

PossibleAzureCognitiveSearchChatExtensionConfigurationTypeValues returns the possible values for the AzureCognitiveSearchChatExtensionConfigurationType const type.

type AzureCognitiveSearchIndexFieldMappingOptions added in v0.2.0

type AzureCognitiveSearchIndexFieldMappingOptions struct {
	// The names of index fields that should be treated as content.
	ContentFieldNames []string

	// The separator pattern that content fields should use.
	ContentFieldSeparator *string

	// The name of the index field to use as a filepath.
	FilepathField *string

	// The name of the index field to use as a title.
	TitleField *string

	// The name of the index field to use as a URL.
	URLField *string

	// The names of fields that represent vector data.
	VectorFields []string
}

AzureCognitiveSearchIndexFieldMappingOptions - Optional settings to control how fields are processed when using a configured Azure Cognitive Search resource.

func (AzureCognitiveSearchIndexFieldMappingOptions) MarshalJSON added in v0.2.0

MarshalJSON implements the json.Marshaller interface for type AzureCognitiveSearchIndexFieldMappingOptions.

func (*AzureCognitiveSearchIndexFieldMappingOptions) UnmarshalJSON added in v0.2.0

func (a *AzureCognitiveSearchIndexFieldMappingOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type AzureCognitiveSearchIndexFieldMappingOptions.

type AzureCognitiveSearchQueryType added in v0.2.0

type AzureCognitiveSearchQueryType string

AzureCognitiveSearchQueryType - The type of Azure Cognitive Search retrieval query that should be executed when using it as an Azure OpenAI chat extension.

const (
	AzureCognitiveSearchQueryTypeSemantic             AzureCognitiveSearchQueryType = "semantic"
	AzureCognitiveSearchQueryTypeSimple               AzureCognitiveSearchQueryType = "simple"
	AzureCognitiveSearchQueryTypeVector               AzureCognitiveSearchQueryType = "vector"
	AzureCognitiveSearchQueryTypeVectorSemanticHybrid AzureCognitiveSearchQueryType = "vectorSemanticHybrid"
	AzureCognitiveSearchQueryTypeVectorSimpleHybrid   AzureCognitiveSearchQueryType = "vectorSimpleHybrid"
)

func PossibleAzureCognitiveSearchQueryTypeValues added in v0.2.0

func PossibleAzureCognitiveSearchQueryTypeValues() []AzureCognitiveSearchQueryType

PossibleAzureCognitiveSearchQueryTypeValues returns the possible values for the AzureCognitiveSearchQueryType const type.

type ChatChoice

type ChatChoice struct {
	// REQUIRED; The reason that this chat completions choice completed its generated.
	FinishReason *CompletionsFinishReason

	// REQUIRED; The ordered index associated with this chat completions choice.
	Index *int32

	// Information about the content filtering category (hate, sexual, violence, selfharm), if it has been detected, as well as
	// the severity level (verylow, low, medium, high-scale that determines the
	// intensity and risk level of harmful content) and if it has been filtered or not.
	ContentFilterResults *ChatChoiceContentFilterResults

	// The delta message content for a streaming response.
	Delta *ChatChoiceDelta

	// The chat message for a given chat completions prompt.
	Message *ChatChoiceMessage
}

ChatChoice - The representation of a single prompt completion as part of an overall chat completions request. Generally, n choices are generated per provided prompt with a default value of 1. Token limits and other settings may limit the number of choices generated.

func (ChatChoice) MarshalJSON

func (c ChatChoice) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatChoice.

func (*ChatChoice) UnmarshalJSON

func (c *ChatChoice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatChoice.

type ChatChoiceContentFilterResults

type ChatChoiceContentFilterResults struct {
	// Describes language attacks or uses that include pejorative or discriminatory language with reference to a person or identity
	// group on the basis of certain differentiating attributes of these groups
	// including but not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion,
	// immigration status, ability status, personal appearance, and body size.
	Hate *ContentFilterResultsHate

	// Describes language related to physical actions intended to purposely hurt, injure, or damage one’s body, or kill oneself.
	SelfHarm *ContentFilterResultsSelfHarm

	// Describes language related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate
	// terms, physical sexual acts, including those portrayed as an assault or a
	// forced sexual violent act against one’s will, prostitution, pornography, and abuse.
	Sexual *ContentFilterResultsSexual

	// Describes language related to physical actions intended to hurt, injure, damage, or kill someone or something; describes
	// weapons, etc.
	Violence *ContentFilterResultsViolence
}

ChatChoiceContentFilterResults - Information about the content filtering category (hate, sexual, violence, selfharm), if it has been detected, as well as the severity level (verylow, low, medium, high-scale that determines the intensity and risk level of harmful content) and if it has been filtered or not.

func (ChatChoiceContentFilterResults) MarshalJSON

func (c ChatChoiceContentFilterResults) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatChoiceContentFilterResults.

func (*ChatChoiceContentFilterResults) UnmarshalJSON

func (c *ChatChoiceContentFilterResults) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatChoiceContentFilterResults.

type ChatChoiceDelta

type ChatChoiceDelta struct {
	// REQUIRED; The text associated with this message payload.
	Content *string

	// REQUIRED; The role associated with this message payload.
	Role *ChatRole

	// Additional context data associated with a chat message when requesting chat completions using compatible Azure OpenAI chat
	// extensions. This includes information like the intermediate data source
	// retrievals used to service a request. This context information is only populated when using Azure OpenAI with chat extensions
	// capabilities configured.
	Context *ChatMessageContext

	// The name and arguments of a function that should be called, as generated by the model.
	FunctionCall *ChatMessageFunctionCall

	// The name of the author of this message. name is required if role is function, and it should be the name of the function
	// whose response is in the content. May contain a-z, A-Z, 0-9, and underscores,
	// with a maximum length of 64 characters.
	Name *string
}

ChatChoiceDelta - The delta message content for a streaming response.

func (ChatChoiceDelta) MarshalJSON

func (c ChatChoiceDelta) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatChoiceDelta.

func (*ChatChoiceDelta) UnmarshalJSON

func (c *ChatChoiceDelta) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatChoiceDelta.

type ChatChoiceMessage

type ChatChoiceMessage struct {
	// REQUIRED; The text associated with this message payload.
	Content *string

	// REQUIRED; The role associated with this message payload.
	Role *ChatRole

	// Additional context data associated with a chat message when requesting chat completions using compatible Azure OpenAI chat
	// extensions. This includes information like the intermediate data source
	// retrievals used to service a request. This context information is only populated when using Azure OpenAI with chat extensions
	// capabilities configured.
	Context *ChatMessageContext

	// The name and arguments of a function that should be called, as generated by the model.
	FunctionCall *ChatMessageFunctionCall

	// The name of the author of this message. name is required if role is function, and it should be the name of the function
	// whose response is in the content. May contain a-z, A-Z, 0-9, and underscores,
	// with a maximum length of 64 characters.
	Name *string
}

ChatChoiceMessage - The chat message for a given chat completions prompt.

func (ChatChoiceMessage) MarshalJSON

func (c ChatChoiceMessage) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatChoiceMessage.

func (*ChatChoiceMessage) UnmarshalJSON

func (c *ChatChoiceMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatChoiceMessage.

type ChatCompletions

type ChatCompletions struct {
	// REQUIRED; The collection of completions choices associated with this completions response. Generally, n choices are generated
	// per provided prompt with a default value of 1. Token limits and other settings may
	// limit the number of choices generated.
	Choices []ChatChoice

	// REQUIRED; The first timestamp associated with generation activity for this completions response, represented as seconds
	// since the beginning of the Unix epoch of 00:00 on 1 Jan 1970.
	Created *time.Time

	// REQUIRED; A unique identifier associated with this chat completions response.
	ID *string

	// REQUIRED; Usage information for tokens processed and generated as part of this completions operation.
	Usage *CompletionsUsage

	// Content filtering results for zero or more prompts in the request. In a streaming request, results for different prompts
	// may arrive at different times or in different orders.
	PromptAnnotations []PromptFilterResult
}

ChatCompletions - Representation of the response data from a chat completions request. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.

func (ChatCompletions) MarshalJSON

func (c ChatCompletions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatCompletions.

func (*ChatCompletions) UnmarshalJSON

func (c *ChatCompletions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatCompletions.

type ChatCompletionsOptions

type ChatCompletionsOptions struct {
	// REQUIRED; The collection of context messages associated with this chat completions request. Typical usage begins with a
	// chat message for the System role that provides instructions for the behavior of the
	// assistant, followed by alternating messages between the User and Assistant roles.
	Messages []ChatMessage

	// The configuration entries for Azure OpenAI chat extensions that use them. This additional specification is only compatible
	// with Azure OpenAI.
	AzureExtensionsOptions *AzureChatExtensionOptions

	// A value that influences the probability of generated tokens appearing based on their cumulative frequency in generated
	// text. Positive values will make tokens less likely to appear as their frequency
	// increases and decrease the likelihood of the model repeating the same statements verbatim.
	FrequencyPenalty *float32

	// Controls how the model responds to function calls. "none" means the model does not call a function, and responds to the
	// end-user. "auto" means the model can pick between an end-user or calling a
	// function. Specifying a particular function via {"name": "my_function"} forces the model to call that function. "none" is
	// the default when no functions are present. "auto" is the default if functions
	// are present.
	FunctionCall *ChatCompletionsOptionsFunctionCall

	// A list of functions the model may generate JSON inputs for.
	Functions []FunctionDefinition

	// A map between GPT token IDs and bias scores that influences the probability of specific tokens appearing in a completions
	// response. Token IDs are computed via external tokenizer tools, while bias
	// scores reside in the range of -100 to 100 with minimum and maximum values corresponding to a full ban or exclusive selection
	// of a token, respectively. The exact behavior of a given bias score varies
	// by model.
	LogitBias map[string]*int32

	// The maximum number of tokens to generate.
	MaxTokens *int32

	// REQUIRED: Deployment specifies the name of the deployment (for Azure OpenAI) or model (for OpenAI) to use for this request.
	Deployment string

	// The number of chat completions choices that should be generated for a chat completions response. Because this setting can
	// generate many completions, it may quickly consume your token quota. Use
	// carefully and ensure reasonable settings for max_tokens and stop.
	N *int32

	// A value that influences the probability of generated tokens appearing based on their existing presence in generated text.
	// Positive values will make tokens less likely to appear when they already exist
	// and increase the model's likelihood to output new topics.
	PresencePenalty *float32

	// A collection of textual sequences that will end completions generation.
	Stop []string

	// The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make
	// output more random while lower values will make results more focused and
	// deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction
	// of these two settings is difficult to predict.
	Temperature *float32

	// An alternative to sampling with temperature called nucleus sampling. This value causes the model to consider the results
	// of tokens with the provided probability mass. As an example, a value of 0.15
	// will cause only the tokens comprising the top 15% of probability mass to be considered. It is not recommended to modify
	// temperature and top_p for the same completions request as the interaction of
	// these two settings is difficult to predict.
	TopP *float32

	// An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes.
	User *string
}

ChatCompletionsOptions - The configuration information for a chat completions request. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.

func (ChatCompletionsOptions) MarshalJSON

func (c ChatCompletionsOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatCompletionsOptions.

func (*ChatCompletionsOptions) UnmarshalJSON

func (c *ChatCompletionsOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatCompletionsOptions.

type ChatCompletionsOptionsFunctionCall

type ChatCompletionsOptionsFunctionCall struct {
	// IsFunction is true if Value refers to a function name.
	IsFunction bool

	// Value is one of:
	// - "auto", meaning the model can pick between an end-user or calling a function
	// - "none", meaning the model does not call a function,
	// - name of a function, in which case [IsFunction] should be set to true.
	Value *string
}

ChatCompletionsOptionsFunctionCall - Controls how the model responds to function calls. "none" means the model does not call a function, and responds to the end-user. "auto" means the model can pick between an end-user or calling a function. Specifying a particular function via {"name": "my_function"} forces the model to call that function. "none" is the default when no functions are present. "auto" is the default if functions are present.

func (ChatCompletionsOptionsFunctionCall) MarshalJSON

func (c ChatCompletionsOptionsFunctionCall) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatCompletionsOptionsFunctionCall.

type ChatMessage

type ChatMessage struct {
	// REQUIRED; The text associated with this message payload.
	Content *string

	// REQUIRED; The role associated with this message payload.
	Role *ChatRole

	// Additional context data associated with a chat message when requesting chat completions using compatible Azure OpenAI chat
	// extensions. This includes information like the intermediate data source
	// retrievals used to service a request. This context information is only populated when using Azure OpenAI with chat extensions
	// capabilities configured.
	Context *ChatMessageContext

	// The name and arguments of a function that should be called, as generated by the model.
	FunctionCall *ChatMessageFunctionCall

	// The name of the author of this message. name is required if role is function, and it should be the name of the function
	// whose response is in the content. May contain a-z, A-Z, 0-9, and underscores,
	// with a maximum length of 64 characters.
	Name *string
}

ChatMessage - A single, role-attributed message within a chat completion interaction.

func (ChatMessage) MarshalJSON

func (c ChatMessage) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatMessage.

func (*ChatMessage) UnmarshalJSON

func (c *ChatMessage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatMessage.

type ChatMessageContext added in v0.2.0

type ChatMessageContext struct {
	// The contextual message payload associated with the Azure chat extensions used for a chat completions request. These messages
	// describe the data source retrievals, plugin invocations, and other
	// intermediate steps taken in the course of generating a chat completions response that was augmented by capabilities from
	// Azure OpenAI chat extensions.
	Messages []ChatMessage
}

ChatMessageContext - Additional context data associated with a chat message when requesting chat completions using compatible Azure OpenAI chat extensions. This includes information like the intermediate data source retrievals used to service a request. This context information is only populated when using Azure OpenAI with chat extensions capabilities configured.

func (ChatMessageContext) MarshalJSON added in v0.2.0

func (c ChatMessageContext) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatMessageContext.

func (*ChatMessageContext) UnmarshalJSON added in v0.2.0

func (c *ChatMessageContext) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatMessageContext.

type ChatMessageFunctionCall

type ChatMessageFunctionCall struct {
	// REQUIRED; The arguments to call the function with, as generated by the model in JSON format. Note that the model does not
	// always generate valid JSON, and may hallucinate parameters not defined by your function
	// schema. Validate the arguments in your code before calling your function.
	Arguments *string

	// REQUIRED; The name of the function to call.
	Name *string
}

ChatMessageFunctionCall - The name and arguments of a function that should be called, as generated by the model.

func (ChatMessageFunctionCall) MarshalJSON

func (c ChatMessageFunctionCall) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChatMessageFunctionCall.

func (*ChatMessageFunctionCall) UnmarshalJSON

func (c *ChatMessageFunctionCall) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChatMessageFunctionCall.

type ChatRole

type ChatRole string

ChatRole - A description of the intended purpose of a message within a chat completions interaction.

const (
	ChatRoleAssistant ChatRole = "assistant"
	ChatRoleFunction  ChatRole = "function"
	ChatRoleSystem    ChatRole = "system"
	ChatRoleTool      ChatRole = "tool"
	ChatRoleUser      ChatRole = "user"
)

func PossibleChatRoleValues

func PossibleChatRoleValues() []ChatRole

PossibleChatRoleValues returns the possible values for the ChatRole const type.

type Choice

type Choice struct {
	// REQUIRED; Reason for finishing
	FinishReason *CompletionsFinishReason

	// REQUIRED; The ordered index associated with this completions choice.
	Index *int32

	// REQUIRED; The log probabilities model for tokens associated with this completions choice.
	LogProbs *ChoiceLogProbs

	// REQUIRED; The generated text for a given completions prompt.
	Text *string

	// Information about the content filtering category (hate, sexual, violence, selfharm), if it has been detected, as well as
	// the severity level (verylow, low, medium, high-scale that determines the
	// intensity and risk level of harmful content) and if it has been filtered or not.
	ContentFilterResults *ChoiceContentFilterResults
}

Choice - The representation of a single prompt completion as part of an overall completions request. Generally, n choices are generated per provided prompt with a default value of 1. Token limits and other settings may limit the number of choices generated.

func (Choice) MarshalJSON

func (c Choice) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Choice.

func (*Choice) UnmarshalJSON

func (c *Choice) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Choice.

type ChoiceContentFilterResults

type ChoiceContentFilterResults struct {
	// Describes language attacks or uses that include pejorative or discriminatory language with reference to a person or identity
	// group on the basis of certain differentiating attributes of these groups
	// including but not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion,
	// immigration status, ability status, personal appearance, and body size.
	Hate *ContentFilterResultsHate

	// Describes language related to physical actions intended to purposely hurt, injure, or damage one’s body, or kill oneself.
	SelfHarm *ContentFilterResultsSelfHarm

	// Describes language related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate
	// terms, physical sexual acts, including those portrayed as an assault or a
	// forced sexual violent act against one’s will, prostitution, pornography, and abuse.
	Sexual *ContentFilterResultsSexual

	// Describes language related to physical actions intended to hurt, injure, damage, or kill someone or something; describes
	// weapons, etc.
	Violence *ContentFilterResultsViolence
}

ChoiceContentFilterResults - Information about the content filtering category (hate, sexual, violence, selfharm), if it has been detected, as well as the severity level (verylow, low, medium, high-scale that determines the intensity and risk level of harmful content) and if it has been filtered or not.

func (ChoiceContentFilterResults) MarshalJSON

func (c ChoiceContentFilterResults) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChoiceContentFilterResults.

func (*ChoiceContentFilterResults) UnmarshalJSON

func (c *ChoiceContentFilterResults) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChoiceContentFilterResults.

type ChoiceLogProbs

type ChoiceLogProbs struct {
	// REQUIRED; The text offsets associated with tokens in this completions data.
	TextOffset []int32

	// REQUIRED; A collection of log probability values for the tokens in this completions data.
	TokenLogProbs []float32

	// REQUIRED; The textual forms of tokens evaluated in this probability model.
	Tokens []string

	// REQUIRED; A mapping of tokens to maximum log probability values in this completions data.
	TopLogProbs []any
}

ChoiceLogProbs - The log probabilities model for tokens associated with this completions choice.

func (ChoiceLogProbs) MarshalJSON

func (c ChoiceLogProbs) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ChoiceLogProbs.

func (*ChoiceLogProbs) UnmarshalJSON

func (c *ChoiceLogProbs) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ChoiceLogProbs.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client contains the methods for the OpenAI group. Don't use this type directly, use a constructor function instead.

func NewClient

func NewClient(endpoint string, credential azcore.TokenCredential, options *ClientOptions) (*Client, error)

NewClient creates a new instance of Client that connects to an Azure OpenAI endpoint.

  • endpoint - Azure OpenAI service endpoint, for example: https://{your-resource-name}.openai.azure.com
  • credential - used to authorize requests. Usually a credential from github.com/Azure/azure-sdk-for-go/sdk/azidentity.
  • options - client options, pass nil to accept the default values.
Example
package main

import (
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)

func main() {
	dac, err := azidentity.NewDefaultAzureCredential(nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// NOTE: this constructor creates a client that connects to an Azure OpenAI endpoint.
	// To connect to the public OpenAI endpoint, use azopenai.NewClientForOpenAI
	client, err := azopenai.NewClient("https://<your-azure-openai-host>.openai.azure.com", dac, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client
}

func NewClientForOpenAI

func NewClientForOpenAI(endpoint string, credential KeyCredential, options *ClientOptions) (*Client, error)

NewClientForOpenAI creates a new instance of Client which connects to the public OpenAI endpoint.

  • endpoint - OpenAI service endpoint, for example: https://api.openai.com/v1
  • credential - used to authorize requests with an API Key credential
  • options - client options, pass nil to accept the default values.
Example
package main

import (
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
)

func main() {
	keyCredential, err := azopenai.NewKeyCredential("<OpenAI-APIKey>")

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// NOTE: this constructor creates a client that connects to the public OpenAI endpoint.
	// To connect to an Azure OpenAI endpoint, use azopenai.NewClient() or azopenai.NewClientWithyKeyCredential.
	client, err := azopenai.NewClientForOpenAI("https://api.openai.com/v1", keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client
}

func NewClientWithKeyCredential

func NewClientWithKeyCredential(endpoint string, credential KeyCredential, options *ClientOptions) (*Client, error)

NewClientWithKeyCredential creates a new instance of Client that connects to an Azure OpenAI endpoint.

  • endpoint - Azure OpenAI service endpoint, for example: https://{your-resource-name}.openai.azure.com
  • credential - used to authorize requests with an API Key credential
  • options - client options, pass nil to accept the default values.
Example
package main

import (
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
)

func main() {
	keyCredential, err := azopenai.NewKeyCredential("<Azure-OpenAI-APIKey>")

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// NOTE: this constructor creates a client that connects to an Azure OpenAI endpoint.
	// To connect to the public OpenAI endpoint, use azopenai.NewClientForOpenAI
	client, err := azopenai.NewClientWithKeyCredential("https://<your-azure-openai-host>.openai.azure.com", keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	_ = client
}

func (*Client) CreateImage

func (client *Client) CreateImage(ctx context.Context, body ImageGenerationOptions, options *CreateImageOptions) (CreateImageResponse, error)

CreateImage creates an image using the Dall-E API.

Example
package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.CreateImage(context.TODO(), azopenai.ImageGenerationOptions{
		Prompt:         to.Ptr("a cat"),
		ResponseFormat: to.Ptr(azopenai.ImageGenerationResponseFormatURL),
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	for _, generatedImage := range resp.Data {
		// the underlying type for the generatedImage is dictated by the value of
		// ImageGenerationOptions.ResponseFormat. In this example we used `azopenai.ImageGenerationResponseFormatURL`,
		// so the underlying type will be ImageLocation.

		resp, err := http.Head(*generatedImage.URL)

		if err != nil {
			// TODO: handle error
			log.Fatalf("ERROR: %s", err)
		}

		fmt.Fprintf(os.Stderr, "Image generated, HEAD request on URL returned %d\n", resp.StatusCode)
	}

}

func (*Client) GetChatCompletions

func (client *Client) GetChatCompletions(ctx context.Context, body ChatCompletionsOptions, options *GetChatCompletionsOptions) (GetChatCompletionsResponse, error)

GetChatCompletions - Gets chat completions for the provided chat messages. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data. If the operation fails it returns an *azcore.ResponseError type.

Example
package main

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

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_CHAT_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// This is a conversation in progress.
	// NOTE: all messages, regardless of role, count against token usage for this API.
	messages := []azopenai.ChatMessage{
		// You set the tone and rules of the conversation with a prompt as the system role.
		{Role: to.Ptr(azopenai.ChatRoleSystem), Content: to.Ptr("You are a helpful assistant. You will talk like a pirate.")},

		// The user asks a question
		{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Can you help me?")},

		// The reply would come back from the ChatGPT. You'd add it to the conversation so we can maintain context.
		{Role: to.Ptr(azopenai.ChatRoleAssistant), Content: to.Ptr("Arrrr! Of course, me hearty! What can I do for ye?")},

		// The user answers the question based on the latest reply.
		{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("What's the best way to train a parrot?")},

		// from here you'd keep iterating, sending responses back from ChatGPT
	}

	gotReply := false

	resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
		// This is a conversation in progress.
		// NOTE: all messages count against token usage for this API.
		Messages:   messages,
		Deployment: modelDeploymentID,
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	for _, choice := range resp.Choices {
		gotReply = true
		fmt.Fprintf(os.Stderr, "Content[%d]: %s\n", *choice.Index, *choice.Message.Content)
	}

	if gotReply {
		fmt.Fprintf(os.Stderr, "Got chat completions reply\n")
	}

}
Example (BringYourOwnDataWithCognitiveSearch)

With Azure OpenAI you can integrate data you've already uploaded to an Azure Cognitive Search index. For more information about this feature see the article "Azure OpenAI on your data".

package main

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

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_CHAT_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	// Azure Cognitive Search configuration
	searchIndex := os.Getenv("COGNITIVE_SEARCH_API_INDEX")
	searchEndpoint := os.Getenv("COGNITIVE_SEARCH_API_ENDPOINT")
	searchAPIKey := os.Getenv("COGNITIVE_SEARCH_API_KEY")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" || searchIndex == "" || searchEndpoint == "" || searchAPIKey == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
		Messages: []azopenai.ChatMessage{
			{Content: to.Ptr("What are the differences between Azure Machine Learning and Azure AI services?"), Role: to.Ptr(azopenai.ChatRoleUser)},
		},
		MaxTokens: to.Ptr[int32](512),
		AzureExtensionsOptions: &azopenai.AzureChatExtensionOptions{
			Extensions: []azopenai.AzureChatExtensionConfiguration{
				{
					// This allows Azure OpenAI to use an Azure Cognitive Search index.
					//
					// > Because the model has access to, and can reference specific sources to support its responses, answers are not only based on its pretrained knowledge
					// > but also on the latest information available in the designated data source. This grounding data also helps the model avoid generating responses
					// > based on outdated or incorrect information.
					//
					// Quote from here: https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/use-your-data
					Type: to.Ptr(azopenai.AzureChatExtensionTypeAzureCognitiveSearch),
					Parameters: azopenai.AzureCognitiveSearchChatExtensionConfiguration{
						Endpoint:  &searchEndpoint,
						IndexName: &searchIndex,
						Key:       &searchAPIKey,
					},
				},
			},
		},
		Deployment: "gpt-4",
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// Contains contextual information from your Azure chat completion extensions, configured above in `AzureExtensionsOptions`
	msgContext := resp.Choices[0].Message.Context

	fmt.Fprintf(os.Stderr, "Extensions Context Role: %s\nExtensions Context (length): %d\n",
		*msgContext.Messages[0].Role,
		len(*msgContext.Messages[0].Content))

	fmt.Fprintf(os.Stderr, "ChatRole: %s\nChat content: %s\n",
		*resp.Choices[0].Message.Role,
		*resp.Choices[0].Message.Content,
	)

}
Example (Functions)
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_CHAT_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.GetChatCompletions(context.TODO(), azopenai.ChatCompletionsOptions{
		Deployment: modelDeploymentID,
		Messages: []azopenai.ChatMessage{
			{
				Role:    to.Ptr(azopenai.ChatRoleUser),
				Content: to.Ptr("What's the weather like in Boston, MA, in celsius?"),
			},
		},
		FunctionCall: &azopenai.ChatCompletionsOptionsFunctionCall{
			Value: to.Ptr("auto"),
		},
		Functions: []azopenai.FunctionDefinition{
			{
				Name:        to.Ptr("get_current_weather"),
				Description: to.Ptr("Get the current weather in a given location"),

				Parameters: map[string]any{
					"required": []string{"location"},
					"type":     "object",
					"properties": map[string]any{
						"location": map[string]any{
							"type":        "string",
							"description": "The city and state, e.g. San Francisco, CA",
						},
						"unit": map[string]any{
							"type": "string",
							"enum": []string{"celsius", "fahrenheit"},
						},
					},
				},
			},
		},
		Temperature: to.Ptr[float32](0.0),
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	funcCall := resp.ChatCompletions.Choices[0].Message.FunctionCall

	// This is the function name we gave in the call to GetCompletions
	// Prints: Function name: "get_current_weather"
	fmt.Fprintf(os.Stderr, "Function name: %q\n", *funcCall.Name)

	// The arguments for your function come back as a JSON string
	var funcParams *struct {
		Location string `json:"location"`
		Unit     string `json:"unit"`
	}
	err = json.Unmarshal([]byte(*funcCall.Arguments), &funcParams)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// Prints:
	// Parameters: azopenai_test.location{Location:"Boston, MA", Unit:"celsius"}
	fmt.Fprintf(os.Stderr, "Parameters: %#v\n", *funcParams)

}

func (*Client) GetChatCompletionsStream

GetChatCompletionsStream - Return the chat completions for a given prompt as a sequence of events. If the operation fails it returns an *azcore.ResponseError type.

  • options - GetCompletionsOptions contains the optional parameters for the Client.GetCompletions method.
Example
package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_CHAT_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// This is a conversation in progress.
	// NOTE: all messages, regardless of role, count against token usage for this API.
	messages := []azopenai.ChatMessage{
		// You set the tone and rules of the conversation with a prompt as the system role.
		{Role: to.Ptr(azopenai.ChatRoleSystem), Content: to.Ptr("You are a helpful assistant. You will talk like a pirate and limit your responses to 20 words or less.")},

		// The user asks a question
		{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("Can you help me?")},

		// The reply would come back from the ChatGPT. You'd add it to the conversation so we can maintain context.
		{Role: to.Ptr(azopenai.ChatRoleAssistant), Content: to.Ptr("Arrrr! Of course, me hearty! What can I do for ye?")},

		// The user answers the question based on the latest reply.
		{Role: to.Ptr(azopenai.ChatRoleUser), Content: to.Ptr("What's the best way to train a parrot?")},

		// from here you'd keep iterating, sending responses back from ChatGPT
	}

	resp, err := client.GetChatCompletionsStream(context.TODO(), azopenai.ChatCompletionsOptions{
		// This is a conversation in progress.
		// NOTE: all messages count against token usage for this API.
		Messages:   messages,
		N:          to.Ptr[int32](1),
		Deployment: modelDeploymentID,
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	defer resp.ChatCompletionsStream.Close()

	gotReply := false

	for {
		chatCompletions, err := resp.ChatCompletionsStream.Read()

		if errors.Is(err, io.EOF) {
			break
		}

		if err != nil {
			// TODO: handle error
			log.Fatalf("ERROR: %s", err)
		}

		for _, choice := range chatCompletions.Choices {
			gotReply = true

			text := ""

			if choice.Delta.Content != nil {
				text = *choice.Delta.Content
			}

			role := ""

			if choice.Delta.Role != nil {
				role = string(*choice.Delta.Role)
			}

			fmt.Fprintf(os.Stderr, "Content[%d], role %q: %q\n", *choice.Index, role, text)
		}
	}

	if gotReply {
		fmt.Fprintf(os.Stderr, "Got chat completions streaming reply\n")
	}

}

func (*Client) GetCompletions

func (client *Client) GetCompletions(ctx context.Context, body CompletionsOptions, options *GetCompletionsOptions) (GetCompletionsResponse, error)

GetCompletions - Gets completions for the provided input prompts. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2023-08-01-preview

  • options - GetCompletionsOptions contains the optional parameters for the Client.GetCompletions method.
Example
package main

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

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeployment := os.Getenv("AOAI_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeployment == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.GetCompletions(context.TODO(), azopenai.CompletionsOptions{
		Prompt:      []string{"What is Azure OpenAI, in 20 words or less"},
		MaxTokens:   to.Ptr(int32(2048)),
		Temperature: to.Ptr(float32(0.0)),
		Deployment:  modelDeployment,
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	for _, choice := range resp.Choices {
		fmt.Fprintf(os.Stderr, "Result: %s\n", *choice.Text)
	}

}

func (*Client) GetCompletionsStream

func (client *Client) GetCompletionsStream(ctx context.Context, body CompletionsOptions, options *GetCompletionsStreamOptions) (GetCompletionsStreamResponse, error)

GetCompletionsStream - Return the completions for a given prompt as a sequence of events. If the operation fails it returns an *azcore.ResponseError type.

  • options - GetCompletionsOptions contains the optional parameters for the Client.GetCompletions method.
Example
package main

import (
	"context"
	"errors"
	"fmt"
	"io"
	"log"
	"os"

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_COMPLETIONS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.GetCompletionsStream(context.TODO(), azopenai.CompletionsOptions{
		Prompt:      []string{"What is Azure OpenAI, in 20 words or less?"},
		MaxTokens:   to.Ptr(int32(2048)),
		Temperature: to.Ptr(float32(0.0)),
		Deployment:  modelDeploymentID,
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	defer resp.CompletionsStream.Close()

	for {
		entry, err := resp.CompletionsStream.Read()

		if errors.Is(err, io.EOF) {
			fmt.Fprintf(os.Stderr, "\n*** No more completions ***\n")
			break
		}

		if err != nil {
			// TODO: handle error
			log.Fatalf("ERROR: %s", err)
		}

		for _, choice := range entry.Choices {
			fmt.Fprintf(os.Stderr, "Result: %s\n", *choice.Text)
		}
	}

}

func (*Client) GetEmbeddings

func (client *Client) GetEmbeddings(ctx context.Context, body EmbeddingsOptions, options *GetEmbeddingsOptions) (GetEmbeddingsResponse, error)

GetEmbeddings - Return the embeddings for a given prompt. If the operation fails it returns an *azcore.ResponseError type.

Generated from API version 2023-08-01-preview

  • options - GetEmbeddingsOptions contains the optional parameters for the Client.GetEmbeddings method.
Example
package main

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

	"github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
)

func main() {
	azureOpenAIKey := os.Getenv("AOAI_API_KEY")
	modelDeploymentID := os.Getenv("AOAI_EMBEDDINGS_MODEL_DEPLOYMENT")

	// Ex: "https://<your-azure-openai-host>.openai.azure.com"
	azureOpenAIEndpoint := os.Getenv("AOAI_ENDPOINT")

	if azureOpenAIKey == "" || modelDeploymentID == "" || azureOpenAIEndpoint == "" {
		fmt.Fprintf(os.Stderr, "Skipping example, environment variables missing\n")
		return
	}

	keyCredential, err := azopenai.NewKeyCredential(azureOpenAIKey)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	// In Azure OpenAI you must deploy a model before you can use it in your client. For more information
	// see here: https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource
	client, err := azopenai.NewClientWithKeyCredential(azureOpenAIEndpoint, keyCredential, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	resp, err := client.GetEmbeddings(context.TODO(), azopenai.EmbeddingsOptions{
		Input:      []string{"The food was delicious and the waiter..."},
		Deployment: modelDeploymentID,
	}, nil)

	if err != nil {
		//  TODO: Update the following line with your application specific error handling logic
		log.Fatalf("ERROR: %s", err)
	}

	for _, embed := range resp.Data {
		// embed.Embedding contains the embeddings for this input index.
		fmt.Fprintf(os.Stderr, "Got embeddings for input %d\n", *embed.Index)
	}

}

type ClientOptions

type ClientOptions struct {
	azcore.ClientOptions
}

ClientOptions contains optional settings for Client.

type Completions

type Completions struct {
	// REQUIRED; The collection of completions choices associated with this completions response. Generally, n choices are generated
	// per provided prompt with a default value of 1. Token limits and other settings may
	// limit the number of choices generated.
	Choices []Choice

	// REQUIRED; The first timestamp associated with generation activity for this completions response, represented as seconds
	// since the beginning of the Unix epoch of 00:00 on 1 Jan 1970.
	Created *time.Time

	// REQUIRED; A unique identifier associated with this completions response.
	ID *string

	// REQUIRED; Usage information for tokens processed and generated as part of this completions operation.
	Usage *CompletionsUsage

	// Content filtering results for zero or more prompts in the request. In a streaming request, results for different prompts
	// may arrive at different times or in different orders.
	PromptAnnotations []PromptFilterResult
}

Completions - Representation of the response data from a completions request. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.

func (Completions) MarshalJSON

func (c Completions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Completions.

func (*Completions) UnmarshalJSON

func (c *Completions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Completions.

type CompletionsFinishReason

type CompletionsFinishReason string

CompletionsFinishReason - Representation of the manner in which a completions response concluded.

const (
	CompletionsFinishReasonContentFilter CompletionsFinishReason = "content_filter"
	CompletionsFinishReasonFunctionCall  CompletionsFinishReason = "function_call"
	CompletionsFinishReasonLength        CompletionsFinishReason = "length"
	CompletionsFinishReasonStop          CompletionsFinishReason = "stop"
)

func PossibleCompletionsFinishReasonValues

func PossibleCompletionsFinishReasonValues() []CompletionsFinishReason

PossibleCompletionsFinishReasonValues returns the possible values for the CompletionsFinishReason const type.

type CompletionsLogProbabilityModel

type CompletionsLogProbabilityModel struct {
	// REQUIRED; The text offsets associated with tokens in this completions data.
	TextOffset []int32

	// REQUIRED; A collection of log probability values for the tokens in this completions data.
	TokenLogProbs []float32

	// REQUIRED; The textual forms of tokens evaluated in this probability model.
	Tokens []string

	// REQUIRED; A mapping of tokens to maximum log probability values in this completions data.
	TopLogProbs []any
}

CompletionsLogProbabilityModel - Representation of a log probabilities model for a completions generation.

func (CompletionsLogProbabilityModel) MarshalJSON

func (c CompletionsLogProbabilityModel) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type CompletionsLogProbabilityModel.

func (*CompletionsLogProbabilityModel) UnmarshalJSON

func (c *CompletionsLogProbabilityModel) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type CompletionsLogProbabilityModel.

type CompletionsOptions

type CompletionsOptions struct {
	// REQUIRED; The prompts to generate completions from.
	Prompt []string

	// A value that controls how many completions will be internally generated prior to response formulation. When used together
	// with n, bestof controls the number of candidate completions and must be
	// greater than n. Because this setting can generate many completions, it may quickly consume your token quota. Use carefully
	// and ensure reasonable settings for maxtokens and stop.
	BestOf *int32

	// A value specifying whether completions responses should include input prompts as prefixes to their generated output.
	Echo *bool

	// A value that influences the probability of generated tokens appearing based on their cumulative frequency in generated
	// text. Positive values will make tokens less likely to appear as their frequency
	// increases and decrease the likelihood of the model repeating the same statements verbatim.
	FrequencyPenalty *float32

	// A map between GPT token IDs and bias scores that influences the probability of specific tokens appearing in a completions
	// response. Token IDs are computed via external tokenizer tools, while bias
	// scores reside in the range of -100 to 100 with minimum and maximum values corresponding to a full ban or exclusive selection
	// of a token, respectively. The exact behavior of a given bias score varies
	// by model.
	LogitBias map[string]*int32

	// A value that controls the emission of log probabilities for the provided number of most likely tokens within a completions
	// response.
	LogProbs *int32

	// The maximum number of tokens to generate.
	MaxTokens *int32

	// REQUIRED: Deployment specifies the name of the deployment (for Azure OpenAI) or model (for OpenAI) to use for this request.
	Deployment string

	// The number of completions choices that should be generated per provided prompt as part of an overall completions response.
	// Because this setting can generate many completions, it may quickly consume
	// your token quota. Use carefully and ensure reasonable settings for max_tokens and stop.
	N *int32

	// A value that influences the probability of generated tokens appearing based on their existing presence in generated text.
	// Positive values will make tokens less likely to appear when they already exist
	// and increase the model's likelihood to output new topics.
	PresencePenalty *float32

	// A collection of textual sequences that will end completions generation.
	Stop []string

	// The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make
	// output more random while lower values will make results more focused and
	// deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction
	// of these two settings is difficult to predict.
	Temperature *float32

	// An alternative to sampling with temperature called nucleus sampling. This value causes the model to consider the results
	// of tokens with the provided probability mass. As an example, a value of 0.15
	// will cause only the tokens comprising the top 15% of probability mass to be considered. It is not recommended to modify
	// temperature and top_p for the same completions request as the interaction of
	// these two settings is difficult to predict.
	TopP *float32

	// An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes.
	User *string
}

CompletionsOptions - The configuration information for a completions request. Completions support a wide variety of tasks and generate text that continues from or "completes" provided prompt data.

func (CompletionsOptions) MarshalJSON

func (c CompletionsOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type CompletionsOptions.

func (*CompletionsOptions) UnmarshalJSON

func (c *CompletionsOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type CompletionsOptions.

type CompletionsUsage

type CompletionsUsage struct {
	// REQUIRED; The number of tokens generated across all completions emissions.
	CompletionTokens *int32

	// REQUIRED; The number of tokens in the provided prompts for the completions request.
	PromptTokens *int32

	// REQUIRED; The total number of tokens processed for the completions request and response.
	TotalTokens *int32
}

CompletionsUsage - Representation of the token counts processed for a completions request. Counts consider all tokens across prompts, choices, choice alternates, best_of generations, and other consumers.

func (CompletionsUsage) MarshalJSON

func (c CompletionsUsage) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type CompletionsUsage.

func (*CompletionsUsage) UnmarshalJSON

func (c *CompletionsUsage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type CompletionsUsage.

type ContentFilterResponseError

type ContentFilterResponseError struct {
	azcore.ResponseError

	// ContentFilterResults contains Information about the content filtering category, if it has been detected.
	ContentFilterResults *ContentFilterResults
}

ContentFilterResponseError is an error as a result of a request being filtered.

func (*ContentFilterResponseError) Unwrap

func (e *ContentFilterResponseError) Unwrap() error

Unwrap returns the inner error for this error.

type ContentFilterResults

type ContentFilterResults struct {
	// Describes language attacks or uses that include pejorative or discriminatory language with reference to a person or identity
	// group on the basis of certain differentiating attributes of these groups
	// including but not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion,
	// immigration status, ability status, personal appearance, and body size.
	Hate *ContentFilterResultsHate

	// Describes language related to physical actions intended to purposely hurt, injure, or damage one’s body, or kill oneself.
	SelfHarm *ContentFilterResultsSelfHarm

	// Describes language related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate
	// terms, physical sexual acts, including those portrayed as an assault or a
	// forced sexual violent act against one’s will, prostitution, pornography, and abuse.
	Sexual *ContentFilterResultsSexual

	// Describes language related to physical actions intended to hurt, injure, damage, or kill someone or something; describes
	// weapons, etc.
	Violence *ContentFilterResultsViolence
}

ContentFilterResults - Information about the content filtering category, if it has been detected.

func (ContentFilterResults) MarshalJSON

func (c ContentFilterResults) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ContentFilterResults.

func (*ContentFilterResults) UnmarshalJSON

func (c *ContentFilterResults) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ContentFilterResults.

type ContentFilterResultsHate

type ContentFilterResultsHate struct {
	// REQUIRED; A value indicating whether or not the content has been filtered.
	Filtered *bool

	// REQUIRED; Ratings for the intensity and risk level of filtered content.
	Severity *ContentFilterSeverity
}

ContentFilterResultsHate - Describes language attacks or uses that include pejorative or discriminatory language with reference to a person or identity group on the basis of certain differentiating attributes of these groups including but not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion, immigration status, ability status, personal appearance, and body size.

func (ContentFilterResultsHate) MarshalJSON

func (c ContentFilterResultsHate) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ContentFilterResultsHate.

func (*ContentFilterResultsHate) UnmarshalJSON

func (c *ContentFilterResultsHate) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ContentFilterResultsHate.

type ContentFilterResultsSelfHarm

type ContentFilterResultsSelfHarm struct {
	// REQUIRED; A value indicating whether or not the content has been filtered.
	Filtered *bool

	// REQUIRED; Ratings for the intensity and risk level of filtered content.
	Severity *ContentFilterSeverity
}

ContentFilterResultsSelfHarm - Describes language related to physical actions intended to purposely hurt, injure, or damage one’s body, or kill oneself.

func (ContentFilterResultsSelfHarm) MarshalJSON

func (c ContentFilterResultsSelfHarm) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ContentFilterResultsSelfHarm.

func (*ContentFilterResultsSelfHarm) UnmarshalJSON

func (c *ContentFilterResultsSelfHarm) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ContentFilterResultsSelfHarm.

type ContentFilterResultsSexual

type ContentFilterResultsSexual struct {
	// REQUIRED; A value indicating whether or not the content has been filtered.
	Filtered *bool

	// REQUIRED; Ratings for the intensity and risk level of filtered content.
	Severity *ContentFilterSeverity
}

ContentFilterResultsSexual - Describes language related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate terms, physical sexual acts, including those portrayed as an assault or a forced sexual violent act against one’s will, prostitution, pornography, and abuse.

func (ContentFilterResultsSexual) MarshalJSON

func (c ContentFilterResultsSexual) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ContentFilterResultsSexual.

func (*ContentFilterResultsSexual) UnmarshalJSON

func (c *ContentFilterResultsSexual) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ContentFilterResultsSexual.

type ContentFilterResultsViolence

type ContentFilterResultsViolence struct {
	// REQUIRED; A value indicating whether or not the content has been filtered.
	Filtered *bool

	// REQUIRED; Ratings for the intensity and risk level of filtered content.
	Severity *ContentFilterSeverity
}

ContentFilterResultsViolence - Describes language related to physical actions intended to hurt, injure, damage, or kill someone or something; describes weapons, etc.

func (ContentFilterResultsViolence) MarshalJSON

func (c ContentFilterResultsViolence) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ContentFilterResultsViolence.

func (*ContentFilterResultsViolence) UnmarshalJSON

func (c *ContentFilterResultsViolence) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ContentFilterResultsViolence.

type ContentFilterSeverity

type ContentFilterSeverity string

ContentFilterSeverity - Ratings for the intensity and risk level of harmful content.

const (
	ContentFilterSeverityHigh   ContentFilterSeverity = "high"
	ContentFilterSeverityLow    ContentFilterSeverity = "low"
	ContentFilterSeverityMedium ContentFilterSeverity = "medium"
	ContentFilterSeveritySafe   ContentFilterSeverity = "safe"
)

func PossibleContentFilterSeverityValues

func PossibleContentFilterSeverityValues() []ContentFilterSeverity

PossibleContentFilterSeverityValues returns the possible values for the ContentFilterSeverity const type.

type CreateImageOptions

type CreateImageOptions struct {
}

CreateImageOptions contains the optional parameters for the Client.CreateImage method.

type CreateImageResponse

type CreateImageResponse struct {
	ImageGenerations
}

CreateImageResponse contains the response from method Client.CreateImage.

type Deployment

type Deployment struct {
	// READ-ONLY; Specifies either the model deployment name (when using Azure OpenAI) or model name (when using non-Azure OpenAI)
	// to use for this request.
	DeploymentID *string
}

Deployment - A specific deployment

func (Deployment) MarshalJSON

func (d Deployment) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Deployment.

func (*Deployment) UnmarshalJSON

func (d *Deployment) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Deployment.

type EmbeddingItem

type EmbeddingItem struct {
	// REQUIRED; List of embeddings value for the input prompt. These represent a measurement of the vector-based relatedness
	// of the provided input.
	Embedding []float32

	// REQUIRED; Index of the prompt to which the EmbeddingItem corresponds.
	Index *int32
}

EmbeddingItem - Representation of a single embeddings relatedness comparison.

func (EmbeddingItem) MarshalJSON

func (e EmbeddingItem) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type EmbeddingItem.

func (*EmbeddingItem) UnmarshalJSON

func (e *EmbeddingItem) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingItem.

type Embeddings

type Embeddings struct {
	// REQUIRED; Embedding values for the prompts submitted in the request.
	Data []EmbeddingItem

	// REQUIRED; Usage counts for tokens input using the embeddings API.
	Usage *EmbeddingsUsage
}

Embeddings - Representation of the response data from an embeddings request. Embeddings measure the relatedness of text strings and are commonly used for search, clustering, recommendations, and other similar scenarios.

func (Embeddings) MarshalJSON

func (e Embeddings) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type Embeddings.

func (*Embeddings) UnmarshalJSON

func (e *Embeddings) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type Embeddings.

type EmbeddingsOptions

type EmbeddingsOptions struct {
	// REQUIRED; Input texts to get embeddings for, encoded as a an array of strings. Each input must not exceed 2048 tokens in
	// length.
	// Unless you are embedding code, we suggest replacing newlines (\n) in your input with a single space, as we have observed
	// inferior results when newlines are present.
	Input []string

	// REQUIRED: Deployment specifies the name of the deployment (for Azure OpenAI) or model (for OpenAI) to use for this request.
	Deployment string

	// An identifier for the caller or end user of the operation. This may be used for tracking or rate-limiting purposes.
	User *string
}

EmbeddingsOptions - The configuration information for an embeddings request. Embeddings measure the relatedness of text strings and are commonly used for search, clustering, recommendations, and other similar scenarios.

func (EmbeddingsOptions) MarshalJSON

func (e EmbeddingsOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type EmbeddingsOptions.

func (*EmbeddingsOptions) UnmarshalJSON

func (e *EmbeddingsOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingsOptions.

type EmbeddingsUsage

type EmbeddingsUsage struct {
	// REQUIRED; Number of tokens sent in the original request.
	PromptTokens *int32

	// REQUIRED; Total number of tokens transacted in this request/response.
	TotalTokens *int32
}

EmbeddingsUsage - Usage counts for tokens input using the embeddings API.

func (EmbeddingsUsage) MarshalJSON

func (e EmbeddingsUsage) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type EmbeddingsUsage.

func (*EmbeddingsUsage) UnmarshalJSON

func (e *EmbeddingsUsage) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingsUsage.

type EmbeddingsUsageAutoGenerated

type EmbeddingsUsageAutoGenerated struct {
	// REQUIRED; Number of tokens sent in the original request.
	PromptTokens *int32

	// REQUIRED; Total number of tokens transacted in this request/response.
	TotalTokens *int32
}

EmbeddingsUsageAutoGenerated - Measurement of the amount of tokens used in this request and response.

func (EmbeddingsUsageAutoGenerated) MarshalJSON

func (e EmbeddingsUsageAutoGenerated) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type EmbeddingsUsageAutoGenerated.

func (*EmbeddingsUsageAutoGenerated) UnmarshalJSON

func (e *EmbeddingsUsageAutoGenerated) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type EmbeddingsUsageAutoGenerated.

type EventReader

type EventReader[T any] struct {
	// contains filtered or unexported fields
}

EventReader streams events dynamically from an OpenAI endpoint.

func (*EventReader[T]) Close

func (er *EventReader[T]) Close() error

Close closes the EventReader and any applicable inner stream state.

func (*EventReader[T]) Read

func (er *EventReader[T]) Read() (T, error)

Read reads the next event from the stream. Returns io.EOF when there are no further events.

type FunctionCall

type FunctionCall struct {
	// REQUIRED; The arguments to call the function with, as generated by the model in JSON format. Note that the model does not
	// always generate valid JSON, and may hallucinate parameters not defined by your function
	// schema. Validate the arguments in your code before calling your function.
	Arguments *string

	// REQUIRED; The name of the function to call.
	Name *string
}

FunctionCall - The name and arguments of a function that should be called, as generated by the model.

func (FunctionCall) MarshalJSON

func (f FunctionCall) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type FunctionCall.

func (*FunctionCall) UnmarshalJSON

func (f *FunctionCall) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type FunctionCall.

type FunctionCallPreset

type FunctionCallPreset string

FunctionCallPreset - The collection of predefined behaviors for handling request-provided function information in a chat completions operation.

const (
	FunctionCallPresetAuto FunctionCallPreset = "auto"
	FunctionCallPresetNone FunctionCallPreset = "none"
)

func PossibleFunctionCallPresetValues

func PossibleFunctionCallPresetValues() []FunctionCallPreset

PossibleFunctionCallPresetValues returns the possible values for the FunctionCallPreset const type.

type FunctionDefinition

type FunctionDefinition struct {
	// REQUIRED; The name of the function to be called.
	Name *string

	// A description of what the function does. The model will use this description when selecting the function and interpreting
	// its parameters.
	Description *string

	// The parameters the functions accepts, described as a JSON Schema object.
	Parameters any
}

FunctionDefinition - The definition of a caller-specified function that chat completions may invoke in response to matching user input.

func (FunctionDefinition) MarshalJSON

func (f FunctionDefinition) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type FunctionDefinition.

func (*FunctionDefinition) UnmarshalJSON

func (f *FunctionDefinition) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type FunctionDefinition.

type FunctionName

type FunctionName struct {
	// REQUIRED; The name of the function to call.
	Name *string
}

FunctionName - A structure that specifies the exact name of a specific, request-provided function to use when processing a chat completions operation.

func (FunctionName) MarshalJSON

func (f FunctionName) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type FunctionName.

func (*FunctionName) UnmarshalJSON

func (f *FunctionName) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type FunctionName.

type GetChatCompletionsOptions

type GetChatCompletionsOptions struct {
}

GetChatCompletionsOptions contains the optional parameters for the Client.GetChatCompletions method.

type GetChatCompletionsResponse

type GetChatCompletionsResponse struct {
	// Representation of the response data from a chat completions request.
	// Completions support a wide variety of tasks and generate text that continues from or "completes"
	// provided prompt data.
	ChatCompletions
}

GetChatCompletionsResponse contains the response from method Client.GetChatCompletions.

type GetChatCompletionsStreamOptions

type GetChatCompletionsStreamOptions struct {
}

GetChatCompletionsStreamOptions contains the optional parameters for the Client.GetChatCompletionsStream method.

type GetChatCompletionsStreamResponse

type GetChatCompletionsStreamResponse struct {
	// ChatCompletionsStream returns the stream of completions. Token limits and other settings may limit the number of chat completions returned by the service.
	ChatCompletionsStream *EventReader[ChatCompletions]
}

GetChatCompletionsStreamResponse is the response from Client.GetChatCompletionsStream.

type GetChatCompletionsWithAzureExtensionsOptions added in v0.2.0

type GetChatCompletionsWithAzureExtensionsOptions struct {
}

GetChatCompletionsWithAzureExtensionsOptions contains the optional parameters for the Client.GetChatCompletionsWithAzureExtensions method.

type GetChatCompletionsWithAzureExtensionsResponse added in v0.2.0

type GetChatCompletionsWithAzureExtensionsResponse struct {
	// Representation of the response data from a chat completions request.
	// Completions support a wide variety of tasks and generate text that continues from or "completes"
	// provided prompt data.
	ChatCompletions
}

GetChatCompletionsWithAzureExtensionsResponse contains the response from method Client.GetChatCompletionsWithAzureExtensions.

type GetCompletionsOptions

type GetCompletionsOptions struct {
}

GetCompletionsOptions contains the optional parameters for the Client.GetCompletions method.

type GetCompletionsResponse

type GetCompletionsResponse struct {
	// Representation of the response data from a completions request.
	// Completions support a wide variety of tasks and generate text that continues from or "completes"
	// provided prompt data.
	Completions
}

GetCompletionsResponse contains the response from method Client.GetCompletions.

type GetCompletionsStreamOptions

type GetCompletionsStreamOptions struct {
}

GetCompletionsStreamOptions contains the optional parameters for the Client.GetCompletionsStream method.

type GetCompletionsStreamResponse

type GetCompletionsStreamResponse struct {
	// CompletionsStream returns the stream of completions. Token limits and other settings may limit the number of completions returned by the service.
	CompletionsStream *EventReader[Completions]
}

GetCompletionsStreamResponse is the response from Client.GetCompletionsStream.

type GetEmbeddingsOptions

type GetEmbeddingsOptions struct {
}

GetEmbeddingsOptions contains the optional parameters for the Client.GetEmbeddings method.

type GetEmbeddingsResponse

type GetEmbeddingsResponse struct {
	// Representation of the response data from an embeddings request.
	// Embeddings measure the relatedness of text strings and are commonly used for search, clustering,
	// recommendations, and other similar scenarios.
	Embeddings
}

GetEmbeddingsResponse contains the response from method Client.GetEmbeddings.

type ImageGenerationOptions

type ImageGenerationOptions struct {
	// REQUIRED; A description of the desired images.
	Prompt *string

	// The number of images to generate (defaults to 1).
	N *int32

	// The format in which image generation response items should be presented. Azure OpenAI only supports URL response items.
	ResponseFormat *ImageGenerationResponseFormat

	// The desired size of the generated images. Must be one of 256x256, 512x512, or 1024x1024 (defaults to 1024x1024).
	Size *ImageSize

	// A unique identifier representing your end-user, which can help to monitor and detect abuse.
	User *string
}

ImageGenerationOptions - Represents the request data used to generate images.

func (ImageGenerationOptions) MarshalJSON

func (i ImageGenerationOptions) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ImageGenerationOptions.

func (*ImageGenerationOptions) UnmarshalJSON

func (i *ImageGenerationOptions) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ImageGenerationOptions.

type ImageGenerationResponseFormat

type ImageGenerationResponseFormat string

ImageGenerationResponseFormat - The format in which the generated images are returned.

const (
	ImageGenerationResponseFormatB64JSON ImageGenerationResponseFormat = "b64_json"
	ImageGenerationResponseFormatURL     ImageGenerationResponseFormat = "url"
)

func PossibleImageGenerationResponseFormatValues

func PossibleImageGenerationResponseFormatValues() []ImageGenerationResponseFormat

PossibleImageGenerationResponseFormatValues returns the possible values for the ImageGenerationResponseFormat const type.

type ImageGenerations

type ImageGenerations struct {
	// REQUIRED; A timestamp when this job or item was created (in unix epochs).
	Created *time.Time

	// REQUIRED; The images generated by the operator.
	Data []ImageGenerationsDataItem
}

ImageGenerations - The result of the operation if the operation succeeded.

func (ImageGenerations) MarshalJSON

func (i ImageGenerations) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type ImageGenerations.

func (*ImageGenerations) UnmarshalJSON

func (i *ImageGenerations) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type ImageGenerations.

type ImageGenerationsDataItem

type ImageGenerationsDataItem struct {
	// Base64Data is set to image data, encoded as a base64 string, if [ImageGenerationOptions.ResponseFormat]
	// was set to [ImageGenerationResponseFormatB64JSON].
	Base64Data *string `json:"b64_json"`

	// URL is the address of a generated image if [ImageGenerationOptions.ResponseFormat] was set
	// to [ImageGenerationResponseFormatURL].
	URL *string `json:"url"`
}

ImageGenerationsDataItem contains the results of image generation.

The field that's set will be based on [ImageGenerationOptions.ResponseFormat] and are mutually exclusive.

type ImageLocation

type ImageLocation struct {
	// REQUIRED; The URL that provides temporary access to download the generated image.
	URL *string
}

ImageLocation - An image response item that provides a URL from which an image may be accessed.

type ImagePayload

type ImagePayload struct {
	// REQUIRED; The complete data for an image represented as a base64-encoded string.
	B64JSON *string
}

ImagePayload - An image response item that directly represents the image data as a base64-encoded string.

type ImageSize

type ImageSize string

ImageSize - The desired size of the generated images. Must be one of 256x256, 512x512, or 1024x1024.

const (
	ImageSize512x512   ImageSize = "512x512"
	ImageSize1024x1024 ImageSize = "1024x1024"
	ImageSize256x256   ImageSize = "256x256"
)

func PossibleImageSizeValues

func PossibleImageSizeValues() []ImageSize

PossibleImageSizeValues returns the possible values for the ImageSize const type.

type KeyCredential

type KeyCredential struct {
	// contains filtered or unexported fields
}

KeyCredential is used when doing APIKey-based authentication.

func NewKeyCredential

func NewKeyCredential(apiKey string) (KeyCredential, error)

NewKeyCredential creates a KeyCredential containing an API key for either Azure OpenAI or OpenAI.

type PromptFilterResult

type PromptFilterResult struct {
	// REQUIRED; The index of this prompt in the set of prompt results
	PromptIndex *int32

	// Content filtering results for this prompt
	ContentFilterResults *PromptFilterResultContentFilterResults
}

PromptFilterResult - Content filtering results for a single prompt in the request.

func (PromptFilterResult) MarshalJSON

func (p PromptFilterResult) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type PromptFilterResult.

func (*PromptFilterResult) UnmarshalJSON

func (p *PromptFilterResult) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type PromptFilterResult.

type PromptFilterResultContentFilterResults

type PromptFilterResultContentFilterResults struct {
	// Describes language attacks or uses that include pejorative or discriminatory language with reference to a person or identity
	// group on the basis of certain differentiating attributes of these groups
	// including but not limited to race, ethnicity, nationality, gender identity and expression, sexual orientation, religion,
	// immigration status, ability status, personal appearance, and body size.
	Hate *ContentFilterResultsHate

	// Describes language related to physical actions intended to purposely hurt, injure, or damage one’s body, or kill oneself.
	SelfHarm *ContentFilterResultsSelfHarm

	// Describes language related to anatomical organs and genitals, romantic relationships, acts portrayed in erotic or affectionate
	// terms, physical sexual acts, including those portrayed as an assault or a
	// forced sexual violent act against one’s will, prostitution, pornography, and abuse.
	Sexual *ContentFilterResultsSexual

	// Describes language related to physical actions intended to hurt, injure, damage, or kill someone or something; describes
	// weapons, etc.
	Violence *ContentFilterResultsViolence
}

PromptFilterResultContentFilterResults - Content filtering results for this prompt

func (PromptFilterResultContentFilterResults) MarshalJSON

func (p PromptFilterResultContentFilterResults) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaller interface for type PromptFilterResultContentFilterResults.

func (*PromptFilterResultContentFilterResults) UnmarshalJSON

func (p *PromptFilterResultContentFilterResults) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaller interface for type PromptFilterResultContentFilterResults.

Jump to

Keyboard shortcuts

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