Skip to content

Commit

Permalink
Update README.md with Azure OpenAI Embeddings example (sashabaranov#318)
Browse files Browse the repository at this point in the history
  • Loading branch information
coggsfl committed May 22, 2023
1 parent faae8b4 commit a18c18d
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,54 @@ func main() {
```
</details>

<details>
<summary>Azure OpenAI Embeddings</summary>

```go
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")
config.APIVersion = "2023-05-15" // optional update to latest API version

//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]
//}

input := "Text to vectorize"

client := openai.NewClientWithConfig(config)
resp, err := client.CreateEmbeddings(
context.Background(),
openai.EmbeddingRequest{
Input: []string{input},
Model: openai.AdaEmbeddingV2,
})

if err != nil {
fmt.Printf("CreateEmbeddings error: %v\n", err)
return
}

vectors := resp.Data[0].Embedding // []float32 with 1536 dimensions

fmt.Println(vectors[:10], "...", vectors[len(vectors)-10:])
}
```
</details>

<details>
<summary>Error handling</summary>

Expand Down

0 comments on commit a18c18d

Please sign in to comment.