Skip to content

Commit a18c18d

Browse files
authored
Update README.md with Azure OpenAI Embeddings example (#318)
1 parent faae8b4 commit a18c18d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,54 @@ func main() {
469469
```
470470
</details>
471471

472+
<details>
473+
<summary>Azure OpenAI Embeddings</summary>
474+
475+
```go
476+
package main
477+
478+
import (
479+
"context"
480+
"fmt"
481+
482+
openai "github.com/sashabaranov/go-openai"
483+
)
484+
485+
func main() {
486+
487+
config := openai.DefaultAzureConfig("your Azure OpenAI Key", "https://your Azure OpenAI Endpoint")
488+
config.APIVersion = "2023-05-15" // optional update to latest API version
489+
490+
//If you use a deployment name different from the model name, you can customize the AzureModelMapperFunc function
491+
//config.AzureModelMapperFunc = func(model string) string {
492+
// azureModelMapping = map[string]string{
493+
// "gpt-3.5-turbo":"your gpt-3.5-turbo deployment name",
494+
// }
495+
// return azureModelMapping[model]
496+
//}
497+
498+
input := "Text to vectorize"
499+
500+
client := openai.NewClientWithConfig(config)
501+
resp, err := client.CreateEmbeddings(
502+
context.Background(),
503+
openai.EmbeddingRequest{
504+
Input: []string{input},
505+
Model: openai.AdaEmbeddingV2,
506+
})
507+
508+
if err != nil {
509+
fmt.Printf("CreateEmbeddings error: %v\n", err)
510+
return
511+
}
512+
513+
vectors := resp.Data[0].Embedding // []float32 with 1536 dimensions
514+
515+
fmt.Println(vectors[:10], "...", vectors[len(vectors)-10:])
516+
}
517+
```
518+
</details>
519+
472520
<details>
473521
<summary>Error handling</summary>
474522

0 commit comments

Comments
 (0)