sashabaranov / go-openai

OpenAI ChatGPT, GPT-3, GPT-4, DALL·E, Whisper API wrapper for Go
Apache License 2.0
8.71k stars 1.32k forks source link

Refactor API providers #726

Open sashabaranov opened 2 months ago

sashabaranov commented 2 months ago

We have a growing number of different API providers: OpenAI, Azure, Cloudflare Getaway. At the same time other tools keep adding their OpenAI-compatible APIs (e.g. Ollama).

Considering #693, here's a sketch of how we can add new API providers painlessly:

type apiProvider interface { // might want to expose that
    SetAuthForRequest(req *http.Request)
    GetFullURL(suffix string, args ...any) // Shouldn't be `any`, but a type-safe list like requestOption
}

type APIProviderConfig interface {
    // whatever we need here
}

client usage:

config := openai.DefaultConfigWithAPIProvider(
    openai.AzureOpenAIProviderConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint"),
)
client := openai.NewClientWithConfig(config)
qingdi commented 2 months ago

your kind, Cloudflare how to do ?

sashabaranov commented 2 months ago

@qingdi pretty much the same!

config := openai.DefaultConfigWithAPIProvider(
    openai.CloudflareProviderConfig(...),
)
client := openai.NewClientWithConfig(config)