openai / openai-go

The official Go library for the OpenAI API
Apache License 2.0
304 stars 19 forks source link

Support easy use of Azure OpenAI #106

Closed MrHuangjiacong closed 4 hours ago

MrHuangjiacong commented 4 hours ago

just like this https://github.com/sashabaranov/go-openai

This third-party library can easily use Azure's OpenAI

package main

import (
    "context"
    "fmt"

    openai "github.com/sashabaranov/go-openai"
)

func main() {
    config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint")
    // If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
    config.AzureModelMapperFunc = func(model string) string {
        azureModelMapping := map[string]string{
            "gpt-3.5-turbo": "your gpt-3.5-turbo deployment name",
        }
        return azureModelMapping[model]
     }

    client := openai.NewClientWithConfig(config)
    resp, err := client.CreateChatCompletion(
        context.Background(),
        openai.ChatCompletionRequest{
            Model: openai.GPT3Dot5Turbo,
            Messages: []openai.ChatCompletionMessage{
                {
                    Role:    openai.ChatMessageRoleUser,
                    Content: "Hello Azure OpenAI!",
                },
            },
        },
    )
    if err != nil {
        fmt.Printf("ChatCompletion error: %v\n", err)
        return
    }

    fmt.Println(resp.Choices[0].Message.Content)
}
RobertCraigie commented 4 hours ago

We have builtin support for Azure, see this section of the README.