ai.azure
ballerinax/ai.azure Ballerina library
Overview
This module offers APIs for connecting with AzureOpenAI Large Language Models (LLM).
Prerequisites
Before using this module in your Ballerina application, first you must obtain the nessary configuration to engage the LLM.
- Create an Azure account.
- Create an Azure OpenAI resource.
- Obtain the tokens. Refer to the Azure OpenAI Authentication guide to learn how to generate and use tokens.
Quickstart
To use the ai.azure module in your Ballerina application, update the .bal file as follows:
Step 1: Import the module
Import the ai.azure; module.
import ballerinax/ai.azure;
Step 2: Intialize the Model Provider
Here's how to initialize the Model Provider:
import ballerina/ai; import ballerinax/ai.azure; final ai:ModelProvider azureOpenAiModel = check new azure:OpenAiModelProvider("https://service-url", "api-key", "deployment-id", "deployment-version");
Step 4: Invoke chat completion
ai:ChatMessage[] chatMessages = [{role: "user", content: "hi"}]; ai:ChatAssistantMessage response = check azureOpenAiModel->chat(chatMessages, tools = []); chatMessages.push(response);
Classes
ai.azure: AiSearchKnowledgeBase
Represents the Azure Search Knowledge Base implementation.
Constructor
Initializes a new AiSearchKnowledgeBase instance.
init (string serviceUrl, string apiKey, string|SearchIndex index, EmbeddingProvider embeddingModel, Chunker|AUTO|DISABLE chunker, boolean verbose, string apiVersion, string contentFieldName, ConnectionConfig searchClientConnectionConfig, ConnectionConfig indexClientConnectionConfig)- serviceUrl string - The service URL of the Azure AI Search instance
- apiKey string - The API key for authenticating with the Azure AI Search service
- index string|SearchIndex - The name of an existing search index or a
search:SearchIndexdefinition to create, When creating a new index, ensure that it contains one key field of type string.
- embeddingModel EmbeddingProvider - The embedding model to use for generating embeddings
- verbose boolean false - Whether to enable verbose logging. Defaults to
false.
- apiVersion string AI_AZURE_KNOWLEDGE_BASE_API_VERSION - The API version to use for requests.
- contentFieldName string CONTENT_FIELD_NAME - The name of the field in the index that contains the main content. Defaults to "content".
- searchClientConnectionConfig ConnectionConfig {} - Connection configuration for the Azure AI search client.
This configuration is only required when the
indexparameter is provided as ansearch:SearchIndex
- indexClientConnectionConfig ConnectionConfig {} - Connection configuration for the Azure AI index client.
ingest
Ingests documents into the Azure search knowledge base.
Parameters
Return Type
- Error? - An
ai:Errorif ingestion fails, otherwisenil
retrieve
function retrieve(string query, int maxLimit, MetadataFilters? filters) returns QueryMatch[]|ErrorRetrieves relevant chunks for the given query using vector search.
Parameters
- query string - The text query to search for
- maxLimit int (default 10) - The maximum number of items to return
- filters MetadataFilters? (default ()) - Optional metadata filters to apply during retrieval
Return Type
- QueryMatch[]|Error - An array of matching chunks with similarity scores, or an
ai:Errorif retrieval fails
deleteByFilter
function deleteByFilter(MetadataFilters filters) returns Error?Deletes chunks that match the given metadata filters.
Parameters
- filters MetadataFilters - The metadata filters used to identify which chunks to delete
Return Type
- Error? - An
ai:Errorif the deletion fails, otherwisenil
Clients
ai.azure: EmbeddingProvider
EmbeddingProvider provides an interface for interacting with Azure OpenAI Embedding Models.
Constructor
Initializes the OpenAI embedding model with the given connection configuration.
init (string serviceUrl, string accessToken, string apiVersion, string deploymentId, *ConnectionConfig config)- serviceUrl string - The base URL of OpenAI API endpoint
- accessToken string - The access token for authenticating API requests
- apiVersion string - The API version of the Azure OpenAI API
- deploymentId string - The deployment ID of the embedding model
- config *ConnectionConfig - The connection configurations for the HTTP endpoint
embed
Generates an embedding vector for the provided chunk.
Parameters
- chunk Chunk - The
ai:Chunkcontaining the content to embed
batchEmbed
Converts a batch of chunks into embeddings.
Parameters
- chunks Chunk[] - The array of chunks to be converted into embeddings
ai.azure: OpenAiModelProvider
OpenAiProvider is a client class that provides an interface for interacting with Azure-hosted OpenAI language models.
Constructor
Initializes the Azure OpenAI model with the given connection configuration and model configuration.
init (string serviceUrl, string apiKey, string deploymentId, string apiVersion, int maxTokens, decimal temperature, *ConnectionConfig connectionConfig)- serviceUrl string - The base URL of Azure OpenAI API endpoint
- apiKey string - The Azure OpenAI API key
- deploymentId string - The deployment identifier for the specific model deployment in Azure
- apiVersion string - The Azure OpenAI API version (e.g., "2023-07-01-preview")
- maxTokens int DEFAULT_MAX_TOKEN_COUNT - The upper limit for the number of tokens in the response generated by the model
- temperature decimal DEFAULT_TEMPERATURE - The temperature for controlling randomness in the model's output
- connectionConfig *ConnectionConfig - Additional HTTP connection configuration
chat
function chat(ChatMessage[]|ChatUserMessage messages, ChatCompletionFunctions[] tools, string? stop) returns ChatAssistantMessage|ErrorSends a chat request to the OpenAI model with the given messages and tools.
Parameters
- messages ChatMessage[]|ChatUserMessage - List of chat messages or a user message
- tools ChatCompletionFunctions[] - Tool definitions to be used for the tool call
- stop string? (default ()) - Stop sequence to stop the completion
Return Type
- ChatAssistantMessage|Error - Function to be called, chat response or an error in-case of failures
generate
Sends a chat request to the model and generates a value that belongs to the type corresponding to the type descriptor argument.
Parameters
- prompt Prompt - The prompt to use in the chat messages
- td typedesc<anydata> (default <>) - Type descriptor specifying the expected return type format
Return Type
- td|Error - Generates a value that belongs to the type, or an error if generation fails
Annotations
ai.azure: JsonSchema
Records
ai.azure: ConnectionConfig
Configurations for controlling the behaviours when communicating with a remote HTTP endpoint.
Fields
- httpVersion HttpVersion(default http:HTTP_2_0) - The HTTP version understood by the client
- http1Settings? ClientHttp1Settings - Configurations related to HTTP/1.x protocol
- http2Settings? ClientHttp2Settings - Configurations related to HTTP/2 protocol
- timeout decimal(default 60) - The maximum time to wait (in seconds) for a response before closing the connection
- forwarded string(default "disable") - The choice of setting
forwarded/x-forwardedheader
- poolConfig? PoolConfiguration - Configurations associated with request pooling
- cache? CacheConfig - HTTP caching related configurations
- compression Compression(default http:COMPRESSION_AUTO) - Specifies the way of handling compression (
accept-encoding) header
- circuitBreaker? CircuitBreakerConfig - Configurations associated with the behaviour of the Circuit Breaker
- retryConfig? RetryConfig - Configurations associated with retrying
- responseLimits? ResponseLimitConfigs - Configurations associated with inbound response size limits
- secureSocket? ClientSecureSocket - SSL/TLS-related options
- proxy? ProxyConfig - Proxy server related options
- validation boolean(default true) - Enables the inbound payload validation functionality which provided by the constraint package. Enabled by default
Import
import ballerinax/ai.azure;Metadata
Released date: 7 days ago
Version: 1.2.0
License: Apache-2.0
Compatibility
Platform: java21
Ballerina version: 2201.12.0
GraalVM compatible: Yes
Pull count
Total: 2716
Current verison: 11
Weekly downloads
Keywords
AI
Agent
Azure
Model
Provider
Contributors
Dependencies