azure.go

 1// Package azure provides an implementation of the fantasy AI SDK for Azure's language models.
 2package azure
 3
 4import (
 5	"charm.land/fantasy"
 6	"charm.land/fantasy/providers/openaicompat"
 7	"github.com/openai/openai-go/v2/azure"
 8	"github.com/openai/openai-go/v2/option"
 9)
10
11type options struct {
12	baseURL    string
13	apiKey     string
14	apiVersion string
15
16	openaiOptions []openaicompat.Option
17}
18
19const (
20	// Name is the name of the Azure provider.
21	Name = "azure"
22	// defaultAPIVersion is the default API version for Azure.
23	defaultAPIVersion = "2025-01-01-preview"
24)
25
26// Option defines a function that configures Azure provider options.
27type Option = func(*options)
28
29// New creates a new Azure provider with the given options.
30func New(opts ...Option) fantasy.Provider {
31	o := options{
32		apiVersion: defaultAPIVersion,
33	}
34	for _, opt := range opts {
35		opt(&o)
36	}
37	return openaicompat.New(
38		append(
39			o.openaiOptions,
40			openaicompat.WithName(Name),
41			openaicompat.WithSDKOptions(
42				azure.WithEndpoint(o.baseURL, o.apiVersion),
43				azure.WithAPIKey(o.apiKey),
44			),
45		)...,
46	)
47}
48
49// WithBaseURL sets the base URL for the Azure provider.
50func WithBaseURL(baseURL string) Option {
51	return func(o *options) {
52		o.baseURL = baseURL
53	}
54}
55
56// WithAPIKey sets the API key for the Azure provider.
57func WithAPIKey(apiKey string) Option {
58	return func(o *options) {
59		o.apiKey = apiKey
60	}
61}
62
63// WithHeaders sets the headers for the Azure provider.
64func WithHeaders(headers map[string]string) Option {
65	return func(o *options) {
66		o.openaiOptions = append(o.openaiOptions, openaicompat.WithHeaders(headers))
67	}
68}
69
70// WithAPIVersion sets the API version for the Azure provider.
71func WithAPIVersion(version string) Option {
72	return func(o *options) {
73		o.apiVersion = version
74	}
75}
76
77// WithHTTPClient sets the HTTP client for the Azure provider.
78func WithHTTPClient(client option.HTTPClient) Option {
79	return func(o *options) {
80		o.openaiOptions = append(o.openaiOptions, openaicompat.WithHTTPClient(client))
81	}
82}