* feat: add azure openai support * chore: refine config * chore: make config options like the python one * chore: adjust config struct field order * test: fix tests * style: make the linter happy * fix: support Azure API Key authentication in sendRequest * chore: check error in CreateChatCompletionStream * chore: pass tests * chore: try pass tests again * chore: change ClientConfig back due to this lib does not like WithXxx config style * chore: revert fix to CreateChatCompletionStream() due to cause tests not pass * chore: at least add some comment about the required fields * chore: re order ClientConfig fields * chore: add DefaultAzure() * chore: set default api_version the same as py one "2023-03-15-preview" * style: fixup typo * test: add api_internal_test.go * style: make lint happy * chore: add constant AzureAPIKeyHeader * chore: use AzureAPIKeyHeader for api-key header, fix azure base url auto trim suffix / * test: add TestAzureFullURL, TestRequestAuthHeader and TestOpenAIFullURL * test: simplify TestRequestAuthHeader * test: refine TestOpenAIFullURL * chore: refine comments * feat: DefaultAzureConfig
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package openai
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
openaiAPIURLv1 = "https://api.openai.com/v1"
|
|
defaultEmptyMessagesLimit uint = 300
|
|
|
|
azureAPIPrefix = "openai"
|
|
azureDeploymentsPrefix = "deployments"
|
|
)
|
|
|
|
type APIType string
|
|
|
|
const (
|
|
APITypeOpenAI APIType = "OPEN_AI"
|
|
APITypeAzure APIType = "AZURE"
|
|
APITypeAzureAD APIType = "AZURE_AD"
|
|
)
|
|
|
|
const AzureAPIKeyHeader = "api-key"
|
|
|
|
// ClientConfig is a configuration of a client.
|
|
type ClientConfig struct {
|
|
authToken string
|
|
|
|
BaseURL string
|
|
OrgID string
|
|
APIType APIType
|
|
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD
|
|
Engine string // required when APIType is APITypeAzure or APITypeAzureAD
|
|
|
|
HTTPClient *http.Client
|
|
|
|
EmptyMessagesLimit uint
|
|
}
|
|
|
|
func DefaultConfig(authToken string) ClientConfig {
|
|
return ClientConfig{
|
|
authToken: authToken,
|
|
BaseURL: openaiAPIURLv1,
|
|
APIType: APITypeOpenAI,
|
|
OrgID: "",
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
EmptyMessagesLimit: defaultEmptyMessagesLimit,
|
|
}
|
|
}
|
|
|
|
func DefaultAzureConfig(apiKey, baseURL, engine string) ClientConfig {
|
|
return ClientConfig{
|
|
authToken: apiKey,
|
|
BaseURL: baseURL,
|
|
OrgID: "",
|
|
APIType: APITypeAzure,
|
|
APIVersion: "2023-03-15-preview",
|
|
Engine: engine,
|
|
|
|
HTTPClient: &http.Client{},
|
|
|
|
EmptyMessagesLimit: defaultEmptyMessagesLimit,
|
|
}
|
|
}
|