You can integrate Azure OpenAI by using the azure-openai provider. Because Azure uses a specific endpoint structure, you must construct the endpoint URL manually using your resource name, deployment name, and API version, then pass it to gollm.NewLLM via config.SetExtraHeaders using the azure_endpoint key. Note that for Azure, the model parameter should be set to your deployment name.
package main
import (
"context"
"fmt"
"os"
"github.com/teilomillet/gollm"
"github.com/teilomillet/gollm/config"
)
func main() {
// Get configuration from environment
apiKey := os.Getenv("AZURE_OPENAI_API_KEY")
resourceName := os.Getenv("AZURE_OPENAI_RESOURCE_NAME")
deploymentName := os.Getenv("AZURE_OPENAI_DEPLOYMENT_NAME")
apiVersion := os.Getenv("AZURE_OPENAI_API_VERSION")
if apiKey == "" || resourceName == "" || deploymentName == "" {
fmt.Println("Error: Missing required environment variables")
fmt.Println("Please set: AZURE_OPENAI_API_KEY, AZURE_OPENAI_RESOURCE_NAME, AZURE_OPENAI_DEPLOYMENT_NAME")
os.Exit(1)
}
if apiVersion == "" {
apiVersion = "2023-05-15" // Default value
}
// Create the endpoint URL
endpoint := fmt.Sprintf(
"https://%s.openai.azure.com/openai/deployments/%s/chat/completions?api-version=%s",
resourceName, deploymentName, apiVersion,
)
// Create the LLM instance
llm, err := gollm.NewLLM(
config.SetProvider("azure-openai"),
config.SetAPIKey(apiKey),
config.SetModel(deploymentName),
config.SetExtraHeaders(map[string]string{
"azure_endpoint": endpoint,
}),
)
if err != nil {
fmt.Printf("Error creating LLM: %v\n", err)
os.Exit(1)
}
// Create a prompt
ctx := context.Background()
prompt := gollm.NewPrompt("Explain what Azure OpenAI Service is in 3 sentences.")
// Generate a response
response, err := llm.Generate(ctx, prompt)
if err != nil {
fmt.Printf("Error generating response: %v\n", err)
os.Exit(1)
}
// Print the response
fmt.Println("Response from Azure OpenAI:")
fmt.Println(response)
}